Skip to content

Commit 3c027b6

Browse files
committed
Fix build scripts targeting the wrong architecture
PR #1081 started passing CFLAGS to build scripts, which cargo does too. But unfortunately Bazel's cpp toolchain adds an explicit target to the CFLAGS it provides, and that target uses the host platform instead of the target platform. When building a crate like blake3 or zstd on a Mac with iOS as the target, this ends up in the crate being built for macOS instead of iOS.
1 parent d288ed6 commit 3c027b6

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

cargo/cargo_build_script.bzl

+29-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,31 @@ load("//rust/private:rustc.bzl", "BuildInfo", "get_compilation_mode_opts", "get_
1313
# buildifier: disable=bzl-visibility
1414
load("//rust/private:utils.bzl", "dedent", "expand_dict_value_locations", "find_cc_toolchain", "find_toolchain", "name_to_crate_name")
1515

16+
def strip_target(elems):
17+
"""Remove '-target xxx' from C(XX)FLAGS.
18+
19+
The cpp toolchain adds '-target xxx' and '-isysroot xxx' to CFLAGS. If it is not stripped out before
20+
the CFLAGS are provided to build scripts, it can cause the build to take on the host architecture
21+
instead of the target architecture.
22+
23+
Args:
24+
elems (list): A list of args
25+
26+
Returns:
27+
list: the modified args
28+
"""
29+
skip_next = False
30+
out_elems = []
31+
for elem in elems:
32+
if skip_next:
33+
skip_next = False
34+
continue
35+
if elem == "-target" or elem == "-isysroot":
36+
skip_next = True
37+
continue
38+
out_elems.append(elem)
39+
return out_elems
40+
1641
def get_cc_compile_args_and_env(cc_toolchain, feature_configuration):
1742
"""Gather cc environment variables from the given `cc_toolchain`
1843
@@ -30,16 +55,16 @@ def get_cc_compile_args_and_env(cc_toolchain, feature_configuration):
3055
feature_configuration = feature_configuration,
3156
cc_toolchain = cc_toolchain,
3257
)
33-
cc_c_args = cc_common.get_memory_inefficient_command_line(
58+
cc_c_args = strip_target(cc_common.get_memory_inefficient_command_line(
3459
feature_configuration = feature_configuration,
3560
action_name = C_COMPILE_ACTION_NAME,
3661
variables = compile_variables,
37-
)
38-
cc_cxx_args = cc_common.get_memory_inefficient_command_line(
62+
))
63+
cc_cxx_args = strip_target(cc_common.get_memory_inefficient_command_line(
3964
feature_configuration = feature_configuration,
4065
action_name = CPP_COMPILE_ACTION_NAME,
4166
variables = compile_variables,
42-
)
67+
))
4368
cc_env = cc_common.get_environment_variables(
4469
feature_configuration = feature_configuration,
4570
action_name = C_COMPILE_ACTION_NAME,

0 commit comments

Comments
 (0)