mirror of
https://github.com/bazel-contrib/bazel-lib
synced 2024-11-27 17:43:27 +00:00
189 lines
5.9 KiB
Python
189 lines
5.9 KiB
Python
"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.5/tar_darwin_amd64",
|
|
"6c7752869833c5cd4cf9728f90afd37f6665d7f9e06a4fbfdeb0fa41267575cf",
|
|
),
|
|
"darwin_arm64": (
|
|
"https://github.com/aspect-build/bsdtar-prebuilt/releases/download/v3.7.5/tar_darwin_arm64",
|
|
"1718c23919bae478ed13c682a341e389de6ba4396220fc8e75a7f0e5d6024019",
|
|
),
|
|
"linux_amd64": (
|
|
"https://github.com/aspect-build/bsdtar-prebuilt/releases/download/v3.7.5/tar_linux_amd64",
|
|
"b8c492a51fb9e4c59dfa71b3c62191210c48280b98c2a88f7cef8ba973a9ad39",
|
|
),
|
|
"linux_arm64": (
|
|
"https://github.com/aspect-build/bsdtar-prebuilt/releases/download/v3.7.5/tar_linux_arm64",
|
|
"aa760c25a5445320bb3cde06e60c99ebc95fb29f330af0212bd099dd24e1cc54",
|
|
),
|
|
"windows_amd64": (
|
|
"https://github.com/aspect-build/bsdtar-prebuilt/releases/download/v3.7.5/tar_windows_amd64.exe",
|
|
"80c109b9477ed44e9f8626cd9d8a635c8761c4506030c91f46b289798c7a22b6",
|
|
),
|
|
}
|
|
|
|
def _bsdtar_binary_repo(rctx):
|
|
(url, sha256) = BSDTAR_PREBUILT[rctx.attr.platform]
|
|
binary = "tar.exe" if rctx.attr.platform.startswith("windows") else "tar"
|
|
rctx.download(
|
|
url = url,
|
|
output = binary,
|
|
executable = True,
|
|
sha256 = sha256,
|
|
)
|
|
|
|
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"),
|
|
},
|
|
)
|