From 197b2da974b7adc0718f77d2f7d532d8136667dc Mon Sep 17 00:00:00 2001 From: Sahin Yort Date: Fri, 1 Mar 2024 14:51:47 -0800 Subject: [PATCH] feat: support location expansion in tar (#774) --- docs/tar.md | 5 +++-- lib/BUILD.bazel | 2 +- lib/tar.bzl | 19 ++++++++++++++----- lib/tests/tar/BUILD.bazel | 18 ++++++++++++++++++ 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/docs/tar.md b/docs/tar.md index 8a08485..4b0c59a 100644 --- a/docs/tar.md +++ b/docs/tar.md @@ -100,7 +100,7 @@ Rule that executes BSD `tar`. Most users should use the [`tar`](#tar) macro, rat ## tar
-tar(name, mtree, kwargs)
+tar(name, mtree, stamp, kwargs)
 
Wrapper macro around [`tar_rule`](#tar_rule). @@ -136,7 +136,8 @@ https://man.freebsd.org/cgi/man.cgi?mtree(8) | Name | Description | Default Value | | :------------- | :------------- | :------------- | | name | name of resulting tar_rule | none | -| mtree | "auto", or an array of specification lines, or a label of a file that contains the lines. | "auto" | +| mtree | "auto", or an array of specification lines, or a label of a file that contains the lines. Subject to [$(location)](https://bazel.build/reference/be/make-variables#predefined_label_variables) and ["Make variable"](https://bazel.build/reference/be/make-variables) substitution. | "auto" | +| stamp | should mtree attribute be stamped | 0 | | kwargs | additional named parameters to pass to tar_rule | none | diff --git a/lib/BUILD.bazel b/lib/BUILD.bazel index 0a56285..442f5d8 100644 --- a/lib/BUILD.bazel +++ b/lib/BUILD.bazel @@ -104,10 +104,10 @@ bzl_library( name = "tar", srcs = ["tar.bzl"], deps = [ + "//lib:expand_template", "//lib:utils", "//lib/private:tar", "@bazel_skylib//lib:types", - "@bazel_skylib//rules:write_file", ], ) diff --git a/lib/tar.bzl b/lib/tar.bzl index f62fef6..3dd7a65 100644 --- a/lib/tar.bzl +++ b/lib/tar.bzl @@ -50,7 +50,7 @@ TODO: """ load("@bazel_skylib//lib:types.bzl", "types") -load("@bazel_skylib//rules:write_file.bzl", "write_file") +load("//lib:expand_template.bzl", "expand_template") load("//lib:utils.bzl", "propagate_common_rule_attributes") load("//lib/private:tar.bzl", _tar = "tar", _tar_lib = "tar_lib") @@ -64,7 +64,7 @@ tar_rule = _tar tar_lib = _tar_lib -def tar(name, mtree = "auto", **kwargs): +def tar(name, mtree = "auto", stamp = 0, **kwargs): """Wrapper macro around [`tar_rule`](#tar_rule). ### Options for mtree @@ -94,6 +94,9 @@ def tar(name, mtree = "auto", **kwargs): Args: name: name of resulting `tar_rule` mtree: "auto", or an array of specification lines, or a label of a file that contains the lines. + Subject to [$(location)](https://bazel.build/reference/be/make-variables#predefined_label_variables) + and ["Make variable"](https://bazel.build/reference/be/make-variables) substitution. + stamp: should mtree attribute be stamped **kwargs: additional named parameters to pass to `tar_rule` """ mtree_target = "_{}.mtree".format(name) @@ -105,12 +108,18 @@ def tar(name, mtree = "auto", **kwargs): **propagate_common_rule_attributes(kwargs) ) elif types.is_list(mtree): - write_file( + expand_template( name = mtree_target, out = "{}.txt".format(mtree_target), + data = kwargs["srcs"], # Ensure there's a trailing newline, as bsdtar will ignore a last line without one - content = mtree + [""], - newline = "unix", + template = ["#mtree", "{content}", ""], + substitutions = { + # expand_template only expands strings in "substitions" dict. Here + # we expand mtree and then replace the template with expanded mtree. + "{content}": "\n".join(mtree), + }, + stamp = stamp, **propagate_common_rule_attributes(kwargs) ) else: diff --git a/lib/tests/tar/BUILD.bazel b/lib/tests/tar/BUILD.bazel index 010e8e9..0036d30 100644 --- a/lib/tests/tar/BUILD.bazel +++ b/lib/tests/tar/BUILD.bazel @@ -310,3 +310,21 @@ assert_archive_contains( "LICENSE", ], ) + +# Case 10: Can reference generated files +tar( + name = "tar_location_expansion", + srcs = ["@bazel_skylib//:LICENSE"], + out = "10.tar", + mtree = [ + "a uid=0 gid=0 time=1672560000 mode=0755 type=file content=$(location @bazel_skylib//:LICENSE)", + ], +) + +assert_tar_listing( + name = "test_tar_location_expansion", + actual = "tar_location_expansion", + expected = [ + "-rwxr-xr-x 0 0 0 11358 Jan 1 2023 a", + ], +)