Skip to content

Commit afb41c2

Browse files
committed
Fix build scripts targeting the wrong architecture
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 f0cdced commit afb41c2

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

cargo/cargo_build_script.bzl

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@ 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 (at least on macOS) adds '-target xxx' to CFLAGS. If it is not stripped out before the CFLAGS are
20+
provided to build scripts, it can cause the build to take on the host architecture instead of the target architecture.
21+
22+
Args:
23+
elems (list): A list of args
24+
25+
Returns:
26+
list: the modified args
27+
"""
28+
skip_next = False
29+
out_elems = []
30+
for elem in elems:
31+
if skip_next:
32+
skip_next = False
33+
continue
34+
if elem == "-target":
35+
skip_next = True
36+
continue
37+
out_elems.append(elem)
38+
return out_elems
39+
1640
def get_cc_compile_args_and_env(cc_toolchain, feature_configuration):
1741
"""Gather cc environment variables from the given `cc_toolchain`
1842
@@ -30,16 +54,16 @@ def get_cc_compile_args_and_env(cc_toolchain, feature_configuration):
3054
feature_configuration = feature_configuration,
3155
cc_toolchain = cc_toolchain,
3256
)
33-
cc_c_args = cc_common.get_memory_inefficient_command_line(
57+
cc_c_args = strip_target(cc_common.get_memory_inefficient_command_line(
3458
feature_configuration = feature_configuration,
3559
action_name = C_COMPILE_ACTION_NAME,
3660
variables = compile_variables,
37-
)
38-
cc_cxx_args = cc_common.get_memory_inefficient_command_line(
61+
))
62+
cc_cxx_args = strip_target(cc_common.get_memory_inefficient_command_line(
3963
feature_configuration = feature_configuration,
4064
action_name = CPP_COMPILE_ACTION_NAME,
4165
variables = compile_variables,
42-
)
66+
))
4367
cc_env = cc_common.get_environment_variables(
4468
feature_configuration = feature_configuration,
4569
action_name = C_COMPILE_ACTION_NAME,

0 commit comments

Comments
 (0)