Skip to content

Commit da3d522

Browse files
authored
Fix build scripts targeting the wrong architecture (#1564)
* 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. * Add an end-to-end test to make sure building zstd for iOS works
1 parent d288ed6 commit da3d522

12 files changed

+756
-4
lines changed

.bazelci/presubmit.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,14 @@ tasks:
366366
working_directory: examples/ios
367367
build_targets:
368368
- "//..."
369+
ios_build:
370+
name: iOS build script cross compile test
371+
platform: macos
372+
working_directory: examples/ios_build
373+
test_flags:
374+
- "--platforms=//:ios_x86_64"
375+
test_targets:
376+
- "//..."
369377

370378
buildifier:
371379
version: latest

cargo/cargo_build_script.bzl

Lines changed: 29 additions & 4 deletions
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,

examples/.bazelignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ android
22
cargo_manifest_dir/external_crate
33
crate_universe
44
ios
5+
ios_build

examples/ios_build/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bazel-*

examples/ios_build/BUILD.bazel

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
load("@rules_rust//rust:defs.bzl", "rust_static_library")
2+
3+
rust_static_library(
4+
name = "ios_build_lib",
5+
srcs = glob(["**/*.rs"]),
6+
edition = "2018",
7+
deps = [
8+
"@ios_build//:zstd",
9+
],
10+
)
11+
12+
filegroup(
13+
name = "lib",
14+
srcs = ["ios_build_lib"],
15+
)
16+
17+
sh_test(
18+
name = "check_arch",
19+
srcs = ["check_arch.sh"],
20+
args = ["$(location :lib)"],
21+
data = [":lib"],
22+
)
23+
24+
platform(
25+
name = "macos_x86_64",
26+
constraint_values = [
27+
"@platforms//cpu:x86_64",
28+
"@platforms//os:macos",
29+
],
30+
)
31+
32+
platform(
33+
name = "macos_arm64",
34+
constraint_values = [
35+
"@platforms//cpu:arm64",
36+
"@platforms//os:macos",
37+
],
38+
)
39+
40+
platform(
41+
name = "ios_x86_64",
42+
constraint_values = [
43+
"@platforms//cpu:x86_64",
44+
"@platforms//os:ios",
45+
"@build_bazel_apple_support//constraints:simulator",
46+
],
47+
)
48+
49+
platform(
50+
name = "ios_sim_arm64",
51+
constraint_values = [
52+
"@platforms//cpu:arm64",
53+
"@platforms//os:ios",
54+
"@build_bazel_apple_support//constraints:simulator",
55+
],
56+
)
57+
58+
platform(
59+
name = "ios_arm64",
60+
constraint_values = [
61+
"@platforms//cpu:arm64",
62+
"@platforms//os:ios",
63+
"@build_bazel_apple_support//constraints:device",
64+
],
65+
)

examples/ios_build/Cargo.Bazel.lock

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# This file is automatically @generated by Cargo.
2+
# It is not intended for manual editing.
3+
version = 3
4+
5+
[[package]]
6+
name = "cc"
7+
version = "1.0.73"
8+
source = "registry+https://github.com/rust-lang/crates.io-index"
9+
checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11"
10+
dependencies = [
11+
"jobserver",
12+
]
13+
14+
[[package]]
15+
name = "ios_build"
16+
version = "0.1.0"
17+
dependencies = [
18+
"zstd",
19+
]
20+
21+
[[package]]
22+
name = "jobserver"
23+
version = "0.1.25"
24+
source = "registry+https://github.com/rust-lang/crates.io-index"
25+
checksum = "068b1ee6743e4d11fb9c6a1e6064b3693a1b600e7f5f5988047d98b3dc9fb90b"
26+
dependencies = [
27+
"libc",
28+
]
29+
30+
[[package]]
31+
name = "libc"
32+
version = "0.2.134"
33+
source = "registry+https://github.com/rust-lang/crates.io-index"
34+
checksum = "329c933548736bc49fd575ee68c89e8be4d260064184389a5b77517cddd99ffb"
35+
36+
[[package]]
37+
name = "zstd"
38+
version = "0.11.2+zstd.1.5.2"
39+
source = "registry+https://github.com/rust-lang/crates.io-index"
40+
checksum = "20cc960326ece64f010d2d2107537f26dc589a6573a316bd5b1dba685fa5fde4"
41+
dependencies = [
42+
"zstd-safe",
43+
]
44+
45+
[[package]]
46+
name = "zstd-safe"
47+
version = "5.0.2+zstd.1.5.2"
48+
source = "registry+https://github.com/rust-lang/crates.io-index"
49+
checksum = "1d2a5585e04f9eea4b2a3d1eca508c4dee9592a89ef6f450c11719da0726f4db"
50+
dependencies = [
51+
"libc",
52+
"zstd-sys",
53+
]
54+
55+
[[package]]
56+
name = "zstd-sys"
57+
version = "2.0.1+zstd.1.5.2"
58+
source = "registry+https://github.com/rust-lang/crates.io-index"
59+
checksum = "9fd07cbbc53846d9145dbffdf6dd09a7a0aa52be46741825f5c97bdd4f73f12b"
60+
dependencies = [
61+
"cc",
62+
"libc",
63+
]

examples/ios_build/Cargo.lock

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/ios_build/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "ios_build"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
zstd = "0.11.2"

examples/ios_build/WORKSPACE.bazel

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
workspace(name = "examples")
2+
3+
local_repository(
4+
name = "rules_rust",
5+
path = "../../",
6+
)
7+
8+
load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")
9+
10+
rules_rust_dependencies()
11+
12+
rust_register_toolchains(
13+
edition = "2018",
14+
extra_target_triples = [
15+
"aarch64-apple-ios-sim",
16+
"x86_64-apple-ios",
17+
],
18+
)
19+
20+
load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies")
21+
22+
crate_universe_dependencies(bootstrap = True)
23+
24+
load("@rules_rust//crate_universe:defs.bzl", "crates_repository", "splicing_config")
25+
26+
crates_repository(
27+
name = "ios_build",
28+
cargo_lockfile = "//:Cargo.Bazel.lock",
29+
# `generator` is not necessary in official releases.
30+
# See load satement for `cargo_bazel_bootstrap`.
31+
generator = "@cargo_bazel_bootstrap//:cargo-bazel",
32+
lockfile = "//:cargo-bazel-lock.json",
33+
manifests = ["//:Cargo.toml"],
34+
splicing_config = splicing_config(
35+
resolver_version = "2",
36+
),
37+
)
38+
39+
load(
40+
"@ios_build//:defs.bzl",
41+
ios_build_crate_repositories = "crate_repositories",
42+
)
43+
44+
ios_build_crate_repositories()
45+
46+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
47+
48+
# Used for Bazel CI
49+
http_archive(
50+
name = "bazelci_rules",
51+
sha256 = "eca21884e6f66a88c358e580fd67a6b148d30ab57b1680f62a96c00f9bc6a07e",
52+
strip_prefix = "bazelci_rules-1.0.0",
53+
url = "https://github.com/bazelbuild/continuous-integration/releases/download/rules-1.0.0/bazelci_rules-1.0.0.tar.gz",
54+
)
55+
56+
load("@bazelci_rules//:rbe_repo.bzl", "rbe_preconfig")
57+
58+
# Creates a default toolchain config for RBE.
59+
# Use this as is if you are using the rbe_ubuntu16_04 container,
60+
# otherwise refer to RBE docs.
61+
rbe_preconfig(
62+
name = "buildkite_config",
63+
toolchain = "ubuntu1804-bazel-java11",
64+
)

0 commit comments

Comments
 (0)