• 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
130mkdir -p "$OUTPUT"/log
131
132# Make bootstrap compiler (no $TYPE, dynamically linked against host libc)
133# We build the rest of the cross compilers with this so they're linked against
134# musl-libc, because glibc doesn't fully support static linking and dynamic
135# binaries aren't really portable between distributions
136TARGET=$BOOTSTRAP make_toolchain 2>&1 | tee -a "$OUTPUT/log/$BOOTSTRAP"-host.log
137
138if [ $# -gt 0 ]
139then
140  for i in "$@"
141  do
142    make_tuple "$i"
143  done
144else
145  # Here's the list of cross compilers supported by this build script.
146
147  # First target builds a proper version of the $BOOTSTRAP compiler above,
148  # which is used to build the rest (in alphabetical order)
149  for i in i686:: \
150         aarch64:eabi: armv4l:eabihf:"--with-arch=armv5t --with-float=soft" \
151         armv5l:eabihf:--with-arch=armv5t armv7l:eabihf:--with-arch=armv7-a \
152         "armv7m:eabi:--with-arch=armv7-m --with-mode=thumb --disable-libatomic --enable-default-pie" \
153         armv7r:eabihf:"--with-arch=armv7-r --enable-default-pie" \
154         i486:: m68k:: microblaze:: mips:: mips64:: mipsel:: powerpc:: \
155         powerpc64:: powerpc64le:: s390x:: sh2eb:fdpic:--with-cpu=mj2 \
156         sh4::--enable-incomplete-targets x86_64:: x86_64@x32:x32:
157  do
158    make_tuple "$i"
159  done
160fi
161