make.sh 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #!/bin/bash
  2. # Grab default values for $CFLAGS and such.
  3. set -o pipefail
  4. source scripts/portability.sh
  5. # Default to running one more parallel cc instance than we have processors
  6. : ${CPUS:=$(($(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null)+1))}
  7. # Respond to V= by echoing command lines as well as running them
  8. DOTPROG=
  9. do_loudly()
  10. {
  11. [ ! -z "$V" ] && echo "$@" || echo -n "$DOTPROG"
  12. "$@"
  13. }
  14. # Is anything under directory $2 newer than file $1
  15. isnewer()
  16. {
  17. CHECK="$1"
  18. shift
  19. [ ! -z "$(find "$@" -newer "$CHECK" 2>/dev/null || echo yes)" ]
  20. }
  21. echo "Generate headers from toys/*/*.c..."
  22. mkdir -p "${UNSTRIPPED:=$GENDIR/unstripped}"
  23. if isnewer "$GENDIR"/Config.in toys || isnewer "$GENDIR"/Config.in Config.in
  24. then
  25. echo "Extract configuration information from toys/*.c files..."
  26. scripts/genconfig.sh
  27. fi
  28. # Create a list of all the commands toybox can provide. Note that the first
  29. # entry is out of order on purpose (the toybox multiplexer command must be the
  30. # first element of the array). The rest must be sorted in alphabetical order
  31. # for fast binary search.
  32. if isnewer "$GENDIR"/newtoys.h toys
  33. then
  34. echo -n "$GENDIR/newtoys.h "
  35. echo "USE_TOYBOX(NEWTOY(toybox, NULL, TOYFLAG_STAYROOT|TOYFLAG_NOHELP))" \
  36. > "$GENDIR"/newtoys.h
  37. $SED -n -e 's/^USE_[A-Z0-9_]*(/&/p' toys/*/*.c \
  38. | $SED 's/\(.*TOY(\)\([^,]*\),\(.*\)/\2 \1\2,\3/' | sort -s -k 1,1 \
  39. | $SED 's/[^ ]* //' >> "$GENDIR"/newtoys.h
  40. [ $? -ne 0 ] && exit 1
  41. fi
  42. [ ! -z "$V" ] && echo "Which C files to build..."
  43. # Extract a list of toys/*/*.c files to compile from the data in $KCONFIG_CONFIG
  44. # (First command names, then filenames with relevant {NEW,OLD}TOY() macro.)
  45. [ -d ".git" ] && [ ! -z "$(which git 2>/dev/null)" ] &&
  46. GITHASH="-DTOYBOX_VERSION=\"$(git describe --tags --abbrev=12 2>/dev/null)\""
  47. TOYFILES="$($SED -n 's/^CONFIG_\([^=]*\)=.*/\1/p' "$KCONFIG_CONFIG" | xargs | tr ' [A-Z]' '|[a-z]')"
  48. TOYFILES="main.c $(egrep -l "TOY[(]($TOYFILES)[ ,]" toys/*/*.c | xargs)"
  49. BUILD="$(echo ${CROSS_COMPILE}${CC} $CFLAGS -I . $OPTIMIZE $GITHASH)"
  50. if [ "${TOYFILES/pending//}" != "$TOYFILES" ]
  51. then
  52. echo -e "\n\033[1;31mwarning: using unfinished code from toys/pending\033[0m"
  53. fi
  54. genbuildsh()
  55. {
  56. # Write a canned build line for use on crippled build machines.
  57. LLINK="$(echo $LDOPTIMIZE $LDFLAGS $(cat "$GENDIR"/optlibs.dat))"
  58. echo -e "#!/bin/sh\n\nPATH='$PATH'\nBUILD='$BUILD'\nLINK='$LLINK'\n"
  59. echo -e "\$BUILD lib/*.c $TOYFILES \$LINK -o $OUTNAME"
  60. }
  61. if ! cmp -s <(genbuildsh 2>/dev/null | head -n 5) \
  62. <(head -n 5 "$GENDIR"/build.sh 2>/dev/null | $SED '5s/ -o .*//')
  63. then
  64. echo -n "Library probe"
  65. # --as-needed removes libraries we don't use any symbols out of, but the
  66. # compiler has no way to ignore a library that doesn't exist, so detect
  67. # and skip nonexistent libraries for it.
  68. > "$GENDIR"/optlibs.dat
  69. for i in util crypt m resolv selinux smack attr crypto z log iconv tls ssl
  70. do
  71. echo "int main(int argc, char *argv[]) {return 0;}" | \
  72. ${CROSS_COMPILE}${CC} $CFLAGS $LDFLAGS -xc - -o "$GENDIR"/libprobe -l$i > /dev/null 2>/dev/null &&
  73. echo -l$i >> "$GENDIR"/optlibs.dat
  74. echo -n .
  75. done
  76. rm -f "$GENDIR"/libprobe
  77. echo
  78. fi
  79. genbuildsh > "$GENDIR"/build.sh && chmod +x "$GENDIR"/build.sh || exit 1
  80. #TODO: "make $SED && make" doesn't regenerate config.h because diff .config
  81. if true #isnewer "$GENDIR"/config.h "$KCONFIG_CONFIG"
  82. then
  83. echo "Make $GENDIR/config.h from $KCONFIG_CONFIG."
  84. # This long and roundabout sed invocation is to make old versions of sed
  85. # happy. New ones have '\n' so can replace one line with two without all
  86. # the branches and tedious mucking about with hyperspace.
  87. # TODO: clean this up to use modern stuff.
  88. $SED -n \
  89. -e 's/^# CONFIG_\(.*\) is not set.*/\1/' \
  90. -e 't notset' \
  91. -e 's/^CONFIG_\(.*\)=y.*/\1/' \
  92. -e 't isset' \
  93. -e 's/^CONFIG_\([^=]*\)=\(.*\)/#define CFG_\1 \2/p' \
  94. -e 'd' \
  95. -e ':notset' \
  96. -e 'h' \
  97. -e 's/.*/#define CFG_& 0/p' \
  98. -e 'g' \
  99. -e 's/.*/#define USE_&(...)/p' \
  100. -e 'd' \
  101. -e ':isset' \
  102. -e 'h' \
  103. -e 's/.*/#define CFG_& 1/p' \
  104. -e 'g' \
  105. -e 's/.*/#define USE_&(...) __VA_ARGS__/p' \
  106. $KCONFIG_CONFIG > "$GENDIR"/config.h || exit 1
  107. fi
  108. if [ ! -f "$GENDIR"/mkflags ] || [ "$GENDIR"/mkflags -ot scripts/mkflags.c ]
  109. then
  110. do_loudly $HOSTCC scripts/mkflags.c -o "$UNSTRIPPED"/mkflags || exit 1
  111. fi
  112. # Process config.h and newtoys.h to generate FLAG_x macros. Note we must
  113. # always #define the relevant macro, even when it's disabled, because we
  114. # allow multiple NEWTOY() in the same C file. (When disabled the FLAG is 0,
  115. # so flags&0 becomes a constant 0 allowing dead code elimination.)
  116. if isnewer "$GENDIR"/flags.h toys "$KCONFIG_CONFIG"
  117. then
  118. echo -n "$GENDIR/flags.h "
  119. # Parse files through C preprocessor twice, once to get flags for current
  120. # .config and once to get flags for allyesconfig
  121. for I in A B
  122. do
  123. (
  124. # define macros and select header files with option string data
  125. echo "#define NEWTOY(aa,bb,cc) aa $I bb"
  126. echo '#define OLDTOY(...)'
  127. if [ "$I" == A ]
  128. then
  129. cat "$GENDIR"/config.h
  130. else
  131. $SED '/USE_.*([^)]*)$/s/$/ __VA_ARGS__/' "$GENDIR"/config.h
  132. fi
  133. echo '#include "lib/toyflags.h"'
  134. cat "$GENDIR"/newtoys.h
  135. # Run result through preprocessor, glue together " " gaps leftover from USE
  136. # macros, delete comment lines, print any line with a quoted optstring,
  137. # turn any non-quoted opstring (NULL or 0) into " " (because fscanf can't
  138. # handle "" with nothing in it, and mkflags uses that).
  139. ) | ${CROSS_COMPILE}${CC} -E - | \
  140. $SED -n -e 's/" *"//g;/^#/d;t clear;:clear;s/"/"/p;t;s/\( [AB] \).*/\1 " "/p'
  141. # Sort resulting line pairs and glue them together into triplets of
  142. # command "flags" "allflags"
  143. # to feed into mkflags C program that outputs actual flag macros
  144. # If no pair (because command's disabled in config), use " " for flags
  145. # so allflags can define the appropriate zero macros.
  146. done | sort -s | $SED -n -e 's/ A / /;t pair;h;s/\([^ ]*\).*/\1 " "/;x' \
  147. -e 'b single;:pair;h;n;:single;s/[^ ]* B //;H;g;s/\n/ /;p' | \
  148. tee "$GENDIR"/flags.raw | "$UNSTRIPPED"/mkflags > "$GENDIR"/flags.h || exit 1
  149. fi
  150. # Extract global structure definitions and flag definitions from toys/*/*.c
  151. function getglobals()
  152. {
  153. for i in toys/*/*.c
  154. do
  155. # alas basename -s isn't in posix yet.
  156. NAME="$(echo $i | $SED 's@.*/\(.*\)\.c@\1@')"
  157. DATA="$($SED -n -e '/^GLOBALS(/,/^)/b got;b;:got' \
  158. -e 's/^GLOBALS(/_data {/' \
  159. -e 's/^)/};/' -e 'p' $i)"
  160. [ ! -z "$DATA" ] && echo -e "// $i\n\nstruct $NAME$DATA\n"
  161. done
  162. }
  163. if isnewer "$GENDIR"/globals.h toys
  164. then
  165. echo -n "$GENDIR/globals.h "
  166. GLOBSTRUCT="$(getglobals)"
  167. (
  168. echo "$GLOBSTRUCT"
  169. echo
  170. echo "extern union global_union {"
  171. echo "$GLOBSTRUCT" | \
  172. $SED -n 's/struct \(.*\)_data {/ struct \1_data \1;/p'
  173. echo "} this;"
  174. ) > "$GENDIR"/globals.h
  175. fi
  176. if [ ! -f "$UNSTRIPPED"/mktags ] || [ "$UNSTRIPPED"/mktags -ot scripts/mktags.c ]
  177. then
  178. do_loudly $HOSTCC scripts/mktags.c -o "$UNSTRIPPED"/mktags || exit 1
  179. fi
  180. if isnewer "$GENDIR"/tags.h toys
  181. then
  182. echo -n "$GENDIR/tags.h "
  183. $SED -n '/TAGGED_ARRAY(/,/^)/{s/.*TAGGED_ARRAY[(]\([^,]*\),/\1/;p}' \
  184. toys/*/*.c lib/*.c | "$UNSTRIPPED"/mktags > "$GENDIR"/tags.h
  185. fi
  186. if [ ! -f "$UNSTRIPPED"/config2help ] || [ "$UNSTRIPPED"/config2help -ot scripts/config2help.c ]
  187. then
  188. do_loudly $HOSTCC scripts/config2help.c -o "$UNSTRIPPED"/config2help || exit 1
  189. fi
  190. if isnewer "$GENDIR"/help.h "$GENDIR"/Config.in
  191. then
  192. echo "$GENDIR/help.h"
  193. "$UNSTRIPPED"/config2help Config.in $KCONFIG_CONFIG > "$GENDIR"/help.h || exit 1
  194. fi
  195. [ ! -z "$NOBUILD" ] && exit 0
  196. echo -n "Compile $OUTNAME"
  197. [ ! -z "$V" ] && echo
  198. DOTPROG=.
  199. # This is a parallel version of: do_loudly $BUILD $FILES $LLINK || exit 1
  200. # Any headers newer than the oldest generated/obj file?
  201. X="$(ls -1t "$GENDIR"/obj/* 2>/dev/null | tail -n 1)"
  202. # TODO: redo this
  203. if [ ! -e "$X" ] || [ ! -z "$(find toys -name "*.h" -newer "$X")" ]
  204. then
  205. rm -rf "$GENDIR"/obj && mkdir -p "$GENDIR"/obj || exit 1
  206. else
  207. rm -f "$GENDIR"/obj/main.o || exit 1
  208. fi
  209. # build each generated/obj/*.o file in parallel
  210. unset PENDING LNKFILES CLICK
  211. DONE=0
  212. COUNT=0
  213. for i in lib/*.c click $TOYFILES
  214. do
  215. [ "$i" == click ] && CLICK=1 && continue
  216. X=${i/lib\//lib_}
  217. X=${X##*/}
  218. OUT="$GENDIR/obj/${X%%.c}.o"
  219. LNKFILES="$LNKFILES $OUT"
  220. # Library files don't need to be rebuilt if older than .config.
  221. # ($TOYFILES contents can depend on CONFIG symbols, lib/*.c never should.)
  222. [ "$OUT" -nt "$i" ] && [ -z "$CLICK" -o "$OUT" -nt "$KCONFIG_CONFIG" ] &&
  223. continue
  224. do_loudly $BUILD -c $i -o $OUT &
  225. # ratelimit to $CPUS many parallel jobs, detecting errors
  226. [ $((++COUNT)) -ge $CPUS ] && { wait $DASHN; DONE=$?; : $((--COUNT)); }
  227. [ $DONE -ne 0 ] && break
  228. done
  229. # wait for all background jobs, detecting errors
  230. while [ $((COUNT--)) -gt 0 ]
  231. do
  232. wait $DASHN;
  233. DONE=$((DONE+$?))
  234. done
  235. [ $DONE -ne 0 ] && exit 1
  236. UNSTRIPPED="$UNSTRIPPED/${OUTNAME/*\//}"
  237. do_loudly $BUILD $LNKFILES $LLINK -o "$UNSTRIPPED" || exit 1
  238. if [ ! -z "$NOSTRIP" ] ||
  239. ! do_loudly ${CROSS_COMPILE}${STRIP} "$UNSTRIPPED" -o "$OUTNAME"
  240. then
  241. [ -z "$NOSTRIP" ] && echo "strip failed, using unstripped"
  242. rm -f "$OUTNAME" &&
  243. cp "$UNSTRIPPED" "$OUTNAME" || exit 1
  244. fi
  245. # gcc 4.4's strip command is buggy, and doesn't set the executable bit on
  246. # its output the way SUSv4 suggests it do so. While we're at it, make sure
  247. # we don't have the "w" bit set so things like bzip2's "cp -f" install don't
  248. # overwrite our binary through the symlink.
  249. do_loudly chmod 555 "$OUTNAME" || exit 1
  250. echo