diff --git a/docs/copy_file.md b/docs/copy_file.md index bfb2e9995..45527f693 100644 --- a/docs/copy_file.md +++ b/docs/copy_file.md @@ -96,6 +96,18 @@ the TreeArtifact to the file to copy. 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, +) +``` + **PARAMETERS** diff --git a/docs/copy_to_bin.md b/docs/copy_to_bin.md index 5258d8c62..0260f8ef4 100644 --- a/docs/copy_to_bin.md +++ b/docs/copy_to_bin.md @@ -25,6 +25,18 @@ returned. 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_FILE_TO_BIN_TOOLCHAINS") + +my_rule = rule( + ..., + toolchains = COPY_FILE_TO_BIN_TOOLCHAINS, +) +``` + **PARAMETERS** diff --git a/lib/copy_file.bzl b/lib/copy_file.bzl index 1f853d87f..4dd962916 100644 --- a/lib/copy_file.bzl +++ b/lib/copy_file.bzl @@ -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 diff --git a/lib/copy_to_bin.bzl b/lib/copy_to_bin.bzl index 066c875a9..660dca5f4 100644 --- a/lib/copy_to_bin.bzl +++ b/lib/copy_to_bin.bzl @@ -9,6 +9,7 @@ https://github.com/bazelbuild/rules_nodejs/blob/8b5d27400db51e7027fe95ae413eeabe load( "//lib/private:copy_to_bin.bzl", + _COPY_FILE_TO_BIN_TOOLCHAINS = "COPY_FILE_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", @@ -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_FILE_TO_BIN_TOOLCHAINS = _COPY_FILE_TO_BIN_TOOLCHAINS diff --git a/lib/private/copy_file.bzl b/lib/private/copy_file.bzl index 2bb35e2ec..f667ff95b 100644 --- a/lib/private/copy_file.bzl +++ b/lib/private/copy_file.bzl @@ -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, @@ -93,6 +97,18 @@ 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, + ) + ``` + Args: ctx: The rule context. src: The source file to copy or TreeArtifact to copy a single file out of. @@ -164,6 +180,7 @@ _copy_file = rule( implementation = _copy_file_impl, provides = [DefaultInfo], attrs = _ATTRS, + toolchains = COPY_FILE_TOOLCHAINS, ) _copy_xfile = rule( @@ -171,6 +188,7 @@ _copy_xfile = rule( executable = True, provides = [DefaultInfo], attrs = _ATTRS, + toolchains = COPY_FILE_TOOLCHAINS, ) def copy_file(name, src, out, is_executable = False, allow_symlink = False, **kwargs): diff --git a/lib/private/copy_to_bin.bzl b/lib/private/copy_to_bin.bzl index a4621b775..a63f195ef 100644 --- a/lib/private/copy_to_bin.bzl +++ b/lib/private/copy_to_bin.bzl @@ -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_FILE_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. @@ -26,6 +28,18 @@ 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_FILE_TO_BIN_TOOLCHAINS") + + my_rule = rule( + ..., + toolchains = COPY_FILE_TO_BIN_TOOLCHAINS, + ) + ``` + Args: ctx: The rule context. file: The file to copy. @@ -125,6 +139,7 @@ _copy_to_bin = rule( attrs = { "srcs": attr.label_list(mandatory = True, allow_files = True), }, + toolchains = COPY_FILE_TO_BIN_TOOLCHAINS, ) def copy_to_bin(name, srcs, **kwargs):