blob: 38b8f07159b99916216a388a66043b15dc3c0a6b [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 Landley5d16faa2014-08-24 22:42:47 -050010[ -z "$CPUS" ] && CPUS=$(($(echo /sys/devices/system/cpu/cpu[0-9]* | wc -w)+1))
Rob Landley3a9241a2012-08-25 14:25:22 -050011
Rob Landley86cafe12014-01-03 18:23:09 -060012# Respond to V= by echoing command lines as well as running them
13do_loudly()
14{
15 [ ! -z "$V" ] && echo "$@"
16 "$@"
17}
18
Rob Landleyd04dc1f2013-08-30 01:53:31 -050019echo "Make generated/config.h from $KCONFIG_CONFIG."
Rob Landley28964802008-01-19 17:08:39 -060020
21# This long and roundabout sed invocation is to make old versions of sed happy.
22# New ones have '\n' so can replace one line with two without all the branches
23# and tedious mucking about with hold space.
24
Rob Landley1193a022009-01-30 16:10:55 -060025sed -n \
26 -e 's/^# CONFIG_\(.*\) is not set.*/\1/' \
27 -e 't notset' \
28 -e 's/^CONFIG_\(.*\)=y.*/\1/' \
29 -e 't isset' \
Rob Landley687bea52009-01-30 16:17:35 -060030 -e 's/^CONFIG_\([^=]*\)=\(.*\)/#define CFG_\1 \2/p' \
Rob Landley1193a022009-01-30 16:10:55 -060031 -e 'd' \
32 -e ':notset' \
33 -e 'h' \
34 -e 's/.*/#define CFG_& 0/p' \
35 -e 'g' \
36 -e 's/.*/#define USE_&(...)/p' \
37 -e 'd' \
38 -e ':isset' \
39 -e 'h' \
40 -e 's/.*/#define CFG_& 1/p' \
41 -e 'g' \
42 -e 's/.*/#define USE_&(...) __VA_ARGS__/p' \
Rob Landleyd04dc1f2013-08-30 01:53:31 -050043 $KCONFIG_CONFIG > generated/config.h || exit 1
Rob Landley28964802008-01-19 17:08:39 -060044
Rob Landleyc0e56ed2012-10-08 00:02:30 -050045
46echo "Extract configuration information from toys/*.c files..."
47scripts/genconfig.sh
48
49echo "Generate headers from toys/*/*.c..."
50
Rob Landley3a489e42013-06-22 14:23:06 -050051# Create a list of all the commands toybox can provide. Note that the first
52# entry is out of order on purpose (the toybox multiplexer command must be the
53# first element of the array). The rest must be sorted in alphabetical order
Rob Landleyc0e56ed2012-10-08 00:02:30 -050054# for fast binary search.
55
Rob Landley46c80692013-12-28 17:06:55 -060056echo -n "generated/newtoys.h "
Rob Landleyc0e56ed2012-10-08 00:02:30 -050057
Rob Landleyd04dc1f2013-08-30 01:53:31 -050058echo "USE_TOYBOX(NEWTOY(toybox, NULL, TOYFLAG_STAYROOT))" > generated/newtoys.h
Rob Landleyc0e56ed2012-10-08 00:02:30 -050059sed -n -e 's/^USE_[A-Z0-9_]*(/&/p' toys/*/*.c \
60 | sed 's/\(.*TOY(\)\([^,]*\),\(.*\)/\2 \1\2,\3/' | sort -k 1,1 \
61 | sed 's/[^ ]* //' >> generated/newtoys.h
Rob Landley75a18152013-09-22 03:37:39 -050062sed -n -e 's/.*(NEWTOY(\([^,]*\), *\(\("[^"]*"[^,]*\)*\),.*/#define OPTSTR_\1\t\2/p' \
Rob Landleydd2d2392013-08-12 01:48:27 -050063 generated/newtoys.h > generated/oldtoys.h
Rob Landleyc0e56ed2012-10-08 00:02:30 -050064
Rob Landley86cafe12014-01-03 18:23:09 -060065do_loudly $HOSTCC scripts/mkflags.c -o generated/mkflags || exit 1
Rob Landley6cf0a112012-11-26 14:14:29 -060066
Rob Landley46c80692013-12-28 17:06:55 -060067echo -n "generated/flags.h "
Rob Landley207cada2013-10-03 03:18:00 -050068
Rob Landleye36a9dd2014-02-23 20:11:06 -060069# Process config.h and newtoys.h to generate FLAG_x macros. Note we must
70# always #define the relevant macro, even when it's disabled, because we
71# allow multiple NEWTOY() in the same C file. (When disabled the FLAG is 0,
72# so flags&0 becomes a constant 0 allowing dead code elimination.)
73
Rob Landley207cada2013-10-03 03:18:00 -050074# Parse files through C preprocessor twice, once to get flags for current
75# .config and once to get flags for allyesconfig
76for I in A B
77do
78 (
79 # define macros and select header files with option string data
80
81 echo "#define NEWTOY(aa,bb,cc) aa $I bb"
82 echo '#define OLDTOY(...)'
83 if [ "$I" == A ]
84 then
85 cat generated/config.h
86 else
87 sed '/USE_.*([^)]*)$/s/$/ __VA_ARGS__/' generated/config.h
88 fi
89 cat generated/newtoys.h
90
91 # Run result through preprocessor, glue together " " gaps leftover from USE
92 # macros, delete comment lines, print any line with a quoted optstring,
93 # turn any non-quoted opstring (NULL or 0) into " " (because fscanf can't
Rob Landley170c3972014-02-28 20:46:16 -060094 # handle "" with nothing in it, and mkflags uses that).
Rob Landley207cada2013-10-03 03:18:00 -050095
Rob Landley73fff3b2013-10-23 02:52:01 -050096 ) | ${CROSS_COMPILE}${CC} -E - | \
Rob Landley170c3972014-02-28 20:46:16 -060097 sed -n -e 's/" *"//g;/^#/d;t clear;:clear;s/"/"/p;t;s/\( [AB] \).*/\1 " "/p'
Rob Landley207cada2013-10-03 03:18:00 -050098
99# Sort resulting line pairs and glue them together into triplets of
100# command "flags" "allflags"
101# to feed into mkflags C program that outputs actual flag macros
Rob Landleye36a9dd2014-02-23 20:11:06 -0600102# If no pair (because command's disabled in config), use " " for flags
103# so allflags can define the appropriate zero macros.
Rob Landley207cada2013-10-03 03:18:00 -0500104
Rob Landleye36a9dd2014-02-23 20:11:06 -0600105done | 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 -0500106generated/mkflags > generated/flags.h || exit 1
Rob Landley6cf0a112012-11-26 14:14:29 -0600107
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500108# Extract global structure definitions and flag definitions from toys/*/*.c
109
110function getglobals()
111{
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500112 for i in toys/*/*.c
113 do
114 NAME="$(echo $i | sed 's@.*/\(.*\)\.c@\1@')"
Rob Landley207cada2013-10-03 03:18:00 -0500115 DATA="$(sed -n -e '/^GLOBALS(/,/^)/b got;b;:got' \
116 -e 's/^GLOBALS(/struct '"$NAME"'_data {/' \
117 -e 's/^)/};/' -e 'p' $i)"
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500118
Rob Landley207cada2013-10-03 03:18:00 -0500119 [ ! -z "$DATA" ] && echo -e "// $i\n\n$DATA\n"
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500120 done
121}
122
Rob Landley46c80692013-12-28 17:06:55 -0600123echo -n "generated/globals.h "
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500124
125GLOBSTRUCT="$(getglobals)"
126(
127 echo "$GLOBSTRUCT"
128 echo
129 echo "extern union global_union {"
130 echo "$GLOBSTRUCT" | sed -n 's/struct \(.*\)_data {/ struct \1_data \1;/p'
131 echo "} this;"
132) > generated/globals.h
133
134echo "generated/help.h"
Rob Landley86cafe12014-01-03 18:23:09 -0600135do_loudly $HOSTCC scripts/config2help.c -I . lib/xwrap.c lib/llist.c lib/lib.c \
136 -o generated/config2help && \
Rob Landley0e040df2014-02-04 06:14:30 -0600137generated/config2help Config.in $KCONFIG_CONFIG > generated/help.h || exit 1
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500138
Rob Landleyfb98c1e2012-05-23 21:54:16 -0500139echo "Library probe..."
140
Rob Landley3a9241a2012-08-25 14:25:22 -0500141# We trust --as-needed to remove each library if we don't use any symbols
142# out of it, this loop is because the compiler has no way to ignore a library
143# that doesn't exist, so we have to detect and skip nonexistent libraries
144# for it.
145
Rob Landley91b360a2014-08-09 14:33:34 -0500146> generated/optlibs.dat
147for i in util crypt m resolv
148do
149 echo "int main(int argc, char *argv[]) {return 0;}" | \
150 ${CROSS_COMPILE}${CC} $CFLAGS -xc - -o /dev/null -Wl,--as-needed -l$i > /dev/null 2>/dev/null &&
151 echo -l$i >> generated/optlibs.dat
152done
Rob Landleyfb98c1e2012-05-23 21:54:16 -0500153
Rob Landley91b360a2014-08-09 14:33:34 -0500154echo -n "Compile toybox"
155[ ! -z "$V" ] && echo
156
157# Extract a list of toys/*/*.c files to compile from the data in $KCONFIG_CONFIG
158
159# Get a list of C files in toys/* and wash it through sed to chop out the
160# filename, convert - to _, and glue the result together into a new regex
161# we we can feed to grep to match any one of them (whole word, not substring).
162
163TOYFILES="^$(ls toys/*/*.c | sed -n 's@^.*/\(.*\)\.c$@\1@;s/-/_/g;H;${g;s/\n//;s/\n/$|^/gp}')\$"
164
165# 1) Grab the XXX part of all CONFIG_XXX entries from KCONFIG
166# 2) Sort the list, keeping only one of each entry.
167# 3) Convert to lower case.
168# 4) Remove any config symbol not recognized as a filename from step 1.
169# 5) Add "toys/*/" prefix and ".c" suffix.
170
171TOYFILES=$(sed -nre 's/^CONFIG_(.*)=y/\1/p' < "$KCONFIG_CONFIG" \
172 | sort -u | tr A-Z a-z | grep -E "$TOYFILES" | sed 's@\(.*\)@toys/\*/\1.c@')
Rob Landley28964802008-01-19 17:08:39 -0600173
Rob Landley1aa66ba2012-02-17 12:06:12 -0600174do_loudly()
175{
Rob Landley91b360a2014-08-09 14:33:34 -0500176 [ ! -z "$V" ] && echo "$@" || echo -n .
Rob Landley1aa66ba2012-02-17 12:06:12 -0600177 "$@"
178}
179
Rob Landley91b360a2014-08-09 14:33:34 -0500180BUILD="${CROSS_COMPILE}${CC} $CFLAGS -I . $OPTIMIZE"
181FILES="$(ls lib/*.c) main.c $TOYFILES"
182LINK="-o toybox_unstripped -Wl,--as-needed $(cat generated/optlibs.dat)"
183
184# This is a parallel version of: do_loudly $BUILD $FILES $LINK || exit 1
185
186rm -f generated/*.o
Rob Landley5d16faa2014-08-24 22:42:47 -0500187PENDING=
Rob Landley91b360a2014-08-09 14:33:34 -0500188for i in $FILES
189do
190 # build each generated/*.o file in parallel
191
192 X=${i/lib\//lib_}
193 X=${X##*/}
194 do_loudly $BUILD -c $i -o generated/${X%%.c}.o &
195
196 # ratelimit to $CPUS many parallel jobs, detecting errors
197
198 while true
199 do
Rob Landley5d16faa2014-08-24 22:42:47 -0500200 PENDING="$(echo $PENDING $(jobs -rp) | tr ' ' '\n' | sort -u)"
201 [ $(echo $PENDING | wc -l) -lt "$CPUS" ] && break;
202
203 wait $(echo $PENDING | head -n 1) || exit 1
204 PENDING="$(echo "$PENDING" | tail -n +2)"
Rob Landley91b360a2014-08-09 14:33:34 -0500205 done
206done
207
208# wait for all background jobs, detecting errors
209
Rob Landley5d16faa2014-08-24 22:42:47 -0500210for i in $PENDING
Rob Landley91b360a2014-08-09 14:33:34 -0500211do
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