Add archiver flags to cpp crosstool config
There are three changes proposed in this pr that do not alter the current behaviour but that allow more flexibility when defining a toolchain:
- Move default macos archiver flags from [CppActionConfigs.java#L622](https://github.com/bazelbuild/bazel/blob/feea781b30788997c0b97ad9103a13fdc3f627c8/src/main/java/com/google/devtools/build/lib/rules/cpp/CppActionConfigs.java#L622) to the crosstools definition.
- Add `archive_flag` attribute to allow passing additional flags to the archive action.
- Add libtool feature to switch between macos `libtool`'s flags and `ar`'s.
Closes #14873.
PiperOrigin-RevId: 450893196
Change-Id: Ibbbc6383d69c453fd8e86aee2c7399ee9988fa22
diff --git a/tools/cpp/unix_cc_toolchain_config.bzl b/tools/cpp/unix_cc_toolchain_config.bzl
index b6cd42a..9e2e744 100644
--- a/tools/cpp/unix_cc_toolchain_config.bzl
+++ b/tools/cpp/unix_cc_toolchain_config.bzl
@@ -927,6 +927,26 @@
expand_if_available = "output_execpath",
),
],
+ with_features = [
+ with_feature_set(
+ not_features = ["libtool"],
+ ),
+ ],
+ ),
+ flag_set(
+ actions = [ACTION_NAMES.cpp_link_static_library],
+ flag_groups = [
+ flag_group(flags = ["-static", "-s"]),
+ flag_group(
+ flags = ["-o", "%{output_execpath}"],
+ expand_if_available = "output_execpath",
+ ),
+ ],
+ with_features = [
+ with_feature_set(
+ features = ["libtool"],
+ ),
+ ],
),
flag_set(
actions = [ACTION_NAMES.cpp_link_static_library],
@@ -954,6 +974,14 @@
),
],
),
+ flag_set(
+ actions = [ACTION_NAMES.cpp_link_static_library],
+ flag_groups = ([
+ flag_group(
+ flags = ctx.attr.archive_flags,
+ ),
+ ] if ctx.attr.archive_flags else []),
+ ),
],
)
@@ -1147,6 +1175,10 @@
)
is_linux = ctx.attr.target_libc != "macosx"
+ libtool_feature = feature(
+ name = "libtool",
+ enabled = not is_linux,
+ )
# TODO(#8303): Mac crosstool should also declare every feature.
if is_linux:
@@ -1175,6 +1207,7 @@
output_execpath_flags_feature,
runtime_library_search_directories_feature,
library_search_directories_feature,
+ libtool_feature,
archiver_flags_feature,
force_pic_flags_feature,
fission_support_feature,
@@ -1211,6 +1244,8 @@
),
]
features = [
+ libtool_feature,
+ archiver_flags_feature,
supports_pic_feature,
] + (
[
@@ -1267,6 +1302,7 @@
"opt_compile_flags": attr.string_list(),
"cxx_flags": attr.string_list(),
"link_flags": attr.string_list(),
+ "archive_flags": attr.string_list(),
"link_libs": attr.string_list(),
"opt_link_flags": attr.string_list(),
"unfiltered_compile_flags": attr.string_list(),