Skip to content

Commit 6b474dd

Browse files
committed
ci: Specify target and static linking of C runtime explicitly
Without specifying the target, cargo uses the host target, which in case of Linux runners is `*-unknown-linux-gnu`. To test static linking to the full extent, use `*-unknown-linux-musl` targets. 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[0][1], but it's being phased out in favor of dynamic linking being the default option.[2][3][4] [0] https://github.com/rust-lang/rust/blob/672388edbee9e93c35e5fdf7dac818a6612a5103/compiler/rustc_target/src/spec/targets/x86_64_unknown_linux_musl.rs#L19-L20 [1] https://github.com/rust-lang/rust/blob/672388edbee9e93c35e5fdf7dac818a6612a5103/compiler/rustc_target/src/spec/targets/aarch64_unknown_linux_musl.rs#L17-L18 [2] rust-lang/compiler-team#422 [3] rust-lang/rust#133386 [4] rust-lang/rust#14451
1 parent 2a078ad commit 6b474dd

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

.github/workflows/ci.yml

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ jobs:
119119
LLVM_EXCLUDE_FEATURES_DYNAMIC: llvm-link-static,no-llvm-linking
120120
RUSTC_LLVM_INSTALL_DIR_DYNAMIC: /tmp/rustc-llvm-dynamic
121121
RUSTC_LLVM_INSTALL_DIR_STATIC: /tmp/rustc-llvm-static
122+
SYSROOT_DIR: /tmp/sysroot
122123

123124
steps:
124125
- uses: actions/checkout@v6
@@ -313,6 +314,15 @@ jobs:
313314
run: |
314315
echo "${RUSTC_LLVM_INSTALL_DIR_STATIC}/bin" >> $GITHUB_PATH
315316
317+
- name: Prepare sysroot
318+
if: runner.os == 'Linux'
319+
run: |
320+
set -euxo pipefail
321+
mkdir -p $SYSROOT_DIR
322+
wget -q -O - $(cargo xtask musl-sysroot-url) | \
323+
tar -xJ -C $SYSROOT_DIR --xattrs-include='*.*' --numeric-owner --wildcards './usr/lib/*'
324+
echo "CXXSTDLIB_PATH=$(dirname $(find $SYSROOT_DIR -name libstdc++.a))" >> $GITHUB_ENV
325+
316326
- name: Install static libraries (macOS)
317327
if: runner.os == 'macOS'
318328
# macOS does not provide any static libraries. Homebrew does provide
@@ -337,24 +347,31 @@ jobs:
337347
# (multiple builds) increases the disk usage massively. Therefore we
338348
# perform all static builds with only one fixed feature set.
339349
run: |
340-
cargo check --no-default-features --features \
341-
${{ env.LLVM_FEATURES_STATIC }}
350+
set -euxo pipefail
351+
# Link libc and C runtime statically.
352+
echo "RUSTFLAGS=-C target-feature=+crt-static" >> $GITHUB_ENV
353+
cargo check --no-default-features \
354+
--features ${{ env.LLVM_FEATURES_STATIC }} \
355+
--target ${{ matrix.platform.static-target }}
342356
343357
- name: Build (static linking, single feature set)
344358
run: |
345-
cargo build --no-default-features --features \
346-
${{ env.LLVM_FEATURES_STATIC }}
359+
cargo build --no-default-features \
360+
--features ${{ env.LLVM_FEATURES_STATIC }} \
361+
--target ${{ matrix.platform.static-target }}
347362
348363
- name: Test (sysroot built on demand, static linking)
349364
run: |
350-
RUSTC_BOOTSTRAP=1 cargo test --no-default-features --features \
351-
${{ env.LLVM_FEATURES_STATIC }}
365+
RUSTC_BOOTSTRAP=1 cargo test --no-default-features \
366+
--features ${{ env.LLVM_FEATURES_STATIC }} \
367+
--target ${{ matrix.platform.static-target }}
352368
353369
- name: Test (prebuilt BPF standard library, static linking)
354370
run: |
355371
BPFEL_SYSROOT_DIR="${{ github.workspace }}/bpf-sysroot" \
356-
cargo test --no-default-features --features \
357-
${{ env.LLVM_FEATURES_STATIC }}
372+
cargo test --no-default-features \
373+
--features ${{ env.LLVM_FEATURES_STATIC }} \
374+
--target ${{ matrix.platform.static-target }}
358375
359376
- name: Report disk usage
360377
if: ${{ always() }}

0 commit comments

Comments
 (0)