Skip to content

Commit 2ac985b

Browse files
Do not forward ARFLAGS
This is a partial revert of #3704. It does NOT revert logic that properly finds the hermetic archiver (binary and associated tests). cc-rs needs full ownership of flag handling for the archiver. This is in part due to `ar` having very specific positional flag behaviors, and partially due to some special logic in how cc-rs constructs static libraries. The change to enable ARFLAGS was causing many toolchains that pass flags to the static archiver to error out due to conflicting expectations for flag handling. Example bad `ar` invocation, where `cq` is treated as the destination archive instead of the `ar` tool mode: ar rcsD cq dest.a src1.o src2.o This drops forwarding of `ARFLAGS` to fix the common case.
1 parent 3cbb3fc commit 2ac985b

File tree

3 files changed

+10
-25
lines changed

3 files changed

+10
-25
lines changed

cargo/private/cargo_build_script.bzl

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ def get_cc_compile_args_and_env(cc_toolchain, feature_configuration):
131131
tuple: A tuple of the following items:
132132
- (sequence): A flattened list of C command line flags.
133133
- (sequence): A flattened list of CXX command line flags.
134-
- (sequence): A flattened list of AR command line flags.
135134
- (dict): C environment variables to be set for this configuration.
136135
"""
137136
compile_variables = cc_common.create_compile_variables(
@@ -148,17 +147,12 @@ def get_cc_compile_args_and_env(cc_toolchain, feature_configuration):
148147
action_name = ACTION_NAMES.cpp_compile,
149148
variables = compile_variables,
150149
)
151-
cc_ar_args = cc_common.get_memory_inefficient_command_line(
152-
feature_configuration = feature_configuration,
153-
action_name = ACTION_NAMES.cpp_link_static_library,
154-
variables = compile_variables,
155-
)
156150
cc_env = cc_common.get_environment_variables(
157151
feature_configuration = feature_configuration,
158152
action_name = ACTION_NAMES.c_compile,
159153
variables = compile_variables,
160154
)
161-
return cc_c_args, cc_cxx_args, cc_ar_args, cc_env
155+
return cc_c_args, cc_cxx_args, cc_env
162156

163157
def _pwd_flags_sysroot(args):
164158
"""Prefix execroot-relative paths of known arguments with ${pwd}.
@@ -429,12 +423,13 @@ def _cargo_build_script_impl(ctx):
429423
env["CC"] = "${{pwd}}/{}".format(ctx.executable._fallback_cc.path)
430424
env["CXX"] = "${{pwd}}/{}".format(ctx.executable._fallback_cxx.path)
431425
env["AR"] = "${{pwd}}/{}".format(ctx.executable._fallback_ar.path)
426+
env["ARFLAGS"] = ""
432427
env["CFLAGS"] = ""
433428
env["CXXFLAGS"] = ""
434429

435430
if cc_toolchain:
436431
# MSVC requires INCLUDE to be set
437-
cc_c_args, cc_cxx_args, cc_ar_args, cc_env = get_cc_compile_args_and_env(cc_toolchain, feature_configuration)
432+
cc_c_args, cc_cxx_args, cc_env = get_cc_compile_args_and_env(cc_toolchain, feature_configuration)
438433
include = cc_env.get("INCLUDE")
439434
if include:
440435
env["INCLUDE"] = include
@@ -464,7 +459,9 @@ def _cargo_build_script_impl(ctx):
464459
# for example, itself derived from the `macos_minimum_os` Bazel argument).
465460
env["CFLAGS"] = " ".join(_pwd_flags(cc_c_args))
466461
env["CXXFLAGS"] = " ".join(_pwd_flags(cc_cxx_args))
467-
env["ARFLAGS"] = " ".join(_pwd_flags(cc_ar_args))
462+
# It may be tempting to forward ARFLAGS, but cc-rs is opinionated enough
463+
# that doing so is more likely to hurt than help. If you need to change
464+
# ARFLAGS, make changes to cc-rs.
468465

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

test/cargo_build_script/cc_args_and_env/BUILD.bazel

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
load(
22
"cc_args_and_env_test.bzl",
3-
"ar_flags_test",
43
"bindir_absolute_test",
54
"bindir_relative_test",
65
"fsanitize_ignorelist_absolute_test",
@@ -31,6 +30,4 @@ fsanitize_ignorelist_absolute_test(name = "fsanitize_ignorelist_absolute_test")
3130

3231
fsanitize_ignorelist_relative_test(name = "fsanitize_ignorelist_relative_test")
3332

34-
ar_flags_test(name = "ar_flags_test")
35-
3633
legacy_cc_toolchain_test(name = "legacy_cc_toolchain_test")

test/cargo_build_script/cc_args_and_env/cc_args_and_env_test.bzl

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,12 @@ def _cc_args_and_env_analysis_test_impl(ctx):
207207
)
208208

209209
_ENV_VAR_TO_EXPECTED_ARGS = {
210-
"ARFLAGS": ctx.attr.expected_arflags,
211210
"CFLAGS": ctx.attr.expected_cflags,
212211
"CXXFLAGS": ctx.attr.expected_cxxflags,
212+
# ARFLAGS is intentionally always empty. cc-rs tightly controls the
213+
# archiver flags in such a way that forwarding standard flags as
214+
# set up by most Bazel C/C++ toolchains is extremely error-prone.
215+
"ARFLAGS": [],
213216
}
214217

215218
for env_var, expected_flags in _ENV_VAR_TO_EXPECTED_ARGS.items():
@@ -227,7 +230,6 @@ cc_args_and_env_analysis_test = analysistest.make(
227230
impl = _cc_args_and_env_analysis_test_impl,
228231
doc = """An analysistest to examine the custom cargo flags of an cargo_build_script target.""",
229232
attrs = {
230-
"expected_arflags": attr.string_list(default = ["-x"]),
231233
"expected_cflags": attr.string_list(default = ["-Wall"]),
232234
"expected_cxxflags": attr.string_list(default = ["-fno-rtti"]),
233235
"legacy_cc_toolchain": attr.bool(default = False),
@@ -394,17 +396,6 @@ def fsanitize_ignorelist_absolute_test(name):
394396
expected_cflags = ["-fsanitize-ignorelist=/test/absolute/path"],
395397
)
396398

397-
def ar_flags_test(name):
398-
cargo_build_script_with_extra_cc_compile_flags(
399-
name = "%s/cargo_build_script" % name,
400-
extra_ar_flags = ["-static"],
401-
)
402-
cc_args_and_env_analysis_test(
403-
name = name,
404-
target_under_test = "%s/cargo_build_script" % name,
405-
expected_arflags = ["-static"],
406-
)
407-
408399
def legacy_cc_toolchain_test(name):
409400
cargo_build_script_with_extra_cc_compile_flags(
410401
name = "%s/cargo_build_script" % name,

0 commit comments

Comments
 (0)