From c1c14419a2312ce98cf1b9c4452147ec010590c1 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 7 Apr 2021 15:19:23 +1000 Subject: [PATCH] switch esbuild to a toolchain --- ts/esbuild.bzl | 5 ----- ts/esbuild/BUILD.bazel | 8 +++++++ ts/esbuild/esbuild_repo.bzl | 3 +++ ts/esbuild/toolchain.bzl | 42 +++++++++++++++++++++++++++++++++++++ ts/esbuild/upstream.bzl | 13 +++++------- 5 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 ts/esbuild/toolchain.bzl diff --git a/ts/esbuild.bzl b/ts/esbuild.bzl index e0c3021b7..3de24a8f5 100644 --- a/ts/esbuild.bzl +++ b/ts/esbuild.bzl @@ -3,11 +3,6 @@ load("//ts/esbuild:upstream.bzl", _esbuild = "esbuild_macro") def esbuild(name, **kwargs): _esbuild( name = name, - tool = select({ - "@bazel_tools//src/conditions:darwin": "@esbuild_darwin//:bin/esbuild", - "@bazel_tools//src/conditions:windows": "@esbuild_windows//:esbuild.exe", - "@bazel_tools//src/conditions:linux_x86_64": "@esbuild_linux//:bin/esbuild", - }), minify = select({ "//:release": True, "//conditions:default": False, diff --git a/ts/esbuild/BUILD.bazel b/ts/esbuild/BUILD.bazel index e69de29bb..59a42b85e 100644 --- a/ts/esbuild/BUILD.bazel +++ b/ts/esbuild/BUILD.bazel @@ -0,0 +1,8 @@ +load(":toolchain.bzl", "define_default_toolchains", "esbuild_toolchain") + +toolchain_type( + name = "toolchain_type", + visibility = ["//visibility:public"], +) + +define_default_toolchains() diff --git a/ts/esbuild/esbuild_repo.bzl b/ts/esbuild/esbuild_repo.bzl index 1bad45a7d..c507d9809 100644 --- a/ts/esbuild/esbuild_repo.bzl +++ b/ts/esbuild/esbuild_repo.bzl @@ -5,6 +5,7 @@ Helper macro for fetching esbuild versions for internal tests and examples in ru """ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +load(":toolchain.bzl", "register_default_toolchains") _VERSION = "0.11.5" @@ -40,3 +41,5 @@ def esbuild_dependencies(): build_file_content = """exports_files(["bin/esbuild"])""", sha256 = "113c2e84895f4422a3676db4e15d9f01b2b4fac041edab25284fdb9574ba58a0", ) + + register_default_toolchains() diff --git a/ts/esbuild/toolchain.bzl b/ts/esbuild/toolchain.bzl new file mode 100644 index 000000000..dfd6d5898 --- /dev/null +++ b/ts/esbuild/toolchain.bzl @@ -0,0 +1,42 @@ +def _esbuild_toolchain_impl(ctx): + return [platform_common.ToolchainInfo( + binary = ctx.executable.binary, + )] + +esbuild_toolchain = rule( + implementation = _esbuild_toolchain_impl, + attrs = { + "binary": attr.label(allow_single_file = True, executable = True, cfg = "exec"), + }, +) + +_package_path = "@net_ankiweb_anki//ts/esbuild" + +TOOLCHAIN = _package_path + ":toolchain_type" + +_default_toolchains = [ + ["@esbuild_darwin//:bin/esbuild", "macos"], + ["@esbuild_linux//:bin/esbuild", "linux"], + ["@esbuild_windows//:esbuild.exe", "windows"], +] + +def define_default_toolchains(): + for repo_path, platform in _default_toolchains: + esbuild_toolchain( + name = "esbuild_" + platform, + binary = repo_path, + ) + + native.toolchain( + name = "esbuild_{}_toolchain".format(platform), + exec_compatible_with = [ + "@platforms//os:" + platform, + "@platforms//cpu:x86_64", + ], + toolchain = ":esbuild_" + platform, + toolchain_type = ":toolchain_type", + ) + +def register_default_toolchains(): + for _, platform in _default_toolchains: + native.register_toolchains(_package_path + ":esbuild_{}_toolchain".format(platform)) diff --git a/ts/esbuild/upstream.bzl b/ts/esbuild/upstream.bzl index 80d37b888..190addaee 100644 --- a/ts/esbuild/upstream.bzl +++ b/ts/esbuild/upstream.bzl @@ -5,6 +5,7 @@ esbuild rule load("@build_bazel_rules_nodejs//:providers.bzl", "JSEcmaScriptModuleInfo", "JSModuleInfo", "NpmPackageInfo", "node_modules_aspect") load("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl", "MODULE_MAPPINGS_ASPECT_RESULTS_NAME", "module_mappings_aspect") load(":helpers.bzl", "filter_files", "generate_path_mapping", "resolve_js_input", "write_jsconfig_file") +load(":toolchain.bzl", "TOOLCHAIN") def _esbuild_impl(ctx): # For each dep, JSEcmaScriptModuleInfo is used if found, then JSModuleInfo and finally @@ -128,7 +129,7 @@ def _esbuild_impl(ctx): ctx.actions.run( inputs = inputs, outputs = outputs, - executable = ctx.executable.tool, + executable = ctx.toolchains[TOOLCHAIN].binary, arguments = [args], progress_message = "%s Javascript %s [esbuild]" % ("Bundling" if not ctx.attr.output_dir else "Splitting", entry_point.short_path), execution_requirements = execution_requirements, @@ -268,19 +269,15 @@ edge16, node10, default esnext) See https://esbuild.github.io/api/#target for more details """, ), - "tool": attr.label( - allow_single_file = True, - mandatory = True, - executable = True, - cfg = "exec", - doc = "An executable for the esbuild binary", - ), }, implementation = _esbuild_impl, doc = """Runs the esbuild bundler under Bazel For further information about esbuild, see https://esbuild.github.io/ """, + toolchains = [ + TOOLCHAIN, + ], ) def esbuild_macro(name, output_dir = False, output_css = False, **kwargs):