-
Notifications
You must be signed in to change notification settings - Fork 446
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 c40d559
Showing
9 changed files
with
141 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,20 @@ | ||
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"], | ||
) | ||
|
||
platform_transition_binary( | ||
name = "hello_linux_musl", | ||
binary = ":hello", | ||
target_platform = "//platforms:linux_x86_64_musl", | ||
) | ||
|
||
sh_test( | ||
name = "hello_linux_musl_test", | ||
srcs = ["hello_linux_musl_test.sh"], | ||
args = ["$(rootpath :hello_linux_musl)"], | ||
data = [":hello_linux_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,63 @@ | ||
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_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"]}, | ||
versions = [RUST_VERSION], | ||
) | ||
|
||
rust_repository_set( | ||
name = "darwin_arm64_to_musl_tuple", | ||
edition = EDITION, | ||
exec_triple = "aarch64-apple-darwin", | ||
extra_target_triples = {"x86_64-unknown-linux-musl": ["@//linker_config:musl"]}, | ||
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 = "bbc0a05fd6f5e8ea76cd0c4aeb82530d98c8cc491e61d86093d18fbf19c3a95a", | ||
url = "https://github.com/bazel-contrib/musl-toolchain/releases/download/v0.1.6/musl_toolchain-v0.1.6.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,17 @@ | ||
#!/bin/bash | ||
|
||
set -euo pipefail | ||
|
||
if [[ $# -ne 1 ]]; then | ||
echo >&2 "Usage: $0 /path/to/binary" | ||
exit 1 | ||
fi | ||
|
||
binary="$1" | ||
|
||
out="$(file "${binary}")" | ||
|
||
if [[ "${out}" != *"ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), static-pie linked"* ]]; 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,9 @@ | ||
platform( | ||
name = "linux_x86_64_musl", | ||
constraint_values = [ | ||
"@//linker_config:musl", | ||
"@platforms//cpu:x86_64", | ||
"@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"); | ||
} |