Skip to content

Commit

Permalink
Add example of cross-compiling with musl (#2535)
Browse files Browse the repository at this point in the history
  • Loading branch information
illicitonion committed Mar 5, 2024
1 parent 5d4dc3f commit 8b977b5
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,14 @@ tasks:
# working_directory: examples/zig_cross_compiling
# build_targets:
# - "//..."
musl_cross_compiling_macos_to_linux:
name: Musl cross compiling test from macOS to Linux
platform: macos
working_directory: examples/musl_cross_compiling
build_targets:
- "//..."
test_targets:
- "//..."
nix_cross_compiling:
name: Nix cross compiling test
platform: ubuntu2204
Expand Down
1 change: 1 addition & 0 deletions examples/.bazelignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ crate_universe
crate_universe_unnamed
ios
ios_build
musl_cross_compiling
nix_cross_compiling
zig_cross_compiling
1 change: 1 addition & 0 deletions examples/musl_cross_compiling/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bazel-*
40 changes: 40 additions & 0 deletions examples/musl_cross_compiling/BUILD.bazel
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"],
)
96 changes: 96 additions & 0 deletions examples/musl_cross_compiling/WORKSPACE.bazel
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()
18 changes: 18 additions & 0 deletions examples/musl_cross_compiling/hello_linux_musl_test.sh
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
19 changes: 19 additions & 0 deletions examples/musl_cross_compiling/linker_config/BUILD.bazel
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"],
)
19 changes: 19 additions & 0 deletions examples/musl_cross_compiling/platforms/BUILD.bazel
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"],
)
3 changes: 3 additions & 0 deletions examples/musl_cross_compiling/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Yo");
}

0 comments on commit 8b977b5

Please sign in to comment.