Skip to content

Commit 0914925

Browse files
authored
Try #1134: --target *musl*
2 parents 5108b9f + 99d89f8 commit 0914925

10 files changed

+102
-136
lines changed

.changes/1063-1125.json renamed to .changes/1063-1125-1134.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
{
1414
"type": "fixed",
15-
"description": "always link to libc when compiling alpine since static libstdc++ may have undefined references to libc symbols.",
15+
"description": "use a linker script for musl libstdc++ to ensure the archive links to libc, libm, and libgcc as needed.",
1616
"issues": [1124]
1717
}
1818
]

docker/Dockerfile.aarch64-unknown-linux-musl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ ENV CROSS_SYSROOT=/usr/local/aarch64-linux-musl
2121
COPY musl-symlink.sh /
2222
RUN /musl-symlink.sh $CROSS_SYSROOT aarch64
2323

24-
COPY aarch64-linux-musl-gcc.sh /usr/bin/
25-
COPY rustc_info.sh /
26-
24+
COPY COPY musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh
2725
COPY qemu-runner base-runner.sh /
2826

2927
ENV CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc.sh \
@@ -32,4 +30,5 @@ ENV CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc.
3230
CXX_aarch64_unknown_linux_musl="$CROSS_TOOLCHAIN_PREFIX"g++ \
3331
BINDGEN_EXTRA_CLANG_ARGS_aarch64_unknown_linux_musl="--sysroot=$CROSS_SYSROOT" \
3432
QEMU_LD_PREFIX="$CROSS_SYSROOT" \
35-
RUST_TEST_THREADS=1
33+
RUST_TEST_THREADS=1 \
34+
CROSS_BUILTINS_PATCHED_MINOR_VERSION=48

docker/Dockerfile.armv5te-unknown-linux-musleabi

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@ ENV CROSS_SYSROOT=/usr/local/arm-linux-musleabi
2525
COPY musl-symlink.sh /
2626
RUN /musl-symlink.sh $CROSS_SYSROOT arm
2727

28+
COPY COPY musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh
2829
COPY qemu-runner base-runner.sh /
2930

30-
COPY rust-1-65-musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh
31-
COPY rustc_info.sh /
32-
3331
ENV CARGO_TARGET_ARMV5TE_UNKNOWN_LINUX_MUSLEABI_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc.sh \
3432
CARGO_TARGET_ARMV5TE_UNKNOWN_LINUX_MUSLEABI_RUNNER="/qemu-runner arm" \
3533
CC_armv5te_unknown_linux_musleabi="$CROSS_TOOLCHAIN_PREFIX"gcc \
3634
CXX_armv5te_unknown_linux_musleabi="$CROSS_TOOLCHAIN_PREFIX"g++ \
3735
BINDGEN_EXTRA_CLANG_ARGS_armv5te_unknown_linux_musleabi="--sysroot=$CROSS_SYSROOT" \
3836
QEMU_LD_PREFIX="$CROSS_SYSROOT" \
39-
RUST_TEST_THREADS=1
37+
RUST_TEST_THREADS=1 \
38+
CROSS_BUILTINS_PATCHED_MINOR_VERSION=65

docker/Dockerfile.mips64-unknown-linux-muslabi64

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ RUN mkdir -p $CROSS_SYSROOT/usr/lib64
2828
RUN ln -s $CROSS_SYSROOT/usr/lib/libc.so $CROSS_SYSROOT/usr/lib64/libc.so
2929
RUN ln -s $CROSS_SYSROOT/usr/lib/libc.so.1 $CROSS_SYSROOT/usr/lib64/libc.so.1
3030

31-
COPY rust-1-65-musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh
32-
COPY rustc_info.sh /
33-
31+
COPY COPY musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh
3432
COPY qemu-runner base-runner.sh /
3533

3634
ENV CARGO_TARGET_MIPS64_UNKNOWN_LINUX_MUSLABI64_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc.sh \
@@ -39,4 +37,5 @@ ENV CARGO_TARGET_MIPS64_UNKNOWN_LINUX_MUSLABI64_LINKER="$CROSS_TOOLCHAIN_PREFIX"
3937
CXX_mips64_unknown_linux_muslabi64="$CROSS_TOOLCHAIN_PREFIX"g++ \
4038
BINDGEN_EXTRA_CLANG_ARGS_mips64_unknown_linux_muslabi64="--sysroot=$CROSS_SYSROOT" \
4139
QEMU_LD_PREFIX="$CROSS_SYSROOT" \
42-
RUST_TEST_THREADS=1
40+
RUST_TEST_THREADS=1 \
41+
CROSS_BUILTINS_PATCHED_MINOR_VERSION=65

docker/Dockerfile.mips64el-unknown-linux-muslabi64

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ RUN mkdir -p $CROSS_SYSROOT/usr/lib64
2828
RUN ln -s $CROSS_SYSROOT/usr/lib/libc.so $CROSS_SYSROOT/usr/lib64/libc.so
2929
RUN ln -s $CROSS_SYSROOT/usr/lib/libc.so.1 $CROSS_SYSROOT/usr/lib64/libc.so.1
3030

31-
COPY rust-1-65-musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh
32-
COPY rustc_info.sh /
33-
31+
COPY COPY musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh
3432
COPY qemu-runner base-runner.sh /
3533

3634
ENV CARGO_TARGET_MIPS64EL_UNKNOWN_LINUX_MUSLABI64_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc.sh \
@@ -39,4 +37,5 @@ ENV CARGO_TARGET_MIPS64EL_UNKNOWN_LINUX_MUSLABI64_LINKER="$CROSS_TOOLCHAIN_PREFI
3937
CXX_mips64el_unknown_linux_muslabi64="$CROSS_TOOLCHAIN_PREFIX"g++ \
4038
BINDGEN_EXTRA_CLANG_ARGS_mips64el_unknown_linux_muslabi64="--sysroot=$CROSS_SYSROOT" \
4139
QEMU_LD_PREFIX="$CROSS_SYSROOT" \
42-
RUST_TEST_THREADS=1
40+
RUST_TEST_THREADS=1 \
41+
CROSS_BUILTINS_PATCHED_MINOR_VERSION=65

docker/aarch64-linux-musl-gcc.sh

Lines changed: 0 additions & 37 deletions
This file was deleted.

docker/musl-gcc.sh

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,82 @@
11
#!/bin/bash
22

3-
# this works around missing libc routines when compiling
4-
# against a static libstdc++, which we always do on musl after
5-
# https://github.com/cross-rs/cross/issues/902. the reason
6-
# otherwise, we are missing crucial routines as as `setlocale`,
7-
# `__cxa_atexit`, and others.
3+
# this linker works around missing builtins in older rust versions.
4+
# we also have custom linker scripts for our static libstdc++ for all versions
5+
# which is found in `musl-symblink.sh`.
6+
#
7+
# for other targets, issues in older versions of compiler-builtins require
8+
# manually linking to libgcc to compensate for missing builtins.
9+
# target-specific details include:
10+
#
11+
# aarch64-unknown-linux-musl (fixed in 1.48)
12+
# https://github.com/rust-lang/compiler-builtins/pull/377
13+
#
14+
# armv5te-unknown-linux-musleabi (fixed in 1.65)
15+
# missing sync `sync_X_and_fetch`
16+
# https://github.com/rust-lang/compiler-builtins/pull/484
17+
#
18+
# mips64-unknown-linux-muslabi64, mips64el-unknown-linux-muslabi64 (fixed in 1.65)
19+
# missing soft-fp routine `__trunctfsf2`
20+
# https://github.com/rust-lang/compiler-builtins/pull/483
821

922
set -x
1023
set -euo pipefail
1124

1225
main() {
13-
if [[ $# -eq 0 ]]; then
14-
exec "${CROSS_TOOLCHAIN_PREFIX}"gcc "${@}"
26+
local minor
27+
local flags=()
28+
local patched_minor="${CROSS_BUILTINS_PATCHED_MINOR_VERSION:-0}"
29+
30+
if [[ $# -eq 0 ]] || [[ "${patched_minor}" == 0 ]]; then
31+
# called without arguments or no patch needed, don't add additional flags
32+
true
1533
else
16-
exec "${CROSS_TOOLCHAIN_PREFIX}"gcc "${@}" -lc
34+
# might need to patch an older rustc version due to missing builtins
35+
# we determine the minor version lazily to avoid any unnecessary overhead
36+
minor=$(rustc_minor_version)
37+
if [[ "${minor}" -lt "${patched_minor}" ]]; then
38+
flags+=(-lgcc -static-libgcc)
39+
fi
40+
fi
41+
42+
exec "${CROSS_TOOLCHAIN_PREFIX}"gcc "${@}" "${flags[@]}"
43+
}
44+
45+
# FIXME: the rest of the contents of this file can be removed later on,
46+
# especially after 0.3.0 has been released so we can ensure everyone is
47+
# using a cross version at least as recent as images requiring the rust
48+
# versions provided as environment variables. these functions are wrappers
49+
# around these environment variables for backwards compatibility.
50+
# https://github.com/cross-rs/cross/issues/1046
51+
52+
# NOTE: this will fail if rustc does not provide version
53+
# info, which may happen with a custom toolchain.
54+
rustc_version() {
55+
rustc -Vv | grep '^release:' | cut -d ':' -f2
56+
}
57+
58+
rustc_major_version() {
59+
if [[ -z "${CROSS_RUSTC_MAJOR_VERSION:-}" ]]; then
60+
CROSS_RUSTC_MAJOR_VERSION=$(rustc_version | cut -d '.' -f1)
61+
export CROSS_RUSTC_MAJOR_VERSION
62+
fi
63+
echo "${CROSS_RUSTC_MAJOR_VERSION}"
64+
}
65+
66+
rustc_minor_version() {
67+
if [[ -z "${CROSS_RUSTC_MINOR_VERSION:-}" ]]; then
68+
CROSS_RUSTC_MINOR_VERSION=$(rustc_version | cut -d '.' -f2)
69+
export CROSS_RUSTC_MINOR_VERSION
70+
fi
71+
echo "${CROSS_RUSTC_MINOR_VERSION}"
72+
}
73+
74+
rustc_patch_version() {
75+
if [[ -z "${CROSS_RUSTC_PATCH_VERSION:-}" ]]; then
76+
CROSS_RUSTC_PATCH_VERSION=$(rustc_version | cut -d '.' -f3)
77+
export CROSS_RUSTC_PATCH_VERSION
1778
fi
79+
echo "${CROSS_RUSTC_PATCH_VERSION}"
1880
}
1981

2082
main "${@}"

docker/musl-symlink.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,25 @@ main() {
5353
# https://github.com/cross-rs/cross/issues/902
5454
rm "${sysroot}"/lib/libstdc++.so* || true
5555

56+
# now, we create a linker script that adds all the required dependencies
57+
# because we link to a static libstdc++ to avoid runtime issues and
58+
# with the shared libstdc++, we can have missing symbols that are reference
59+
# in libstdc++, such as those from libc like `setlocale` and `__cxa_atexit`,
60+
# as well as those from libgcc, like `__extendsftf2`. all musl targets
61+
# can require symbols from libc, however, only the following are known
62+
# to require symbols from libgcc:
63+
# - aarch64-unknown-linux-musl
64+
# - mips64-unknown-linux-muslabi64
65+
# - mips64el-unknown-linux-muslabi64
66+
echo '/* cross-rs linker script
67+
* this allows us to statically link libstdc++ to avoid segfaults
68+
* https://github.com/cross-rs/cross/issues/902
69+
*/
70+
GROUP ( libstdc++.a AS_NEEDED( -lgcc -lc -lm ) )
71+
' > "${sysroot}"/lib/libstdc++.so.6.0.27
72+
ln -s libstdc++.so.6.0.27 "${sysroot}"/lib/libstdc++.so.6
73+
ln -s libstdc++.so.6.0.27 "${sysroot}"/lib/libstdc++.so
74+
5675
echo "${sysroot}/lib" >> "/etc/ld-musl-${arch}.path"
5776

5877
rm -rf "${0}"

docker/rust-1-65-musl-gcc.sh

Lines changed: 0 additions & 35 deletions
This file was deleted.

docker/rustc_info.sh

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)