Skip to content

Commit 553c3f0

Browse files
Properly configure AR for the cargo_build_script rule
There were two issues with AR in the cargo_build_script rule: 1. ARFLAGS was not set. 2. For many toolchains, the ar tool was not properly configured. This change fixes both of those problems.
1 parent 76a0b8f commit 553c3f0

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

cargo/private/cargo_build_script.bzl

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,10 @@ def get_cc_compile_args_and_env(cc_toolchain, feature_configuration):
130130
131131
Returns:
132132
tuple: A tuple of the following items:
133-
- (sequence): A flattened C command line flags for given action.
134-
- (sequence): A flattened CXX command line flags for given action.
135-
- (dict): C environment variables to be set for given action.
133+
- (sequence): A flattened list of C command line flags.
134+
- (sequence): A flattened list of CXX command line flags.
135+
- (sequence): A flattened list of AR command line flags.
136+
- (dict): C environment variables to be set for this configuration.
136137
"""
137138
compile_variables = cc_common.create_compile_variables(
138139
feature_configuration = feature_configuration,
@@ -148,12 +149,17 @@ def get_cc_compile_args_and_env(cc_toolchain, feature_configuration):
148149
action_name = ACTION_NAMES.cpp_compile,
149150
variables = compile_variables,
150151
)
152+
cc_ar_args = cc_common.get_memory_inefficient_command_line(
153+
feature_configuration = feature_configuration,
154+
action_name = ACTION_NAMES.cpp_link_static_library,
155+
variables = compile_variables,
156+
)
151157
cc_env = cc_common.get_environment_variables(
152158
feature_configuration = feature_configuration,
153159
action_name = ACTION_NAMES.c_compile,
154160
variables = compile_variables,
155161
)
156-
return cc_c_args, cc_cxx_args, cc_env
162+
return cc_c_args, cc_cxx_args, cc_ar_args, cc_env
157163

158164
def _pwd_flags_sysroot(args):
159165
"""Prefix execroot-relative paths of known arguments with ${pwd}.
@@ -423,7 +429,7 @@ def _cargo_build_script_impl(ctx):
423429
env["LDFLAGS"] = " ".join(_pwd_flags(link_args))
424430

425431
# MSVC requires INCLUDE to be set
426-
cc_c_args, cc_cxx_args, cc_env = get_cc_compile_args_and_env(cc_toolchain, feature_configuration)
432+
cc_c_args, cc_cxx_args, cc_ar_args, cc_env = get_cc_compile_args_and_env(cc_toolchain, feature_configuration)
427433
include = cc_env.get("INCLUDE")
428434
if include:
429435
env["INCLUDE"] = include
@@ -444,11 +450,17 @@ def _cargo_build_script_impl(ctx):
444450
action_name = ACTION_NAMES.cpp_link_static_library,
445451
)
446452

453+
# Many C/C++ toolchains are missing an action_config for AR because
454+
# one was never included in the unix_cc_toolchain_config.
455+
if not env["AR"]:
456+
env["AR"] == cc_toolchain.ar_executable
457+
447458
# Populate CFLAGS and CXXFLAGS that cc-rs relies on when building from source, in particular
448459
# to determine the deployment target when building for apple platforms (`macosx-version-min`
449460
# for example, itself derived from the `macos_minimum_os` Bazel argument).
450461
env["CFLAGS"] = " ".join(_pwd_flags(cc_c_args))
451462
env["CXXFLAGS"] = " ".join(_pwd_flags(cc_cxx_args))
463+
env["ARFLAGS"] = " ".join(_pwd_flags(cc_ar_args))
452464

453465
# Inform build scripts of rustc flags
454466
# https://github.com/rust-lang/cargo/issues/9600

0 commit comments

Comments
 (0)