blob: 66fdad3b6bb36962b5e8a817165dad8a247ad76a [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 Landley28964802008-01-19 17:08:39 -06007source ./configure
8
Rob Landleyd04dc1f2013-08-30 01:53:31 -05009[ -z "$KCONFIG_CONFIG" ] && KCONFIG_CONFIG=".config"
10
Rob Landley5d16faa2014-08-24 22:42:47 -050011[ -z "$CPUS" ] && CPUS=$(($(echo /sys/devices/system/cpu/cpu[0-9]* | wc -w)+1))
Rob Landley3a9241a2012-08-25 14:25:22 -050012
Rob Landley86cafe12014-01-03 18:23:09 -060013# Respond to V= by echoing command lines as well as running them
14do_loudly()
15{
16 [ ! -z "$V" ] && echo "$@"
17 "$@"
18}
19
Rob Landleyd04dc1f2013-08-30 01:53:31 -050020echo "Make generated/config.h from $KCONFIG_CONFIG."
Rob Landley28964802008-01-19 17:08:39 -060021
22# This long and roundabout sed invocation is to make old versions of sed happy.
23# New ones have '\n' so can replace one line with two without all the branches
24# and tedious mucking about with hold space.
25
Rob Landley1193a022009-01-30 16:10:55 -060026sed -n \
27 -e 's/^# CONFIG_\(.*\) is not set.*/\1/' \
28 -e 't notset' \
29 -e 's/^CONFIG_\(.*\)=y.*/\1/' \
30 -e 't isset' \
Rob Landley687bea52009-01-30 16:17:35 -060031 -e 's/^CONFIG_\([^=]*\)=\(.*\)/#define CFG_\1 \2/p' \
Rob Landley1193a022009-01-30 16:10:55 -060032 -e 'd' \
33 -e ':notset' \
34 -e 'h' \
35 -e 's/.*/#define CFG_& 0/p' \
36 -e 'g' \
37 -e 's/.*/#define USE_&(...)/p' \
38 -e 'd' \
39 -e ':isset' \
40 -e 'h' \
41 -e 's/.*/#define CFG_& 1/p' \
42 -e 'g' \
43 -e 's/.*/#define USE_&(...) __VA_ARGS__/p' \
Rob Landleyd04dc1f2013-08-30 01:53:31 -050044 $KCONFIG_CONFIG > generated/config.h || exit 1
Rob Landley28964802008-01-19 17:08:39 -060045
Rob Landleyc0e56ed2012-10-08 00:02:30 -050046
47echo "Extract configuration information from toys/*.c files..."
48scripts/genconfig.sh
49
50echo "Generate headers from toys/*/*.c..."
51
Rob Landley3a489e42013-06-22 14:23:06 -050052# Create a list of all the commands toybox can provide. Note that the first
53# entry is out of order on purpose (the toybox multiplexer command must be the
54# first element of the array). The rest must be sorted in alphabetical order
Rob Landleyc0e56ed2012-10-08 00:02:30 -050055# for fast binary search.
56
Rob Landley46c80692013-12-28 17:06:55 -060057echo -n "generated/newtoys.h "
Rob Landleyc0e56ed2012-10-08 00:02:30 -050058
Rob Landleyd04dc1f2013-08-30 01:53:31 -050059echo "USE_TOYBOX(NEWTOY(toybox, NULL, TOYFLAG_STAYROOT))" > generated/newtoys.h
Rob Landleyc0e56ed2012-10-08 00:02:30 -050060sed -n -e 's/^USE_[A-Z0-9_]*(/&/p' toys/*/*.c \
61 | sed 's/\(.*TOY(\)\([^,]*\),\(.*\)/\2 \1\2,\3/' | sort -k 1,1 \
62 | sed 's/[^ ]* //' >> generated/newtoys.h
Rob Landley75a18152013-09-22 03:37:39 -050063sed -n -e 's/.*(NEWTOY(\([^,]*\), *\(\("[^"]*"[^,]*\)*\),.*/#define OPTSTR_\1\t\2/p' \
Rob Landleydd2d2392013-08-12 01:48:27 -050064 generated/newtoys.h > generated/oldtoys.h
Rob Landleyc0e56ed2012-10-08 00:02:30 -050065
Rob Landley86cafe12014-01-03 18:23:09 -060066do_loudly $HOSTCC scripts/mkflags.c -o generated/mkflags || exit 1
Rob Landley6cf0a112012-11-26 14:14:29 -060067
Rob Landley46c80692013-12-28 17:06:55 -060068echo -n "generated/flags.h "
Rob Landley207cada2013-10-03 03:18:00 -050069
Rob Landleye36a9dd2014-02-23 20:11:06 -060070# Process config.h and newtoys.h to generate FLAG_x macros. Note we must
71# always #define the relevant macro, even when it's disabled, because we
72# allow multiple NEWTOY() in the same C file. (When disabled the FLAG is 0,
73# so flags&0 becomes a constant 0 allowing dead code elimination.)
74
Rob Landley207cada2013-10-03 03:18:00 -050075# Parse files through C preprocessor twice, once to get flags for current
76# .config and once to get flags for allyesconfig
77for I in A B
78do
79 (
80 # define macros and select header files with option string data
81
82 echo "#define NEWTOY(aa,bb,cc) aa $I bb"
83 echo '#define OLDTOY(...)'
84 if [ "$I" == A ]
85 then
86 cat generated/config.h
87 else
88 sed '/USE_.*([^)]*)$/s/$/ __VA_ARGS__/' generated/config.h
89 fi
90 cat generated/newtoys.h
91
92 # Run result through preprocessor, glue together " " gaps leftover from USE
93 # macros, delete comment lines, print any line with a quoted optstring,
94 # turn any non-quoted opstring (NULL or 0) into " " (because fscanf can't
Rob Landley170c3972014-02-28 20:46:16 -060095 # handle "" with nothing in it, and mkflags uses that).
Rob Landley207cada2013-10-03 03:18:00 -050096
Rob Landley73fff3b2013-10-23 02:52:01 -050097 ) | ${CROSS_COMPILE}${CC} -E - | \
Rob Landley170c3972014-02-28 20:46:16 -060098 sed -n -e 's/" *"//g;/^#/d;t clear;:clear;s/"/"/p;t;s/\( [AB] \).*/\1 " "/p'
Rob Landley207cada2013-10-03 03:18:00 -050099
100# Sort resulting line pairs and glue them together into triplets of
101# command "flags" "allflags"
102# to feed into mkflags C program that outputs actual flag macros
Rob Landleye36a9dd2014-02-23 20:11:06 -0600103# If no pair (because command's disabled in config), use " " for flags
104# so allflags can define the appropriate zero macros.
Rob Landley207cada2013-10-03 03:18:00 -0500105
Rob Landleye36a9dd2014-02-23 20:11:06 -0600106done | 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 -0500107generated/mkflags > generated/flags.h || exit 1
Rob Landley6cf0a112012-11-26 14:14:29 -0600108
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500109# Extract global structure definitions and flag definitions from toys/*/*.c
110
111function getglobals()
112{
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500113 for i in toys/*/*.c
114 do
115 NAME="$(echo $i | sed 's@.*/\(.*\)\.c@\1@')"
Rob Landley207cada2013-10-03 03:18:00 -0500116 DATA="$(sed -n -e '/^GLOBALS(/,/^)/b got;b;:got' \
117 -e 's/^GLOBALS(/struct '"$NAME"'_data {/' \
118 -e 's/^)/};/' -e 'p' $i)"
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500119
Rob Landley207cada2013-10-03 03:18:00 -0500120 [ ! -z "$DATA" ] && echo -e "// $i\n\n$DATA\n"
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500121 done
122}
123
Rob Landley46c80692013-12-28 17:06:55 -0600124echo -n "generated/globals.h "
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500125
126GLOBSTRUCT="$(getglobals)"
127(
128 echo "$GLOBSTRUCT"
129 echo
130 echo "extern union global_union {"
131 echo "$GLOBSTRUCT" | sed -n 's/struct \(.*\)_data {/ struct \1_data \1;/p'
132 echo "} this;"
133) > generated/globals.h
134
135echo "generated/help.h"
Rob Landley86cafe12014-01-03 18:23:09 -0600136do_loudly $HOSTCC scripts/config2help.c -I . lib/xwrap.c lib/llist.c lib/lib.c \
137 -o generated/config2help && \
Rob Landley0e040df2014-02-04 06:14:30 -0600138generated/config2help Config.in $KCONFIG_CONFIG > generated/help.h || exit 1
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500139
Rob Landleyfb98c1e2012-05-23 21:54:16 -0500140echo "Library probe..."
141
Rob Landley3a9241a2012-08-25 14:25:22 -0500142# We trust --as-needed to remove each library if we don't use any symbols
143# out of it, this loop is because the compiler has no way to ignore a library
144# that doesn't exist, so we have to detect and skip nonexistent libraries
145# for it.
146
Rob Landley91b360a2014-08-09 14:33:34 -0500147> generated/optlibs.dat
148for i in util crypt m resolv
149do
150 echo "int main(int argc, char *argv[]) {return 0;}" | \
151 ${CROSS_COMPILE}${CC} $CFLAGS -xc - -o /dev/null -Wl,--as-needed -l$i > /dev/null 2>/dev/null &&
152 echo -l$i >> generated/optlibs.dat
153done
Rob Landleyfb98c1e2012-05-23 21:54:16 -0500154
Rob Landley91b360a2014-08-09 14:33:34 -0500155echo -n "Compile toybox"
156[ ! -z "$V" ] && echo
157
158# Extract a list of toys/*/*.c files to compile from the data in $KCONFIG_CONFIG
159
160# Get a list of C files in toys/* and wash it through sed to chop out the
161# filename, convert - to _, and glue the result together into a new regex
162# we we can feed to grep to match any one of them (whole word, not substring).
163
164TOYFILES="^$(ls toys/*/*.c | sed -n 's@^.*/\(.*\)\.c$@\1@;s/-/_/g;H;${g;s/\n//;s/\n/$|^/gp}')\$"
165
166# 1) Grab the XXX part of all CONFIG_XXX entries from KCONFIG
167# 2) Sort the list, keeping only one of each entry.
168# 3) Convert to lower case.
169# 4) Remove any config symbol not recognized as a filename from step 1.
170# 5) Add "toys/*/" prefix and ".c" suffix.
171
172TOYFILES=$(sed -nre 's/^CONFIG_(.*)=y/\1/p' < "$KCONFIG_CONFIG" \
173 | sort -u | tr A-Z a-z | grep -E "$TOYFILES" | sed 's@\(.*\)@toys/\*/\1.c@')
Rob Landley28964802008-01-19 17:08:39 -0600174
Rob Landley1aa66ba2012-02-17 12:06:12 -0600175do_loudly()
176{
Rob Landley91b360a2014-08-09 14:33:34 -0500177 [ ! -z "$V" ] && echo "$@" || echo -n .
Rob Landley1aa66ba2012-02-17 12:06:12 -0600178 "$@"
179}
180
Rob Landley91b360a2014-08-09 14:33:34 -0500181BUILD="${CROSS_COMPILE}${CC} $CFLAGS -I . $OPTIMIZE"
182FILES="$(ls lib/*.c) main.c $TOYFILES"
183LINK="-o toybox_unstripped -Wl,--as-needed $(cat generated/optlibs.dat)"
184
185# This is a parallel version of: do_loudly $BUILD $FILES $LINK || exit 1
186
187rm -f generated/*.o
Rob Landley5d16faa2014-08-24 22:42:47 -0500188PENDING=
Rob Landley91b360a2014-08-09 14:33:34 -0500189for i in $FILES
190do
191 # build each generated/*.o file in parallel
192
193 X=${i/lib\//lib_}
194 X=${X##*/}
195 do_loudly $BUILD -c $i -o generated/${X%%.c}.o &
196
197 # ratelimit to $CPUS many parallel jobs, detecting errors
198
199 while true
200 do
Rob Landley5d16faa2014-08-24 22:42:47 -0500201 PENDING="$(echo $PENDING $(jobs -rp) | tr ' ' '\n' | sort -u)"
202 [ $(echo $PENDING | wc -l) -lt "$CPUS" ] && break;
203
204 wait $(echo $PENDING | head -n 1) || exit 1
205 PENDING="$(echo "$PENDING" | tail -n +2)"
Rob Landley91b360a2014-08-09 14:33:34 -0500206 done
207done
208
209# wait for all background jobs, detecting errors
210
Rob Landley5d16faa2014-08-24 22:42:47 -0500211for i in $PENDING
Rob Landley91b360a2014-08-09 14:33:34 -0500212do
213 wait $i || exit 1
214done
215
216do_loudly $BUILD generated/*.o $LINK || exit 1
Rob Landley344c2672012-03-03 10:56:11 -0600217do_loudly ${CROSS_COMPILE}${STRIP} toybox_unstripped -o toybox || exit 1
Rob Landleyb704ad22009-12-13 00:12:26 -0600218# gcc 4.4's strip command is buggy, and doesn't set the executable bit on
219# its output the way SUSv4 suggests it do so.
Rob Landley1aa66ba2012-02-17 12:06:12 -0600220do_loudly chmod +x toybox || exit 1
Rob Landley91b360a2014-08-09 14:33:34 -0500221echo