• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2# This script runs `musl-cross-make` to prepare C toolchain (Binutils, GCC, musl itself)
3# and builds static libunwind that we distribute for static target.
4#
5# Versions of the toolchain components are configurable in `musl-cross-make/Makefile` and
6# musl unlike GLIBC is forward compatible so upgrading it shouldn't break old distributions.
7# Right now we have: Binutils 2.31.1, GCC 9.2.0, musl 1.2.3.
8
9# ignore-tidy-linelength
10
11set -ex
12
13hide_output() {
14  set +x
15  on_err="
16echo ERROR: An error was encountered with the build.
17cat /tmp/build.log
18exit 1
19"
20  trap "$on_err" ERR
21  bash -c "while true; do sleep 30; echo \$(date) - building ...; done" &
22  PING_LOOP_PID=$!
23  "$@" &> /tmp/build.log
24  trap - ERR
25  kill $PING_LOOP_PID
26  rm /tmp/build.log
27  set -x
28}
29
30ARCH=$1
31TARGET=$ARCH-linux-musl
32
33# Don't depend on the mirrors of sabotage linux that musl-cross-make uses.
34LINUX_HEADERS_SITE=https://ci-mirrors.rust-lang.org/rustc/sabotage-linux-tarballs
35LINUX_VER=headers-4.19.88
36
37OUTPUT=/usr/local
38shift
39
40# Ancient binutils versions don't understand debug symbols produced by more recent tools.
41# Apparently applying `-fPIC` everywhere allows them to link successfully.
42# Enable debug info. If we don't do so, users can't debug into musl code,
43# debuggers can't walk the stack, etc. Fixes #90103.
44export CFLAGS="-fPIC -g1 $CFLAGS"
45
46git clone https://github.com/richfelker/musl-cross-make # -b v0.9.9
47cd musl-cross-make
48# A version that includes support for building musl 1.2.3
49git checkout fe915821b652a7fa37b34a596f47d8e20bc72338
50
51hide_output make -j$(nproc) TARGET=$TARGET MUSL_VER=1.2.3 LINUX_HEADERS_SITE=$LINUX_HEADERS_SITE LINUX_VER=$LINUX_VER
52hide_output make install TARGET=$TARGET MUSL_VER=1.2.3 LINUX_HEADERS_SITE=$LINUX_HEADERS_SITE LINUX_VER=$LINUX_VER OUTPUT=$OUTPUT
53
54cd -
55
56# Install musl library to make binaries executable
57ln -s $OUTPUT/$TARGET/lib/libc.so /lib/ld-musl-$ARCH.so.1
58echo $OUTPUT/$TARGET/lib >> /etc/ld-musl-$ARCH.path
59
60# Now when musl bootstraps itself create proper toolchain symlinks to make build and tests easier
61if [ "$REPLACE_CC" = "1" ]; then
62    for exec in cc gcc; do
63        ln -s $TARGET-gcc /usr/local/bin/$exec
64    done
65    for exec in cpp c++ g++; do
66        ln -s $TARGET-g++ /usr/local/bin/$exec
67    done
68fi
69