blob: 502c8cd11edcebe5f2855141999d16bf2f41cdab [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 Landley28964802008-01-19 17:08:39 -06006source ./configure
7
Rob Landleyd04dc1f2013-08-30 01:53:31 -05008[ -z "$KCONFIG_CONFIG" ] && KCONFIG_CONFIG=".config"
9
Rob Landley91b360a2014-08-09 14:33:34 -050010if [ -z "$CPUS" ]
Rob Landley3a9241a2012-08-25 14:25:22 -050011then
Rob Landley91b360a2014-08-09 14:33:34 -050012 CPUS=$(echo /sys/devices/system/cpu/cpu[0-9]* | wc -w)
13 CPUS=$(($CPUS+1))
Rob Landley3a9241a2012-08-25 14:25:22 -050014fi
15
Rob Landley86cafe12014-01-03 18:23:09 -060016# Respond to V= by echoing command lines as well as running them
17do_loudly()
18{
19 [ ! -z "$V" ] && echo "$@"
20 "$@"
21}
22
Rob Landleyd04dc1f2013-08-30 01:53:31 -050023echo "Make generated/config.h from $KCONFIG_CONFIG."
Rob Landley28964802008-01-19 17:08:39 -060024
25# This long and roundabout sed invocation is to make old versions of sed happy.
26# New ones have '\n' so can replace one line with two without all the branches
27# and tedious mucking about with hold space.
28
Rob Landley1193a022009-01-30 16:10:55 -060029sed -n \
30 -e 's/^# CONFIG_\(.*\) is not set.*/\1/' \
31 -e 't notset' \
32 -e 's/^CONFIG_\(.*\)=y.*/\1/' \
33 -e 't isset' \
Rob Landley687bea52009-01-30 16:17:35 -060034 -e 's/^CONFIG_\([^=]*\)=\(.*\)/#define CFG_\1 \2/p' \
Rob Landley1193a022009-01-30 16:10:55 -060035 -e 'd' \
36 -e ':notset' \
37 -e 'h' \
38 -e 's/.*/#define CFG_& 0/p' \
39 -e 'g' \
40 -e 's/.*/#define USE_&(...)/p' \
41 -e 'd' \
42 -e ':isset' \
43 -e 'h' \
44 -e 's/.*/#define CFG_& 1/p' \
45 -e 'g' \
46 -e 's/.*/#define USE_&(...) __VA_ARGS__/p' \
Rob Landleyd04dc1f2013-08-30 01:53:31 -050047 $KCONFIG_CONFIG > generated/config.h || exit 1
Rob Landley28964802008-01-19 17:08:39 -060048
Rob Landleyc0e56ed2012-10-08 00:02:30 -050049
50echo "Extract configuration information from toys/*.c files..."
51scripts/genconfig.sh
52
53echo "Generate headers from toys/*/*.c..."
54
Rob Landley3a489e42013-06-22 14:23:06 -050055# Create a list of all the commands toybox can provide. Note that the first
56# entry is out of order on purpose (the toybox multiplexer command must be the
57# first element of the array). The rest must be sorted in alphabetical order
Rob Landleyc0e56ed2012-10-08 00:02:30 -050058# for fast binary search.
59
Rob Landley46c80692013-12-28 17:06:55 -060060echo -n "generated/newtoys.h "
Rob Landleyc0e56ed2012-10-08 00:02:30 -050061
Rob Landleyd04dc1f2013-08-30 01:53:31 -050062echo "USE_TOYBOX(NEWTOY(toybox, NULL, TOYFLAG_STAYROOT))" > generated/newtoys.h
Rob Landleyc0e56ed2012-10-08 00:02:30 -050063sed -n -e 's/^USE_[A-Z0-9_]*(/&/p' toys/*/*.c \
64 | sed 's/\(.*TOY(\)\([^,]*\),\(.*\)/\2 \1\2,\3/' | sort -k 1,1 \
65 | sed 's/[^ ]* //' >> generated/newtoys.h
Rob Landley75a18152013-09-22 03:37:39 -050066sed -n -e 's/.*(NEWTOY(\([^,]*\), *\(\("[^"]*"[^,]*\)*\),.*/#define OPTSTR_\1\t\2/p' \
Rob Landleydd2d2392013-08-12 01:48:27 -050067 generated/newtoys.h > generated/oldtoys.h
Rob Landleyc0e56ed2012-10-08 00:02:30 -050068
Rob Landley86cafe12014-01-03 18:23:09 -060069do_loudly $HOSTCC scripts/mkflags.c -o generated/mkflags || exit 1
Rob Landley6cf0a112012-11-26 14:14:29 -060070
Rob Landley46c80692013-12-28 17:06:55 -060071echo -n "generated/flags.h "
Rob Landley207cada2013-10-03 03:18:00 -050072
Rob Landleye36a9dd2014-02-23 20:11:06 -060073# Process config.h and newtoys.h to generate FLAG_x macros. Note we must
74# always #define the relevant macro, even when it's disabled, because we
75# allow multiple NEWTOY() in the same C file. (When disabled the FLAG is 0,
76# so flags&0 becomes a constant 0 allowing dead code elimination.)
77
Rob Landley207cada2013-10-03 03:18:00 -050078# Parse files through C preprocessor twice, once to get flags for current
79# .config and once to get flags for allyesconfig
80for I in A B
81do
82 (
83 # define macros and select header files with option string data
84
85 echo "#define NEWTOY(aa,bb,cc) aa $I bb"
86 echo '#define OLDTOY(...)'
87 if [ "$I" == A ]
88 then
89 cat generated/config.h
90 else
91 sed '/USE_.*([^)]*)$/s/$/ __VA_ARGS__/' generated/config.h
92 fi
93 cat generated/newtoys.h
94
95 # Run result through preprocessor, glue together " " gaps leftover from USE
96 # macros, delete comment lines, print any line with a quoted optstring,
97 # turn any non-quoted opstring (NULL or 0) into " " (because fscanf can't
Rob Landley170c3972014-02-28 20:46:16 -060098 # handle "" with nothing in it, and mkflags uses that).
Rob Landley207cada2013-10-03 03:18:00 -050099
Rob Landley73fff3b2013-10-23 02:52:01 -0500100 ) | ${CROSS_COMPILE}${CC} -E - | \
Rob Landley170c3972014-02-28 20:46:16 -0600101 sed -n -e 's/" *"//g;/^#/d;t clear;:clear;s/"/"/p;t;s/\( [AB] \).*/\1 " "/p'
Rob Landley207cada2013-10-03 03:18:00 -0500102
103# Sort resulting line pairs and glue them together into triplets of
104# command "flags" "allflags"
105# to feed into mkflags C program that outputs actual flag macros
Rob Landleye36a9dd2014-02-23 20:11:06 -0600106# If no pair (because command's disabled in config), use " " for flags
107# so allflags can define the appropriate zero macros.
Rob Landley207cada2013-10-03 03:18:00 -0500108
Rob Landleye36a9dd2014-02-23 20:11:06 -0600109done | 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 -0500110generated/mkflags > generated/flags.h || exit 1
Rob Landley6cf0a112012-11-26 14:14:29 -0600111
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500112# Extract global structure definitions and flag definitions from toys/*/*.c
113
114function getglobals()
115{
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500116 for i in toys/*/*.c
117 do
118 NAME="$(echo $i | sed 's@.*/\(.*\)\.c@\1@')"
Rob Landley207cada2013-10-03 03:18:00 -0500119 DATA="$(sed -n -e '/^GLOBALS(/,/^)/b got;b;:got' \
120 -e 's/^GLOBALS(/struct '"$NAME"'_data {/' \
121 -e 's/^)/};/' -e 'p' $i)"
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500122
Rob Landley207cada2013-10-03 03:18:00 -0500123 [ ! -z "$DATA" ] && echo -e "// $i\n\n$DATA\n"
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500124 done
125}
126
Rob Landley46c80692013-12-28 17:06:55 -0600127echo -n "generated/globals.h "
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500128
129GLOBSTRUCT="$(getglobals)"
130(
131 echo "$GLOBSTRUCT"
132 echo
133 echo "extern union global_union {"
134 echo "$GLOBSTRUCT" | sed -n 's/struct \(.*\)_data {/ struct \1_data \1;/p'
135 echo "} this;"
136) > generated/globals.h
137
138echo "generated/help.h"
Rob Landley86cafe12014-01-03 18:23:09 -0600139do_loudly $HOSTCC scripts/config2help.c -I . lib/xwrap.c lib/llist.c lib/lib.c \
140 -o generated/config2help && \
Rob Landley0e040df2014-02-04 06:14:30 -0600141generated/config2help Config.in $KCONFIG_CONFIG > generated/help.h || exit 1
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500142
Rob Landleyfb98c1e2012-05-23 21:54:16 -0500143echo "Library probe..."
144
Rob Landley3a9241a2012-08-25 14:25:22 -0500145# We trust --as-needed to remove each library if we don't use any symbols
146# out of it, this loop is because the compiler has no way to ignore a library
147# that doesn't exist, so we have to detect and skip nonexistent libraries
148# for it.
149
Rob Landley91b360a2014-08-09 14:33:34 -0500150> generated/optlibs.dat
151for i in util crypt m resolv
152do
153 echo "int main(int argc, char *argv[]) {return 0;}" | \
154 ${CROSS_COMPILE}${CC} $CFLAGS -xc - -o /dev/null -Wl,--as-needed -l$i > /dev/null 2>/dev/null &&
155 echo -l$i >> generated/optlibs.dat
156done
Rob Landleyfb98c1e2012-05-23 21:54:16 -0500157
Rob Landley91b360a2014-08-09 14:33:34 -0500158echo -n "Compile toybox"
159[ ! -z "$V" ] && echo
160
161# Extract a list of toys/*/*.c files to compile from the data in $KCONFIG_CONFIG
162
163# Get a list of C files in toys/* and wash it through sed to chop out the
164# filename, convert - to _, and glue the result together into a new regex
165# we we can feed to grep to match any one of them (whole word, not substring).
166
167TOYFILES="^$(ls toys/*/*.c | sed -n 's@^.*/\(.*\)\.c$@\1@;s/-/_/g;H;${g;s/\n//;s/\n/$|^/gp}')\$"
168
169# 1) Grab the XXX part of all CONFIG_XXX entries from KCONFIG
170# 2) Sort the list, keeping only one of each entry.
171# 3) Convert to lower case.
172# 4) Remove any config symbol not recognized as a filename from step 1.
173# 5) Add "toys/*/" prefix and ".c" suffix.
174
175TOYFILES=$(sed -nre 's/^CONFIG_(.*)=y/\1/p' < "$KCONFIG_CONFIG" \
176 | sort -u | tr A-Z a-z | grep -E "$TOYFILES" | sed 's@\(.*\)@toys/\*/\1.c@')
Rob Landley28964802008-01-19 17:08:39 -0600177
Rob Landley1aa66ba2012-02-17 12:06:12 -0600178do_loudly()
179{
Rob Landley91b360a2014-08-09 14:33:34 -0500180 [ ! -z "$V" ] && echo "$@" || echo -n .
Rob Landley1aa66ba2012-02-17 12:06:12 -0600181 "$@"
182}
183
Rob Landley91b360a2014-08-09 14:33:34 -0500184BUILD="${CROSS_COMPILE}${CC} $CFLAGS -I . $OPTIMIZE"
185FILES="$(ls lib/*.c) main.c $TOYFILES"
186LINK="-o toybox_unstripped -Wl,--as-needed $(cat generated/optlibs.dat)"
187
188# This is a parallel version of: do_loudly $BUILD $FILES $LINK || exit 1
189
190rm -f generated/*.o
191for i in $FILES
192do
193 # build each generated/*.o file in parallel
194
195 X=${i/lib\//lib_}
196 X=${X##*/}
197 do_loudly $BUILD -c $i -o generated/${X%%.c}.o &
198
199 # ratelimit to $CPUS many parallel jobs, detecting errors
200
201 while true
202 do
203 [ $(jobs -rp | wc -w) -lt "$CPUS" ] && break;
204 wait $(jobs -p | head -n 1) || exit 1
205 done
206done
207
208# wait for all background jobs, detecting errors
209
210for i in $(jobs -p)
211do
212 wait $i || exit 1
213done
214
215do_loudly $BUILD generated/*.o $LINK || exit 1
Rob Landley344c2672012-03-03 10:56:11 -0600216do_loudly ${CROSS_COMPILE}${STRIP} toybox_unstripped -o toybox || exit 1
Rob Landleyb704ad22009-12-13 00:12:26 -0600217# gcc 4.4's strip command is buggy, and doesn't set the executable bit on
218# its output the way SUSv4 suggests it do so.
Rob Landley1aa66ba2012-02-17 12:06:12 -0600219do_loudly chmod +x toybox || exit 1
Rob Landley91b360a2014-08-09 14:33:34 -0500220echo