Skip to content

Commit 2637ebb

Browse files
feat: Introduces a new target_tar_toolchain_type (#65)
This PR is a follow-up to bazel-contrib/bazel-lib#1180 which is supposed to help with the issue described in bazel-contrib/rules_oci#769. The description of the issue and proposal are in bazel-contrib/rules_oci#844 PR. CC @alexeagle @fmeum
1 parent a0465e9 commit 2637ebb

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

tar/tests/BUILD

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,33 @@ assert_archive_contains(
4646
],
4747
)
4848

49+
# Test that resolved_target_toolchain works similarly to resolved_toolchain.
50+
# This demonstrates target-platform-based toolchain resolution.
51+
genrule(
52+
name = "tar_genrule_target_toolchain",
53+
srcs = [
54+
":fixture1",
55+
"src_file",
56+
],
57+
outs = ["1_target.tar"],
58+
cmd = "$(BSDTAR_BIN) --create --dereference --file $@ -s '#$(BINDIR)##' $(execpath :fixture1) $(execpath src_file)",
59+
target_compatible_with = select({
60+
# bsdtar.exe: -s is not supported by this version of bsdtar
61+
"@platforms//os:windows": ["@platforms//:incompatible"],
62+
"//conditions:default": [],
63+
}),
64+
toolchains = ["@bsd_tar_toolchains//:resolved_target_toolchain"],
65+
)
66+
67+
assert_archive_contains(
68+
name = "test_genrule_target_toolchain",
69+
archive = "1_target.tar",
70+
expected = [
71+
"tar/tests/generated.txt",
72+
"tar/tests/src_file",
73+
],
74+
)
75+
4976
#############
5077
# Example 2: exact control of the resulting tar file, using a custom specification in the "mtree" format.
5178
# Copied from the output of `man tar`:

tar/toolchain/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ package(default_visibility = ["//visibility:public"])
44

55
toolchain_type(name = "type")
66

7+
toolchain_type(name = "target_type")
8+
79
utf8_environment(name = "utf8_environment")

tar/toolchain/toolchain.bzl

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,29 @@ resolved_toolchain = rule(
8484
implementation = _resolved_toolchain_impl,
8585
toolchains = ["@tar.bzl//tar/toolchain:type"],
8686
)
87+
88+
def _resolved_target_toolchain_impl(ctx):
89+
toolchain_info = ctx.toolchains["@tar.bzl//tar/toolchain:target_type"]
90+
return [
91+
toolchain_info,
92+
toolchain_info.default,
93+
toolchain_info.tarinfo,
94+
toolchain_info.template_variables,
95+
]
96+
97+
resolved_target_toolchain = rule(
98+
implementation = _resolved_target_toolchain_impl,
99+
toolchains = ["@tar.bzl//tar/toolchain:target_type"],
100+
)
87101
"""
88102
rctx.file("defs.bzl", starlark_content)
89103

90104
build_content = """# @generated by @tar.bzl//tar/toolchain:toolchain.bzl
91-
load(":defs.bzl", "resolved_toolchain")
105+
load(":defs.bzl", "resolved_toolchain", "resolved_target_toolchain")
92106
93-
resolved_toolchain(name = "resolved_toolchain", visibility = ["//visibility:public"])"""
107+
resolved_toolchain(name = "resolved_toolchain", visibility = ["//visibility:public"])
108+
109+
resolved_target_toolchain(name = "resolved_target_toolchain", visibility = ["//visibility:public"])"""
94110

95111
for [platform, meta] in BSDTAR_PLATFORMS.items():
96112
build_content += """
@@ -100,6 +116,13 @@ toolchain(
100116
toolchain = "@{user_repository_name}_{platform}//:bsdtar_toolchain",
101117
toolchain_type = "@tar.bzl//tar/toolchain:type",
102118
)
119+
120+
toolchain(
121+
name = "{platform}_target_toolchain",
122+
target_compatible_with = {compatible_with},
123+
toolchain = "@{user_repository_name}_{platform}//:bsdtar_toolchain",
124+
toolchain_type = "@tar.bzl//tar/toolchain:target_type",
125+
)
103126
""".format(
104127
platform = platform,
105128
user_repository_name = rctx.attr.user_repository_name,

0 commit comments

Comments
 (0)