| "Provide access to a BSD tar" |
| |
| BSDTAR_PLATFORMS = { |
| "darwin_amd64": struct( |
| compatible_with = [ |
| "@platforms//os:osx", |
| "@platforms//cpu:x86_64", |
| ], |
| ), |
| "darwin_arm64": struct( |
| compatible_with = [ |
| "@platforms//os:osx", |
| "@platforms//cpu:aarch64", |
| ], |
| ), |
| "linux_amd64": struct( |
| compatible_with = [ |
| "@platforms//os:linux", |
| "@platforms//cpu:x86_64", |
| ], |
| ), |
| "linux_arm64": struct( |
| compatible_with = [ |
| "@platforms//os:linux", |
| "@platforms//cpu:aarch64", |
| ], |
| ), |
| "windows_amd64": struct( |
| release_platform = "win64", |
| compatible_with = [ |
| "@platforms//os:windows", |
| "@platforms//cpu:x86_64", |
| ], |
| ), |
| } |
| |
| BSDTAR_PREBUILT = { |
| "darwin_amd64": ( |
| "https://github.com/aspect-build/bsdtar-prebuilt/releases/download/v3.7.2/tar_darwin_amd64", |
| "a3bd0e7be92bcddcb70341f1efc48c29ef99b3ad57349b433e9a3182b68cb0c1", |
| ), |
| "darwin_arm64": ( |
| "https://github.com/aspect-build/bsdtar-prebuilt/releases/download/v3.7.2/tar_darwin_arm64", |
| "63ee769e2d870d1ed3542e292c919dc8a3934544d17b2de34213c18c41c5437f", |
| ), |
| "linux_amd64": ( |
| "https://github.com/aspect-build/bsdtar-prebuilt/releases/download/v3.7.2/tar_linux_amd64", |
| "d40582e64aace892e2f9588045edc5e67023ca3371cd575e7043b0c2a08205b4", |
| ), |
| "linux_arm64": ( |
| "https://github.com/aspect-build/bsdtar-prebuilt/releases/download/v3.7.2/tar_linux_arm64", |
| "e2527be38499e94e021c7c02476b4cff8083313d38c85bdf46fc5751d31d32aa", |
| ), |
| "windows_amd64": ( |
| "https://github.com/libarchive/libarchive/releases/download/v3.7.2/libarchive-v3.7.2-amd64.zip", |
| "e06f10043b1b148eb38ad06cff678af05beade0bdd2edd8735a198c521fa3993", |
| ), |
| } |
| |
| def _bsdtar_binary_repo(rctx): |
| (url, sha256) = BSDTAR_PREBUILT[rctx.attr.platform] |
| if rctx.attr.platform.startswith("windows"): |
| rctx.download_and_extract( |
| url = url, |
| type = "zip", |
| sha256 = sha256, |
| ) |
| binary = "libarchive/bin/bsdtar.exe" |
| else: |
| rctx.download( |
| url = url, |
| output = "tar", |
| executable = True, |
| sha256 = sha256, |
| ) |
| binary = "tar" |
| |
| rctx.file("BUILD.bazel", """\ |
| # @generated by @aspect_bazel_lib//lib/private:tar_toolchain.bzl |
| |
| load("@aspect_bazel_lib//lib/private:tar_toolchain.bzl", "tar_toolchain") |
| |
| package(default_visibility = ["//visibility:public"]) |
| |
| tar_toolchain(name = "bsdtar_toolchain", binary = "{}") |
| """.format(binary)) |
| |
| bsdtar_binary_repo = repository_rule( |
| implementation = _bsdtar_binary_repo, |
| attrs = { |
| "platform": attr.string(mandatory = True, values = BSDTAR_PLATFORMS.keys()), |
| }, |
| ) |
| |
| TarInfo = provider( |
| doc = "Provide info for executing BSD tar", |
| fields = { |
| "binary": "bsdtar executable", |
| }, |
| ) |
| |
| def _tar_toolchain_impl(ctx): |
| binary = ctx.executable.binary |
| |
| # Make the $(BSDTAR_BIN) variable available in places like genrules. |
| # See https://docs.bazel.build/versions/main/be/make-variables.html#custom_variables |
| template_variables = platform_common.TemplateVariableInfo({ |
| "BSDTAR_BIN": binary.path, |
| }) |
| |
| default_info = DefaultInfo( |
| files = depset(ctx.files.binary + ctx.files.files), |
| ) |
| tarinfo = TarInfo( |
| binary = binary, |
| ) |
| |
| # Export all the providers inside our ToolchainInfo |
| # so the resolved_toolchain rule can grab and re-export them. |
| toolchain_info = platform_common.ToolchainInfo( |
| tarinfo = tarinfo, |
| template_variables = template_variables, |
| default = default_info, |
| ) |
| |
| return [toolchain_info, template_variables, default_info] |
| |
| tar_toolchain = rule( |
| implementation = _tar_toolchain_impl, |
| attrs = { |
| "binary": attr.label( |
| doc = "a command to find on the system path", |
| allow_files = True, |
| executable = True, |
| cfg = "exec", |
| ), |
| "files": attr.label_list(allow_files = True), |
| }, |
| ) |
| |
| def _tar_toolchains_repo_impl(rctx): |
| # Expose a concrete toolchain which is the result of Bazel resolving the toolchain |
| # for the execution or target platform. |
| # Workaround for https://github.com/bazelbuild/bazel/issues/14009 |
| starlark_content = """\ |
| # @generated by @aspect_bazel_lib//lib/private:tar_toolchain.bzl |
| |
| # Forward all the providers |
| def _resolved_toolchain_impl(ctx): |
| toolchain_info = ctx.toolchains["@aspect_bazel_lib//lib:tar_toolchain_type"] |
| return [ |
| toolchain_info, |
| toolchain_info.default, |
| toolchain_info.tarinfo, |
| toolchain_info.template_variables, |
| ] |
| |
| # Copied from java_toolchain_alias |
| # https://cs.opensource.google/bazel/bazel/+/master:tools/jdk/java_toolchain_alias.bzl |
| resolved_toolchain = rule( |
| implementation = _resolved_toolchain_impl, |
| toolchains = ["@aspect_bazel_lib//lib:tar_toolchain_type"], |
| incompatible_use_toolchain_transition = True, |
| ) |
| """ |
| rctx.file("defs.bzl", starlark_content) |
| |
| build_content = """# @generated by @aspect_bazel_lib//lib/private:tar_toolchain.bzl |
| load(":defs.bzl", "resolved_toolchain") |
| load("@local_config_platform//:constraints.bzl", "HOST_CONSTRAINTS") |
| |
| resolved_toolchain(name = "resolved_toolchain", visibility = ["//visibility:public"])""" |
| |
| for [platform, meta] in BSDTAR_PLATFORMS.items(): |
| build_content += """ |
| toolchain( |
| name = "{platform}_toolchain", |
| exec_compatible_with = {compatible_with}, |
| toolchain = "@{user_repository_name}_{platform}//:bsdtar_toolchain", |
| toolchain_type = "@aspect_bazel_lib//lib:tar_toolchain_type", |
| ) |
| """.format( |
| platform = platform, |
| user_repository_name = rctx.attr.user_repository_name, |
| compatible_with = meta.compatible_with, |
| ) |
| |
| rctx.file("BUILD.bazel", build_content) |
| |
| tar_toolchains_repo = repository_rule( |
| _tar_toolchains_repo_impl, |
| doc = """Creates a repository that exposes a tar_toolchain_type target.""", |
| attrs = { |
| "user_repository_name": attr.string(doc = "Base name for toolchains repository"), |
| }, |
| ) |