Skip to content

Commit af64118

Browse files
committed
ci/release: Specify target and static linking of C runtime explicitly
Without specifying the target, `upload-rust-binary-action` uses the host target, which in case of Linux runners is `*-unknown-linux-gnu`. We are are interested in producing `*-musl` binaries, not `*-gnu`, since the latter does not support static linking of libc and C runtime and therefore: 1) It won't work on distros using other libcs. 2) It won't work even on GNU distros with glibc older than the one used for build (glibc is forwards compatible, but not backwards compatible). Static `*-musl` binaries will work fine on all distros, including the ones with GNU userland. cargo-binstall falls back to downloading `*-musl` binaries even on `*-gnu` hosts if a `*-gnu` binary is not available[0]. Make use of the `xtask musl-sysroot-url` command to fetch the sysroot containing libstd++ linked to musl, then pass the found library via `CXXSTDLIB_PATH` environment variable. On top of that, make sure that libc and C runtime are actually linked statically by specifying `-C target-feature=+crt-static` rustflag. As of today, that's still a default option on the most of `*-musl` targets[1][2], but it's being phased out in favor of dynamic linking being the default option.[3][4][5] [0] https://github.com/cargo-bins/cargo-binstall/blob/efd0e93316a9af26e9847b5090b5ab2419befad4/crates/detect-targets/src/detect/linux.rs#L78-L81 [1] https://github.com/rust-lang/rust/blob/672388edbee9e93c35e5fdf7dac818a6612a5103/compiler/rustc_target/src/spec/targets/x86_64_unknown_linux_musl.rs#L19-L20 [2] https://github.com/rust-lang/rust/blob/672388edbee9e93c35e5fdf7dac818a6612a5103/compiler/rustc_target/src/spec/targets/aarch64_unknown_linux_musl.rs#L17-L18 [3] rust-lang/compiler-team#422 [4] rust-lang/rust#133386 [5] rust-lang/rust#144513
1 parent 0f9ed18 commit af64118

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

.github/workflows/release.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,20 @@ jobs:
2424
target: aarch64-unknown-linux-musl
2525
env:
2626
RUST_CI_LLVM_INSTALL_DIR: /tmp/rustc-llvm
27+
SYSROOT_DIR: /tmp/sysroot
2728
steps:
2829
- uses: actions/checkout@v6
2930
- uses: Swatinem/rust-cache@v2
3031

32+
- name: Prepare sysroot
33+
if: runner.os == 'Linux'
34+
run: |
35+
set -euxo pipefail
36+
mkdir -p $SYSROOT_DIR
37+
wget -q -O - $(cargo xtask musl-sysroot-url) | \
38+
tar -xJ -C $SYSROOT_DIR --xattrs-include='*.*' --numeric-owner --wildcards './usr/lib/*'
39+
echo "CXXSTDLIB_PATH=$(dirname $(find $SYSROOT_DIR -name libstdc++.a))" >> $GITHUB_ENV
40+
3141
- name: Install prerequisites
3242
if: runner.os == 'macOS'
3343
# macOS does not provide any static libraries. Homebrew does provide
@@ -55,9 +65,15 @@ jobs:
5565
bin: bpf-linker
5666
features: llvm-21,llvm-link-static
5767
no-default-features: true
68+
target: ${{ matrix.platform.target }}
69+
# Use vanilla cargo instead of cross-rs, even though we specify
70+
# targets explicitly.
71+
build-tool: cargo
5872
dry-run: ${{ github.event_name != 'release' }}
5973
env:
6074
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
75+
# Link libc and C runtime statically.
76+
RUSTFLAGS: -C target-feature=+crt-static
6177

6278
- name: Report disk usage
6379
if: ${{ always() }}

0 commit comments

Comments
 (0)