1#!/usr/local/bin/bash
2
3# Script to build all cross and native compilers supported by musl-libc.
4# This isn't directly used by toybox, but is useful for testing.
5
6trap "exit 1" INT
7
8if [ ! -d litecross ]
9then
10  echo Run this script in musl-cross-make directory to make "ccc" directory.
11  echo
12  echo "  "git clone https://github.com/richfelker/musl-cross-make
13  echo "  "cd musl-cross-make
14  echo '  ~/toybox/scripts/mcm-buildall.sh'
15  exit 1
16fi
17
18# All toolchains after the first are themselves cross compiled (so they
19# can be statically linked against musl on the host, for binary portability.)
20# static i686 binaries are basically "poor man's x32".
21BOOTSTRAP=i686-linux-musl
22
23[ -z "$OUTPUT" ] && OUTPUT="$PWD/ccc"
24
25if [ "$1" == clean ]
26then
27  rm -rf "$OUTPUT" host-* *.log
28  make clean
29  exit
30fi
31
32make_toolchain()
33{
34  # Set cross compiler path
35  LP="$PATH"
36  if [ -z "$TYPE" ]
37  then
38    OUTPUT="$PWD/host-$TARGET"
39    EXTRASUB=y
40  else
41    if [ "$TYPE" == static ]
42    then
43      HOST=$BOOTSTRAP
44      [ "$TARGET" = "$HOST" ] && LP="$PWD/host-$HOST/bin:$LP"
45      TYPE=cross
46      EXTRASUB=y
47      LP="$OUTPUT/$HOST-cross/bin:$LP"
48    else
49      HOST="$TARGET"
50      export NATIVE=y
51      LP="$OUTPUT/${RENAME:-$TARGET}-cross/bin:$LP"
52      [ -z "$(PATH="$LP" which $TARGET-cc)" ] &&
53         echo "no $TARGET-cc in $LP" && return
54    fi
55    COMMON_CONFIG="CC=\"$HOST-gcc -static --static\" CXX=\"$HOST-g++ -static --static\""
56    export -n HOST
57    OUTPUT="$OUTPUT/${RENAME:-$TARGET}-$TYPE"
58  fi
59
60  if [ -e "$OUTPUT.sqf" ] || [ -e "$OUTPUT/bin/$TARGET-cc" ] ||
61     [ -e "$OUTPUT/bin/cc" ]
62  then
63    return
64  fi
65
66  # Change title bar to say what we're currently building
67
68  echo === building $TARGET-$TYPE
69  echo -en "\033]2;$TARGET-$TYPE\007"
70
71  rm -rf build/"$TARGET" "$OUTPUT" &&
72  [ -z "$CPUS" ] && CPUS=$(($(nproc)+1))
73  set -x &&
74  PATH="$LP" make OUTPUT="$OUTPUT" TARGET="$TARGET" \
75    GCC_CONFIG="--disable-nls --disable-libquadmath --disable-decimal-float --disable-multilib --enable-languages=c,c++ $GCC_CONFIG" \
76    COMMON_CONFIG="CFLAGS=\"$CFLAGS -g0 -Os\" CXXFLAGS=\"$CXXFLAGS -g0 -Os\" LDFLAGS=\"$LDFLAGS -s\" $COMMON_CONFIG" \
77    install -j$CPUS || exit 1
78  set +x
79  echo -e '#ifndef __MUSL__\n#define __MUSL__ 1\n#endif' \
80    >> "$OUTPUT/${EXTRASUB:+$TARGET/}include/features.h"
81
82  if [ ! -z "$RENAME" ] && [ "$TYPE" == cross ]
83  then
84    CONTEXT="output/$RENAME-cross/bin"
85    for i in "$CONTEXT/$TARGET-"*
86    do
87      X="$(echo $i | sed "s@.*/$TARGET-\([^-]*\)@\1@")"
88      ln -sf "$TARGET-$X" "$CONTEXT/$RENAME-$X"
89    done
90  fi
91
92  # Prevent cross compiler reusing dynamically linked host build files for
93  # $BOOTSTRAP arch
94  [ -z "$TYPE" ] && make clean
95
96  if [ "$TYPE" == native ]
97  then
98    # gcc looks in "../usr/include" but not "/bin/../include" (relative to the
99    # executable). That means /usr/bin/gcc looks in /usr/usr/include, so that's
100    # not a fix either. So add a NOP symlink as a workaround for The Crazy.
101    ln -s . "$OUTPUT/usr" || exit 1
102    [ ! -z "$(which mksquashfs 2>/dev/null)" ] &&
103      mksquashfs "$OUTPUT" "$OUTPUT.sqf" -all-root &&
104      [ -z "$CLEANUP" ] && rm -rf "$OUTPUT"
105  fi
106}
107
108# Expand compressed target into binutils/gcc "tuple" and call make_toolchain
109make_tuple()
110{
111  PART1=${1/:*/}
112  PART3=${1/*:/}
113  PART2=${1:$((${#PART1}+1)):$((${#1}-${#PART3}-${#PART1}-2))}
114
115  # Do we need to rename this toolchain after building it?
116  RENAME=${PART1/*@/}
117  [ "$RENAME" == "$PART1" ] && RENAME=
118  PART1=${PART1/@*/}
119  TARGET=${PART1}-linux-musl${PART2}
120
121  [ -z "$NOCLEAN" ] && rm -rf build
122
123  for TYPE in static native
124  do
125    TYPE=$TYPE TARGET=$TARGET GCC_CONFIG="$PART3" RENAME="$RENAME" \
126      make_toolchain 2>&1 | tee "$OUTPUT"/log/${RENAME:-$PART1}-${TYPE}.log
127  done
128}
129
130# Packages detect nommu via the absence of fork(). Musl provides a broken fork()
131# on nommu builds that always returns -ENOSYS at runtime. Rip it out.
132# (Currently only for superh/jcore.)
133fix_nommu()
134{
135  # Rich won't merge this
136  sed -i 's/--enable-fdpic$/& --enable-twoprocess/' litecross/Makefile
137
138  PP=patches/musl-"$(sed -n 's/MUSL_VER[ \t]*=[ \t]*//p' Makefile)"
139  mkdir -p "$PP" &&
140  cat > "$PP"/0001-nommu.patch << 'EOF'
141--- a/include/features.h
142+++ b/include/features.h
143@@ -3,2 +3,4 @@
144
145+#define __MUSL__ 1
146+
147 #if defined(_ALL_SOURCE) && !defined(_GNU_SOURCE)
148--- a/src/legacy/daemon.c
149+++ b/src/legacy/daemon.c
150@@ -17,3 +17,3 @@
151
152-	switch(fork()) {
153+	switch(vfork()) {
154 	case 0: break;
155@@ -25,3 +25,3 @@
156
157-	switch(fork()) {
158+	switch(vfork()) {
159 	case 0: break;
160--- a/src/misc/forkpty.c
161+++ b/src/misc/forkpty.c
162@@ -8,2 +8,3 @@
163
164+#ifndef __SH_FDPIC__
165 int forkpty(int *pm, char *name, const struct termios *tio, const struct winsize *ws)
166@@ -57,1 +58,2 @@
167 }
168+#endif
169--- a/src/misc/wordexp.c
170+++ b/src/misc/wordexp.c
171@@ -25,2 +25,3 @@
172
173+#ifndef __SH_FDPIC__
174 static int do_wordexp(const char *s, wordexp_t *we, int flags)
175@@ -177,2 +178,3 @@
176 }
177+#endif
178
179--- a/src/process/fork.c
180+++ b/src/process/fork.c
181@@ -7,2 +7,3 @@
182
183+#ifndef __SH_FDPIC__
184 static void dummy(int x)
185@@ -37,1 +38,2 @@
186 }
187+#endif
188--- a/Makefile
189+++ b/Makefile
190@@ -100,3 +100,3 @@
191 	cp $< $@
192-	sed -n -e s/__NR_/SYS_/p < $< >> $@
193+	sed -e s/__NR_/SYS_/ < $< >> $@
194
195--- a/arch/sh/bits/syscall.h.in
196+++ b/arch/sh/bits/syscall.h.in
197@@ -2,3 +2,5 @@
198 #define __NR_exit                   1
199+#ifndef __SH_FDPIC__
200 #define __NR_fork                   2
201+#endif
202 #define __NR_read                   3
203EOF
204
205  # I won't sign the FSF's copyright assignment
206  tee $(for i in patches/gcc-*; do echo $i/099-vfork.patch; done) > /dev/null << 'EOF'
207--- gcc-8.3.0/fixincludes/procopen.c	2005-08-14 19:50:43.000000000 -0500
208+++ gcc-bak/fixincludes/procopen.c	2020-02-06 23:27:15.408071708 -0600
209@@ -116,3 +116,3 @@
210    */
211-  ch_id = fork ();
212+  ch_id = vfork ();
213   switch (ch_id)
214EOF
215}
216
217fix_nommu || exit 1
218mkdir -p "$OUTPUT"/log
219
220# Make bootstrap compiler (no $TYPE, dynamically linked against host libc)
221# We build the rest of the cross compilers with this so they're linked against
222# musl-libc, because glibc doesn't fully support static linking and dynamic
223# binaries aren't really portable between distributions
224TARGET=$BOOTSTRAP make_toolchain 2>&1 | tee -a "$OUTPUT/log/$BOOTSTRAP"-host.log
225
226if [ $# -gt 0 ]
227then
228  for i in "$@"
229  do
230    make_tuple "$i"
231  done
232else
233  # Here's the list of cross compilers supported by this build script.
234
235  # First target builds a proper version of the $BOOTSTRAP compiler above,
236  # which is used to build the rest (in alphabetical order)
237  for i in i686:: \
238         aarch64:eabi: armv4l:eabihf:"--with-arch=armv5t --with-float=soft" \
239         "armv5l:eabihf:--with-arch=armv5t --with-fpu=vfpv2 --with-float=hard" \
240         "armv7l:eabihf:--with-arch=armv7-a --with-fpu=vfpv3-d16 --with-float=hard" \
241         "armv7m:eabi:--with-arch=armv7-m --with-mode=thumb --disable-libatomic --enable-default-pie" \
242         armv7r:eabihf:"--with-arch=armv7-r --enable-default-pie" \
243         i486:: m68k:: microblaze:: mips:: mips64:: mipsel:: powerpc:: \
244         powerpc64:: powerpc64le:: s390x:: sh2eb:fdpic:--with-cpu=mj2 \
245         sh4::--enable-incomplete-targets x86_64::--with-mtune=nocona \
246         x86_64@x32:x32:
247  do
248    make_tuple "$i"
249  done
250fi
251