-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example of cross-compiling with musl
- Loading branch information
1 parent
5d4dc3f
commit f2ff47c
Showing
9 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,6 @@ crate_universe | |
crate_universe_unnamed | ||
ios | ||
ios_build | ||
musl_cross_compiling | ||
nix_cross_compiling | ||
zig_cross_compiling |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/bazel-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_binary") | ||
load("@rules_rust//rust:defs.bzl", "rust_binary") | ||
|
||
rust_binary( | ||
name = "hello", | ||
srcs = ["src/main.rs"], | ||
tags = ["manual"], | ||
) | ||
|
||
platform_transition_binary( | ||
name = "hello_linux_x86_64_musl", | ||
binary = ":hello", | ||
target_platform = "//platforms:linux_x86_64_musl", | ||
) | ||
|
||
sh_test( | ||
name = "hello_linux_x86_64_musl_test", | ||
srcs = ["hello_linux_musl_test.sh"], | ||
args = [ | ||
"$(rootpath :hello_linux_x86_64_musl)", | ||
"'ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), static-pie linked'", | ||
], | ||
data = [":hello_linux_x86_64_musl"], | ||
) | ||
|
||
platform_transition_binary( | ||
name = "hello_linux_arm64_musl", | ||
binary = ":hello", | ||
target_platform = "//platforms:linux_arm64_musl", | ||
) | ||
|
||
sh_test( | ||
name = "hello_linux_arm64_musl_test", | ||
srcs = ["hello_linux_musl_test.sh"], | ||
args = [ | ||
"$(rootpath :hello_linux_arm64_musl)", | ||
"'ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked'", | ||
], | ||
data = [":hello_linux_arm64_musl"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
local_repository( | ||
name = "rules_rust", | ||
path = "../..", | ||
) | ||
|
||
load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains", "rust_repository_set") | ||
|
||
rules_rust_dependencies() | ||
|
||
EDITION = "2021" | ||
|
||
RUST_VERSION = "1.76.0" | ||
|
||
rust_register_toolchains( | ||
edition = EDITION, | ||
) | ||
|
||
rust_repository_set( | ||
name = "darwin_x86_64_to_x86_64_musl_tuple", | ||
edition = EDITION, | ||
exec_triple = "x86_64-apple-darwin", | ||
# Setting this extra_target_triples allows differentiating the musl case from the non-musl case, in case multiple linux-targeting toolchains are registered. | ||
extra_target_triples = {"x86_64-unknown-linux-musl": [ | ||
"@//linker_config:musl", | ||
"@platforms//cpu:x86_64", | ||
"@platforms//os:linux", | ||
]}, | ||
versions = [RUST_VERSION], | ||
) | ||
|
||
rust_repository_set( | ||
name = "darwin_arm64_to_x86_64_musl_tuple", | ||
edition = EDITION, | ||
exec_triple = "aarch64-apple-darwin", | ||
extra_target_triples = {"x86_64-unknown-linux-musl": [ | ||
"@//linker_config:musl", | ||
"@platforms//cpu:x86_64", | ||
"@platforms//os:linux", | ||
]}, | ||
versions = [RUST_VERSION], | ||
) | ||
|
||
rust_repository_set( | ||
name = "darwin_x86_64_to_arm64_musl_tuple", | ||
edition = EDITION, | ||
exec_triple = "x86_64-apple-darwin", | ||
# Setting this extra_target_triples allows differentiating the musl case from the non-musl case, in case multiple linux-targeting toolchains are registered. | ||
extra_target_triples = {"aarch64-unknown-linux-musl": [ | ||
"@//linker_config:musl", | ||
"@platforms//cpu:arm64", | ||
"@platforms//os:linux", | ||
]}, | ||
versions = [RUST_VERSION], | ||
) | ||
|
||
rust_repository_set( | ||
name = "darwin_arm64_to_arm64_musl_tuple", | ||
edition = EDITION, | ||
exec_triple = "aarch64-apple-darwin", | ||
extra_target_triples = {"aarch64-unknown-linux-musl": [ | ||
"@//linker_config:musl", | ||
"@platforms//cpu:arm64", | ||
"@platforms//os:linux", | ||
]}, | ||
versions = [RUST_VERSION], | ||
) | ||
|
||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | ||
|
||
http_archive( | ||
name = "aspect_bazel_lib", | ||
sha256 = "f5ea76682b209cc0bd90d0f5a3b26d2f7a6a2885f0c5f615e72913f4805dbb0d", | ||
strip_prefix = "bazel-lib-2.5.0", | ||
url = "https://github.com/aspect-build/bazel-lib/releases/download/v2.5.0/bazel-lib-v2.5.0.tar.gz", | ||
) | ||
|
||
load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies", "aspect_bazel_lib_register_toolchains") | ||
|
||
aspect_bazel_lib_dependencies() | ||
|
||
aspect_bazel_lib_register_toolchains() | ||
|
||
http_archive( | ||
name = "musl_toolchains", | ||
sha256 = "f9f077b9ae74a0545f7cb7108462cb061593eef10fd09d25db4554e281ee880b", | ||
url = "https://github.com/bazel-contrib/musl-toolchain/releases/download/v0.1.7/musl_toolchain-v0.1.7.tar.gz", | ||
) | ||
|
||
load("@musl_toolchains//:repositories.bzl", "load_musl_toolchains") | ||
|
||
# Setting this extra_target_triples allows differentiating the musl case from the non-musl case, in case multiple linux-targeting toolchains are registered. | ||
load_musl_toolchains(extra_target_compatible_with = ["@//linker_config:musl"]) | ||
|
||
load("@musl_toolchains//:toolchains.bzl", "register_musl_toolchains") | ||
|
||
register_musl_toolchains() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/bin/bash | ||
|
||
set -euo pipefail | ||
|
||
if [[ $# -ne 2 ]]; then | ||
echo >&2 "Usage: $0 /path/to/binary file-output" | ||
exit 1 | ||
fi | ||
|
||
binary="$1" | ||
want_file_output="$2" | ||
|
||
out="$(file "${binary}")" | ||
|
||
if [[ "${out}" != *"${want_file_output}"* ]]; then | ||
echo >&2 "Wrong file type: ${out}" | ||
exit 1 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
constraint_setting( | ||
name = "linker", | ||
default_constraint_value = ":unknown", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
constraint_value( | ||
name = "musl", | ||
constraint_setting = ":linker", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
# Default linker for anyone not setting the linker to `musl`. | ||
# You shouldn't ever need to set this value manually. | ||
constraint_value( | ||
name = "unknown", | ||
constraint_setting = ":linker", | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
platform( | ||
name = "linux_x86_64_musl", | ||
constraint_values = [ | ||
"@//linker_config:musl", | ||
"@platforms//cpu:x86_64", | ||
"@platforms//os:linux", | ||
], | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
platform( | ||
name = "linux_arm64_musl", | ||
constraint_values = [ | ||
"@//linker_config:musl", | ||
"@platforms//cpu:arm64", | ||
"@platforms//os:linux", | ||
], | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
println!("Yo"); | ||
} |