Skip to content

Commit d6da8a6

Browse files
Do not forward ARFLAGS (#3763)
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 d6da8a6

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-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: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ def _test_cc_config_impl(ctx):
5454
name = "default_ar_flags",
5555
enabled = True,
5656
flag_sets = [
57+
flag_set(
58+
actions = [ACTION_NAMES.cpp_link_static_library],
59+
flag_groups = ([
60+
flag_group(
61+
# Simulate a case where a toolchain always passes
62+
# rcsD to `ar`.
63+
flags = ["rcsD"],
64+
),
65+
]),
66+
),
5767
flag_set(
5868
actions = [ACTION_NAMES.cpp_link_static_library],
5969
flag_groups = ([
@@ -207,7 +217,6 @@ def _cc_args_and_env_analysis_test_impl(ctx):
207217
)
208218

209219
_ENV_VAR_TO_EXPECTED_ARGS = {
210-
"ARFLAGS": ctx.attr.expected_arflags,
211220
"CFLAGS": ctx.attr.expected_cflags,
212221
"CXXFLAGS": ctx.attr.expected_cxxflags,
213222
}
@@ -221,13 +230,22 @@ def _cc_args_and_env_analysis_test_impl(ctx):
221230
"error: expected '{}' to be in cargo {}: '{}'".format(flag, env_var, actual_flags),
222231
)
223232

233+
arflags = cargo_action.env["ARFLAGS"]
234+
asserts.equals(
235+
env,
236+
[],
237+
arflags.split(" ") if arflags else [],
238+
"ARFLAGS is intentionally always empty. cc-rs tightly controls the " +
239+
"archiver flags in such a way that forwarding standard flags as " +
240+
"set up by most Bazel C/C++ toolchains is extremely error-prone.",
241+
)
242+
224243
return analysistest.end(env)
225244

226245
cc_args_and_env_analysis_test = analysistest.make(
227246
impl = _cc_args_and_env_analysis_test_impl,
228247
doc = """An analysistest to examine the custom cargo flags of an cargo_build_script target.""",
229248
attrs = {
230-
"expected_arflags": attr.string_list(default = ["-x"]),
231249
"expected_cflags": attr.string_list(default = ["-Wall"]),
232250
"expected_cxxflags": attr.string_list(default = ["-fno-rtti"]),
233251
"legacy_cc_toolchain": attr.bool(default = False),
@@ -394,17 +412,6 @@ def fsanitize_ignorelist_absolute_test(name):
394412
expected_cflags = ["-fsanitize-ignorelist=/test/absolute/path"],
395413
)
396414

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-
408415
def legacy_cc_toolchain_test(name):
409416
cargo_build_script_with_extra_cc_compile_flags(
410417
name = "%s/cargo_build_script" % name,

0 commit comments

Comments
 (0)