Skip to content

Commit

Permalink
feat: expose toolchains used for copy actions
Browse files Browse the repository at this point in the history
  • Loading branch information
kormide committed Nov 16, 2023
1 parent 7dbb96e commit 5517d50
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 1 deletion.
21 changes: 21 additions & 0 deletions docs/copy_file.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions docs/copy_to_bin.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/copy_file.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ query --@aspect_bazel_lib//lib:copy_use_local_execution=false

load(
"//lib/private:copy_file.bzl",
_COPY_FILE_TOOLCHAINS = "COPY_FILE_TOOLCHAINS",
_copy_file = "copy_file",
_copy_file_action = "copy_file_action",
)

copy_file = _copy_file
copy_file_action = _copy_file_action
COPY_FILE_TOOLCHAINS = _COPY_FILE_TOOLCHAINS
2 changes: 2 additions & 0 deletions lib/copy_to_bin.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ https://github.com/bazelbuild/rules_nodejs/blob/8b5d27400db51e7027fe95ae413eeabe

load(
"//lib/private:copy_to_bin.bzl",
_COPY_TO_BIN_TOOLCHAINS = "COPY_TO_BIN_TOOLCHAINS",
_copy_file_to_bin_action = "copy_file_to_bin_action",
_copy_files_to_bin_actions = "copy_files_to_bin_actions",
_copy_to_bin = "copy_to_bin",
Expand All @@ -17,3 +18,4 @@ load(
copy_file_to_bin_action = _copy_file_to_bin_action
copy_files_to_bin_actions = _copy_files_to_bin_actions
copy_to_bin = _copy_to_bin
COPY_TO_BIN_TOOLCHAINS = _COPY_TO_BIN_TOOLCHAINS
27 changes: 27 additions & 0 deletions lib/private/copy_file.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ load(":copy_common.bzl", "execution_requirements_for_copy", _progress_path = "pr
load(":directory_path.bzl", "DirectoryPathInfo")
load(":platform_utils.bzl", _platform_utils = "platform_utils")

# Declare toolchains used by copy file actions so that downstream rulesets can pass it into
# the `toolchains` attribute of their rule.
COPY_FILE_TOOLCHAINS = []

def _copy_cmd(ctx, src, src_path, dst, override_execution_requirements = None):
# Most Windows binaries built with MSVC use a certain argument quoting
# scheme. Bazel uses that scheme too to quote arguments. However,
Expand Down Expand Up @@ -93,6 +97,27 @@ def copy_file_action(ctx, src, dst, dir_path = None, is_windows = None):
This helper is used by copy_file. It is exposed as a public API so it can be used within
other rule implementations.
To use `copy_file_action` in your own rules, you need to include the toolchains it uses
in your rule definition. For example:
```starlark
load("@aspect_bazel_lib//lib:copy_file.bzl", "COPY_FILE_TOOLCHAINS")
my_rule = rule(
...,
toolchains = COPY_FILE_TOOLCHAINS,
)
```
Additionally, you must ensure that the coreutils toolchain is has been registered in your
WORKSPACE if you are not using bzlmod:
```starlark
load("@aspect_bazel_lib//lib:repositories.bzl", "register_coreutils_toolchains")
register_coreutils_toolchains()
```
Args:
ctx: The rule context.
src: The source file to copy or TreeArtifact to copy a single file out of.
Expand Down Expand Up @@ -164,13 +189,15 @@ _copy_file = rule(
implementation = _copy_file_impl,
provides = [DefaultInfo],
attrs = _ATTRS,
toolchains = COPY_FILE_TOOLCHAINS,
)

_copy_xfile = rule(
implementation = _copy_file_impl,
executable = True,
provides = [DefaultInfo],
attrs = _ATTRS,
toolchains = COPY_FILE_TOOLCHAINS,
)

def copy_file(name, src, out, is_executable = False, allow_symlink = False, **kwargs):
Expand Down
26 changes: 25 additions & 1 deletion lib/private/copy_to_bin.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"""Implementation of copy_to_bin macro and underlying rules."""

load("@bazel_skylib//lib:paths.bzl", "paths")
load(":copy_file.bzl", "copy_file_action")
load(":copy_file.bzl", "COPY_FILE_TOOLCHAINS", "copy_file_action")

COPY_TO_BIN_TOOLCHAINS = COPY_FILE_TOOLCHAINS

def copy_file_to_bin_action(ctx, file, is_windows = None):
"""Factory function that creates an action to copy a file to the output tree.
Expand All @@ -26,6 +28,27 @@ def copy_file_to_bin_action(ctx, file, is_windows = None):
If the file passed in is already in the output tree is then it is returned
without a copy action.
To use `copy_file_to_bin_action` in your own rules, you need to include the toolchains it uses
in your rule definition. For example:
```starlark
load("@aspect_bazel_lib//lib:copy_file.bzl", "COPY_TO_BIN_TOOLCHAINS")
my_rule = rule(
...,
toolchains = COPY_TO_BIN_TOOLCHAINS,
)
```
Additionally, you must ensure that the coreutils toolchain is has been registered in your
WORKSPACE if you are not using bzlmod:
```starlark
load("@aspect_bazel_lib//lib:repositories.bzl", "register_coreutils_toolchains")
register_coreutils_toolchains()
```
Args:
ctx: The rule context.
file: The file to copy.
Expand Down Expand Up @@ -125,6 +148,7 @@ _copy_to_bin = rule(
attrs = {
"srcs": attr.label_list(mandatory = True, allow_files = True),
},
toolchains = COPY_TO_BIN_TOOLCHAINS,
)

def copy_to_bin(name, srcs, **kwargs):
Expand Down

0 comments on commit 5517d50

Please sign in to comment.