• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3# Grab default values for $CFLAGS and such.
4
5export LANG=c
6export LC_ALL=C
7set -o pipefail
8source ./configure
9
10[ -z "$KCONFIG_CONFIG" ] && KCONFIG_CONFIG=".config"
11
12# 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
17# Respond to V= by echoing command lines as well as running them
18DOTPROG=
19do_loudly()
20{
21  [ ! -z "$V" ] && echo "$@" || echo -n "$DOTPROG"
22  "$@"
23}
24
25# 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
33mkdir -p generated
34
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 -s -k 1,1 \
53	| sed 's/[^ ]* //'  >> generated/newtoys.h || exit 1
54fi
55
56[ ! -z "$V" ] && echo "Which C files to build..."
57
58# Extract a list of toys/*/*.c files to compile from the data in $KCONFIG_CONFIG
59# (First command names, then filenames with relevant {NEW,OLD}TOY() macro.)
60
61GITHASH="$(git describe --tags --abbrev=12 2>/dev/null)"
62[ ! -z "$GITHASH" ] && GITHASH="-DTOYBOX_VERSION=\"$GITHASH\""
63TOYFILES="$(sed -n 's/^CONFIG_\([^=]*\)=.*/\1/p' "$KCONFIG_CONFIG" | xargs | tr ' [A-Z]' '|[a-z]')"
64TOYFILES="$(egrep -l "TOY[(]($TOYFILES)[ ,]" toys/*/*.c)"
65CFLAGS="$CFLAGS $(cat generated/cflags)"
66BUILD="$(echo ${CROSS_COMPILE}${CC} $CFLAGS -I . $OPTIMIZE $GITHASH)"
67FILES="$(echo lib/*.c main.c $TOYFILES)"
68
69if [ "${TOYFILES/pending//}" != "$TOYFILES" ]
70then
71  echo -e "\n\033[1;31mwarning: using unfinished code from toys/pending\033[0m"
72fi
73
74genbuildsh()
75{
76  # Write a canned build line for use on crippled build machines.
77
78  echo "#!/bin/sh"
79  echo
80  echo "BUILD=\"$BUILD\""
81  echo
82  echo "FILES=\"$FILES\""
83  echo
84  echo "LINK=\"$LINK\""
85  echo
86  echo
87  echo '$BUILD $FILES $LINK'
88}
89
90if ! cmp -s <(genbuildsh | head -n 3) \
91          <(head -n 3 generated/build.sh 2>/dev/null)
92then
93  echo -n "Library probe"
94
95  # We trust --as-needed to remove each library if we don't use any symbols
96  # out of it, this loop is because the compiler has no way to ignore a library
97  # that doesn't exist, so we have to detect and skip nonexistent libraries
98  # for it.
99
100  > generated/optlibs.dat
101  for i in util crypt m resolv selinux smack attr rt
102  do
103    echo "int main(int argc, char *argv[]) {return 0;}" | \
104    ${CROSS_COMPILE}${CC} $CFLAGS -xc - -o generated/libprobe -Wl,--as-needed -l$i > /dev/null 2>/dev/null &&
105    echo -l$i >> generated/optlibs.dat
106    echo -n .
107  done
108  rm -f generated/libprobe
109  echo
110fi
111
112# LINK needs optlibs.dat, above
113
114LINK="$(echo $LDOPTIMIZE $LDFLAGS -o toybox_unstripped -Wl,--as-needed $(cat generated/optlibs.dat))"
115genbuildsh > generated/build.sh && chmod +x generated/build.sh || exit 1
116
117echo "Make generated/config.h from $KCONFIG_CONFIG."
118
119# This long and roundabout sed invocation is to make old versions of sed happy.
120# New ones have '\n' so can replace one line with two without all the branches
121# and tedious mucking about with hold space.
122
123sed -n \
124  -e 's/^# CONFIG_\(.*\) is not set.*/\1/' \
125  -e 't notset' \
126  -e 's/^CONFIG_\(.*\)=y.*/\1/' \
127  -e 't isset' \
128  -e 's/^CONFIG_\([^=]*\)=\(.*\)/#define CFG_\1 \2/p' \
129  -e 'd' \
130  -e ':notset' \
131  -e 'h' \
132  -e 's/.*/#define CFG_& 0/p' \
133  -e 'g' \
134  -e 's/.*/#define USE_&(...)/p' \
135  -e 'd' \
136  -e ':isset' \
137  -e 'h' \
138  -e 's/.*/#define CFG_& 1/p' \
139  -e 'g' \
140  -e 's/.*/#define USE_&(...) __VA_ARGS__/p' \
141  $KCONFIG_CONFIG > generated/config.h || exit 1
142
143if [ generated/mkflags -ot scripts/mkflags.c ]
144then
145  do_loudly $HOSTCC scripts/mkflags.c -o generated/mkflags || exit 1
146fi
147
148echo -n "generated/flags.h "
149
150# Process config.h and newtoys.h to generate FLAG_x macros. Note we must
151# always #define the relevant macro, even when it's disabled, because we
152# allow multiple NEWTOY() in the same C file. (When disabled the FLAG is 0,
153# so flags&0 becomes a constant 0 allowing dead code elimination.)
154
155# Parse files through C preprocessor twice, once to get flags for current
156# .config and once to get flags for allyesconfig
157for I in A B
158do
159  (
160  # define macros and select header files with option string data
161
162  echo "#define NEWTOY(aa,bb,cc) aa $I bb"
163  echo '#define OLDTOY(...)'
164  if [ "$I" == A ]
165  then
166    cat generated/config.h
167  else
168    sed '/USE_.*([^)]*)$/s/$/ __VA_ARGS__/' generated/config.h
169  fi
170  cat generated/newtoys.h
171
172  # Run result through preprocessor, glue together " " gaps leftover from USE
173  # macros, delete comment lines, print any line with a quoted optstring,
174  # turn any non-quoted opstring (NULL or 0) into " " (because fscanf can't
175  # handle "" with nothing in it, and mkflags uses that).
176
177  ) | ${CROSS_COMPILE}${CC} -E - | \
178    sed -n -e 's/" *"//g;/^#/d;t clear;:clear;s/"/"/p;t;s/\( [AB] \).*/\1 " "/p'
179
180# Sort resulting line pairs and glue them together into triplets of
181#   command "flags" "allflags"
182# to feed into mkflags C program that outputs actual flag macros
183# If no pair (because command's disabled in config), use " " for flags
184# so allflags can define the appropriate zero macros.
185
186done | sort -s | sed -n 's/ A / /;t pair;h;s/\([^ ]*\).*/\1 " "/;x;b single;:pair;h;n;:single;s/[^ ]* B //;H;g;s/\n/ /;p' | tee generated/flags.raw | \
187generated/mkflags > generated/flags.h || exit 1
188
189# Extract global structure definitions and flag definitions from toys/*/*.c
190
191function getglobals()
192{
193  for i in toys/*/*.c
194  do
195    NAME="$(echo $i | sed 's@.*/\(.*\)\.c@\1@')"
196    DATA="$(sed -n -e '/^GLOBALS(/,/^)/b got;b;:got' \
197            -e 's/^GLOBALS(/struct '"$NAME"'_data {/' \
198            -e 's/^)/};/' -e 'p' $i)"
199
200    [ ! -z "$DATA" ] && echo -e "// $i\n\n$DATA\n"
201  done
202}
203
204if isnewer generated/globals.h toys
205then
206  echo -n "generated/globals.h "
207  GLOBSTRUCT="$(getglobals)"
208  (
209    echo "$GLOBSTRUCT"
210    echo
211    echo "extern union global_union {"
212    echo "$GLOBSTRUCT" | \
213      sed -n 's/struct \(.*\)_data {/	struct \1_data \1;/p'
214    echo "} this;"
215  ) > generated/globals.h
216fi
217
218if [ generated/mktags -ot scripts/mktags.c ]
219then
220  do_loudly $HOSTCC scripts/mktags.c -o generated/mktags || exit 1
221fi
222
223if isnewer generated/tags.h toys
224then
225  echo -n "generated/tags.h "
226
227  sed -n '/TAGGED_ARRAY(/,/^)/{s/.*TAGGED_ARRAY[(]\([^,]*\),/\1/;p}' \
228    toys/*/*.c lib/*.c | generated/mktags > generated/tags.h
229fi
230
231echo "generated/help.h"
232if [ generated/config2help -ot scripts/config2help.c ]
233then
234  do_loudly $HOSTCC scripts/config2help.c -I . lib/xwrap.c lib/llist.c \
235    lib/lib.c lib/portability.c -o generated/config2help || exit 1
236fi
237generated/config2help Config.in $KCONFIG_CONFIG > generated/help.h || exit 1
238
239[ ! -z "$NOBUILD" ] && exit 0
240
241echo -n "Compile toybox"
242[ ! -z "$V" ] && echo
243DOTPROG=.
244
245# This is a parallel version of: do_loudly $BUILD $FILES $LINK || exit 1
246
247X="$(ls -1t generated/obj/* 2>/dev/null | tail -n 1)"
248if [ ! -e "$X" ] || [ ! -z "$(find toys -name "*.h" -newer "$X")" ]
249then
250  rm -rf generated/obj && mkdir -p generated/obj || exit 1
251else
252  rm -f generated/obj/{main,lib_help}.o || exit 1
253fi
254PENDING=
255LFILES=
256DONE=0
257for i in $FILES
258do
259  # build each generated/obj/*.o file in parallel
260
261  X=${i/lib\//lib_}
262  X=${X##*/}
263  OUT="generated/obj/${X%%.c}.o"
264  LFILES="$LFILES $OUT"
265  [ "$OUT" -nt "$i" ] && continue
266  do_loudly $BUILD -c $i -o $OUT &
267
268  # ratelimit to $CPUS many parallel jobs, detecting errors
269
270  while true
271  do
272    PENDING="$(echo $PENDING $(jobs -rp) | tr ' ' '\n' | sort -u)"
273    [ $(echo -n "$PENDING" | wc -l) -lt "$CPUS" ] && break;
274
275    wait $(echo "$PENDING" | head -n 1)
276    DONE=$(($DONE+$?))
277    PENDING="$(echo "$PENDING" | tail -n +2)"
278  done
279  [ $DONE -ne 0 ] && break
280done
281
282# wait for all background jobs, detecting errors
283
284for i in $PENDING
285do
286  wait $i
287  DONE=$(($DONE+$?))
288done
289
290[ $DONE -ne 0 ] && exit 1
291
292do_loudly $BUILD $LFILES $LINK || exit 1
293if [ ! -z "$NOSTRIP" ] || ! do_loudly ${CROSS_COMPILE}strip toybox_unstripped -o toybox
294then
295  echo "strip failed, using unstripped" && cp toybox_unstripped toybox ||
296  exit 1
297fi
298
299# gcc 4.4's strip command is buggy, and doesn't set the executable bit on
300# its output the way SUSv4 suggests it do so. While we're at it, make sure
301# we don't have the "w" bit set so things like bzip2's "cp -f" install don't
302# overwrite our binary through the symlink.
303do_loudly chmod 555 toybox || exit 1
304
305echo
306