Skip to content

Commit 9489051

Browse files
Add support for make var expansion
1 parent 8c7c2cf commit 9489051

File tree

6 files changed

+48
-2
lines changed

6 files changed

+48
-2
lines changed

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module(
99
bazel_dep(name = "rules_license", version = "1.0.0")
1010
bazel_dep(name = "rules_python", version = "1.0.0")
1111
bazel_dep(name = "bazel_skylib", version = "1.7.1")
12+
bazel_dep(name = "aspect_bazel_lib", version = "2.14.0")
1213

1314
# Only for development
1415
bazel_dep(name = "platforms", version = "0.0.10", dev_dependency = True)

WORKSPACE

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,19 @@ http_archive(
8181
load("@rules_cc//cc:repositories.bzl", "rules_cc_dependencies")
8282

8383
rules_cc_dependencies()
84+
85+
http_archive(
86+
name = "aspect_bazel_lib",
87+
sha256 = "6d636cfdecc7f5c1a5d82b9790fb5d5d5e8aa6ea8b53a71a75f1ba53c8d29f61",
88+
strip_prefix = "bazel-lib-2.21.0",
89+
url = "https://github.com/bazel-contrib/bazel-lib/releases/download/v2.21.0/bazel-lib-v2.21.0.tar.gz",
90+
)
91+
92+
aspect_bazel_lib_dependencies()
93+
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
94+
load("@platforms//host:extension.bzl", "host_platform_repo")
95+
96+
maybe(
97+
host_platform_repo,
98+
name = "host_platform",
99+
)

doc_build/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ genrule(
7272
deps = [
7373
":rules_pkg_lib",
7474
"//toolchains/rpm:standard_package",
75+
"@aspect_bazel_lib//lib:expand_make_vars",
7576
],
7677
)
7778
for rule, src in ORDER

pkg/private/tar/tar.bzl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
"""Rules for making .tar files."""
1515

16+
load("@aspect_bazel_lib//lib:expand_make_vars.bzl", "expand_variables")
1617
load("//pkg:providers.bzl", "PackageVariablesInfo")
1718
load(
1819
"//pkg/private:pkg_files.bzl",
@@ -104,7 +105,8 @@ def _pkg_tar_impl(ctx):
104105
args.add("--mtime", "portable")
105106
if ctx.attr.modes:
106107
for key in ctx.attr.modes:
107-
args.add("--modes", "%s=%s" % (_quote(key), ctx.attr.modes[key]))
108+
expanded_key = expand_variables(ctx, key)
109+
args.add("--modes", "%s=%s" % (_quote(expanded_key), ctx.attr.modes[key]))
108110
if ctx.attr.owners:
109111
for key in ctx.attr.owners:
110112
args.add("--owners", "%s=%s" % (_quote(key), ctx.attr.owners[key]))
@@ -119,8 +121,14 @@ def _pkg_tar_impl(ctx):
119121

120122
# Now we begin processing the files.
121123
path_mapper = None
124+
expanded_remap_paths = {}
122125
if ctx.attr.remap_paths:
123-
path_mapper = lambda path: _remap(ctx.attr.remap_paths, path)
126+
for prefix, replacement in ctx.attr.remap_paths.items():
127+
expanded_prefix = expand_variables(ctx, prefix)
128+
expanded_replacement = expand_variables(ctx, replacement)
129+
expanded_remap_paths[expanded_prefix] = expanded_replacement
130+
131+
path_mapper = lambda path: _remap(expanded_remap_paths, path)
124132

125133
mapping_context = create_mapping_context_from_ctx(
126134
ctx,

tests/tar/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ py_test(
464464
":test-pkg-tar-from-pkg-files-with-attributes",
465465
":test-pkg-tar-with-attributes",
466466
":test-remap-paths-tree-artifact",
467+
":test-remap-paths-variables",
467468
":test-respect-externally-defined-duplicates.tar",
468469
":test-tar-compression_level--1",
469470
":test-tar-compression_level-3",
@@ -628,6 +629,16 @@ pkg_tar(
628629
},
629630
)
630631

632+
pkg_tar(
633+
name = "test-remap-paths-variables",
634+
srcs = [
635+
":tree_artifact_to_rename",
636+
],
637+
remap_paths = {
638+
"/rename_me": "$(COMPILATION_MODE)",
639+
},
640+
)
641+
631642
#
632643
# Test with symlinks
633644
#

tests/tar/pkg_tar_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,15 @@ def test_remap_paths_tree_artifact(self):
285285
]
286286
self.assertTarFileContent('test-remap-paths-tree-artifact.tar', content)
287287

288+
def test_remap_paths_make_variables(self):
289+
content = [
290+
{'name': 'fastbuild', 'isdir': True},
291+
{'name': 'fastbuild/a'},
292+
{'name': 'fastbuild/rename_me', 'isdir': True},
293+
{'name': 'fastbuild/rename_me/should_not_rename'},
294+
]
295+
self.assertTarFileContent('test-remap-paths-variables.tar', content)
296+
288297
def test_externally_defined_duplicate_structure(self):
289298
content = [
290299
{'name': './a'},

0 commit comments

Comments
 (0)