Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cc/toolchains/args.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def _cc_args_impl(ctx):
)
files = nested.files
else:
files = collect_files(ctx.attr.data + ctx.attr.allowlist_include_directories)
files = collect_files(ctx.attr.data)

requires = collect_provider(ctx.attr.requires_any_of, FeatureConstraintInfo)

Expand Down Expand Up @@ -262,6 +262,9 @@ def cc_args(
your toolchain and you've ensured that the toolchain is compiling with the
`-no-canonical-prefixes` and/or `-fno-canonical-system-headers` arguments.

These files are not automatically passed to each action. If they
need to be, add them to 'data' as well.

This can help work around errors like:
`the source file 'main.c' includes the following non-builtin files with absolute paths
(if these are builtin files, make sure these paths are in your toolchain)`.
Expand Down
2 changes: 1 addition & 1 deletion cc/toolchains/impl/nested_args.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def nested_args_provider_from_ctx(ctx, maybe_used_vars = []):
args = ctx.attr.args,
format = ctx.attr.format,
nested = collect_provider(ctx.attr.nested, NestedArgsInfo),
files = collect_files(ctx.attr.data + getattr(ctx.attr, "allowlist_include_directories", [])),
files = collect_files(ctx.attr.data),
iterate_over = ctx.attr.iterate_over,
requires_not_none = _var(ctx.attr.requires_not_none),
requires_none = _var(ctx.attr.requires_none),
Expand Down
5 changes: 4 additions & 1 deletion cc/toolchains/tool.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _cc_tool_impl(ctx):
else:
fail("Expected cc_tool's src attribute to be either an executable or a single file")

runfiles = collect_data(ctx, ctx.attr.data + [ctx.attr.src] + ctx.attr.allowlist_include_directories)
runfiles = collect_data(ctx, ctx.attr.data + [ctx.attr.src])
tool = ToolInfo(
label = ctx.label,
exe = exe,
Expand Down Expand Up @@ -93,6 +93,9 @@ As a rule of thumb, only use this if Bazel is complaining about absolute paths i
toolchain and you've ensured that the toolchain is compiling with the `-no-canonical-prefixes`
and/or `-fno-canonical-system-headers` arguments.

These files are not automatically passed to each action. If they need to be,
add them to 'data' as well.

This can help work around errors like:
`the source file 'main.c' includes the following non-builtin files with absolute paths
(if these are builtin files, make sure these paths are in your toolchain)`.
Expand Down
4 changes: 2 additions & 2 deletions docs/toolchain_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ cc_tool(
| <a id="cc_tool-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="cc_tool-src"></a>src | The underlying binary that this tool represents.<br><br>Usually just a single prebuilt (eg. @toolchain//:bin/clang), but may be any executable label. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` |
| <a id="cc_tool-data"></a>data | Additional files that are required for this tool to run.<br><br>Frequently, clang and gcc require additional files to execute as they often shell out to other binaries (e.g. `cc1`). | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="cc_tool-allowlist_include_directories"></a>allowlist_include_directories | Include paths implied by using this tool.<br><br>Compilers may include a set of built-in headers that are implicitly available unless flags like `-nostdinc` are provided. Bazel checks that all included headers are properly provided by a dependency or allowlisted through this mechanism.<br><br>As a rule of thumb, only use this if Bazel is complaining about absolute paths in your toolchain and you've ensured that the toolchain is compiling with the `-no-canonical-prefixes` and/or `-fno-canonical-system-headers` arguments.<br><br>This can help work around errors like: `the source file 'main.c' includes the following non-builtin files with absolute paths (if these are builtin files, make sure these paths are in your toolchain)`. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="cc_tool-allowlist_include_directories"></a>allowlist_include_directories | Include paths implied by using this tool.<br><br>Compilers may include a set of built-in headers that are implicitly available unless flags like `-nostdinc` are provided. Bazel checks that all included headers are properly provided by a dependency or allowlisted through this mechanism.<br><br>As a rule of thumb, only use this if Bazel is complaining about absolute paths in your toolchain and you've ensured that the toolchain is compiling with the `-no-canonical-prefixes` and/or `-fno-canonical-system-headers` arguments.<br><br>These files are not automatically passed to each action. If they need to be, add them to 'data' as well.<br><br>This can help work around errors like: `the source file 'main.c' includes the following non-builtin files with absolute paths (if these are builtin files, make sure these paths are in your toolchain)`. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="cc_tool-capabilities"></a>capabilities | Declares that a tool is capable of doing something.<br><br>For example, `@rules_cc//cc/toolchains/capabilities:supports_pic`. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |


Expand Down Expand Up @@ -659,7 +659,7 @@ For more extensive examples, see the usages here:
| :------------- | :------------- | :------------- |
| <a id="cc_args-name"></a>name | (str) The name of the target. | none |
| <a id="cc_args-actions"></a>actions | (List[Label]) A list of labels of [`cc_action_type`](#cc_action_type) or [`cc_action_type_set`](#cc_action_type_set) rules that dictate which actions these arguments should be applied to. | `None` |
| <a id="cc_args-allowlist_include_directories"></a>allowlist_include_directories | (List[Label]) A list of include paths that are implied by using this rule. These must point to a skylib [directory](https://github.com/bazelbuild/bazel-skylib/tree/main/docs/directory_doc.md#directory) or [subdirectory](https://github.com/bazelbuild/bazel-skylib/tree/main/docs/directory_subdirectory_doc.md#subdirectory) rule. Some flags (e.g. --sysroot) imply certain include paths are available despite not explicitly specifying a normal include path flag (`-I`, `-isystem`, etc.). Bazel checks that all included headers are properly provided by a dependency or allowlisted through this mechanism.<br><br>As a rule of thumb, only use this if Bazel is complaining about absolute paths in your toolchain and you've ensured that the toolchain is compiling with the `-no-canonical-prefixes` and/or `-fno-canonical-system-headers` arguments.<br><br>This can help work around errors like: `the source file 'main.c' includes the following non-builtin files with absolute paths (if these are builtin files, make sure these paths are in your toolchain)`. | `None` |
| <a id="cc_args-allowlist_include_directories"></a>allowlist_include_directories | (List[Label]) A list of include paths that are implied by using this rule. These must point to a skylib [directory](https://github.com/bazelbuild/bazel-skylib/tree/main/docs/directory_doc.md#directory) or [subdirectory](https://github.com/bazelbuild/bazel-skylib/tree/main/docs/directory_subdirectory_doc.md#subdirectory) rule. Some flags (e.g. --sysroot) imply certain include paths are available despite not explicitly specifying a normal include path flag (`-I`, `-isystem`, etc.). Bazel checks that all included headers are properly provided by a dependency or allowlisted through this mechanism.<br><br>As a rule of thumb, only use this if Bazel is complaining about absolute paths in your toolchain and you've ensured that the toolchain is compiling with the `-no-canonical-prefixes` and/or `-fno-canonical-system-headers` arguments.<br><br>These files are not automatically passed to each action. If they need to be, add them to 'data' as well.<br><br>This can help work around errors like: `the source file 'main.c' includes the following non-builtin files with absolute paths (if these are builtin files, make sure these paths are in your toolchain)`. | `None` |
| <a id="cc_args-allowlist_absolute_include_directories"></a>allowlist_absolute_include_directories | (List[str]) Allowlists absolute include directories, preventing Bazel from emitting errors when an #include of local system files in the directory occurs. Be careful when adding directories to this list, as it inherently causes leaks in hermeticity. Prefer to reserve use of this for cases like Xcode, where the conventional expectation is to allowlist well-known system-absolute include paths rather than redistributing the SDK. | `None` |
| <a id="cc_args-args"></a>args | (List[str]) The command-line arguments that are applied by using this rule. This is mutually exclusive with [nested](#cc_args-nested). | `None` |
| <a id="cc_args-data"></a>data | (List[Label]) A list of runtime data dependencies that are required for these arguments to work as intended. | `None` |
Expand Down
9 changes: 9 additions & 0 deletions tests/rule_based_toolchain/args/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ util.helper_target(
args = ["--secret-builtin-include-dir"],
)

util.helper_target(
cc_args,
name = "with_dir_and_data",
actions = ["//tests/rule_based_toolchain/actions:all_compile"],
allowlist_include_directories = ["//tests/rule_based_toolchain/testdata:directory"],
args = ["--secret-builtin-include-dir"],
data = ["//tests/rule_based_toolchain/testdata:directory"],
)

util.helper_target(
cc_args,
name = "good_env_format",
Expand Down
14 changes: 13 additions & 1 deletion tests/rule_based_toolchain/args/args_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,21 @@ def _env_only_requires_test(env, targets):
def _with_dir_test(env, targets):
with_dir = env.expect.that_target(targets.with_dir).provider(ArgsInfo)
with_dir.allowlist_include_directories().contains_exactly([_TOOL_DIRECTORY])
with_dir.files().contains_at_least(_SIMPLE_FILES)
with_dir.files().contains_exactly([])

c_compile = env.expect.that_target(targets.with_dir).provider(ArgsListInfo).by_action().get(
targets.c_compile[ActionTypeInfo],
)
c_compile.files().contains_exactly([])

def _with_dir_and_data_test(env, targets):
with_dir = env.expect.that_target(targets.with_dir_and_data).provider(ArgsInfo)
with_dir.allowlist_include_directories().contains_exactly([_TOOL_DIRECTORY])
with_dir.files().contains_at_least(_SIMPLE_FILES)

c_compile = env.expect.that_target(targets.with_dir_and_data).provider(ArgsListInfo).by_action().get(
targets.c_compile[ActionTypeInfo],
)
c_compile.files().contains_at_least(_SIMPLE_FILES)

TARGETS = [
Expand All @@ -165,6 +175,7 @@ TARGETS = [
":env_only",
":env_only_requires",
":with_dir",
":with_dir_and_data",
":iterate_over_optional",
":good_env_format",
":good_env_format_optional",
Expand Down Expand Up @@ -349,6 +360,7 @@ TESTS = {
"env_only_test": _env_only_test,
"env_only_requires_test": _env_only_requires_test,
"with_dir_test": _with_dir_test,
"with_dir_and_data_test": _with_dir_and_data_test,
"good_env_format_test": _good_env_format_test,
"good_env_format_optional_test": _good_env_format_optional_test,
}
2 changes: 2 additions & 0 deletions tests/rule_based_toolchain/args_list/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ util.helper_target(
actions = ["//tests/rule_based_toolchain/actions:c_compile"],
allowlist_include_directories = ["//tests/rule_based_toolchain/testdata:subdirectory_1"],
args = ["dir1"],
data = ["//tests/rule_based_toolchain/testdata:subdirectory_1"],
visibility = ["//tests/rule_based_toolchain:__subpackages__"],
)

Expand All @@ -57,6 +58,7 @@ util.helper_target(
actions = ["//tests/rule_based_toolchain/actions:cpp_compile"],
allowlist_include_directories = ["//tests/rule_based_toolchain/testdata:subdirectory_2"],
args = ["dir2"],
data = ["//tests/rule_based_toolchain/testdata:subdirectory_2"],
visibility = ["//tests/rule_based_toolchain:__subpackages__"],
)

Expand Down
1 change: 1 addition & 0 deletions tests/rule_based_toolchain/features/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ util.helper_target(
actions = ["//tests/rule_based_toolchain/actions:c_compile"],
allowlist_include_directories = ["//tests/rule_based_toolchain/testdata:subdirectory_1"],
args = ["--include-builtin-dirs"],
data = ["//tests/rule_based_toolchain/testdata:subdirectory_1"],
)

util.helper_target(
Expand Down
1 change: 1 addition & 0 deletions tests/rule_based_toolchain/tool/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ cc_tool(
name = "tool_with_allowlist_include_directories",
src = "//tests/rule_based_toolchain/testdata:bin_wrapper.sh",
allowlist_include_directories = ["//tests/rule_based_toolchain/testdata:directory"],
data = ["//tests/rule_based_toolchain/testdata:directory"],
visibility = ["//tests/rule_based_toolchain:__subpackages__"],
)

Expand Down
11 changes: 9 additions & 2 deletions tests/rule_based_toolchain/toolchain_config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ util.helper_target(
actions = ["//tests/rule_based_toolchain/actions:c_compile"],
allowlist_include_directories = ["//tests/rule_based_toolchain/testdata:subdirectory_1"],
args = ["c_compile_args"],
data = ["//tests/rule_based_toolchain/testdata:file1"],
data = [
"//tests/rule_based_toolchain/testdata:file1",
"//tests/rule_based_toolchain/testdata:subdirectory_1",
],
)

util.helper_target(
Expand All @@ -48,6 +51,7 @@ cc_tool(
src = "//tests/rule_based_toolchain/testdata:bin_wrapper",
allowlist_include_directories = ["//tests/rule_based_toolchain/testdata:subdirectory_3"],
capabilities = ["//cc/toolchains/capabilities:supports_pic"],
data = ["//tests/rule_based_toolchain/testdata:subdirectory_3"],
)

cc_sysroot(
Expand Down Expand Up @@ -94,7 +98,10 @@ util.helper_target(
actions = ["//tests/rule_based_toolchain/actions:all_compile"],
allowlist_include_directories = ["//tests/rule_based_toolchain/testdata:subdirectory_2"],
args = ["compile_args"],
data = ["//tests/rule_based_toolchain/testdata:file2"],
data = [
"//tests/rule_based_toolchain/testdata:file2",
"//tests/rule_based_toolchain/testdata:subdirectory_2",
],
)

util.helper_target(
Expand Down