mcm-buildall.sh 7.0 KB

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