Skip to content

Commit 830f5c0

Browse files
committed
Add example workspace cross-compiling with zig
We've run into a number of issues, both in rustc and in cargo build scripts, where we're not properly propagating cc_toolchain information to compiles and particularly link actions. These have often been hard to test because we don't have an example set-up with a non-default C++ toolchain. By wiring up a zig cross-compiling toolchain, we have a concrete example we can work with. It's also an interesting potential example for folks who want to set something similar up.
1 parent ddf2a4c commit 830f5c0

File tree

7 files changed

+326
-0
lines changed

7 files changed

+326
-0
lines changed

.bazelci/presubmit.yml

+6
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,12 @@ tasks:
457457
- "--platforms=//:ios_x86_64"
458458
test_targets:
459459
- "//..."
460+
zig_cross_compiling:
461+
name: Zig cross compiling test
462+
platform: ubuntu2004
463+
working_directory: examples/zig_cross_compiling
464+
build_targets:
465+
- "//..."
460466

461467
buildifier:
462468
version: latest

examples/zig_cross_compiling/.bazelrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build --incompatible_enable_cc_toolchain_resolution
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bazel-*
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup")
2+
load("@crate_index//:defs.bzl", "aliases", "all_crate_deps")
3+
load("@rules_rust//rust:defs.bzl", "rust_binary")
4+
5+
rust_binary(
6+
name = "uses_ring",
7+
srcs = ["src/main.rs"],
8+
aliases = aliases(),
9+
deps = all_crate_deps(normal = True),
10+
# We don't care about building this for our own platform, this just exists for the transition
11+
tags = ["manual"],
12+
)
13+
14+
platform(
15+
name = "aarch64_linux",
16+
constraint_values = [
17+
"@platforms//os:linux",
18+
"@platforms//cpu:aarch64",
19+
],
20+
)
21+
22+
platform_transition_filegroup(
23+
name = "uses_ring_arm",
24+
srcs = [":uses_ring"],
25+
target_platform = "aarch64_linux",
26+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
# This file is automatically @generated by Cargo.
2+
# It is not intended for manual editing.
3+
version = 3
4+
5+
[[package]]
6+
name = "bumpalo"
7+
version = "3.11.1"
8+
source = "registry+https://github.com/rust-lang/crates.io-index"
9+
checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba"
10+
11+
[[package]]
12+
name = "cc"
13+
version = "1.0.76"
14+
source = "registry+https://github.com/rust-lang/crates.io-index"
15+
checksum = "76a284da2e6fe2092f2353e51713435363112dfd60030e22add80be333fb928f"
16+
17+
[[package]]
18+
name = "cfg-if"
19+
version = "1.0.0"
20+
source = "registry+https://github.com/rust-lang/crates.io-index"
21+
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
22+
23+
[[package]]
24+
name = "direct-cargo-bazel-deps"
25+
version = "0.0.1"
26+
dependencies = [
27+
"ring",
28+
]
29+
30+
[[package]]
31+
name = "js-sys"
32+
version = "0.3.60"
33+
source = "registry+https://github.com/rust-lang/crates.io-index"
34+
checksum = "49409df3e3bf0856b916e2ceaca09ee28e6871cf7d9ce97a692cacfdb2a25a47"
35+
dependencies = [
36+
"wasm-bindgen",
37+
]
38+
39+
[[package]]
40+
name = "libc"
41+
version = "0.2.137"
42+
source = "registry+https://github.com/rust-lang/crates.io-index"
43+
checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89"
44+
45+
[[package]]
46+
name = "log"
47+
version = "0.4.17"
48+
source = "registry+https://github.com/rust-lang/crates.io-index"
49+
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
50+
dependencies = [
51+
"cfg-if",
52+
]
53+
54+
[[package]]
55+
name = "once_cell"
56+
version = "1.16.0"
57+
source = "registry+https://github.com/rust-lang/crates.io-index"
58+
checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860"
59+
60+
[[package]]
61+
name = "proc-macro2"
62+
version = "1.0.47"
63+
source = "registry+https://github.com/rust-lang/crates.io-index"
64+
checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725"
65+
dependencies = [
66+
"unicode-ident",
67+
]
68+
69+
[[package]]
70+
name = "quote"
71+
version = "1.0.21"
72+
source = "registry+https://github.com/rust-lang/crates.io-index"
73+
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
74+
dependencies = [
75+
"proc-macro2",
76+
]
77+
78+
[[package]]
79+
name = "ring"
80+
version = "0.16.20"
81+
source = "registry+https://github.com/rust-lang/crates.io-index"
82+
checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc"
83+
dependencies = [
84+
"cc",
85+
"libc",
86+
"once_cell",
87+
"spin",
88+
"untrusted",
89+
"web-sys",
90+
"winapi",
91+
]
92+
93+
[[package]]
94+
name = "spin"
95+
version = "0.5.2"
96+
source = "registry+https://github.com/rust-lang/crates.io-index"
97+
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
98+
99+
[[package]]
100+
name = "syn"
101+
version = "1.0.103"
102+
source = "registry+https://github.com/rust-lang/crates.io-index"
103+
checksum = "a864042229133ada95abf3b54fdc62ef5ccabe9515b64717bcb9a1919e59445d"
104+
dependencies = [
105+
"proc-macro2",
106+
"quote",
107+
"unicode-ident",
108+
]
109+
110+
[[package]]
111+
name = "unicode-ident"
112+
version = "1.0.5"
113+
source = "registry+https://github.com/rust-lang/crates.io-index"
114+
checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"
115+
116+
[[package]]
117+
name = "untrusted"
118+
version = "0.7.1"
119+
source = "registry+https://github.com/rust-lang/crates.io-index"
120+
checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a"
121+
122+
[[package]]
123+
name = "wasm-bindgen"
124+
version = "0.2.83"
125+
source = "registry+https://github.com/rust-lang/crates.io-index"
126+
checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268"
127+
dependencies = [
128+
"cfg-if",
129+
"wasm-bindgen-macro",
130+
]
131+
132+
[[package]]
133+
name = "wasm-bindgen-backend"
134+
version = "0.2.83"
135+
source = "registry+https://github.com/rust-lang/crates.io-index"
136+
checksum = "4c8ffb332579b0557b52d268b91feab8df3615f265d5270fec2a8c95b17c1142"
137+
dependencies = [
138+
"bumpalo",
139+
"log",
140+
"once_cell",
141+
"proc-macro2",
142+
"quote",
143+
"syn",
144+
"wasm-bindgen-shared",
145+
]
146+
147+
[[package]]
148+
name = "wasm-bindgen-macro"
149+
version = "0.2.83"
150+
source = "registry+https://github.com/rust-lang/crates.io-index"
151+
checksum = "052be0f94026e6cbc75cdefc9bae13fd6052cdcaf532fa6c45e7ae33a1e6c810"
152+
dependencies = [
153+
"quote",
154+
"wasm-bindgen-macro-support",
155+
]
156+
157+
[[package]]
158+
name = "wasm-bindgen-macro-support"
159+
version = "0.2.83"
160+
source = "registry+https://github.com/rust-lang/crates.io-index"
161+
checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c"
162+
dependencies = [
163+
"proc-macro2",
164+
"quote",
165+
"syn",
166+
"wasm-bindgen-backend",
167+
"wasm-bindgen-shared",
168+
]
169+
170+
[[package]]
171+
name = "wasm-bindgen-shared"
172+
version = "0.2.83"
173+
source = "registry+https://github.com/rust-lang/crates.io-index"
174+
checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f"
175+
176+
[[package]]
177+
name = "web-sys"
178+
version = "0.3.60"
179+
source = "registry+https://github.com/rust-lang/crates.io-index"
180+
checksum = "bcda906d8be16e728fd5adc5b729afad4e444e106ab28cd1c7256e54fa61510f"
181+
dependencies = [
182+
"js-sys",
183+
"wasm-bindgen",
184+
]
185+
186+
[[package]]
187+
name = "winapi"
188+
version = "0.3.9"
189+
source = "registry+https://github.com/rust-lang/crates.io-index"
190+
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
191+
dependencies = [
192+
"winapi-i686-pc-windows-gnu",
193+
"winapi-x86_64-pc-windows-gnu",
194+
]
195+
196+
[[package]]
197+
name = "winapi-i686-pc-windows-gnu"
198+
version = "0.4.0"
199+
source = "registry+https://github.com/rust-lang/crates.io-index"
200+
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
201+
202+
[[package]]
203+
name = "winapi-x86_64-pc-windows-gnu"
204+
version = "0.4.0"
205+
source = "registry+https://github.com/rust-lang/crates.io-index"
206+
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
2+
3+
BAZEL_ZIG_CC_VERSION = "v0.9.2"
4+
5+
http_archive(
6+
name = "bazel-zig-cc",
7+
sha256 = "73afa7e1af49e3dbfa1bae9362438cdc51cb177c359a6041a7a403011179d0b5",
8+
strip_prefix = "bazel-zig-cc-{}".format(BAZEL_ZIG_CC_VERSION),
9+
urls = ["https://git.sr.ht/~motiejus/bazel-zig-cc/archive/{}.tar.gz".format(BAZEL_ZIG_CC_VERSION)],
10+
)
11+
12+
load("@bazel-zig-cc//toolchain:defs.bzl", zig_toolchains = "toolchains")
13+
14+
zig_toolchains()
15+
16+
register_toolchains(
17+
"@zig_sdk//toolchain:linux_arm64_gnu.2.28",
18+
)
19+
20+
local_repository(
21+
name = "rules_rust",
22+
path = "../..",
23+
)
24+
25+
load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains", "rust_repository_set")
26+
27+
rules_rust_dependencies()
28+
29+
load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies")
30+
31+
crate_universe_dependencies(bootstrap = True)
32+
33+
rust_version = "1.65.0"
34+
35+
rust_register_toolchains(
36+
edition = "2021",
37+
extra_target_triples = [],
38+
version = rust_version,
39+
)
40+
41+
rust_repository_set(
42+
name = "macos_x86_64",
43+
edition = "2021",
44+
exec_triple = "x86_64-apple-darwin",
45+
extra_target_triples = ["aarch64-unknown-linux-gnu"],
46+
version = rust_version,
47+
)
48+
49+
rust_repository_set(
50+
name = "linux_x86_64",
51+
edition = "2021",
52+
exec_triple = "x86_64-unknown-linux-gnu",
53+
extra_target_triples = ["aarch64-unknown-linux-gnu"],
54+
version = rust_version,
55+
)
56+
57+
load("@rules_rust//crate_universe:defs.bzl", "crate", "crates_repository")
58+
59+
crates_repository(
60+
name = "crate_index",
61+
cargo_lockfile = "//:Cargo.bazel.lock",
62+
# `generator` is not necessary in official releases.
63+
# See load satement for `cargo_bazel_bootstrap`.
64+
generator = "@cargo_bazel_bootstrap//:cargo-bazel",
65+
packages = {
66+
"ring": crate.spec(version = "0.16.20"),
67+
},
68+
)
69+
70+
load("@crate_index//:defs.bzl", "crate_repositories")
71+
72+
crate_repositories()
73+
74+
http_archive(
75+
name = "aspect_bazel_lib",
76+
sha256 = "3534a27621725fbbf1d3e53daa0c1dda055a2732d9031b8c579f917d7347b6c4",
77+
strip_prefix = "bazel-lib-1.16.1",
78+
url = "https://github.com/aspect-build/bazel-lib/archive/refs/tags/v1.16.1.tar.gz",
79+
)
80+
81+
load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies")
82+
83+
aspect_bazel_lib_dependencies()
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("{:?}", ring::rand::SystemRandom::new());
3+
}

0 commit comments

Comments
 (0)