Skip to content

Commit 8b977b5

Browse files
authored
Add example of cross-compiling with musl (#2535)
1 parent 5d4dc3f commit 8b977b5

File tree

9 files changed

+205
-0
lines changed

9 files changed

+205
-0
lines changed

.bazelci/presubmit.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,14 @@ tasks:
636636
# working_directory: examples/zig_cross_compiling
637637
# build_targets:
638638
# - "//..."
639+
musl_cross_compiling_macos_to_linux:
640+
name: Musl cross compiling test from macOS to Linux
641+
platform: macos
642+
working_directory: examples/musl_cross_compiling
643+
build_targets:
644+
- "//..."
645+
test_targets:
646+
- "//..."
639647
nix_cross_compiling:
640648
name: Nix cross compiling test
641649
platform: ubuntu2204

examples/.bazelignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ crate_universe
55
crate_universe_unnamed
66
ios
77
ios_build
8+
musl_cross_compiling
89
nix_cross_compiling
910
zig_cross_compiling
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bazel-*
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_binary")
2+
load("@rules_rust//rust:defs.bzl", "rust_binary")
3+
4+
rust_binary(
5+
name = "hello",
6+
srcs = ["src/main.rs"],
7+
tags = ["manual"],
8+
)
9+
10+
platform_transition_binary(
11+
name = "hello_linux_x86_64_musl",
12+
binary = ":hello",
13+
target_platform = "//platforms:linux_x86_64_musl",
14+
)
15+
16+
sh_test(
17+
name = "hello_linux_x86_64_musl_test",
18+
srcs = ["hello_linux_musl_test.sh"],
19+
args = [
20+
"$(rootpath :hello_linux_x86_64_musl)",
21+
"'ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), static-pie linked'",
22+
],
23+
data = [":hello_linux_x86_64_musl"],
24+
)
25+
26+
platform_transition_binary(
27+
name = "hello_linux_arm64_musl",
28+
binary = ":hello",
29+
target_platform = "//platforms:linux_arm64_musl",
30+
)
31+
32+
sh_test(
33+
name = "hello_linux_arm64_musl_test",
34+
srcs = ["hello_linux_musl_test.sh"],
35+
args = [
36+
"$(rootpath :hello_linux_arm64_musl)",
37+
"'ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked'",
38+
],
39+
data = [":hello_linux_arm64_musl"],
40+
)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
local_repository(
2+
name = "rules_rust",
3+
path = "../..",
4+
)
5+
6+
load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains", "rust_repository_set")
7+
8+
rules_rust_dependencies()
9+
10+
EDITION = "2021"
11+
12+
RUST_VERSION = "1.76.0"
13+
14+
rust_register_toolchains(
15+
edition = EDITION,
16+
)
17+
18+
rust_repository_set(
19+
name = "darwin_x86_64_to_x86_64_musl_tuple",
20+
edition = EDITION,
21+
exec_triple = "x86_64-apple-darwin",
22+
# Setting this extra_target_triples allows differentiating the musl case from the non-musl case, in case multiple linux-targeting toolchains are registered.
23+
extra_target_triples = {"x86_64-unknown-linux-musl": [
24+
"@//linker_config:musl",
25+
"@platforms//cpu:x86_64",
26+
"@platforms//os:linux",
27+
]},
28+
versions = [RUST_VERSION],
29+
)
30+
31+
rust_repository_set(
32+
name = "darwin_arm64_to_x86_64_musl_tuple",
33+
edition = EDITION,
34+
exec_triple = "aarch64-apple-darwin",
35+
extra_target_triples = {"x86_64-unknown-linux-musl": [
36+
"@//linker_config:musl",
37+
"@platforms//cpu:x86_64",
38+
"@platforms//os:linux",
39+
]},
40+
versions = [RUST_VERSION],
41+
)
42+
43+
rust_repository_set(
44+
name = "darwin_x86_64_to_arm64_musl_tuple",
45+
edition = EDITION,
46+
exec_triple = "x86_64-apple-darwin",
47+
# Setting this extra_target_triples allows differentiating the musl case from the non-musl case, in case multiple linux-targeting toolchains are registered.
48+
extra_target_triples = {"aarch64-unknown-linux-musl": [
49+
"@//linker_config:musl",
50+
"@platforms//cpu:arm64",
51+
"@platforms//os:linux",
52+
]},
53+
versions = [RUST_VERSION],
54+
)
55+
56+
rust_repository_set(
57+
name = "darwin_arm64_to_arm64_musl_tuple",
58+
edition = EDITION,
59+
exec_triple = "aarch64-apple-darwin",
60+
extra_target_triples = {"aarch64-unknown-linux-musl": [
61+
"@//linker_config:musl",
62+
"@platforms//cpu:arm64",
63+
"@platforms//os:linux",
64+
]},
65+
versions = [RUST_VERSION],
66+
)
67+
68+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
69+
70+
http_archive(
71+
name = "aspect_bazel_lib",
72+
sha256 = "f5ea76682b209cc0bd90d0f5a3b26d2f7a6a2885f0c5f615e72913f4805dbb0d",
73+
strip_prefix = "bazel-lib-2.5.0",
74+
url = "https://github.com/aspect-build/bazel-lib/releases/download/v2.5.0/bazel-lib-v2.5.0.tar.gz",
75+
)
76+
77+
load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies", "aspect_bazel_lib_register_toolchains")
78+
79+
aspect_bazel_lib_dependencies()
80+
81+
aspect_bazel_lib_register_toolchains()
82+
83+
http_archive(
84+
name = "musl_toolchains",
85+
sha256 = "f9f077b9ae74a0545f7cb7108462cb061593eef10fd09d25db4554e281ee880b",
86+
url = "https://github.com/bazel-contrib/musl-toolchain/releases/download/v0.1.7/musl_toolchain-v0.1.7.tar.gz",
87+
)
88+
89+
load("@musl_toolchains//:repositories.bzl", "load_musl_toolchains")
90+
91+
# Setting this extra_target_triples allows differentiating the musl case from the non-musl case, in case multiple linux-targeting toolchains are registered.
92+
load_musl_toolchains(extra_target_compatible_with = ["@//linker_config:musl"])
93+
94+
load("@musl_toolchains//:toolchains.bzl", "register_musl_toolchains")
95+
96+
register_musl_toolchains()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
if [[ $# -ne 2 ]]; then
6+
echo >&2 "Usage: $0 /path/to/binary file-output"
7+
exit 1
8+
fi
9+
10+
binary="$1"
11+
want_file_output="$2"
12+
13+
out="$(file "${binary}")"
14+
15+
if [[ "${out}" != *"${want_file_output}"* ]]; then
16+
echo >&2 "Wrong file type: ${out}"
17+
exit 1
18+
fi
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
constraint_setting(
2+
name = "linker",
3+
default_constraint_value = ":unknown",
4+
visibility = ["//visibility:public"],
5+
)
6+
7+
constraint_value(
8+
name = "musl",
9+
constraint_setting = ":linker",
10+
visibility = ["//visibility:public"],
11+
)
12+
13+
# Default linker for anyone not setting the linker to `musl`.
14+
# You shouldn't ever need to set this value manually.
15+
constraint_value(
16+
name = "unknown",
17+
constraint_setting = ":linker",
18+
visibility = ["//visibility:public"],
19+
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
platform(
2+
name = "linux_x86_64_musl",
3+
constraint_values = [
4+
"@//linker_config:musl",
5+
"@platforms//cpu:x86_64",
6+
"@platforms//os:linux",
7+
],
8+
visibility = ["//visibility:public"],
9+
)
10+
11+
platform(
12+
name = "linux_arm64_musl",
13+
constraint_values = [
14+
"@//linker_config:musl",
15+
"@platforms//cpu:arm64",
16+
"@platforms//os:linux",
17+
],
18+
visibility = ["//visibility:public"],
19+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("Yo");
3+
}

0 commit comments

Comments
 (0)