blob: dab520611ffdeb09d6572e016c9c4f6ded80d065 [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
18do_loudly()
19{
20 [ ! -z "$V" ] && echo "$@"
21 "$@"
22}
23
Rob Landley94a46032014-09-20 14:20:28 -050024mkdir -p generated
Rob Landleyd04dc1f2013-08-30 01:53:31 -050025echo "Make generated/config.h from $KCONFIG_CONFIG."
Rob Landley28964802008-01-19 17:08:39 -060026
27# This long and roundabout sed invocation is to make old versions of sed happy.
28# New ones have '\n' so can replace one line with two without all the branches
29# and tedious mucking about with hold space.
30
Rob Landley1193a022009-01-30 16:10:55 -060031sed -n \
32 -e 's/^# CONFIG_\(.*\) is not set.*/\1/' \
33 -e 't notset' \
34 -e 's/^CONFIG_\(.*\)=y.*/\1/' \
35 -e 't isset' \
Rob Landley687bea52009-01-30 16:17:35 -060036 -e 's/^CONFIG_\([^=]*\)=\(.*\)/#define CFG_\1 \2/p' \
Rob Landley1193a022009-01-30 16:10:55 -060037 -e 'd' \
38 -e ':notset' \
39 -e 'h' \
40 -e 's/.*/#define CFG_& 0/p' \
41 -e 'g' \
42 -e 's/.*/#define USE_&(...)/p' \
43 -e 'd' \
44 -e ':isset' \
45 -e 'h' \
46 -e 's/.*/#define CFG_& 1/p' \
47 -e 'g' \
48 -e 's/.*/#define USE_&(...) __VA_ARGS__/p' \
Rob Landleyd04dc1f2013-08-30 01:53:31 -050049 $KCONFIG_CONFIG > generated/config.h || exit 1
Rob Landley28964802008-01-19 17:08:39 -060050
Rob Landleyc0e56ed2012-10-08 00:02:30 -050051
52echo "Extract configuration information from toys/*.c files..."
53scripts/genconfig.sh
54
55echo "Generate headers from toys/*/*.c..."
56
Rob Landley3a489e42013-06-22 14:23:06 -050057# Create a list of all the commands toybox can provide. Note that the first
58# entry is out of order on purpose (the toybox multiplexer command must be the
59# first element of the array). The rest must be sorted in alphabetical order
Rob Landleyc0e56ed2012-10-08 00:02:30 -050060# for fast binary search.
61
Rob Landley46c80692013-12-28 17:06:55 -060062echo -n "generated/newtoys.h "
Rob Landleyc0e56ed2012-10-08 00:02:30 -050063
Rob Landleyd04dc1f2013-08-30 01:53:31 -050064echo "USE_TOYBOX(NEWTOY(toybox, NULL, TOYFLAG_STAYROOT))" > generated/newtoys.h
Rob Landleyc0e56ed2012-10-08 00:02:30 -050065sed -n -e 's/^USE_[A-Z0-9_]*(/&/p' toys/*/*.c \
66 | sed 's/\(.*TOY(\)\([^,]*\),\(.*\)/\2 \1\2,\3/' | sort -k 1,1 \
67 | sed 's/[^ ]* //' >> generated/newtoys.h
Rob Landley75a18152013-09-22 03:37:39 -050068sed -n -e 's/.*(NEWTOY(\([^,]*\), *\(\("[^"]*"[^,]*\)*\),.*/#define OPTSTR_\1\t\2/p' \
Rob Landleydd2d2392013-08-12 01:48:27 -050069 generated/newtoys.h > generated/oldtoys.h
Rob Landleyc0e56ed2012-10-08 00:02:30 -050070
Rob Landley86cafe12014-01-03 18:23:09 -060071do_loudly $HOSTCC scripts/mkflags.c -o generated/mkflags || exit 1
Rob Landley6cf0a112012-11-26 14:14:29 -060072
Rob Landley46c80692013-12-28 17:06:55 -060073echo -n "generated/flags.h "
Rob Landley207cada2013-10-03 03:18:00 -050074
Rob Landleye36a9dd2014-02-23 20:11:06 -060075# Process config.h and newtoys.h to generate FLAG_x macros. Note we must
76# always #define the relevant macro, even when it's disabled, because we
77# allow multiple NEWTOY() in the same C file. (When disabled the FLAG is 0,
78# so flags&0 becomes a constant 0 allowing dead code elimination.)
79
Rob Landley207cada2013-10-03 03:18:00 -050080# Parse files through C preprocessor twice, once to get flags for current
81# .config and once to get flags for allyesconfig
82for I in A B
83do
84 (
85 # define macros and select header files with option string data
86
87 echo "#define NEWTOY(aa,bb,cc) aa $I bb"
88 echo '#define OLDTOY(...)'
89 if [ "$I" == A ]
90 then
91 cat generated/config.h
92 else
93 sed '/USE_.*([^)]*)$/s/$/ __VA_ARGS__/' generated/config.h
94 fi
95 cat generated/newtoys.h
96
97 # Run result through preprocessor, glue together " " gaps leftover from USE
98 # macros, delete comment lines, print any line with a quoted optstring,
99 # turn any non-quoted opstring (NULL or 0) into " " (because fscanf can't
Rob Landley170c3972014-02-28 20:46:16 -0600100 # handle "" with nothing in it, and mkflags uses that).
Rob Landley207cada2013-10-03 03:18:00 -0500101
Rob Landley73fff3b2013-10-23 02:52:01 -0500102 ) | ${CROSS_COMPILE}${CC} -E - | \
Rob Landley170c3972014-02-28 20:46:16 -0600103 sed -n -e 's/" *"//g;/^#/d;t clear;:clear;s/"/"/p;t;s/\( [AB] \).*/\1 " "/p'
Rob Landley207cada2013-10-03 03:18:00 -0500104
105# Sort resulting line pairs and glue them together into triplets of
106# command "flags" "allflags"
107# to feed into mkflags C program that outputs actual flag macros
Rob Landleye36a9dd2014-02-23 20:11:06 -0600108# If no pair (because command's disabled in config), use " " for flags
109# so allflags can define the appropriate zero macros.
Rob Landley207cada2013-10-03 03:18:00 -0500110
Rob Landleye36a9dd2014-02-23 20:11:06 -0600111done | 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 -0500112generated/mkflags > generated/flags.h || exit 1
Rob Landley6cf0a112012-11-26 14:14:29 -0600113
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500114# Extract global structure definitions and flag definitions from toys/*/*.c
115
116function getglobals()
117{
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500118 for i in toys/*/*.c
119 do
120 NAME="$(echo $i | sed 's@.*/\(.*\)\.c@\1@')"
Rob Landley207cada2013-10-03 03:18:00 -0500121 DATA="$(sed -n -e '/^GLOBALS(/,/^)/b got;b;:got' \
122 -e 's/^GLOBALS(/struct '"$NAME"'_data {/' \
123 -e 's/^)/};/' -e 'p' $i)"
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500124
Rob Landley207cada2013-10-03 03:18:00 -0500125 [ ! -z "$DATA" ] && echo -e "// $i\n\n$DATA\n"
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500126 done
127}
128
Rob Landley46c80692013-12-28 17:06:55 -0600129echo -n "generated/globals.h "
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500130
131GLOBSTRUCT="$(getglobals)"
132(
133 echo "$GLOBSTRUCT"
134 echo
135 echo "extern union global_union {"
136 echo "$GLOBSTRUCT" | sed -n 's/struct \(.*\)_data {/ struct \1_data \1;/p'
137 echo "} this;"
138) > generated/globals.h
139
140echo "generated/help.h"
Rob Landley86cafe12014-01-03 18:23:09 -0600141do_loudly $HOSTCC scripts/config2help.c -I . lib/xwrap.c lib/llist.c lib/lib.c \
142 -o generated/config2help && \
Rob Landley0e040df2014-02-04 06:14:30 -0600143generated/config2help Config.in $KCONFIG_CONFIG > generated/help.h || exit 1
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500144
Rob Landleyd3df4232014-09-20 13:07:53 -0500145echo -n "Library probe"
Rob Landleyfb98c1e2012-05-23 21:54:16 -0500146
Rob Landley3a9241a2012-08-25 14:25:22 -0500147# We trust --as-needed to remove each library if we don't use any symbols
148# out of it, this loop is because the compiler has no way to ignore a library
149# that doesn't exist, so we have to detect and skip nonexistent libraries
150# for it.
151
Rob Landley91b360a2014-08-09 14:33:34 -0500152> generated/optlibs.dat
153for i in util crypt m resolv
154do
155 echo "int main(int argc, char *argv[]) {return 0;}" | \
156 ${CROSS_COMPILE}${CC} $CFLAGS -xc - -o /dev/null -Wl,--as-needed -l$i > /dev/null 2>/dev/null &&
157 echo -l$i >> generated/optlibs.dat
Rob Landleyd3df4232014-09-20 13:07:53 -0500158 echo -n .
Rob Landley91b360a2014-08-09 14:33:34 -0500159done
Rob Landleyd3df4232014-09-20 13:07:53 -0500160echo
Rob Landleyfb98c1e2012-05-23 21:54:16 -0500161
Rob Landley91b360a2014-08-09 14:33:34 -0500162echo -n "Compile toybox"
163[ ! -z "$V" ] && echo
164
165# Extract a list of toys/*/*.c files to compile from the data in $KCONFIG_CONFIG
166
Rob Landley7a07c6b2014-09-09 20:13:03 -0500167TOYFILES="$(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 -0600168
Rob Landley1aa66ba2012-02-17 12:06:12 -0600169do_loudly()
170{
Rob Landley91b360a2014-08-09 14:33:34 -0500171 [ ! -z "$V" ] && echo "$@" || echo -n .
Rob Landley1aa66ba2012-02-17 12:06:12 -0600172 "$@"
173}
174
Rob Landley8aa87ab2014-09-11 20:50:10 -0500175BUILD="$(echo ${CROSS_COMPILE}${CC} $CFLAGS -I . $OPTIMIZE)"
176FILES="$(echo lib/*.c main.c $TOYFILES)"
177LINK="$(echo $LDOPTIMIZE -o toybox_unstripped -Wl,--as-needed $(cat generated/optlibs.dat))"
Rob Landley91b360a2014-08-09 14:33:34 -0500178
179# This is a parallel version of: do_loudly $BUILD $FILES $LINK || exit 1
180
Rob Landley8aa87ab2014-09-11 20:50:10 -0500181# Write a canned build line for use on crippled build machines.
182(
183 echo "#!/bin/sh"
184 echo
185 echo "BUILD=\"$BUILD\""
186 echo
187 echo "LINK=\"$LINK\""
188 echo
189 echo "FILES=\"$FILES\""
190 echo
191 echo '$BUILD $FILES $LINK'
192) > generated/build.sh && chmod +x generated/build.sh || echo 1
193
Rob Landley09af6a72014-09-11 00:04:37 -0500194rm -rf generated/obj && mkdir -p generated/obj || exit 1
Rob Landley5d16faa2014-08-24 22:42:47 -0500195PENDING=
Rob Landley91b360a2014-08-09 14:33:34 -0500196for i in $FILES
197do
Rob Landley7a07c6b2014-09-09 20:13:03 -0500198 # build each generated/obj/*.o file in parallel
Rob Landley91b360a2014-08-09 14:33:34 -0500199
200 X=${i/lib\//lib_}
201 X=${X##*/}
Rob Landley7a07c6b2014-09-09 20:13:03 -0500202 do_loudly $BUILD -c $i -o generated/obj/${X%%.c}.o &
Rob Landley91b360a2014-08-09 14:33:34 -0500203
204 # ratelimit to $CPUS many parallel jobs, detecting errors
205
206 while true
207 do
Rob Landley5d16faa2014-08-24 22:42:47 -0500208 PENDING="$(echo $PENDING $(jobs -rp) | tr ' ' '\n' | sort -u)"
Rob Landley9bb73ad2014-09-04 00:23:51 -0500209 [ $(echo -n "$PENDING" | wc -l) -lt "$CPUS" ] && break;
Rob Landley5d16faa2014-08-24 22:42:47 -0500210
Rob Landley658887a2014-08-31 22:07:43 -0500211 wait $(echo "$PENDING" | head -n 1) || exit 1
Rob Landley5d16faa2014-08-24 22:42:47 -0500212 PENDING="$(echo "$PENDING" | tail -n +2)"
Rob Landley91b360a2014-08-09 14:33:34 -0500213 done
214done
215
216# wait for all background jobs, detecting errors
217
Rob Landley5d16faa2014-08-24 22:42:47 -0500218for i in $PENDING
Rob Landley91b360a2014-08-09 14:33:34 -0500219do
220 wait $i || exit 1
221done
222
Rob Landley7a07c6b2014-09-09 20:13:03 -0500223do_loudly $BUILD generated/obj/*.o $LINK || exit 1
Rob Landley344c2672012-03-03 10:56:11 -0600224do_loudly ${CROSS_COMPILE}${STRIP} toybox_unstripped -o toybox || exit 1
Rob Landleyb704ad22009-12-13 00:12:26 -0600225# gcc 4.4's strip command is buggy, and doesn't set the executable bit on
226# its output the way SUSv4 suggests it do so.
Rob Landley1aa66ba2012-02-17 12:06:12 -0600227do_loudly chmod +x toybox || exit 1
Rob Landley91b360a2014-08-09 14:33:34 -0500228echo