From 0f352c3497f480bbebc9507a1cfc2cdc9472b827 Mon Sep 17 00:00:00 2001 From: Googler Date: Fri, 6 Sep 2024 14:49:38 -0700 Subject: [PATCH 1/4] Automated rollback of commit 0bc1ba56ef2f8e791841bc074376a02ad8608dc2. *** Reason for rollback *** TAP failures in Bazel/Blaze, see b/365154741 and discussion at https://chat.google.com/room/AAAAXE3XKrY/_e3TpBFJvus *** Original change description *** Add user-facing documentation for cc_tool_map BEGIN_PUBLIC Add user-facing documentation for cc_tool_map Adds user-facing documentation for the cc_tool_map toolchain rule, including an example and higher level analogies. END_PUBLIC *** PiperOrigin-RevId: 671898382 Change-Id: Iead81a71adb17dff46b1b442471e595769fb5c2e --- MODULE.bazel | 1 - cc/BUILD | 2 +- cc/toolchains/BUILD | 17 -------- cc/toolchains/impl/BUILD | 13 ------ cc/toolchains/impl/documented_api.bzl | 18 -------- cc/toolchains/tool_map.bzl | 50 ++++------------------- cc/toolchains/toolchain_api.md | 59 --------------------------- 7 files changed, 10 insertions(+), 150 deletions(-) delete mode 100644 cc/toolchains/impl/documented_api.bzl delete mode 100644 cc/toolchains/toolchain_api.md diff --git a/MODULE.bazel b/MODULE.bazel index 51a00b2..e7e5603 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -6,7 +6,6 @@ module( bazel_dep(name = "bazel_skylib", version = "1.7.1") bazel_dep(name = "platforms", version = "0.0.7") -bazel_dep(name = "stardoc", version = "0.7.0") cc_configure = use_extension("@bazel_tools//tools/cpp:cc_configure.bzl", "cc_configure_extension") use_repo(cc_configure, "local_config_cc_toolchains") diff --git a/cc/BUILD b/cc/BUILD index d0db658..60c4290 100644 --- a/cc/BUILD +++ b/cc/BUILD @@ -97,7 +97,7 @@ bzl_library( bzl_library( name = "cc_toolchain_config_lib_bzl", srcs = ["cc_toolchain_config_lib.bzl"], - visibility = ["//cc/toolchains:__subpackages__"], + visibility = ["//visibility:private"], ) cc_toolchain_alias(name = "current_cc_toolchain") diff --git a/cc/toolchains/BUILD b/cc/toolchains/BUILD index 8ae4d97..1b001cb 100644 --- a/cc/toolchains/BUILD +++ b/cc/toolchains/BUILD @@ -14,8 +14,6 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") -load("@bazel_skylib//rules:diff_test.bzl", "diff_test") -load("@stardoc//stardoc:stardoc.bzl", "stardoc") bool_flag( name = "experimental_enable_rule_based_toolchains", @@ -29,11 +27,9 @@ bzl_library( visibility = ["//visibility:public"], deps = [ "//cc:action_names_bzl", - "//cc:cc_toolchain_config_lib_bzl", "//cc:find_cc_toolchain_bzl", "//cc/private/rules_impl:cc_flags_supplier_lib_bzl", "//cc/private/rules_impl:native_bzl", - "//cc/toolchains/impl:toolchain_impl_rules", "@bazel_skylib//rules/directory:glob", ], ) @@ -46,16 +42,3 @@ filegroup( ]), visibility = ["//visibility:public"], ) - -stardoc( - name = "toolchain_api", - out = "generated_toolchain_api.md", - input = "//cc/toolchains/impl:documented_api.bzl", - deps = [":toolchain_rules"], -) - -diff_test( - name = "toolchain_api_diff_test", - file1 = ":generated_toolchain_api.md", - file2 = ":toolchain_api.md", -) diff --git a/cc/toolchains/impl/BUILD b/cc/toolchains/impl/BUILD index 2ad5817..8484e1c 100644 --- a/cc/toolchains/impl/BUILD +++ b/cc/toolchains/impl/BUILD @@ -4,16 +4,3 @@ # I wanted to call it private / internal, but then buildifier complains about # referencing it from the tests directory. - -load("@bazel_skylib//:bzl_library.bzl", "bzl_library") - -exports_files( - ["documented_api.bzl"], - visibility = ["//cc/toolchains:__subpackages__"], -) - -bzl_library( - name = "toolchain_impl_rules", - srcs = glob(["*.bzl"]), - visibility = ["//cc/toolchains:__subpackages__"], -) diff --git a/cc/toolchains/impl/documented_api.bzl b/cc/toolchains/impl/documented_api.bzl deleted file mode 100644 index d840b7e..0000000 --- a/cc/toolchains/impl/documented_api.bzl +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright 2024 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This is a list of rules/macros that should be exported as documentation.""" - -load("//cc/toolchains:tool_map.bzl", _cc_tool_map = "cc_tool_map") - -cc_tool_map = _cc_tool_map diff --git a/cc/toolchains/tool_map.bzl b/cc/toolchains/tool_map.bzl index 62e94e6..300523d 100644 --- a/cc/toolchains/tool_map.bzl +++ b/cc/toolchains/tool_map.bzl @@ -51,7 +51,7 @@ _cc_tool_map = rule( mandatory = True, doc = """A list of action names to apply this action to. -See //cc/toolchains/actions:BUILD for valid options. +See @toolchain//actions:all for valid options. """, ), "tools": attr.label_list( @@ -60,7 +60,10 @@ See //cc/toolchains/actions:BUILD for valid options. allow_files = True, doc = """The tool to use for the specified actions. -The tool may be a `cc_tool` or other executable rule. +A tool is usually a binary, but may be a `cc_tool`. + +If multiple tools are specified, the first tool that has `with_features` that +satisfy the currently enabled feature set is used. """, ), }, @@ -68,47 +71,12 @@ The tool may be a `cc_tool` or other executable rule. ) def cc_tool_map(name, tools, **kwargs): - """A toolchain configuration rule that maps toolchain actions to tools. - - A cc_tool_map aggregates all the tools that may be used for a given toolchain and maps them to - their corresponding actions. Conceptually, this is similar to the `CXX=/path/to/clang++` - environment variables that most build systems use to determine which tools to use for a given - action. To simplify usage, some actions have been grouped together (for example, - //cc/toolchains/actions:cpp_compile_actions) to - logically express "all the C++ compile actions". - - In Bazel, there is a little more granularity to the mapping, so the mapping doesn't follow the - traditional `CXX`, `AR`, etc. naming scheme. For a comprehensive list of all the well-known - actions, see //cc/toolchains/actions:BUILD. - - Example usage: - ``` - load("//cc/toolchains:tool_map.bzl", "cc_tool_map") - - cc_tool_map( - name = "all_tools", - tools = { - "//cc/toolchains/actions:assembly_actions": ":asm", - "//cc/toolchains/actions:c_compile": ":clang", - "//cc/toolchains/actions:cpp_compile_actions": ":clang++", - "//cc/toolchains/actions:link_actions": ":lld", - "//cc/toolchains/actions:objcopy_embed_data": ":llvm-objcopy", - "//cc/toolchains/actions:strip": ":llvm-strip", - "//cc/toolchains/actions:ar_actions": ":llvm-ar", - }, - ) - ``` - - Note: - Due to an implementation limitation, if you need to map the same tool to multiple actions, - you will need to create an intermediate alias for the tool for each set of actions. See - https://github.com/bazelbuild/rules_cc/issues/235 for more details. + """Configuration for which actions require which tools. Args: - name: (str) The name of the target. - tools: (Dict[target providing ActionTypeSetInfo, Executable target]) A mapping between `cc_action_type` targets - and the `cc_tool` or executable target that implements that action. - **kwargs: [common attributes](https://bazel.build/reference/be/common-definitions#common-attributes) that should be applied to this rule. + name: (str) The name of the target + tools: (Dict[Action target, Executable target]) + **kwargs: kwargs to be passed to the underlying rule. """ _cc_tool_map( name = name, diff --git a/cc/toolchains/toolchain_api.md b/cc/toolchains/toolchain_api.md deleted file mode 100644 index 3a4a490..0000000 --- a/cc/toolchains/toolchain_api.md +++ /dev/null @@ -1,59 +0,0 @@ - - -This is a list of rules/macros that should be exported as documentation. - - - -## cc_tool_map - -
-cc_tool_map(name, tools, kwargs)
-
- -A toolchain configuration rule that maps toolchain actions to tools. - -A cc_tool_map aggregates all the tools that may be used for a given toolchain and maps them to -their corresponding actions. Conceptually, this is similar to the `CXX=/path/to/clang++` -environment variables that most build systems use to determine which tools to use for a given -action. To simplify usage, some actions have been grouped together (for example, -//third_party/bazel_rules/rules_cc/cc/toolchains/actions:cpp_compile_actions) to -logically express "all the C++ compile actions". - -In Bazel, there is a little more granularity to the mapping, so the mapping doesn't follow the -traditional `CXX`, `AR`, etc. naming scheme. For a comprehensive list of all the well-known -actions, see //third_party/bazel_rules/rules_cc/cc/toolchains/actions:BUILD. - -Example usage: -``` -load("//third_party/bazel_rules/rules_cc/cc/toolchains:tool_map.bzl", "cc_tool_map") - -cc_tool_map( - name = "all_tools", - tools = { - "//third_party/bazel_rules/rules_cc/cc/toolchains/actions:assembly_actions": ":asm", - "//third_party/bazel_rules/rules_cc/cc/toolchains/actions:c_compile": ":clang", - "//third_party/bazel_rules/rules_cc/cc/toolchains/actions:cpp_compile_actions": ":clang++", - "//third_party/bazel_rules/rules_cc/cc/toolchains/actions:link_actions": ":lld", - "//third_party/bazel_rules/rules_cc/cc/toolchains/actions:objcopy_embed_data": ":llvm-objcopy", - "//third_party/bazel_rules/rules_cc/cc/toolchains/actions:strip": ":llvm-strip", - "//third_party/bazel_rules/rules_cc/cc/toolchains/actions:ar_actions": ":llvm-ar", - }, -) -``` - -Note: - Due to an implementation limitation, if you need to map the same tool to multiple actions, - you will need to create an intermediate alias for the tool for each set of actions. See - https://github.com/bazelbuild/rules_cc/issues/235 for more details. - - -**PARAMETERS** - - -| Name | Description | Default Value | -| :------------- | :------------- | :------------- | -| name | (str) The name of the target. | none | -| tools | (Dict[target providing ActionTypeSetInfo, Executable target]) A mapping between `cc_action_type` targets and the `cc_tool` or executable target that implements that action. | none | -| kwargs | [common attributes](https://bazel.build/reference/be/common-definitions#common-attributes) that should be applied to this rule. | none | - - From ca8483971cdd8770cc03be936a96919230b578dc Mon Sep 17 00:00:00 2001 From: Googler Date: Fri, 6 Sep 2024 23:05:14 -0700 Subject: [PATCH 2/4] Automated rollback of commit 0f352c3497f480bbebc9507a1cfc2cdc9472b827. *** Reason for rollback *** Rollforward with fix for b/365154741 *** Original change description *** Automated rollback of commit 0bc1ba56ef2f8e791841bc074376a02ad8608dc2. *** Reason for rollback *** TAP failures in Bazel/Blaze, see b/365154741 and discussion at https://chat.google.com/room/AAAAXE3XKrY/_e3TpBFJvus *** Original change description *** Add user-facing documentation for cc_tool_map BEGIN_PUBLIC Add user-facing documentation for cc_tool_map Adds user-facing documentation for the cc_tool_map toolchain rule, including an example and higher level analogies. END_PUBLIC *** *** PiperOrigin-RevId: 672000172 Change-Id: If7ad64e2378a2016d389a3718944aa04bc5e9759 --- MODULE.bazel | 1 + cc/BUILD | 2 +- cc/toolchains/BUILD | 17 ++++++++ cc/toolchains/impl/BUILD | 13 ++++++ cc/toolchains/impl/documented_api.bzl | 18 ++++++++ cc/toolchains/tool_map.bzl | 50 +++++++++++++++++++---- cc/toolchains/toolchain_api.md | 59 +++++++++++++++++++++++++++ 7 files changed, 150 insertions(+), 10 deletions(-) create mode 100644 cc/toolchains/impl/documented_api.bzl create mode 100644 cc/toolchains/toolchain_api.md diff --git a/MODULE.bazel b/MODULE.bazel index e7e5603..51a00b2 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -6,6 +6,7 @@ module( bazel_dep(name = "bazel_skylib", version = "1.7.1") bazel_dep(name = "platforms", version = "0.0.7") +bazel_dep(name = "stardoc", version = "0.7.0") cc_configure = use_extension("@bazel_tools//tools/cpp:cc_configure.bzl", "cc_configure_extension") use_repo(cc_configure, "local_config_cc_toolchains") diff --git a/cc/BUILD b/cc/BUILD index 60c4290..d0db658 100644 --- a/cc/BUILD +++ b/cc/BUILD @@ -97,7 +97,7 @@ bzl_library( bzl_library( name = "cc_toolchain_config_lib_bzl", srcs = ["cc_toolchain_config_lib.bzl"], - visibility = ["//visibility:private"], + visibility = ["//cc/toolchains:__subpackages__"], ) cc_toolchain_alias(name = "current_cc_toolchain") diff --git a/cc/toolchains/BUILD b/cc/toolchains/BUILD index 1b001cb..8ae4d97 100644 --- a/cc/toolchains/BUILD +++ b/cc/toolchains/BUILD @@ -14,6 +14,8 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") +load("@bazel_skylib//rules:diff_test.bzl", "diff_test") +load("@stardoc//stardoc:stardoc.bzl", "stardoc") bool_flag( name = "experimental_enable_rule_based_toolchains", @@ -27,9 +29,11 @@ bzl_library( visibility = ["//visibility:public"], deps = [ "//cc:action_names_bzl", + "//cc:cc_toolchain_config_lib_bzl", "//cc:find_cc_toolchain_bzl", "//cc/private/rules_impl:cc_flags_supplier_lib_bzl", "//cc/private/rules_impl:native_bzl", + "//cc/toolchains/impl:toolchain_impl_rules", "@bazel_skylib//rules/directory:glob", ], ) @@ -42,3 +46,16 @@ filegroup( ]), visibility = ["//visibility:public"], ) + +stardoc( + name = "toolchain_api", + out = "generated_toolchain_api.md", + input = "//cc/toolchains/impl:documented_api.bzl", + deps = [":toolchain_rules"], +) + +diff_test( + name = "toolchain_api_diff_test", + file1 = ":generated_toolchain_api.md", + file2 = ":toolchain_api.md", +) diff --git a/cc/toolchains/impl/BUILD b/cc/toolchains/impl/BUILD index 8484e1c..2ad5817 100644 --- a/cc/toolchains/impl/BUILD +++ b/cc/toolchains/impl/BUILD @@ -4,3 +4,16 @@ # I wanted to call it private / internal, but then buildifier complains about # referencing it from the tests directory. + +load("@bazel_skylib//:bzl_library.bzl", "bzl_library") + +exports_files( + ["documented_api.bzl"], + visibility = ["//cc/toolchains:__subpackages__"], +) + +bzl_library( + name = "toolchain_impl_rules", + srcs = glob(["*.bzl"]), + visibility = ["//cc/toolchains:__subpackages__"], +) diff --git a/cc/toolchains/impl/documented_api.bzl b/cc/toolchains/impl/documented_api.bzl new file mode 100644 index 0000000..d840b7e --- /dev/null +++ b/cc/toolchains/impl/documented_api.bzl @@ -0,0 +1,18 @@ +# Copyright 2024 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""This is a list of rules/macros that should be exported as documentation.""" + +load("//cc/toolchains:tool_map.bzl", _cc_tool_map = "cc_tool_map") + +cc_tool_map = _cc_tool_map diff --git a/cc/toolchains/tool_map.bzl b/cc/toolchains/tool_map.bzl index 300523d..62e94e6 100644 --- a/cc/toolchains/tool_map.bzl +++ b/cc/toolchains/tool_map.bzl @@ -51,7 +51,7 @@ _cc_tool_map = rule( mandatory = True, doc = """A list of action names to apply this action to. -See @toolchain//actions:all for valid options. +See //cc/toolchains/actions:BUILD for valid options. """, ), "tools": attr.label_list( @@ -60,10 +60,7 @@ See @toolchain//actions:all for valid options. allow_files = True, doc = """The tool to use for the specified actions. -A tool is usually a binary, but may be a `cc_tool`. - -If multiple tools are specified, the first tool that has `with_features` that -satisfy the currently enabled feature set is used. +The tool may be a `cc_tool` or other executable rule. """, ), }, @@ -71,12 +68,47 @@ satisfy the currently enabled feature set is used. ) def cc_tool_map(name, tools, **kwargs): - """Configuration for which actions require which tools. + """A toolchain configuration rule that maps toolchain actions to tools. + + A cc_tool_map aggregates all the tools that may be used for a given toolchain and maps them to + their corresponding actions. Conceptually, this is similar to the `CXX=/path/to/clang++` + environment variables that most build systems use to determine which tools to use for a given + action. To simplify usage, some actions have been grouped together (for example, + //cc/toolchains/actions:cpp_compile_actions) to + logically express "all the C++ compile actions". + + In Bazel, there is a little more granularity to the mapping, so the mapping doesn't follow the + traditional `CXX`, `AR`, etc. naming scheme. For a comprehensive list of all the well-known + actions, see //cc/toolchains/actions:BUILD. + + Example usage: + ``` + load("//cc/toolchains:tool_map.bzl", "cc_tool_map") + + cc_tool_map( + name = "all_tools", + tools = { + "//cc/toolchains/actions:assembly_actions": ":asm", + "//cc/toolchains/actions:c_compile": ":clang", + "//cc/toolchains/actions:cpp_compile_actions": ":clang++", + "//cc/toolchains/actions:link_actions": ":lld", + "//cc/toolchains/actions:objcopy_embed_data": ":llvm-objcopy", + "//cc/toolchains/actions:strip": ":llvm-strip", + "//cc/toolchains/actions:ar_actions": ":llvm-ar", + }, + ) + ``` + + Note: + Due to an implementation limitation, if you need to map the same tool to multiple actions, + you will need to create an intermediate alias for the tool for each set of actions. See + https://github.com/bazelbuild/rules_cc/issues/235 for more details. Args: - name: (str) The name of the target - tools: (Dict[Action target, Executable target]) - **kwargs: kwargs to be passed to the underlying rule. + name: (str) The name of the target. + tools: (Dict[target providing ActionTypeSetInfo, Executable target]) A mapping between `cc_action_type` targets + and the `cc_tool` or executable target that implements that action. + **kwargs: [common attributes](https://bazel.build/reference/be/common-definitions#common-attributes) that should be applied to this rule. """ _cc_tool_map( name = name, diff --git a/cc/toolchains/toolchain_api.md b/cc/toolchains/toolchain_api.md new file mode 100644 index 0000000..3a4a490 --- /dev/null +++ b/cc/toolchains/toolchain_api.md @@ -0,0 +1,59 @@ + + +This is a list of rules/macros that should be exported as documentation. + + + +## cc_tool_map + +
+cc_tool_map(name, tools, kwargs)
+
+ +A toolchain configuration rule that maps toolchain actions to tools. + +A cc_tool_map aggregates all the tools that may be used for a given toolchain and maps them to +their corresponding actions. Conceptually, this is similar to the `CXX=/path/to/clang++` +environment variables that most build systems use to determine which tools to use for a given +action. To simplify usage, some actions have been grouped together (for example, +//third_party/bazel_rules/rules_cc/cc/toolchains/actions:cpp_compile_actions) to +logically express "all the C++ compile actions". + +In Bazel, there is a little more granularity to the mapping, so the mapping doesn't follow the +traditional `CXX`, `AR`, etc. naming scheme. For a comprehensive list of all the well-known +actions, see //third_party/bazel_rules/rules_cc/cc/toolchains/actions:BUILD. + +Example usage: +``` +load("//third_party/bazel_rules/rules_cc/cc/toolchains:tool_map.bzl", "cc_tool_map") + +cc_tool_map( + name = "all_tools", + tools = { + "//third_party/bazel_rules/rules_cc/cc/toolchains/actions:assembly_actions": ":asm", + "//third_party/bazel_rules/rules_cc/cc/toolchains/actions:c_compile": ":clang", + "//third_party/bazel_rules/rules_cc/cc/toolchains/actions:cpp_compile_actions": ":clang++", + "//third_party/bazel_rules/rules_cc/cc/toolchains/actions:link_actions": ":lld", + "//third_party/bazel_rules/rules_cc/cc/toolchains/actions:objcopy_embed_data": ":llvm-objcopy", + "//third_party/bazel_rules/rules_cc/cc/toolchains/actions:strip": ":llvm-strip", + "//third_party/bazel_rules/rules_cc/cc/toolchains/actions:ar_actions": ":llvm-ar", + }, +) +``` + +Note: + Due to an implementation limitation, if you need to map the same tool to multiple actions, + you will need to create an intermediate alias for the tool for each set of actions. See + https://github.com/bazelbuild/rules_cc/issues/235 for more details. + + +**PARAMETERS** + + +| Name | Description | Default Value | +| :------------- | :------------- | :------------- | +| name | (str) The name of the target. | none | +| tools | (Dict[target providing ActionTypeSetInfo, Executable target]) A mapping between `cc_action_type` targets and the `cc_tool` or executable target that implements that action. | none | +| kwargs | [common attributes](https://bazel.build/reference/be/common-definitions#common-attributes) that should be applied to this rule. | none | + + From b8bb882895120c1ba31fac7b79326b36ec3d923d Mon Sep 17 00:00:00 2001 From: Googler Date: Sun, 8 Sep 2024 22:14:21 -0700 Subject: [PATCH 3/4] Fix rule-based implementation of runtime_library_search_directories BEGIN_PUBLIC Fix rule-based implementation of runtime_library_search_directories As runtime_library_search_directories was written and reorganized through review, some instances of `$ORIGIN` were unintentionally renamed to `$EXEC_ORIGIN`. This change fixes those instances to correctly mirror CppActionConfigs.java. END_PUBLIC BUG: 349427627 PiperOrigin-RevId: 672401255 Change-Id: Ic20f9e77eccca8bc7e7f895aeaeb44f85022df28 --- cc/toolchains/args/runtime_library_search_directories/BUILD | 4 ++-- .../goldens/unix/runtime_library_search_directories.textproto | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cc/toolchains/args/runtime_library_search_directories/BUILD b/cc/toolchains/args/runtime_library_search_directories/BUILD index a6bc392..2b980bc 100644 --- a/cc/toolchains/args/runtime_library_search_directories/BUILD +++ b/cc/toolchains/args/runtime_library_search_directories/BUILD @@ -77,7 +77,7 @@ cc_nested_args( "-Xlinker", ] + select({ "@platforms//os:macos": ["@loader_path/{search_path}"], - "//conditions:default": ["$EXEC_ORIGIN/{search_path}"], + "//conditions:default": ["$ORIGIN/{search_path}"], }), format = { "search_path": "//cc/toolchains/variables:runtime_library_search_directories", @@ -113,7 +113,7 @@ cc_nested_args( "-Xlinker", ] + select({ "@platforms//os:macos": ["@loader_path/{search_path}"], - "//conditions:default": ["$EXEC_ORIGIN/{search_path}"], + "//conditions:default": ["$ORIGIN/{search_path}"], }), format = { "search_path": "//cc/toolchains/variables:runtime_library_search_directories", diff --git a/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/runtime_library_search_directories.textproto b/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/runtime_library_search_directories.textproto index 1c69c14..8618b47 100644 --- a/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/runtime_library_search_directories.textproto +++ b/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/runtime_library_search_directories.textproto @@ -21,7 +21,7 @@ flag_sets { flags: "-Xlinker" flags: "-rpath" flags: "-Xlinker" - flags: "$EXEC_ORIGIN/%{runtime_library_search_directories}" + flags: "$ORIGIN/%{runtime_library_search_directories}" } iterate_over: "runtime_library_search_directories" } @@ -43,7 +43,7 @@ flag_sets { flags: "-Xlinker" flags: "-rpath" flags: "-Xlinker" - flags: "$EXEC_ORIGIN/%{runtime_library_search_directories}" + flags: "$ORIGIN/%{runtime_library_search_directories}" iterate_over: "runtime_library_search_directories" } } From 5c1be25800e0806356624c1effd7a23240b3a45e Mon Sep 17 00:00:00 2001 From: Googler Date: Mon, 9 Sep 2024 06:05:13 -0700 Subject: [PATCH 4/4] Copy a few utilities from `@_builtins` to `@rules_cc` PiperOrigin-RevId: 672519388 Change-Id: Icb077dd736af10724ffdbfb8330564516b43fb1d --- cc/common/BUILD | 18 ++++ cc/common/cc_debug_helper.bzl | 181 ++++++++++++++++++++++++++++++++++ cc/common/cc_helper.bzl | 100 ++++++++++++++++++- cc/common/visibility.bzl | 3 + 4 files changed, 298 insertions(+), 4 deletions(-) create mode 100644 cc/common/cc_debug_helper.bzl create mode 100644 cc/common/visibility.bzl diff --git a/cc/common/BUILD b/cc/common/BUILD index 2af8526..fdb3921 100644 --- a/cc/common/BUILD +++ b/cc/common/BUILD @@ -25,6 +25,24 @@ bzl_library( name = "cc_helper_bzl", srcs = ["cc_helper.bzl"], visibility = ["//visibility:public"], + deps = [":visibility_bzl"], +) + +bzl_library( + name = "cc_debug_helper_bzl", + srcs = ["cc_debug_helper.bzl"], + visibility = ["//visibility:public"], + deps = [ + ":cc_helper_bzl", + ":visibility_bzl", + "//cc:find_cc_toolchain_bzl", + ], +) + +bzl_library( + name = "visibility_bzl", + srcs = ["visibility.bzl"], + visibility = ["//visibility:private"], ) filegroup( diff --git a/cc/common/cc_debug_helper.bzl b/cc/common/cc_debug_helper.bzl new file mode 100644 index 0000000..d16e925 --- /dev/null +++ b/cc/common/cc_debug_helper.bzl @@ -0,0 +1,181 @@ +# Copyright 2024 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Utilities for creating cc debug package information outputs""" + +load("//cc:find_cc_toolchain.bzl", "CC_TOOLCHAIN_TYPE") +load(":cc_helper.bzl", "linker_mode") +load(":visibility.bzl", "INTERNAL_VISIBILITY") + +visibility(INTERNAL_VISIBILITY) + +def create_debug_packager_actions( + ctx, + cc_toolchain, + dwp_output, + *, + cc_compilation_outputs, + cc_debug_context, + linking_mode, + use_pic = True, + lto_artifacts = []): + """Creates intermediate and final dwp creation action(s) + + Args: + ctx: (RuleContext) the rule context + cc_toolchain: (CcToolchainInfo) the cc toolchain + dwp_output: (File) the output of the final dwp action + cc_compilation_outputs: (CcCompilationOutputs) + cc_debug_context: (DebugContext) + linking_mode: (str) See cc_helper.bzl%linker_mode + use_pic: (bool) + lto_artifacts: ([CcLtoBackendArtifacts]) + """ + dwo_files = _collect_transitive_dwo_artifacts( + cc_compilation_outputs, + cc_debug_context, + linking_mode, + use_pic, + lto_artifacts, + ) + + # No inputs? Just generate a trivially empty .dwp. + # + # Note this condition automatically triggers for any build where fission is disabled. + # Because rules referencing .dwp targets may be invoked with or without fission, we need + # to support .dwp generation even when fission is disabled. Since no actual functionality + # is expected then, an empty file is appropriate. + dwo_files_list = dwo_files.to_list() + if len(dwo_files_list) == 0: + ctx.actions.write(dwp_output, "", False) + return + + # We apply a hierarchical action structure to limit the maximum number of inputs to any + # single action. + # + # While the dwp tool consumes .dwo files, it can also consume intermediate .dwp files, + # allowing us to split a large input set into smaller batches of arbitrary size and order. + # Aside from the parallelism performance benefits this offers, this also reduces input + # size requirements: if a.dwo, b.dwo, c.dwo, and e.dwo are each 1 KB files, we can apply + # two intermediate actions DWP(a.dwo, b.dwo) --> i1.dwp and DWP(c.dwo, e.dwo) --> i2.dwp. + # When we then apply the final action DWP(i1.dwp, i2.dwp) --> finalOutput.dwp, the inputs + # to this action will usually total far less than 4 KB. + # + # The actions form an n-ary tree with n == MAX_INPUTS_PER_DWP_ACTION. The tree is fuller + # at the leaves than the root, but that both increases parallelism and reduces the final + # action's input size. + packager = _create_intermediate_dwp_packagers(ctx, dwp_output, cc_toolchain, cc_toolchain._dwp_files, dwo_files_list, 1) + packager["outputs"].append(dwp_output) + packager["arguments"].add("-o", dwp_output) + ctx.actions.run( + mnemonic = "CcGenerateDwp", + tools = packager["tools"], + executable = packager["executable"], + toolchain = CC_TOOLCHAIN_TYPE, + arguments = [packager["arguments"]], + inputs = packager["inputs"], + outputs = packager["outputs"], + ) + +def _collect_transitive_dwo_artifacts(cc_compilation_outputs, cc_debug_context, linking_mode, use_pic, lto_backend_artifacts): + dwo_files = [] + transitive_dwo_files = depset() + if use_pic: + dwo_files.extend(cc_compilation_outputs.pic_dwo_files()) + else: + dwo_files.extend(cc_compilation_outputs.dwo_files()) + + if lto_backend_artifacts != None: + for lto_backend_artifact in lto_backend_artifacts: + if lto_backend_artifact.dwo_file() != None: + dwo_files.append(lto_backend_artifact.dwo_file()) + + if linking_mode != linker_mode.LINKING_DYNAMIC: + if use_pic: + transitive_dwo_files = cc_debug_context.pic_files + else: + transitive_dwo_files = cc_debug_context.files + return depset(dwo_files, transitive = [transitive_dwo_files]) + +def _get_intermediate_dwp_file(ctx, dwp_output, order_number): + output_path = dwp_output.short_path + + # Since it is a dwp_output we can assume that it always + # ends with .dwp suffix, because it is declared so in outputs + # attribute. + extension_stripped_output_path = output_path[0:len(output_path) - 4] + intermediate_path = extension_stripped_output_path + "-" + str(order_number) + ".dwp" + + return ctx.actions.declare_file("_dwps/" + intermediate_path) + +def _create_intermediate_dwp_packagers(ctx, dwp_output, cc_toolchain, dwp_files, dwo_files, intermediate_dwp_count): + intermediate_outputs = dwo_files + + # This long loop is a substitution for recursion, which is not currently supported in Starlark. + for _ in range(2147483647): + packagers = [] + current_packager = _new_dwp_action(ctx, cc_toolchain, dwp_files) + inputs_for_current_packager = 0 + + # Step 1: generate our batches. We currently break into arbitrary batches of fixed maximum + # input counts, but we can always apply more intelligent heuristics if the need arises. + for dwo_file in intermediate_outputs: + if inputs_for_current_packager == 100: + packagers.append(current_packager) + current_packager = _new_dwp_action(ctx, cc_toolchain, dwp_files) + inputs_for_current_packager = 0 + current_packager["inputs"].append(dwo_file) + + # add_all expands all directories to their contained files, see + # https://bazel.build/rules/lib/builtins/Args#add_all. add doesn't + # do that, so using add_all on the one-item list here allows us to + # find dwo files in directories. + current_packager["arguments"].add_all([dwo_file]) + inputs_for_current_packager += 1 + + packagers.append(current_packager) + + # Step 2: given the batches, create the actions. + if len(packagers) > 1: + # If we have multiple batches, make them all intermediate actions, then pipe their outputs + # into an additional level. + intermediate_outputs = [] + for packager in packagers: + intermediate_output = _get_intermediate_dwp_file(ctx, dwp_output, intermediate_dwp_count) + intermediate_dwp_count += 1 + packager["outputs"].append(intermediate_output) + packager["arguments"].add("-o", intermediate_output) + ctx.actions.run( + mnemonic = "CcGenerateIntermediateDwp", + tools = packager["tools"], + executable = packager["executable"], + toolchain = CC_TOOLCHAIN_TYPE, + arguments = [packager["arguments"]], + inputs = packager["inputs"], + outputs = packager["outputs"], + ) + intermediate_outputs.append(intermediate_output) + else: + return packagers[0] + + # This is to fix buildifier errors, even though we should never reach this part of the code. + return None + +def _new_dwp_action(ctx, cc_toolchain, dwp_tools): + return { + "arguments": ctx.actions.args(), + "executable": cc_toolchain._tool_paths.get("dwp", None), + "inputs": [], + "outputs": [], + "tools": dwp_tools, + } diff --git a/cc/common/cc_helper.bzl b/cc/common/cc_helper.bzl index 8b161a1..35f6812 100644 --- a/cc/common/cc_helper.bzl +++ b/cc/common/cc_helper.bzl @@ -11,10 +11,82 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - """Utility functions for C++ rules.""" -# LINT.IfChange +load("//cc:find_cc_toolchain.bzl", "CC_TOOLCHAIN_TYPE") +load(":cc_common.bzl", "cc_common") +load(":visibility.bzl", "INTERNAL_VISIBILITY") + +visibility(INTERNAL_VISIBILITY) + +# LINT.IfChange(linker_mode) +linker_mode = struct( + LINKING_DYNAMIC = "dynamic_linking_mode", + LINKING_STATIC = "static_linking_mode", +) +# LINT.ThenChange(https://github.com/bazelbuild/bazel/blob/master/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl:linker_mode) + +# LINT.IfChange(forked_exports) +def _get_static_mode_params_for_dynamic_library_libraries(libs): + linker_inputs = [] + for lib in libs.to_list(): + if lib.pic_static_library: + linker_inputs.append(lib.pic_static_library) + elif lib.static_library: + linker_inputs.append(lib.static_library) + elif lib.interface_library: + linker_inputs.append(lib.interface_library) + else: + linker_inputs.append(lib.dynamic_library) + return linker_inputs + +def _create_strip_action(ctx, cc_toolchain, cpp_config, input, output, feature_configuration): + if cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "no_stripping"): + ctx.actions.symlink( + output = output, + target_file = input, + progress_message = "Symlinking original binary as stripped binary", + ) + return + + if not cc_common.action_is_enabled(feature_configuration = feature_configuration, action_name = "strip"): + fail("Expected action_config for 'strip' to be configured.") + + variables = cc_common.create_compile_variables( + cc_toolchain = cc_toolchain, + feature_configuration = feature_configuration, + output_file = output.path, + input_file = input.path, + strip_opts = cpp_config.strip_opts(), + ) + command_line = cc_common.get_memory_inefficient_command_line( + feature_configuration = feature_configuration, + action_name = "strip", + variables = variables, + ) + env = cc_common.get_environment_variables( + feature_configuration = feature_configuration, + action_name = "strip", + variables = variables, + ) + execution_info = {} + for execution_requirement in cc_common.get_tool_requirement_for_action(feature_configuration = feature_configuration, action_name = "strip"): + execution_info[execution_requirement] = "" + ctx.actions.run( + inputs = depset( + direct = [input], + transitive = [cc_toolchain._strip_files], + ), + outputs = [output], + use_default_shell_env = True, + env = env, + executable = cc_common.get_tool_for_action(feature_configuration = feature_configuration, action_name = "strip"), + toolchain = CC_TOOLCHAIN_TYPE, + execution_requirements = execution_info, + progress_message = "Stripping {} for {}".format(output.short_path, ctx.label), + mnemonic = "CcStrip", + arguments = command_line, + ) def _lookup_var(ctx, additional_vars, var): expanded_make_var_ctx = ctx.var.get(var) @@ -195,9 +267,29 @@ def _tokenize(options, options_string): if force_token or len(token) > 0: options.append("".join(token)) +def _should_use_pic(ctx, cc_toolchain, feature_configuration): + """Whether to use pic files + + Args: + ctx: (RuleContext) + cc_toolchain: (CcToolchainInfo) + feature_configuration: (FeatureConfiguration) + + Returns: + (bool) + """ + return ctx.fragments.cpp.force_pic() or ( + cc_toolchain.needs_pic_for_dynamic_libraries(feature_configuration = feature_configuration) and ( + ctx.var["COMPILATION_MODE"] != "opt" or + cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "prefer_pic_for_opt_binaries") + ) + ) + cc_helper = struct( + create_strip_action = _create_strip_action, get_expanded_env = _get_expanded_env, + get_static_mode_params_for_dynamic_library_libraries = _get_static_mode_params_for_dynamic_library_libraries, + should_use_pic = _should_use_pic, tokenize = _tokenize, ) - -# LINT.ThenChange(https://github.com/bazelbuild/bazel/blob/master/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl) +# LINT.ThenChange(https://github.com/bazelbuild/bazel/blob/master/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl:forked_exports) diff --git a/cc/common/visibility.bzl b/cc/common/visibility.bzl new file mode 100644 index 0000000..dfbc8ce --- /dev/null +++ b/cc/common/visibility.bzl @@ -0,0 +1,3 @@ +"""Bzl load visibility package specs""" + +INTERNAL_VISIBILITY = ["//cc/..."]