mcm-buildall.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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" ] && make clean
  82. if [ "$TYPE" == native ]
  83. then
  84. # gcc looks in "../usr/include" but not "/bin/../include" (relative to the
  85. # executable). That means /usr/bin/gcc looks in /usr/usr/include, so that's
  86. # not a fix either. So add a NOP symlink as a workaround for The Crazy.
  87. ln -s . "$OUTPUT/usr" || exit 1
  88. [ ! -z "$(which mksquashfs 2>/dev/null)" ] &&
  89. mksquashfs "$OUTPUT" "$OUTPUT.sqf" -all-root &&
  90. [ -z "$CLEANUP" ] && rm -rf "$OUTPUT"
  91. fi
  92. }
  93. # Expand compressed target into binutils/gcc "tuple" and call make_toolchain
  94. make_tuple()
  95. {
  96. PART1=${1/:*/}
  97. PART3=${1/*:/}
  98. PART2=${1:$((${#PART1}+1)):$((${#1}-${#PART3}-${#PART1}-2))}
  99. # Do we need to rename this toolchain after building it?
  100. RENAME=${PART1/*@/}
  101. [ "$RENAME" == "$PART1" ] && RENAME=
  102. PART1=${PART1/@*/}
  103. TARGET=${PART1}-linux-musl${PART2}
  104. [ -z "$NOCLEAN" ] && rm -rf build
  105. for TYPE in static native
  106. do
  107. TYPE=$TYPE TARGET=$TARGET GCC_CONFIG="$PART3" RENAME="$RENAME" \
  108. make_toolchain 2>&1 | tee "$OUTPUT"/log/${RENAME:-$PART1}-${TYPE}.log
  109. done
  110. }
  111. # Packages detect nommu via the absence of fork(). Musl provides a broken fork()
  112. # on nommu builds that always returns -ENOSYS at runtime. Rip it out.
  113. # (Currently only for superh/jcore.)
  114. fix_nommu()
  115. {
  116. # Rich won't merge this
  117. sed -i 's/--enable-fdpic$/& --enable-twoprocess/' litecross/Makefile
  118. PP=patches/musl-"$(sed -n 's/MUSL_VER[ \t]*=[ \t]*//p' Makefile)"
  119. mkdir -p "$PP" &&
  120. cat > "$PP"/0001-nommu.patch << 'EOF'
  121. --- a/include/features.h
  122. +++ b/include/features.h
  123. @@ -3,2 +3,4 @@
  124. +#define __MUSL__ 1
  125. +
  126. #if defined(_ALL_SOURCE) && !defined(_GNU_SOURCE)
  127. --- a/src/legacy/daemon.c
  128. +++ b/src/legacy/daemon.c
  129. @@ -17,3 +17,3 @@
  130. - switch(fork()) {
  131. + switch(vfork()) {
  132. case 0: break;
  133. @@ -25,3 +25,3 @@
  134. - switch(fork()) {
  135. + switch(vfork()) {
  136. case 0: break;
  137. --- a/src/misc/forkpty.c
  138. +++ b/src/misc/forkpty.c
  139. @@ -8,2 +8,3 @@
  140. +#ifndef __SH_FDPIC__
  141. int forkpty(int *pm, char *name, const struct termios *tio, const struct winsize *ws)
  142. @@ -57,1 +58,2 @@
  143. }
  144. +#endif
  145. --- a/src/misc/wordexp.c
  146. +++ b/src/misc/wordexp.c
  147. @@ -25,2 +25,3 @@
  148. +#ifndef __SH_FDPIC__
  149. static int do_wordexp(const char *s, wordexp_t *we, int flags)
  150. @@ -177,2 +178,3 @@
  151. }
  152. +#endif
  153. --- a/src/process/fork.c
  154. +++ b/src/process/fork.c
  155. @@ -7,2 +7,3 @@
  156. +#ifndef __SH_FDPIC__
  157. static void dummy(int x)
  158. @@ -37,1 +38,2 @@
  159. }
  160. +#endif
  161. --- a/Makefile
  162. +++ b/Makefile
  163. @@ -100,3 +100,3 @@
  164. cp $< $@
  165. - sed -n -e s/__NR_/SYS_/p < $< >> $@
  166. + sed -e s/__NR_/SYS_/ < $< >> $@
  167. --- a/arch/sh/bits/syscall.h.in
  168. +++ b/arch/sh/bits/syscall.h.in
  169. @@ -2,3 +2,5 @@
  170. #define __NR_exit 1
  171. +#ifndef __SH_FDPIC__
  172. #define __NR_fork 2
  173. +#endif
  174. #define __NR_read 3
  175. EOF
  176. # I won't sign the FSF's copyright assignment
  177. tee $(for i in patches/gcc-*; do echo $i/099-vfork.patch; done) > /dev/null << 'EOF'
  178. --- gcc-8.3.0/fixincludes/procopen.c 2005-08-14 19:50:43.000000000 -0500
  179. +++ gcc-bak/fixincludes/procopen.c 2020-02-06 23:27:15.408071708 -0600
  180. @@ -116,3 +116,3 @@
  181. */
  182. - ch_id = fork ();
  183. + ch_id = vfork ();
  184. switch (ch_id)
  185. EOF
  186. }
  187. fix_nommu || exit 1
  188. mkdir -p "$OUTPUT"/log
  189. # Make bootstrap compiler (no $TYPE, dynamically linked against host libc)
  190. # We build the rest of the cross compilers with this so they're linked against
  191. # musl-libc, because glibc doesn't fully support static linking and dynamic
  192. # binaries aren't really portable between distributions
  193. TARGET=$BOOTSTRAP make_toolchain 2>&1 | tee -a "$OUTPUT/log/$BOOTSTRAP"-host.log
  194. if [ $# -gt 0 ]
  195. then
  196. for i in "$@"
  197. do
  198. make_tuple "$i"
  199. done
  200. else
  201. # Here's the list of cross compilers supported by this build script.
  202. # First target builds a proper version of the $BOOTSTRAP compiler above,
  203. # which is used to build the rest (in alphabetical order)
  204. for i in i686:: \
  205. aarch64:eabi: armv4l:eabihf:"--with-arch=armv5t --with-float=soft" \
  206. "armv5l:eabihf:--with-arch=armv5t --with-fpu=vfpv2 --with-float=hard" \
  207. "armv7l:eabihf:--with-arch=armv7-a --with-fpu=vfpv3-d16 --with-float=hard" \
  208. "armv7m:eabi:--with-arch=armv7-m --with-mode=thumb --disable-libatomic --enable-default-pie" \
  209. armv7r:eabihf:"--with-arch=armv7-r --enable-default-pie" \
  210. i486:: m68k:: microblaze:: mips:: mips64:: mipsel:: powerpc:: \
  211. powerpc64:: powerpc64le:: s390x:: sh2eb:fdpic:--with-cpu=mj2 \
  212. sh4::--enable-incomplete-targets x86_64::--with-mtune=nocona \
  213. x86_64@x32:x32:
  214. do
  215. make_tuple "$i"
  216. done
  217. fi