• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/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
6if [ ! -d litecross ]
7then
8  echo Run this script in musl-cross-make directory to make "ccc" directory.
9  echo
10  echo "  "git clone https://github.com/richfelker/musl-cross-make
11  echo "  "cd musl-cross-make
12  echo '  ~/toybox/scripts/mcm-buildall.sh'
13  exit 1
14fi
15
16# All toolchains after the first are themselves cross compiled (so they
17# can be statically linked against musl on the host, for binary portability.)
18# static i686 binaries are basically "poor man's x32".
19BOOTSTRAP=i686-linux-musl
20
21[ -z "$OUTPUT" ] && OUTPUT="$PWD/ccc"
22
23if [ "$1" == clean ]
24then
25  rm -rf "$OUTPUT" host-* *.log
26  make clean
27  exit
28fi
29
30make_toolchain()
31{
32  # Set cross compiler path
33  LP="$PATH"
34  if [ -z "$TYPE" ]
35  then
36    OUTPUT="$PWD/host-$TARGET"
37    EXTRASUB=y
38  else
39    if [ "$TYPE" == static ]
40    then
41      HOST=$BOOTSTRAP
42      [ "$TARGET" = "$HOST" ] && LP="$PWD/host-$HOST/bin:$LP"
43      TYPE=cross
44      EXTRASUB=y
45      LP="$OUTPUT/$HOST-cross/bin:$LP"
46    else
47      HOST="$TARGET"
48      export NATIVE=y
49      LP="$OUTPUT/${RENAME:-$TARGET}-cross/bin:$LP"
50    fi
51    COMMON_CONFIG="CC=\"$HOST-gcc -static --static\" CXX=\"$HOST-g++ -static --static\""
52    export -n HOST
53    OUTPUT="$OUTPUT/${RENAME:-$TARGET}-$TYPE"
54  fi
55
56  if [ -e "$OUTPUT.sqf" ] || [ -e "$OUTPUT/bin/$TARGET-ld" ] ||
57     [ -e "$OUTPUT/bin/ld" ]
58  then
59    return
60  fi
61
62  # Change title bar to say what we're currently building
63
64  echo === building $TARGET-$TYPE
65  echo -en "\033]2;$TARGET-$TYPE\007"
66
67  rm -rf build/"$TARGET" "$OUTPUT" &&
68  if [ -z "$CPUS" ]
69  then
70    CPUS="$(nproc)"
71    [ "$CPUS" != 1 ] && CPUS=$(($CPUS+1))
72  fi
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-float=vfp" \
240         "armv7l:eabihf:--with-arch=armv7-a --with-float=vfp" \
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:: x86_64@x32:x32:
246  do
247    make_tuple "$i"
248  done
249fi
250