Skip to content

Commit b36aabd

Browse files
authored
Merge pull request tectonic-typesetting#11 from pkgw/add-x86_64-musl
Add x86_64-unknown-linux-musl to the `cross` framework
2 parents 1f7f4a7 + 51aebd3 commit b36aabd

File tree

10 files changed

+134
-42
lines changed

10 files changed

+134
-42
lines changed

.azure-pipelines/variables.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ variables:
99
windows_image: 'windows-2019'
1010
cross_tag: 'v0.1.16'
1111
target_arm_unknown_linux_musleabihf: 'arm-unknown-linux-musleabihf'
12+
target_x86_64_unknown_linux_musl: 'x86_64-unknown-linux-musl'

cross-images/03_alpine_populate.sh

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,27 @@ set -x
44
set -euo pipefail
55

66
TARGET_ARCH="$1"
7-
root=/home/rust/sysroot-$TARGET_ARCH
87

9-
# Packages for other architectures are signed with other keys that we need to load up.
10-
cd /etc/apk/keys
11-
for fp in 524d27bb ; do
12-
wget https://alpinelinux.org/keys/[email protected]$fp.rsa.pub
13-
done
8+
if [ $TARGET_ARCH == native ] ; then
9+
root=/
10+
else
11+
root=/home/rust/sysroot-$TARGET_ARCH
1412

15-
# Propagate the new keys into the sysroot and also tell it to start pulling in
16-
# packages from the internet.
17-
cp /etc/apk/keys/* $root/etc/apk/keys/
18-
cp /etc/apk/repositories $root/etc/apk/
13+
# Packages for other architectures are signed with other keys that we need to load up.
14+
cd /etc/apk/keys
15+
for fp in 4d07755e 524d27bb 58199dcc 58cbb476 58e4f17d ; do
16+
wget https://alpinelinux.org/keys/[email protected]$fp.rsa.pub
17+
done
18+
19+
# Propagate the new keys into the sysroot and also tell it to start pulling in
20+
# packages from the internet.
21+
cp /etc/apk/keys/* $root/etc/apk/keys/
22+
cp /etc/apk/repositories $root/etc/apk/
23+
fi
1924

2025
# g++ is needed to get libstdc++.a, annoyingly
2126
apk --root $root add --no-scripts \
27+
bzip2-dev \
2228
expat-dev \
2329
fontconfig-dev \
2430
freetype-dev \
@@ -36,7 +42,9 @@ apk --root $root add --no-scripts \
3642
openssl-dev \
3743
zlib-dev
3844

39-
# Let's also install those cross-compilers!
40-
apk add build-base-$TARGET_ARCH gcc-$TARGET_ARCH g++-$TARGET_ARCH
45+
if [ $TARGET_ARCH != native ] ; then
46+
# Let's also install those cross-compilers!
47+
apk add build-base-$TARGET_ARCH gcc-$TARGET_ARCH g++-$TARGET_ARCH
48+
fi
4149

4250
rm -f "$0" # self-destruct

cross-images/05_ubuntu_unpriv_setup.sh

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,15 @@ cd
1313
mkdir $HOME/bin
1414

1515
for tmpl in toolwrapper.sh linkwrapper.sh ; do
16-
sed -e "s|@rust_platform@|$rust_platform|g" -e "s|@os_platform@|$os_platform|g" -e "s|@toolprefix@|$toolprefix|g" $tmpl.in >bin/$tmpl
16+
sed -e "s|@rust_platform@|$rust_platform|g" \
17+
-e "s|@os_platform@|$os_platform|g" \
18+
-e "s|@toolprefix@|$toolprefix|g" \
19+
$tmpl.in >bin/$tmpl
1720
chmod +x bin/$tmpl
1821
done
1922

2023
for tool in ar gcc g++ ld pkg-config ; do
21-
ln -s toolwrapper.sh $HOME/bin/$rust_platform-$tool
24+
ln -s toolwrapper.sh $HOME/bin/wrapped-$tool
2225
done
2326

24-
# cat <<EOF >$HOME/bin/pkg-config
25-
# #! /bin/bash
26-
# exec $rust_platform-pkg-config "\$@"
27-
# EOF
28-
# chmod +x $HOME/bin/pkg-config
29-
3027
rm -f "$0" # self-destruct

cross-images/06_setup_env.sh

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,47 @@ qemu_arch="$4"
1212

1313
urp="$(echo $rust_platform |tr - _)"
1414
crp="$(echo $urp |tr '[a-z]' '[A-Z]')"
15-
sysroot=/alpine/home/rust/sysroot-$os_arch
1615

17-
cat <<EOF >/environ
18-
export AR_${urp}=${rust_platform}-ar
19-
export CC_${urp}=${rust_platform}-gcc
20-
export CXX_${urp}=${rust_platform}-g++
16+
if [ $os_arch = native ] ; then
17+
sysroot=/alpine
18+
else
19+
sysroot=/alpine/home/rust/sysroot-$os_arch
20+
fi
2121

22-
export CARGO_TARGET_${crp}_LINKER=/alpine/home/rust/bin/linkwrapper.sh
23-
export CARGO_TARGET_${crp}_RUNNER=qemu-${qemu_arch}
22+
RUSTFLAGS="-C target-feature=+crt-static -L ${sysroot}/usr/lib"
2423

25-
export OPENSSL_DIR=${sysroot}/usr
24+
for tectonic_implicit_dep in bz2 expat uuid stdc++ ; do
25+
RUSTFLAGS="$RUSTFLAGS -l static=${tectonic_implicit_dep}"
26+
done
2627

28+
if [ "$rust_platform" = aarch64-unknown-linux-musl ] ; then
29+
# Monkey-see, monkey-do workaround for
30+
# https://github.com/rust-lang/rust/issues/46651 copied from `cross`.
31+
RUSTFLAGS="$RUSTFLAGS -C link-arg=-lgcc"
32+
fi
33+
34+
cat <<EOF >/environ
2735
export PATH=/alpine/home/rust/bin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
2836
29-
export PKG_CONFIG_ALLOW_CROSS=1
37+
export AR_${urp}=wrapped-ar
38+
export CC_${urp}=wrapped-gcc
39+
export CXX_${urp}=wrapped-g++
40+
export CARGO_TARGET_${crp}_LINKER=/alpine/home/rust/bin/linkwrapper.sh
41+
42+
export OPENSSL_DIR=${sysroot}/usr
3043
export PKG_CONFIG_LIBDIR=${sysroot}/usr/lib/pkgconfig
3144
export PKG_CONFIG_SYSROOT_DIR=${sysroot}
3245
33-
export RUSTFLAGS="-L ${sysroot}/usr/lib -l static=expat -l static=bz2 -l static=uuid -l static=stdc++ -C target-feature=+crt-static"
34-
46+
export RUSTFLAGS="$RUSTFLAGS"
47+
export PKG_CONFIG_ALLOW_CROSS=1
3548
export RUST_TEST_THREADS=1
49+
export RUST_BACKTRACE=1
50+
EOF
51+
52+
if [ $qemu_arch != native ] ; then
53+
cat <<EOF >>/environ
54+
export CARGO_TARGET_${crp}_RUNNER=qemu-${qemu_arch}
3655
EOF
56+
fi
3757

3858
rm -f "$0" # self-destruct

cross-images/Dockerfile.alpine-chroot

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2019 The Tectonic Project
2+
# Licensed under the MIT License.
3+
4+
# We do some fancy Docker gymnastics to set up an Alpine instance that then
5+
# becomes a chroot inside an Ubuntu install. Unlike
6+
# `Dockerfile.alpine-chroot-cross`, though, we don't need to install our own
7+
# cross-compiler or anything.
8+
9+
FROM alpine:3.10 AS chroot
10+
11+
ARG UID=1000
12+
ARG rust_platform
13+
ARG alpine_platform
14+
15+
# Privileged Alpine setup: make an unprivileged user to work inside the
16+
# chroot. This script also sets up that user build packages, which we don't
17+
# need but is quick so there's no point forking the script.
18+
RUN apk update && apk add --update alpine-sdk
19+
ADD 01_alpine_priv_setup.sh /
20+
RUN sh /01_alpine_priv_setup.sh "$UID"
21+
22+
# Now populate the Alpine environment with the packages Tectonic needs.
23+
ADD 03_alpine_populate.sh /
24+
RUN sh /03_alpine_populate.sh native
25+
26+
# We're done setting up Alpine. Now switch to an Ubuntu image and embed
27+
# the Alpine system in it.
28+
FROM ubuntu:18.04
29+
ARG UID=1000
30+
ARG rust_platform
31+
ARG alpine_platform
32+
COPY --from=chroot / /alpine
33+
34+
# Privileged Ubuntu setup
35+
ADD 04_ubuntu_priv_setup.sh /
36+
ADD enter-chroot /alpine/
37+
RUN sh /04_ubuntu_priv_setup.sh "$UID"
38+
39+
# Unprivileged Ubuntu setup
40+
ADD toolwrapper.sh.in /alpine/home/rust/
41+
ADD linkwrapper.sh.in /alpine/home/rust/
42+
ADD --chown=rust 05_ubuntu_unpriv_setup.sh /alpine/home/rust/
43+
RUN sudo -iu rust sh /alpine/home/rust/05_ubuntu_unpriv_setup.sh $rust_platform $alpine_platform /usr/bin/
44+
45+
# Set up the custom environment variables we need -- some of their names are
46+
# build args, so we can't use `ENV` as far as I can tell.
47+
COPY 06_setup_env.sh /
48+
RUN sh /06_setup_env.sh $rust_platform $alpine_platform native native
49+
50+
WORKDIR /project
51+
ADD entrypoint.sh /entrypoint.sh
52+
ENTRYPOINT ["/entrypoint.sh"]
53+
CMD ["cargo", "test", "--release", "--all"]

cross-images/Dockerfile.alpine-chroot-cross

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ COPY qemu.sh /
5353
RUN /qemu.sh $qemu_arch
5454

5555
# Set up the custom environment variables we need -- some of their names are
56-
# build args, so we can't use `ENV` as far as I can tell.
56+
# build args, so we can't use `ENV` as far as I can tell. NOTE: intentionally
57+
# not quoting $qemu_arch here, so that the script only gets the arch name
58+
# in $4 in cases when $qemu_arch has two words (like ppc64le)
5759
COPY 06_setup_env.sh /
5860
RUN sh /06_setup_env.sh $rust_platform $alpine_platform $alpine_arch $qemu_arch
5961

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#! /usr/bin/env bash
2+
# Copyright 2019 The Tectonic Project
3+
# Licensed under the MIT License.
4+
5+
[ -n "$IMGUID" ] || IMGUID="$(id -u)"
6+
rust_platform=x86_64-unknown-linux-musl
7+
alpine_platform=x86_64-alpine-linux-musl
8+
9+
cd "$(dirname $0)" && exec docker build \
10+
-t tectonictypesetting/crossbuild:$rust_platform \
11+
-f Dockerfile.alpine-chroot \
12+
--build-arg UID=$IMGUID \
13+
--build-arg rust_platform=$rust_platform \
14+
--build-arg alpine_platform=$alpine_platform \
15+
.

cross-images/linkwrapper.sh.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ for arg in "$@"; do
2525
fi
2626
done
2727

28-
exec $rust_platform-g++ "${args[@]}"
28+
set -x
29+
exec wrapped-g++ "${args[@]}"

cross-images/mini-aports/scripts/bootstrap.sh

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ EOF
5353
return 1
5454
fi
5555

56-
set -x # XXXXXXXXXXX
57-
5856
if [ ! -d "$CBUILDROOT" ]; then
5957
msg "Creating sysroot in $CBUILDROOT"
6058
mkdir -p "$CBUILDROOT/etc/apk/keys"
@@ -65,10 +63,7 @@ fi
6563
msg "Building cross-compiler"
6664

6765
# Build and install cross binutils (--with-sysroot)
68-
which abuild
69-
ls -l /mini-aports/main/binutils/APKBUILD
70-
71-
CTARGET=$TARGET_ARCH BOOTSTRAP=nobase APKBUILD=$(apkbuildname binutils) abuild -rv
66+
CTARGET=$TARGET_ARCH BOOTSTRAP=nobase APKBUILD=$(apkbuildname binutils) abuild -r
7267

7368
if ! CHOST=$TARGET_ARCH BOOTSTRAP=nolibc APKBUILD=$(apkbuildname musl) abuild up2date 2>/dev/null; then
7469
# C-library headers for target

cross-images/toolwrapper.sh.in

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ toolprefix="@toolprefix@"
1111

1212
exename="$(basename $0)"
1313

14-
if [[ $exename != $rust_platform-* ]] ; then
15-
echo >&2 "error: executable name ($exename) must start with \"$rust_platform-\""
14+
if [[ $exename != wrapped-* ]] ; then
15+
echo >&2 "error: executable name ($exename) must start with \"wrapped-\""
1616
exit 1
1717
fi
1818

19-
exename="${toolprefix}${exename#$rust_platform-}"
19+
exename="${toolprefix}${exename#wrapped-}"
2020

2121
# working directory. We assume that directories of interest are bind-mounted
2222
# inside the /alpine prefix.

0 commit comments

Comments
 (0)