| "Provide access to a BSD tar" |
| |
| load(":repo_utils.bzl", "repo_utils") |
| |
| BSDTAR_PLATFORMS = { |
| "linux_amd64": struct( |
| compatible_with = [ |
| "@platforms//os:linux", |
| "@platforms//cpu:x86_64", |
| ], |
| ), |
| "linux_arm64": struct( |
| compatible_with = [ |
| "@platforms//os:linux", |
| "@platforms//cpu:aarch64", |
| ], |
| ), |
| "darwin_arm64": struct( |
| compatible_with = [ |
| "@platforms//os:macos", |
| "@platforms//cpu:aarch64", |
| ], |
| ), |
| "windows_amd64": struct( |
| release_platform = "win64", |
| compatible_with = [ |
| "@platforms//os:windows", |
| "@platforms//cpu:x86_64", |
| ], |
| ), |
| # WARNING: host toolchain should always come last to make it a fallback toolchain. |
| "host": struct( |
| # loaded by the macro |
| compatible_with = "HOST_CONSTRAINTS", |
| ), |
| } |
| |
| def _find_usable_system_tar(rctx, tar_name): |
| tar = rctx.which(tar_name) |
| if not tar: |
| fail("tar not found on PATH, and we don't handle this case yet") |
| |
| # Run tar --version and see if we are satisfied to use it |
| tar_version = rctx.execute([tar, "--version"]).stdout.strip() |
| |
| # TODO: also check if it's really ancient or compiled without gzip support or something? |
| # TODO: document how users could fetch the source and compile it themselves |
| if tar_version.find("bsdtar") >= 0: |
| return tar |
| |
| fail("tar isn't a BSD tar") |
| |
| EXPERIMENTAL_NIXOS_BSDTAR_TOOLCHAINS = { |
| "linux_arm64": ("https://cache.nixos.org/nar/06sac7pp4zx71jxvrngxqgqgjjbzppwvwkiyrqyhzhl3clmkz1sq.nar.xz", "58873f2b6583c20f3dce3e4ebef9bd7f49f9f0c3fdd9bcbb0ca77f72ef614a1b", (7301328, 6923120)), |
| "linux_amd64": ("https://cache.nixos.org/nar/1zi03mz2l6aidzy8kzc9iri2hvm74dn0mdhv0sk42zpxdl91y2v0.nar.xz", "600b1f126dfd7e41a6061bb60a6c23a76e28628e89fd89fc6f51192a7e1d20fe", (7903336, 7533384)), |
| "darwin_arm64": ("https://cache.nixos.org/nar/0q8ysphisaikwv2fx0pj2fs76rx3xj12j8p4xxggdyyj81p37xxy.nar.xz", "bef7336e40d2fbf65eefe4222982eca36773b413f282eec4e6332a1de1d51e61", (5217904, 3866160)), |
| } |
| |
| WINDOWS_DEPS = ( |
| "e06f10043b1b148eb38ad06cff678af05beade0bdd2edd8735a198c521fa3993", |
| "https://github.com/libarchive/libarchive/releases/download/v3.7.2/libarchive-v3.7.2-amd64.zip", |
| ) |
| |
| NAR_EXTRACT = """\ |
| #!/bin/bash |
| gzcat $1 | tail -c $2 > ./tmp && |
| head -c $3 ./tmp > $4 && |
| chmod +x $4 && |
| rm tmp extract.sh |
| """ |
| |
| def _bsdtar_binary_repo(rctx): |
| tar_name = "tar.exe" if repo_utils.is_windows(rctx) else "tar" |
| build_header = """\ |
| # @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"]) |
| |
| """ |
| |
| # On MacOS, the system `tar` binary on the PATH should already work |
| if rctx.attr.platform == "host": |
| tar = _find_usable_system_tar(rctx, tar_name) |
| output = rctx.path(tar_name) |
| rctx.symlink(tar, output) |
| |
| elif repo_utils.is_windows(rctx): |
| rctx.download_and_extract( |
| url = WINDOWS_DEPS[1], |
| type = "zip", |
| sha256 = WINDOWS_DEPS[0], |
| ) |
| tar_name = "libarchive/bin/bsdtar.exe" |
| |
| else: |
| remote = EXPERIMENTAL_NIXOS_BSDTAR_TOOLCHAINS[rctx.attr.platform] |
| rctx.download( |
| url = remote[0], |
| output = "bsdtar.nar.xz", |
| sha256 = remote[1], |
| ) |
| rctx.file("extract.sh", executable = True, content = NAR_EXTRACT) |
| r = rctx.execute([rctx.path("extract.sh"), "bsdtar.nar.xz", "+%s" % (remote[2][0] + 1), "%s" % remote[2][1], tar_name]) |
| if r.return_code != 0: |
| fail("bsdtar extraction failed.\nstderr: \n{}\nstdout:\n{}".format(r.stderr, r.stdout)) |
| |
| rctx.file("BUILD.bazel", build_header + """tar_toolchain(name = "bsdtar_toolchain", binary = "{}")""".format(tar_name)) |
| |
| 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"), |
| }, |
| ) |