blob: 2274d68b9327f6183f414e35f967b53b11454bfc [file] [log] [blame]
Rob Landley28964802008-01-19 17:08:39 -06001#!/bin/bash
2
3# Grab default values for $CFLAGS and such.
4
Rob Landleyb3d4f0b2013-04-29 16:00:40 -05005export LANG=c
Rob Landley082a9a72014-08-30 16:34:46 -05006export LC_ALL=C
Rob Landley7a07c6b2014-09-09 20:13:03 -05007set -o pipefail
Rob Landley28964802008-01-19 17:08:39 -06008source ./configure
9
Rob Landleyd04dc1f2013-08-30 01:53:31 -050010[ -z "$KCONFIG_CONFIG" ] && KCONFIG_CONFIG=".config"
11
Rob Landleyd3df4232014-09-20 13:07:53 -050012# Since each cc invocation is short, launch half again as many processes
13# as we have processors so they don't exit faster than we can start them.
14[ -z "$CPUS" ] &&
15 CPUS=$((($(echo /sys/devices/system/cpu/cpu[0-9]* | wc -w)*3)/2))
16
Rob Landley86cafe12014-01-03 18:23:09 -060017# Respond to V= by echoing command lines as well as running them
Rob Landley62390fd2014-11-28 16:49:46 -060018DOTPROG=
Rob Landley86cafe12014-01-03 18:23:09 -060019do_loudly()
20{
Rob Landley62390fd2014-11-28 16:49:46 -060021 [ ! -z "$V" ] && echo "$@" || echo -n "$DOTPROG"
Rob Landley86cafe12014-01-03 18:23:09 -060022 "$@"
23}
24
Rob Landley62390fd2014-11-28 16:49:46 -060025# Is anything under directory $2 newer than file $1
26isnewer()
27{
28 [ ! -z "$(find "$2" -newer "$1" 2>/dev/null || echo yes)" ]
29}
30
31echo "Generate headers from toys/*/*.c..."
32
Rob Landley94a46032014-09-20 14:20:28 -050033mkdir -p generated
Rob Landley62390fd2014-11-28 16:49:46 -060034
35if isnewer generated/Config.in toys
36then
37 echo "Extract configuration information from toys/*.c files..."
38 scripts/genconfig.sh
39fi
40
41# Create a list of all the commands toybox can provide. Note that the first
42# entry is out of order on purpose (the toybox multiplexer command must be the
43# first element of the array). The rest must be sorted in alphabetical order
44# for fast binary search.
45
46if isnewer generated/newtoys.h toys
47then
48 echo -n "generated/newtoys.h "
49
50 echo "USE_TOYBOX(NEWTOY(toybox, NULL, TOYFLAG_STAYROOT))" > generated/newtoys.h
51 sed -n -e 's/^USE_[A-Z0-9_]*(/&/p' toys/*/*.c \
52 | sed 's/\(.*TOY(\)\([^,]*\),\(.*\)/\2 \1\2,\3/' | sort -k 1,1 \
53 | sed 's/[^ ]* //' >> generated/newtoys.h &&
54 sed -n -e 's/.*(NEWTOY(\([^,]*\), *\(\("[^"]*"[^,]*\)*\),.*/#define OPTSTR_\1\t\2/p' \
55 generated/newtoys.h > generated/oldtoys.h || exit 1
56fi
57
58[ ! -z "$V" ] && echo "Which C files to build..."
59
60# Extract a list of toys/*/*.c files to compile from the data in $KCONFIG_CONFIG
61TOYFILES="$(egrep -l "TOY[(]($(sed -n 's/^CONFIG_\([^=]*\)=.*/\1/p' "$KCONFIG_CONFIG" | xargs | tr ' [A-Z]' '|[a-z]'))[ ,]" toys/*/*.c)"
62BUILD="$(echo ${CROSS_COMPILE}${CC} $CFLAGS -I . $OPTIMIZE)"
63FILES="$(echo lib/*.c main.c $TOYFILES)"
64
65genbuildsh()
66{
67 # Write a canned build line for use on crippled build machines.
68
69 echo "#!/bin/sh"
70 echo
71 echo "BUILD=\"$BUILD\""
72 echo
73 echo "FILES=\"$FILES\""
74 echo
75 echo "LINK=\"$LINK\""
76 echo
77 echo
78 echo '$BUILD $FILES $LINK'
79}
80
81if ! cmp -s <(genbuildsh | head -n 3) \
82 <(head -n 3 generated/build.sh 2>/dev/null)
83then
84 echo -n "Library probe"
85
86 # We trust --as-needed to remove each library if we don't use any symbols
87 # out of it, this loop is because the compiler has no way to ignore a library
88 # that doesn't exist, so we have to detect and skip nonexistent libraries
89 # for it.
90
91 > generated/optlibs.dat
92 for i in util crypt m resolv
93 do
94 echo "int main(int argc, char *argv[]) {return 0;}" | \
95 ${CROSS_COMPILE}${CC} $CFLAGS -xc - -o /dev/null -Wl,--as-needed -l$i > /dev/null 2>/dev/null &&
96 echo -l$i >> generated/optlibs.dat
97 echo -n .
98 done
99 echo
100fi
101
102# LINK needs optlibs.dat, above
103
104LINK="$(echo $LDOPTIMIZE -o toybox_unstripped -Wl,--as-needed $(cat generated/optlibs.dat))"
105genbuildsh > generated/build.sh && chmod +x generated/build.sh || exit 1
106
Rob Landleyd04dc1f2013-08-30 01:53:31 -0500107echo "Make generated/config.h from $KCONFIG_CONFIG."
Rob Landley28964802008-01-19 17:08:39 -0600108
109# This long and roundabout sed invocation is to make old versions of sed happy.
110# New ones have '\n' so can replace one line with two without all the branches
111# and tedious mucking about with hold space.
112
Rob Landley1193a022009-01-30 16:10:55 -0600113sed -n \
114 -e 's/^# CONFIG_\(.*\) is not set.*/\1/' \
115 -e 't notset' \
116 -e 's/^CONFIG_\(.*\)=y.*/\1/' \
117 -e 't isset' \
Rob Landley687bea52009-01-30 16:17:35 -0600118 -e 's/^CONFIG_\([^=]*\)=\(.*\)/#define CFG_\1 \2/p' \
Rob Landley1193a022009-01-30 16:10:55 -0600119 -e 'd' \
120 -e ':notset' \
121 -e 'h' \
122 -e 's/.*/#define CFG_& 0/p' \
123 -e 'g' \
124 -e 's/.*/#define USE_&(...)/p' \
125 -e 'd' \
126 -e ':isset' \
127 -e 'h' \
128 -e 's/.*/#define CFG_& 1/p' \
129 -e 'g' \
130 -e 's/.*/#define USE_&(...) __VA_ARGS__/p' \
Rob Landleyd04dc1f2013-08-30 01:53:31 -0500131 $KCONFIG_CONFIG > generated/config.h || exit 1
Rob Landley28964802008-01-19 17:08:39 -0600132
Rob Landley62390fd2014-11-28 16:49:46 -0600133if [ generated/mkflags -ot scripts/mkflags.c ]
134then
135 do_loudly $HOSTCC scripts/mkflags.c -o generated/mkflags || exit 1
136fi
Rob Landley6cf0a112012-11-26 14:14:29 -0600137
Rob Landley46c80692013-12-28 17:06:55 -0600138echo -n "generated/flags.h "
Rob Landley207cada2013-10-03 03:18:00 -0500139
Rob Landleye36a9dd2014-02-23 20:11:06 -0600140# Process config.h and newtoys.h to generate FLAG_x macros. Note we must
141# always #define the relevant macro, even when it's disabled, because we
142# allow multiple NEWTOY() in the same C file. (When disabled the FLAG is 0,
143# so flags&0 becomes a constant 0 allowing dead code elimination.)
144
Rob Landley207cada2013-10-03 03:18:00 -0500145# Parse files through C preprocessor twice, once to get flags for current
146# .config and once to get flags for allyesconfig
147for I in A B
148do
149 (
150 # define macros and select header files with option string data
151
152 echo "#define NEWTOY(aa,bb,cc) aa $I bb"
153 echo '#define OLDTOY(...)'
154 if [ "$I" == A ]
155 then
156 cat generated/config.h
157 else
158 sed '/USE_.*([^)]*)$/s/$/ __VA_ARGS__/' generated/config.h
159 fi
160 cat generated/newtoys.h
161
162 # Run result through preprocessor, glue together " " gaps leftover from USE
163 # macros, delete comment lines, print any line with a quoted optstring,
164 # turn any non-quoted opstring (NULL or 0) into " " (because fscanf can't
Rob Landley170c3972014-02-28 20:46:16 -0600165 # handle "" with nothing in it, and mkflags uses that).
Rob Landley207cada2013-10-03 03:18:00 -0500166
Rob Landley73fff3b2013-10-23 02:52:01 -0500167 ) | ${CROSS_COMPILE}${CC} -E - | \
Rob Landley170c3972014-02-28 20:46:16 -0600168 sed -n -e 's/" *"//g;/^#/d;t clear;:clear;s/"/"/p;t;s/\( [AB] \).*/\1 " "/p'
Rob Landley207cada2013-10-03 03:18:00 -0500169
170# Sort resulting line pairs and glue them together into triplets of
171# command "flags" "allflags"
172# to feed into mkflags C program that outputs actual flag macros
Rob Landleye36a9dd2014-02-23 20:11:06 -0600173# If no pair (because command's disabled in config), use " " for flags
174# so allflags can define the appropriate zero macros.
Rob Landley207cada2013-10-03 03:18:00 -0500175
Rob Landleye36a9dd2014-02-23 20:11:06 -0600176done | sort | sed -n 's/ A / /;t pair;h;s/\([^ ]*\).*/\1 " "/;x;b single;:pair;h;n;:single;s/[^ ]* B //;H;g;s/\n/ /;p' |\
Rob Landley207cada2013-10-03 03:18:00 -0500177generated/mkflags > generated/flags.h || exit 1
Rob Landley6cf0a112012-11-26 14:14:29 -0600178
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500179# Extract global structure definitions and flag definitions from toys/*/*.c
180
181function getglobals()
182{
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500183 for i in toys/*/*.c
184 do
185 NAME="$(echo $i | sed 's@.*/\(.*\)\.c@\1@')"
Rob Landley207cada2013-10-03 03:18:00 -0500186 DATA="$(sed -n -e '/^GLOBALS(/,/^)/b got;b;:got' \
187 -e 's/^GLOBALS(/struct '"$NAME"'_data {/' \
188 -e 's/^)/};/' -e 'p' $i)"
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500189
Rob Landley207cada2013-10-03 03:18:00 -0500190 [ ! -z "$DATA" ] && echo -e "// $i\n\n$DATA\n"
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500191 done
192}
193
Rob Landley62390fd2014-11-28 16:49:46 -0600194if isnewer generated/globals.h toys
195then
196 echo -n "generated/globals.h "
197 GLOBSTRUCT="$(getglobals)"
198 (
199 echo "$GLOBSTRUCT"
200 echo
201 echo "extern union global_union {"
202 echo "$GLOBSTRUCT" | \
203 sed -n 's/struct \(.*\)_data {/ struct \1_data \1;/p'
204 echo "} this;"
205 ) > generated/globals.h
206fi
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500207
208echo "generated/help.h"
Rob Landley62390fd2014-11-28 16:49:46 -0600209if [ generated/config2help -ot scripts/config2help.c ]
210then
211 do_loudly $HOSTCC scripts/config2help.c -I . lib/xwrap.c lib/llist.c \
212 lib/lib.c -o generated/config2help || exit 1
213fi
Rob Landley0e040df2014-02-04 06:14:30 -0600214generated/config2help Config.in $KCONFIG_CONFIG > generated/help.h || exit 1
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500215
Rob Landley62390fd2014-11-28 16:49:46 -0600216[ ! -z "$NOBUILD" ] && exit 0
Rob Landleyfb98c1e2012-05-23 21:54:16 -0500217
Rob Landley91b360a2014-08-09 14:33:34 -0500218echo -n "Compile toybox"
219[ ! -z "$V" ] && echo
Rob Landley62390fd2014-11-28 16:49:46 -0600220DOTPROG=.
Rob Landley91b360a2014-08-09 14:33:34 -0500221
222# This is a parallel version of: do_loudly $BUILD $FILES $LINK || exit 1
223
Rob Landley62390fd2014-11-28 16:49:46 -0600224X="$(ls -1t generated/obj/* 2>/dev/null | tail -n 1)"
225if [ ! -e "$X" ] || [ ! -z "$(find toys -name "*.h" -newer "$X")" ]
226then
227 rm -rf generated/obj && mkdir -p generated/obj || exit 1
228else
229 rm -f generated/obj/{main,lib_help}.o || exit 1
230fi
Rob Landley5d16faa2014-08-24 22:42:47 -0500231PENDING=
Rob Landley62390fd2014-11-28 16:49:46 -0600232LFILES=
Rob Landley91b360a2014-08-09 14:33:34 -0500233for i in $FILES
234do
Rob Landley7a07c6b2014-09-09 20:13:03 -0500235 # build each generated/obj/*.o file in parallel
Rob Landley91b360a2014-08-09 14:33:34 -0500236
237 X=${i/lib\//lib_}
238 X=${X##*/}
Rob Landley62390fd2014-11-28 16:49:46 -0600239 OUT="generated/obj/${X%%.c}.o"
240 LFILES="$LFILES $OUT"
241 [ "$OUT" -nt "$i" ] && continue
242 do_loudly $BUILD -c $i -o $OUT &
Rob Landley91b360a2014-08-09 14:33:34 -0500243
244 # ratelimit to $CPUS many parallel jobs, detecting errors
245
246 while true
247 do
Rob Landley5d16faa2014-08-24 22:42:47 -0500248 PENDING="$(echo $PENDING $(jobs -rp) | tr ' ' '\n' | sort -u)"
Rob Landley9bb73ad2014-09-04 00:23:51 -0500249 [ $(echo -n "$PENDING" | wc -l) -lt "$CPUS" ] && break;
Rob Landley5d16faa2014-08-24 22:42:47 -0500250
Rob Landley658887a2014-08-31 22:07:43 -0500251 wait $(echo "$PENDING" | head -n 1) || exit 1
Rob Landley5d16faa2014-08-24 22:42:47 -0500252 PENDING="$(echo "$PENDING" | tail -n +2)"
Rob Landley91b360a2014-08-09 14:33:34 -0500253 done
254done
255
256# wait for all background jobs, detecting errors
257
Rob Landley5d16faa2014-08-24 22:42:47 -0500258for i in $PENDING
Rob Landley91b360a2014-08-09 14:33:34 -0500259do
260 wait $i || exit 1
261done
262
Rob Landley62390fd2014-11-28 16:49:46 -0600263do_loudly $BUILD $LFILES $LINK || exit 1
Rob Landley344c2672012-03-03 10:56:11 -0600264do_loudly ${CROSS_COMPILE}${STRIP} toybox_unstripped -o toybox || exit 1
Rob Landleyb704ad22009-12-13 00:12:26 -0600265# gcc 4.4's strip command is buggy, and doesn't set the executable bit on
266# its output the way SUSv4 suggests it do so.
Rob Landley1aa66ba2012-02-17 12:06:12 -0600267do_loudly chmod +x toybox || exit 1
Rob Landley91b360a2014-08-09 14:33:34 -0500268echo