diff --git a/README.md b/README.md index f5ebd2d..95a0067 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # Aspect's Bazel helpers library -This is code we would contribute to bazel-skylib, -but the declared scope of that project is narrow -and it's very difficult to get anyone's attention -to review PRs there. +This is code we would contribute to +[bazel-skylib](https://github.com/bazelbuild/bazel-skylib), but the declared +scope of that project is narrow and it's very difficult to get anyone's +attention to review PRs there. ## Installation diff --git a/docs/BUILD.bazel b/docs/BUILD.bazel index ebb6307..7931422 100644 --- a/docs/BUILD.bazel +++ b/docs/BUILD.bazel @@ -2,6 +2,11 @@ # so that the dependency on stardoc doesn't leak to them. load("//lib:docs.bzl", "stardoc_with_diff_test", "update_docs") +stardoc_with_diff_test( + name = "copy_file", + bzl_library_target = "//lib:copy_file", +) + stardoc_with_diff_test( name = "copy_to_directory", bzl_library_target = "//lib:copy_to_directory", @@ -53,6 +58,11 @@ stardoc_with_diff_test( bzl_library_target = "//lib:output_files", ) +stardoc_with_diff_test( + name = "run_binary", + bzl_library_target = "//lib:run_binary", +) + stardoc_with_diff_test( name = "transitions", bzl_library_target = "//lib:transitions", diff --git a/docs/copy_file.md b/docs/copy_file.md new file mode 100644 index 0000000..3af9caa --- /dev/null +++ b/docs/copy_file.md @@ -0,0 +1,48 @@ + + +A rule that copies a file to another place. + +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. + +The rule uses a Bash command on Linux/macOS/non-Windows, and a cmd.exe command +on Windows (no Bash is required). + +This fork of bazel-skylib's copy_file adds directory support. + + + + +## copy_file + +
+copy_file(name, src, out, is_directory, is_executable, allow_symlink, kwargs) ++ +Copies a file or directory to another location. + +`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. + +This rule uses a Bash command on Linux/macOS/non-Windows, and a cmd.exe command on Windows (no Bash is required). + +If using this rule with source directories, it is recommended that you use the +`--host_jvm_args=-DBAZEL_TRACK_SOURCE_DIRECTORIES=1` startup option so that changes +to files within source directories are detected. See +https://github.com/bazelbuild/bazel/commit/c64421bc35214f0414e4f4226cc953e8c55fa0d2 +for more context. + + +**PARAMETERS** + + +| Name | Description | Default Value | +| :------------- | :------------- | :------------- | +| name | Name of the rule. | none | +| 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 | +| out | Path of the output file, relative to this package. | none | +| is_directory | treat the source file as a directory Workaround for https://github.com/bazelbuild/bazel/issues/12954 |
False
|
+| is_executable | A boolean. Whether to make the output file executable. When True, the rule's output can be executed using bazel run
and can be in the srcs of binary and test rules that require executable sources. WARNING: If allow_symlink
is True, src
must also be executable. | False
|
+| 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). | False
|
+| kwargs | further keyword arguments, e.g. visibility
| none |
+
+
diff --git a/docs/expand_make_vars.md b/docs/expand_make_vars.md
index 74974f8..955e9a6 100644
--- a/docs/expand_make_vars.md
+++ b/docs/expand_make_vars.md
@@ -44,7 +44,7 @@ expand_locations(ctx,
+
+## run_binary
+
++run_binary(name, args, env, output_dir, outs, srcs, tool) ++ +Runs a binary as a build action.
native.genrule
).
+
+**ATTRIBUTES**
+
+
+| Name | Description | Type | Mandatory | Default |
+| :------------- | :------------- | :------------- | :------------- | :------------- |
+| name | A unique name for this target. | Name | required | |
+| args | Command line arguments of the binary.<br/><br/>Subject to<code><a href="https://docs.bazel.build/versions/main/be/make-variables.html#location">$(location)</a></code> expansion. | List of strings | optional | [] |
+| env | Environment variables of the action.<br/><br/>Subject to <code><a href="https://docs.bazel.build/versions/main/be/make-variables.html#location">$(location)</a></code> expansion. | Dictionary: String -> String | optional | {} |
+| output_dir | Set to True if you want the output to be a directory. Exactly one of outs
, output_dir
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 |
+| outs | Output files generated by the action.<br/><br/>These labels are available for <code>$(location)</code> expansion in <code>args</code> and <code>env</code>. | List of labels | optional | |
+| srcs | Additional inputs of the action.<br/><br/>These labels are available for <code>$(location)</code> expansion in <code>args</code> and <code>env</code>. | List of labels | optional | [] |
+| tool | The tool to run in the action.<br/><br/>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 <code>$(location)</code> expansion in <code>args</code> and <code>env</code>. | Label | required | |
+
+
diff --git a/lib/BUILD.bazel b/lib/BUILD.bazel
index 45f0a54..915069f 100644
--- a/lib/BUILD.bazel
+++ b/lib/BUILD.bazel
@@ -8,6 +8,11 @@ exports_files(
],
)
+toolchain_type(
+ name = "jq_toolchain_type",
+ visibility = ["//visibility:public"],
+)
+
bzl_library(
name = "docs",
srcs = ["docs.bzl"],
@@ -64,6 +69,13 @@ bzl_library(
deps = ["//lib/private:output_files"],
)
+bzl_library(
+ name = "copy_file",
+ srcs = ["copy_file.bzl"],
+ visibility = ["//visibility:public"],
+ deps = ["//lib/private:copy_file"],
+)
+
bzl_library(
name = "copy_to_directory",
srcs = ["copy_to_directory.bzl"],
@@ -71,11 +83,6 @@ bzl_library(
deps = ["//lib/private:copy_to_directory"],
)
-toolchain_type(
- name = "jq_toolchain_type",
- visibility = ["//visibility:public"],
-)
-
bzl_library(
name = "write_source_files",
srcs = ["write_source_files.bzl"],
@@ -90,11 +97,18 @@ bzl_library(
bzl_library(
name = "diff_test",
- srcs = ["diff_test.bzl"], # keep
+ srcs = ["diff_test.bzl"],
visibility = ["//visibility:public"],
deps = ["//lib/private:diff_test"],
)
+bzl_library(
+ name = "run_binary",
+ srcs = ["run_binary.bzl"],
+ visibility = ["//visibility:public"],
+ deps = ["//lib/private:run_binary"],
+)
+
bzl_library(
name = "transitions",
srcs = ["transitions.bzl"],
diff --git a/lib/copy_file.bzl b/lib/copy_file.bzl
index bcde82f..1db8545 100644
--- a/lib/copy_file.bzl
+++ b/lib/copy_file.bzl
@@ -25,6 +25,8 @@ The 'copy_file' rule does this with a simpler interface than genrule.
The rule uses a Bash command on Linux/macOS/non-Windows, and a cmd.exe command
on Windows (no Bash is required).
+
+This fork of bazel-skylib's copy_file adds directory support.
"""
load(
diff --git a/lib/private/BUILD.bazel b/lib/private/BUILD.bazel
index 88cbb7b..bb195ce 100644
--- a/lib/private/BUILD.bazel
+++ b/lib/private/BUILD.bazel
@@ -5,6 +5,13 @@ exports_files(
visibility = ["//docs:__pkg__"],
)
+bzl_library(
+ name = "copy_file",
+ srcs = ["copy_file.bzl"],
+ visibility = ["//lib:__subpackages__"],
+ deps = [":directory_path"],
+)
+
bzl_library(
name = "copy_to_directory",
srcs = ["copy_to_directory.bzl"],
@@ -97,3 +104,13 @@ bzl_library(
srcs = ["diff_test.bzl"],
visibility = ["//lib:__subpackages__"],
)
+
+bzl_library(
+ name = "run_binary",
+ srcs = ["run_binary.bzl"],
+ visibility = ["//lib:__subpackages__"],
+ deps = [
+ ":expand_make_vars",
+ "@bazel_skylib//lib:dicts",
+ ],
+)
diff --git a/lib/private/expand_make_vars.bzl b/lib/private/expand_make_vars.bzl
index e9d6d8e..761f427 100644
--- a/lib/private/expand_make_vars.bzl
+++ b/lib/private/expand_make_vars.bzl
@@ -1,30 +1,9 @@
"Helpers to expand make variables"
-# Convert an runfiles rootpath to a runfiles manifestpath.
-# Runfiles rootpath is returned from ctx.expand_location $(rootpath) and $(rootpaths):
-# - ./file
-# - path/to/file
-# - ../external_repo/path/to/file
-# This is converted to the runfiles manifest path of:
-# - repo/path/to/file
-def _rootpath_to_runfiles_manifest_path(ctx, path):
- if path.startswith("../"):
- return path[len("../"):]
- if path.startswith("./"):
- path = path[len("./"):]
- return ctx.workspace_name + "/" + path
-
-# Expand $(rootpath) and $(rootpaths) to runfiles manifest path.
-# Runfiles manifest path is of the form:
-# - repo/path/to/file
-def _expand_rootpath_to_manifest_path(ctx, input, targets):
- paths = ctx.expand_location(input, targets)
- return " ".join([_rootpath_to_runfiles_manifest_path(ctx, p) for p in paths.split(" ")])
-
def expand_locations(ctx, input, targets = []):
"""Expand location templates.
- Expands all `$(execpath ...)`, `$(rootpath ...)` and legacy `$(location ...)` templates in the
+ Expands all `$(execpath ...)`, `$(rootpath ...)` and deprecated `$(location ...)` templates in the
given string by replacing with the expanded path. Expansion only works for labels that point to direct dependencies
of this rule or that are explicitly listed in the optional argument targets.
@@ -44,20 +23,7 @@ def expand_locations(ctx, input, targets = []):
- `native.genrule
).",
+ attrs = {
+ "tool": attr.label(
+ doc = "The tool to run in the action.$(location)
expansion in args
and env
.",
+ executable = True,
+ allow_files = True,
+ mandatory = True,
+ cfg = "host",
+ ),
+ "env": attr.string_dict(
+ doc = "Environment variables of the action.$(location)
" +
+ " expansion.",
+ ),
+ "srcs": attr.label_list(
+ allow_files = True,
+ doc = "Additional inputs of the action.$(location)
expansion in args
and env
.",
+ ),
+ "output_dir": attr.bool(
+ doc = "Set to True if you want the output to be a directory." +
+ " Exactly one of `outs`, `output_dir` 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.",
+ ),
+ "outs": attr.output_list(
+ doc = "Output files generated by the action.$(location)
expansion in args
and env
.",
+ ),
+ "args": attr.string_list(
+ doc = "Command line arguments of the binary.$(location)
" +
+ " expansion.",
+ ),
+ },
+)
diff --git a/lib/run_binary.bzl b/lib/run_binary.bzl
new file mode 100644
index 0000000..abd8f04
--- /dev/null
+++ b/lib/run_binary.bzl
@@ -0,0 +1,11 @@
+"""Runs a binary as a build action. This rule does not require Bash (unlike native.genrule()).
+
+This fork of bazel-skylib's run_binary adds directory output support and better makevar expansions.
+"""
+
+load(
+ "//lib/private:run_binary.bzl",
+ _run_binary = "run_binary",
+)
+
+run_binary = _run_binary
diff --git a/lib/tests/run_binary/BUILD.bazel b/lib/tests/run_binary/BUILD.bazel
new file mode 100644
index 0000000..3625e93
--- /dev/null
+++ b/lib/tests/run_binary/BUILD.bazel
@@ -0,0 +1,58 @@
+"tests for run_binary"
+
+load("@bazel_skylib//rules:write_file.bzl", "write_file")
+load("//lib:run_binary.bzl", "run_binary")
+load("//lib:diff_test.bzl", "diff_test")
+load("//lib:copy_to_directory.bzl", "copy_to_directory")
+
+write_file(
+ name = "make_dir_a_sh",
+ out = "make_dir_a.sh",
+ content = [
+ "#!/usr/bin/env bash",
+ "set -o errexit -o nounset -o pipefail",
+ "outdir=$1",
+ "echo 'foo' > \"$outdir/foo\"",
+ "echo 'bar' > \"$outdir/bar\"",
+ ],
+ is_executable = True,
+)
+
+sh_binary(
+ name = "make_dir_a",
+ srcs = [":make_dir_a.sh"],
+)
+
+# target-under-test
+run_binary(
+ name = "dir_a",
+ args = ["$(@D)"],
+ output_dir = True,
+ tool = ":make_dir_a",
+)
+
+genrule(
+ name = "make_foo",
+ outs = ["foo"],
+ cmd = "echo 'foo' > $@",
+)
+
+genrule(
+ name = "make_bar",
+ outs = ["bar"],
+ cmd = "echo 'bar' > $@",
+)
+
+copy_to_directory(
+ name = "dir_b",
+ srcs = [
+ ":make_bar",
+ ":make_foo",
+ ],
+)
+
+diff_test(
+ name = "dir_test",
+ file1 = ":dir_a",
+ file2 = ":dir_b",
+)