blob: 049bc4d9ae19563145ac85e34fd98375bbbea8f9 [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 Landley5d16faa2014-08-24 22:42:47 -050012[ -z "$CPUS" ] && CPUS=$(($(echo /sys/devices/system/cpu/cpu[0-9]* | wc -w)+1))
Rob Landley3a9241a2012-08-25 14:25:22 -050013
Rob Landley86cafe12014-01-03 18:23:09 -060014# Respond to V= by echoing command lines as well as running them
15do_loudly()
16{
17 [ ! -z "$V" ] && echo "$@"
18 "$@"
19}
20
Rob Landleyd04dc1f2013-08-30 01:53:31 -050021echo "Make generated/config.h from $KCONFIG_CONFIG."
Rob Landley28964802008-01-19 17:08:39 -060022
23# This long and roundabout sed invocation is to make old versions of sed happy.
24# New ones have '\n' so can replace one line with two without all the branches
25# and tedious mucking about with hold space.
26
Rob Landley1193a022009-01-30 16:10:55 -060027sed -n \
28 -e 's/^# CONFIG_\(.*\) is not set.*/\1/' \
29 -e 't notset' \
30 -e 's/^CONFIG_\(.*\)=y.*/\1/' \
31 -e 't isset' \
Rob Landley687bea52009-01-30 16:17:35 -060032 -e 's/^CONFIG_\([^=]*\)=\(.*\)/#define CFG_\1 \2/p' \
Rob Landley1193a022009-01-30 16:10:55 -060033 -e 'd' \
34 -e ':notset' \
35 -e 'h' \
36 -e 's/.*/#define CFG_& 0/p' \
37 -e 'g' \
38 -e 's/.*/#define USE_&(...)/p' \
39 -e 'd' \
40 -e ':isset' \
41 -e 'h' \
42 -e 's/.*/#define CFG_& 1/p' \
43 -e 'g' \
44 -e 's/.*/#define USE_&(...) __VA_ARGS__/p' \
Rob Landleyd04dc1f2013-08-30 01:53:31 -050045 $KCONFIG_CONFIG > generated/config.h || exit 1
Rob Landley28964802008-01-19 17:08:39 -060046
Rob Landleyc0e56ed2012-10-08 00:02:30 -050047
48echo "Extract configuration information from toys/*.c files..."
49scripts/genconfig.sh
50
51echo "Generate headers from toys/*/*.c..."
52
Rob Landley3a489e42013-06-22 14:23:06 -050053# Create a list of all the commands toybox can provide. Note that the first
54# entry is out of order on purpose (the toybox multiplexer command must be the
55# first element of the array). The rest must be sorted in alphabetical order
Rob Landleyc0e56ed2012-10-08 00:02:30 -050056# for fast binary search.
57
Rob Landley46c80692013-12-28 17:06:55 -060058echo -n "generated/newtoys.h "
Rob Landleyc0e56ed2012-10-08 00:02:30 -050059
Rob Landleyd04dc1f2013-08-30 01:53:31 -050060echo "USE_TOYBOX(NEWTOY(toybox, NULL, TOYFLAG_STAYROOT))" > generated/newtoys.h
Rob Landleyc0e56ed2012-10-08 00:02:30 -050061sed -n -e 's/^USE_[A-Z0-9_]*(/&/p' toys/*/*.c \
62 | sed 's/\(.*TOY(\)\([^,]*\),\(.*\)/\2 \1\2,\3/' | sort -k 1,1 \
63 | sed 's/[^ ]* //' >> generated/newtoys.h
Rob Landley75a18152013-09-22 03:37:39 -050064sed -n -e 's/.*(NEWTOY(\([^,]*\), *\(\("[^"]*"[^,]*\)*\),.*/#define OPTSTR_\1\t\2/p' \
Rob Landleydd2d2392013-08-12 01:48:27 -050065 generated/newtoys.h > generated/oldtoys.h
Rob Landleyc0e56ed2012-10-08 00:02:30 -050066
Rob Landley86cafe12014-01-03 18:23:09 -060067do_loudly $HOSTCC scripts/mkflags.c -o generated/mkflags || exit 1
Rob Landley6cf0a112012-11-26 14:14:29 -060068
Rob Landley46c80692013-12-28 17:06:55 -060069echo -n "generated/flags.h "
Rob Landley207cada2013-10-03 03:18:00 -050070
Rob Landleye36a9dd2014-02-23 20:11:06 -060071# Process config.h and newtoys.h to generate FLAG_x macros. Note we must
72# always #define the relevant macro, even when it's disabled, because we
73# allow multiple NEWTOY() in the same C file. (When disabled the FLAG is 0,
74# so flags&0 becomes a constant 0 allowing dead code elimination.)
75
Rob Landley207cada2013-10-03 03:18:00 -050076# Parse files through C preprocessor twice, once to get flags for current
77# .config and once to get flags for allyesconfig
78for I in A B
79do
80 (
81 # define macros and select header files with option string data
82
83 echo "#define NEWTOY(aa,bb,cc) aa $I bb"
84 echo '#define OLDTOY(...)'
85 if [ "$I" == A ]
86 then
87 cat generated/config.h
88 else
89 sed '/USE_.*([^)]*)$/s/$/ __VA_ARGS__/' generated/config.h
90 fi
91 cat generated/newtoys.h
92
93 # Run result through preprocessor, glue together " " gaps leftover from USE
94 # macros, delete comment lines, print any line with a quoted optstring,
95 # turn any non-quoted opstring (NULL or 0) into " " (because fscanf can't
Rob Landley170c3972014-02-28 20:46:16 -060096 # handle "" with nothing in it, and mkflags uses that).
Rob Landley207cada2013-10-03 03:18:00 -050097
Rob Landley73fff3b2013-10-23 02:52:01 -050098 ) | ${CROSS_COMPILE}${CC} -E - | \
Rob Landley170c3972014-02-28 20:46:16 -060099 sed -n -e 's/" *"//g;/^#/d;t clear;:clear;s/"/"/p;t;s/\( [AB] \).*/\1 " "/p'
Rob Landley207cada2013-10-03 03:18:00 -0500100
101# Sort resulting line pairs and glue them together into triplets of
102# command "flags" "allflags"
103# to feed into mkflags C program that outputs actual flag macros
Rob Landleye36a9dd2014-02-23 20:11:06 -0600104# If no pair (because command's disabled in config), use " " for flags
105# so allflags can define the appropriate zero macros.
Rob Landley207cada2013-10-03 03:18:00 -0500106
Rob Landleye36a9dd2014-02-23 20:11:06 -0600107done | 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 -0500108generated/mkflags > generated/flags.h || exit 1
Rob Landley6cf0a112012-11-26 14:14:29 -0600109
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500110# Extract global structure definitions and flag definitions from toys/*/*.c
111
112function getglobals()
113{
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500114 for i in toys/*/*.c
115 do
116 NAME="$(echo $i | sed 's@.*/\(.*\)\.c@\1@')"
Rob Landley207cada2013-10-03 03:18:00 -0500117 DATA="$(sed -n -e '/^GLOBALS(/,/^)/b got;b;:got' \
118 -e 's/^GLOBALS(/struct '"$NAME"'_data {/' \
119 -e 's/^)/};/' -e 'p' $i)"
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500120
Rob Landley207cada2013-10-03 03:18:00 -0500121 [ ! -z "$DATA" ] && echo -e "// $i\n\n$DATA\n"
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500122 done
123}
124
Rob Landley46c80692013-12-28 17:06:55 -0600125echo -n "generated/globals.h "
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500126
127GLOBSTRUCT="$(getglobals)"
128(
129 echo "$GLOBSTRUCT"
130 echo
131 echo "extern union global_union {"
132 echo "$GLOBSTRUCT" | sed -n 's/struct \(.*\)_data {/ struct \1_data \1;/p'
133 echo "} this;"
134) > generated/globals.h
135
136echo "generated/help.h"
Rob Landley86cafe12014-01-03 18:23:09 -0600137do_loudly $HOSTCC scripts/config2help.c -I . lib/xwrap.c lib/llist.c lib/lib.c \
138 -o generated/config2help && \
Rob Landley0e040df2014-02-04 06:14:30 -0600139generated/config2help Config.in $KCONFIG_CONFIG > generated/help.h || exit 1
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500140
Rob Landleyfb98c1e2012-05-23 21:54:16 -0500141echo "Library probe..."
142
Rob Landley3a9241a2012-08-25 14:25:22 -0500143# We trust --as-needed to remove each library if we don't use any symbols
144# out of it, this loop is because the compiler has no way to ignore a library
145# that doesn't exist, so we have to detect and skip nonexistent libraries
146# for it.
147
Rob Landley91b360a2014-08-09 14:33:34 -0500148> generated/optlibs.dat
149for i in util crypt m resolv
150do
151 echo "int main(int argc, char *argv[]) {return 0;}" | \
152 ${CROSS_COMPILE}${CC} $CFLAGS -xc - -o /dev/null -Wl,--as-needed -l$i > /dev/null 2>/dev/null &&
153 echo -l$i >> generated/optlibs.dat
154done
Rob Landleyfb98c1e2012-05-23 21:54:16 -0500155
Rob Landley91b360a2014-08-09 14:33:34 -0500156echo -n "Compile toybox"
157[ ! -z "$V" ] && echo
158
159# Extract a list of toys/*/*.c files to compile from the data in $KCONFIG_CONFIG
160
Rob Landley7a07c6b2014-09-09 20:13:03 -0500161TOYFILES="$(egrep -l "TOY[(]($(sed -n 's/^CONFIG_\([^=]*\)=.*/\1/p' "$KCONFIG_CONFIG" | xargs | tr ' [A-Z]' '|[a-z]'))[ ,]" toys/*/*.c)"
Rob Landley28964802008-01-19 17:08:39 -0600162
Rob Landley1aa66ba2012-02-17 12:06:12 -0600163do_loudly()
164{
Rob Landley91b360a2014-08-09 14:33:34 -0500165 [ ! -z "$V" ] && echo "$@" || echo -n .
Rob Landley1aa66ba2012-02-17 12:06:12 -0600166 "$@"
167}
168
Rob Landley8aa87ab2014-09-11 20:50:10 -0500169BUILD="$(echo ${CROSS_COMPILE}${CC} $CFLAGS -I . $OPTIMIZE)"
170FILES="$(echo lib/*.c main.c $TOYFILES)"
171LINK="$(echo $LDOPTIMIZE -o toybox_unstripped -Wl,--as-needed $(cat generated/optlibs.dat))"
Rob Landley91b360a2014-08-09 14:33:34 -0500172
173# This is a parallel version of: do_loudly $BUILD $FILES $LINK || exit 1
174
Rob Landley8aa87ab2014-09-11 20:50:10 -0500175# Write a canned build line for use on crippled build machines.
176(
177 echo "#!/bin/sh"
178 echo
179 echo "BUILD=\"$BUILD\""
180 echo
181 echo "LINK=\"$LINK\""
182 echo
183 echo "FILES=\"$FILES\""
184 echo
185 echo '$BUILD $FILES $LINK'
186) > generated/build.sh && chmod +x generated/build.sh || echo 1
187
Rob Landley09af6a72014-09-11 00:04:37 -0500188rm -rf generated/obj && mkdir -p generated/obj || exit 1
Rob Landley5d16faa2014-08-24 22:42:47 -0500189PENDING=
Rob Landley91b360a2014-08-09 14:33:34 -0500190for i in $FILES
191do
Rob Landley7a07c6b2014-09-09 20:13:03 -0500192 # build each generated/obj/*.o file in parallel
Rob Landley91b360a2014-08-09 14:33:34 -0500193
194 X=${i/lib\//lib_}
195 X=${X##*/}
Rob Landley7a07c6b2014-09-09 20:13:03 -0500196 do_loudly $BUILD -c $i -o generated/obj/${X%%.c}.o &
Rob Landley91b360a2014-08-09 14:33:34 -0500197
198 # ratelimit to $CPUS many parallel jobs, detecting errors
199
200 while true
201 do
Rob Landley5d16faa2014-08-24 22:42:47 -0500202 PENDING="$(echo $PENDING $(jobs -rp) | tr ' ' '\n' | sort -u)"
Rob Landley9bb73ad2014-09-04 00:23:51 -0500203 [ $(echo -n "$PENDING" | wc -l) -lt "$CPUS" ] && break;
Rob Landley5d16faa2014-08-24 22:42:47 -0500204
Rob Landley658887a2014-08-31 22:07:43 -0500205 wait $(echo "$PENDING" | head -n 1) || exit 1
Rob Landley5d16faa2014-08-24 22:42:47 -0500206 PENDING="$(echo "$PENDING" | tail -n +2)"
Rob Landley91b360a2014-08-09 14:33:34 -0500207 done
208done
209
210# wait for all background jobs, detecting errors
211
Rob Landley5d16faa2014-08-24 22:42:47 -0500212for i in $PENDING
Rob Landley91b360a2014-08-09 14:33:34 -0500213do
214 wait $i || exit 1
215done
216
Rob Landley7a07c6b2014-09-09 20:13:03 -0500217do_loudly $BUILD generated/obj/*.o $LINK || exit 1
Rob Landley344c2672012-03-03 10:56:11 -0600218do_loudly ${CROSS_COMPILE}${STRIP} toybox_unstripped -o toybox || exit 1
Rob Landleyb704ad22009-12-13 00:12:26 -0600219# gcc 4.4's strip command is buggy, and doesn't set the executable bit on
220# its output the way SUSv4 suggests it do so.
Rob Landley1aa66ba2012-02-17 12:06:12 -0600221do_loudly chmod +x toybox || exit 1
Rob Landley91b360a2014-08-09 14:33:34 -0500222echo