Skip to content

Commit e30e89f

Browse files
authored
feat: add run_binary with output directory support & improved makevar expansion (#57)
1 parent 096133e commit e30e89f

12 files changed

+308
-94
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Aspect's Bazel helpers library
22

3-
This is code we would contribute to bazel-skylib,
4-
but the declared scope of that project is narrow
5-
and it's very difficult to get anyone's attention
6-
to review PRs there.
3+
This is code we would contribute to
4+
[bazel-skylib](https://github.com/bazelbuild/bazel-skylib), but the declared
5+
scope of that project is narrow and it's very difficult to get anyone's
6+
attention to review PRs there.
77

88
## Installation
99

docs/BUILD.bazel

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
# so that the dependency on stardoc doesn't leak to them.
33
load("//lib:docs.bzl", "stardoc_with_diff_test", "update_docs")
44

5+
stardoc_with_diff_test(
6+
name = "copy_file",
7+
bzl_library_target = "//lib:copy_file",
8+
)
9+
510
stardoc_with_diff_test(
611
name = "copy_to_directory",
712
bzl_library_target = "//lib:copy_to_directory",
@@ -53,6 +58,11 @@ stardoc_with_diff_test(
5358
bzl_library_target = "//lib:output_files",
5459
)
5560

61+
stardoc_with_diff_test(
62+
name = "run_binary",
63+
bzl_library_target = "//lib:run_binary",
64+
)
65+
5666
stardoc_with_diff_test(
5767
name = "transitions",
5868
bzl_library_target = "//lib:transitions",

docs/copy_file.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!-- Generated with Stardoc: http://skydoc.bazel.build -->
2+
3+
A rule that copies a file to another place.
4+
5+
native.genrule() is sometimes used to copy files (often wishing to rename them).
6+
The 'copy_file' rule does this with a simpler interface than genrule.
7+
8+
The rule uses a Bash command on Linux/macOS/non-Windows, and a cmd.exe command
9+
on Windows (no Bash is required).
10+
11+
This fork of bazel-skylib's copy_file adds directory support.
12+
13+
14+
<a id="#copy_file"></a>
15+
16+
## copy_file
17+
18+
<pre>
19+
copy_file(<a href="#copy_file-name">name</a>, <a href="#copy_file-src">src</a>, <a href="#copy_file-out">out</a>, <a href="#copy_file-is_directory">is_directory</a>, <a href="#copy_file-is_executable">is_executable</a>, <a href="#copy_file-allow_symlink">allow_symlink</a>, <a href="#copy_file-kwargs">kwargs</a>)
20+
</pre>
21+
22+
Copies a file or directory to another location.
23+
24+
`native.genrule()` is sometimes used to copy files (often wishing to rename them). The 'copy_file' rule does this with a simpler interface than genrule.
25+
26+
This rule uses a Bash command on Linux/macOS/non-Windows, and a cmd.exe command on Windows (no Bash is required).
27+
28+
If using this rule with source directories, it is recommended that you use the
29+
`--host_jvm_args=-DBAZEL_TRACK_SOURCE_DIRECTORIES=1` startup option so that changes
30+
to files within source directories are detected. See
31+
https://github.com/bazelbuild/bazel/commit/c64421bc35214f0414e4f4226cc953e8c55fa0d2
32+
for more context.
33+
34+
35+
**PARAMETERS**
36+
37+
38+
| Name | Description | Default Value |
39+
| :------------- | :------------- | :------------- |
40+
| <a id="copy_file-name"></a>name | Name of the rule. | none |
41+
| <a id="copy_file-src"></a>src | A Label. The file or directory to make a copy of. (Can also be the label of a rule that generates a file or directory.) | none |
42+
| <a id="copy_file-out"></a>out | Path of the output file, relative to this package. | none |
43+
| <a id="copy_file-is_directory"></a>is_directory | treat the source file as a directory Workaround for https://github.com/bazelbuild/bazel/issues/12954 | <code>False</code> |
44+
| <a id="copy_file-is_executable"></a>is_executable | A boolean. Whether to make the output file executable. When True, the rule's output can be executed using <code>bazel run</code> and can be in the srcs of binary and test rules that require executable sources. WARNING: If <code>allow_symlink</code> is True, <code>src</code> must also be executable. | <code>False</code> |
45+
| <a id="copy_file-allow_symlink"></a>allow_symlink | A boolean. Whether to allow symlinking instead of copying. When False, the output is always a hard copy. When True, the output *can* be a symlink, but there is no guarantee that a symlink is created (i.e., at the time of writing, we don't create symlinks on Windows). Set this to True if you need fast copying and your tools can handle symlinks (which most UNIX tools can). | <code>False</code> |
46+
| <a id="copy_file-kwargs"></a>kwargs | further keyword arguments, e.g. <code>visibility</code> | none |
47+
48+

docs/expand_make_vars.md

+2-15
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ expand_locations(<a href="#expand_locations-ctx">ctx</a>, <a href="#expand_locat
4444

4545
Expand location templates.
4646

47-
Expands all `$(execpath ...)`, `$(rootpath ...)` and legacy `$(location ...)` templates in the
47+
Expands all `$(execpath ...)`, `$(rootpath ...)` and deprecated `$(location ...)` templates in the
4848
given string by replacing with the expanded path. Expansion only works for labels that point to direct dependencies
4949
of this rule or that are explicitly listed in the optional argument targets.
5050

@@ -64,20 +64,7 @@ This is of the format:
6464
- `<bin_dir>/path/to/file`
6565
- `<bin_dir>/external/external_repo/path/to/file`
6666

67-
The legacy `$(location)` and `$(locations)` expansions are deprecated as they return the runfiles manifest path of the
68-
format `repo/path/to/file` which behave differently than the built-in `$(location)` expansion in args of *_binary
69-
and *_test rules which returns the rootpath.
70-
See https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes-binaries.
71-
72-
The legacy `$(location)` and `$(locations)` expansion also differs from how the builtin `ctx.expand_location()` expansions
73-
of `$(location)` and `$(locations)` behave as that function returns either the execpath or rootpath depending on the context.
74-
See https://docs.bazel.build/versions/main/be/make-variables.html#predefined_label_variables.
75-
76-
The behavior of `$(location)` and `$(locations)` expansion will be fixed in a future major release to match the
77-
to default Bazel behavior and return the same path as `ctx.expand_location()` returns for these.
78-
79-
The recommended approach is to now use `$(rootpath)` where you previously used $(location). See the docstrings
80-
of `nodejs_binary` or `params_file` for examples of how to use `$(rootpath)` in `templated_args` and `args` respectively.
67+
The deprecated `$(location)` and `$(locations)` expansions returns either the execpath or rootpath depending on the context.
8168

8269

8370
**PARAMETERS**

docs/run_binary.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!-- Generated with Stardoc: http://skydoc.bazel.build -->
2+
3+
Runs a binary as a build action. This rule does not require Bash (unlike native.genrule()).
4+
5+
This fork of bazel-skylib's run_binary adds directory output support and better makevar expansions.
6+
7+
8+
<a id="#run_binary"></a>
9+
10+
## run_binary
11+
12+
<pre>
13+
run_binary(<a href="#run_binary-name">name</a>, <a href="#run_binary-args">args</a>, <a href="#run_binary-env">env</a>, <a href="#run_binary-output_dir">output_dir</a>, <a href="#run_binary-outs">outs</a>, <a href="#run_binary-srcs">srcs</a>, <a href="#run_binary-tool">tool</a>)
14+
</pre>
15+
16+
Runs a binary as a build action.<br/><br/>This rule does not require Bash (unlike <code>native.genrule</code>).
17+
18+
**ATTRIBUTES**
19+
20+
21+
| Name | Description | Type | Mandatory | Default |
22+
| :------------- | :------------- | :------------- | :------------- | :------------- |
23+
| <a id="run_binary-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
24+
| <a id="run_binary-args"></a>args | Command line arguments of the binary.&lt;br/&gt;&lt;br/&gt;Subject to&lt;code&gt;&lt;a href="https://docs.bazel.build/versions/main/be/make-variables.html#location"&gt;$(location)&lt;/a&gt;&lt;/code&gt; expansion. | List of strings | optional | [] |
25+
| <a id="run_binary-env"></a>env | Environment variables of the action.&lt;br/&gt;&lt;br/&gt;Subject to &lt;code&gt;&lt;a href="https://docs.bazel.build/versions/main/be/make-variables.html#location"&gt;$(location)&lt;/a&gt;&lt;/code&gt; expansion. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
26+
| <a id="run_binary-output_dir"></a>output_dir | Set to True if you want the output to be a directory. Exactly one of <code>outs</code>, <code>output_dir</code> may be used. If you output a directory, there can only be one output, which will be a directory named the same as the target. | Boolean | optional | False |
27+
| <a id="run_binary-outs"></a>outs | Output files generated by the action.&lt;br/&gt;&lt;br/&gt;These labels are available for &lt;code&gt;$(location)&lt;/code&gt; expansion in &lt;code&gt;args&lt;/code&gt; and &lt;code&gt;env&lt;/code&gt;. | List of labels | optional | |
28+
| <a id="run_binary-srcs"></a>srcs | Additional inputs of the action.&lt;br/&gt;&lt;br/&gt;These labels are available for &lt;code&gt;$(location)&lt;/code&gt; expansion in &lt;code&gt;args&lt;/code&gt; and &lt;code&gt;env&lt;/code&gt;. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
29+
| <a id="run_binary-tool"></a>tool | The tool to run in the action.&lt;br/&gt;&lt;br/&gt;Must be the label of a *_binary rule, of a rule that generates an executable file, or of a file that can be executed as a subprocess (e.g. an .exe or .bat file on Windows or a binary with executable permission on Linux). This label is available for &lt;code&gt;$(location)&lt;/code&gt; expansion in &lt;code&gt;args&lt;/code&gt; and &lt;code&gt;env&lt;/code&gt;. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
30+
31+

lib/BUILD.bazel

+20-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ exports_files(
88
],
99
)
1010

11+
toolchain_type(
12+
name = "jq_toolchain_type",
13+
visibility = ["//visibility:public"],
14+
)
15+
1116
bzl_library(
1217
name = "docs",
1318
srcs = ["docs.bzl"],
@@ -65,15 +70,17 @@ bzl_library(
6570
)
6671

6772
bzl_library(
68-
name = "copy_to_directory",
69-
srcs = ["copy_to_directory.bzl"],
73+
name = "copy_file",
74+
srcs = ["copy_file.bzl"],
7075
visibility = ["//visibility:public"],
71-
deps = ["//lib/private:copy_to_directory"],
76+
deps = ["//lib/private:copy_file"],
7277
)
7378

74-
toolchain_type(
75-
name = "jq_toolchain_type",
79+
bzl_library(
80+
name = "copy_to_directory",
81+
srcs = ["copy_to_directory.bzl"],
7682
visibility = ["//visibility:public"],
83+
deps = ["//lib/private:copy_to_directory"],
7784
)
7885

7986
bzl_library(
@@ -90,11 +97,18 @@ bzl_library(
9097

9198
bzl_library(
9299
name = "diff_test",
93-
srcs = ["diff_test.bzl"], # keep
100+
srcs = ["diff_test.bzl"],
94101
visibility = ["//visibility:public"],
95102
deps = ["//lib/private:diff_test"],
96103
)
97104

105+
bzl_library(
106+
name = "run_binary",
107+
srcs = ["run_binary.bzl"],
108+
visibility = ["//visibility:public"],
109+
deps = ["//lib/private:run_binary"],
110+
)
111+
98112
bzl_library(
99113
name = "transitions",
100114
srcs = ["transitions.bzl"],

lib/copy_file.bzl

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ The 'copy_file' rule does this with a simpler interface than genrule.
2525
2626
The rule uses a Bash command on Linux/macOS/non-Windows, and a cmd.exe command
2727
on Windows (no Bash is required).
28+
29+
This fork of bazel-skylib's copy_file adds directory support.
2830
"""
2931

3032
load(

lib/private/BUILD.bazel

+17
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ exports_files(
55
visibility = ["//docs:__pkg__"],
66
)
77

8+
bzl_library(
9+
name = "copy_file",
10+
srcs = ["copy_file.bzl"],
11+
visibility = ["//lib:__subpackages__"],
12+
deps = [":directory_path"],
13+
)
14+
815
bzl_library(
916
name = "copy_to_directory",
1017
srcs = ["copy_to_directory.bzl"],
@@ -97,3 +104,13 @@ bzl_library(
97104
srcs = ["diff_test.bzl"],
98105
visibility = ["//lib:__subpackages__"],
99106
)
107+
108+
bzl_library(
109+
name = "run_binary",
110+
srcs = ["run_binary.bzl"],
111+
visibility = ["//lib:__subpackages__"],
112+
deps = [
113+
":expand_make_vars",
114+
"@bazel_skylib//lib:dicts",
115+
],
116+
)

lib/private/expand_make_vars.bzl

+4-69
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,9 @@
11
"Helpers to expand make variables"
22

3-
# Convert an runfiles rootpath to a runfiles manifestpath.
4-
# Runfiles rootpath is returned from ctx.expand_location $(rootpath) and $(rootpaths):
5-
# - ./file
6-
# - path/to/file
7-
# - ../external_repo/path/to/file
8-
# This is converted to the runfiles manifest path of:
9-
# - repo/path/to/file
10-
def _rootpath_to_runfiles_manifest_path(ctx, path):
11-
if path.startswith("../"):
12-
return path[len("../"):]
13-
if path.startswith("./"):
14-
path = path[len("./"):]
15-
return ctx.workspace_name + "/" + path
16-
17-
# Expand $(rootpath) and $(rootpaths) to runfiles manifest path.
18-
# Runfiles manifest path is of the form:
19-
# - repo/path/to/file
20-
def _expand_rootpath_to_manifest_path(ctx, input, targets):
21-
paths = ctx.expand_location(input, targets)
22-
return " ".join([_rootpath_to_runfiles_manifest_path(ctx, p) for p in paths.split(" ")])
23-
243
def expand_locations(ctx, input, targets = []):
254
"""Expand location templates.
265
27-
Expands all `$(execpath ...)`, `$(rootpath ...)` and legacy `$(location ...)` templates in the
6+
Expands all `$(execpath ...)`, `$(rootpath ...)` and deprecated `$(location ...)` templates in the
287
given string by replacing with the expanded path. Expansion only works for labels that point to direct dependencies
298
of this rule or that are explicitly listed in the optional argument targets.
309
@@ -44,20 +23,7 @@ def expand_locations(ctx, input, targets = []):
4423
- `<bin_dir>/path/to/file`
4524
- `<bin_dir>/external/external_repo/path/to/file`
4625
47-
The legacy `$(location)` and `$(locations)` expansions are deprecated as they return the runfiles manifest path of the
48-
format `repo/path/to/file` which behave differently than the built-in `$(location)` expansion in args of *_binary
49-
and *_test rules which returns the rootpath.
50-
See https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes-binaries.
51-
52-
The legacy `$(location)` and `$(locations)` expansion also differs from how the builtin `ctx.expand_location()` expansions
53-
of `$(location)` and `$(locations)` behave as that function returns either the execpath or rootpath depending on the context.
54-
See https://docs.bazel.build/versions/main/be/make-variables.html#predefined_label_variables.
55-
56-
The behavior of `$(location)` and `$(locations)` expansion will be fixed in a future major release to match the
57-
to default Bazel behavior and return the same path as `ctx.expand_location()` returns for these.
58-
59-
The recommended approach is to now use `$(rootpath)` where you previously used $(location). See the docstrings
60-
of `nodejs_binary` or `params_file` for examples of how to use `$(rootpath)` in `templated_args` and `args` respectively.
26+
The deprecated `$(location)` and `$(locations)` expansions returns either the execpath or rootpath depending on the context.
6127
6228
Args:
6329
ctx: context
@@ -67,39 +33,8 @@ def expand_locations(ctx, input, targets = []):
6733
Returns:
6834
The expanded path or the original path
6935
"""
70-
target = "@%s//%s:%s" % (ctx.workspace_name, "/".join(ctx.build_file_path.split("/")[:-1]), ctx.attr.name)
71-
72-
# Loop through input an expand all predefined source/output path variables
73-
# See https://docs.bazel.build/versions/main/be/make-variables.html#predefined_label_variables.
74-
path = ""
75-
length = len(input)
76-
last = 0
77-
for i in range(length):
78-
# Support legacy $(location) and $(locations) expansions which return the runfiles manifest path
79-
# in the format `repo/path/to/file`. This expansion is DEPRECATED. See docstring above.
80-
# TODO: Change location to behave the same as the built-in $(location) expansion for args of *_binary
81-
# and *_test rules. This would be a BREAKING CHANGE.
82-
if input[i:].startswith("$(location ") or input[i:].startswith("$(locations "):
83-
j = input.find(")", i) + 1
84-
if (j == 0):
85-
fail("invalid \"%s\" expansion in string \"%s\" part of target %s" % (input[i:j], input, target))
86-
path += input[last:i]
87-
path += _expand_rootpath_to_manifest_path(ctx, "$(rootpath" + input[i + 10:j], targets)
88-
last = j
89-
i = j
90-
91-
# Expand $(execpath) $(execpaths) $(rootpath) $(rootpaths) with plain ctx.expand_location()
92-
if input[i:].startswith("$(execpath ") or input[i:].startswith("$(execpaths ") or input[i:].startswith("$(rootpath ") or input[i:].startswith("$(rootpaths "):
93-
j = input.find(")", i) + 1
94-
if (j == 0):
95-
fail("invalid \"%s\" expansion in string \"%s\" part of target %s" % (input[i:j], input, target))
96-
path += input[last:i]
97-
path += ctx.expand_location(input[i:j], targets)
98-
last = j
99-
i = j
100-
path += input[last:]
101-
102-
return path
36+
37+
return ctx.expand_location(input, targets = targets)
10338

10439
def expand_variables(ctx, s, outs = [], output_dir = False, attribute_name = "args"):
10540
"""Expand make variables and substitute like genrule does.

0 commit comments

Comments
 (0)