Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Grab default values for $CFLAGS and such. |
| 4 | |
Rob Landley | b3d4f0b | 2013-04-29 16:00:40 -0500 | [diff] [blame] | 5 | export LANG=c |
Rob Landley | 082a9a7 | 2014-08-30 16:34:46 -0500 | [diff] [blame^] | 6 | export LC_ALL=C |
Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 7 | source ./configure |
| 8 | |
Rob Landley | d04dc1f | 2013-08-30 01:53:31 -0500 | [diff] [blame] | 9 | [ -z "$KCONFIG_CONFIG" ] && KCONFIG_CONFIG=".config" |
| 10 | |
Rob Landley | 5d16faa | 2014-08-24 22:42:47 -0500 | [diff] [blame] | 11 | [ -z "$CPUS" ] && CPUS=$(($(echo /sys/devices/system/cpu/cpu[0-9]* | wc -w)+1)) |
Rob Landley | 3a9241a | 2012-08-25 14:25:22 -0500 | [diff] [blame] | 12 | |
Rob Landley | 86cafe1 | 2014-01-03 18:23:09 -0600 | [diff] [blame] | 13 | # Respond to V= by echoing command lines as well as running them |
| 14 | do_loudly() |
| 15 | { |
| 16 | [ ! -z "$V" ] && echo "$@" |
| 17 | "$@" |
| 18 | } |
| 19 | |
Rob Landley | d04dc1f | 2013-08-30 01:53:31 -0500 | [diff] [blame] | 20 | echo "Make generated/config.h from $KCONFIG_CONFIG." |
Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 21 | |
| 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 Landley | 1193a02 | 2009-01-30 16:10:55 -0600 | [diff] [blame] | 26 | sed -n \ |
| 27 | -e 's/^# CONFIG_\(.*\) is not set.*/\1/' \ |
| 28 | -e 't notset' \ |
| 29 | -e 's/^CONFIG_\(.*\)=y.*/\1/' \ |
| 30 | -e 't isset' \ |
Rob Landley | 687bea5 | 2009-01-30 16:17:35 -0600 | [diff] [blame] | 31 | -e 's/^CONFIG_\([^=]*\)=\(.*\)/#define CFG_\1 \2/p' \ |
Rob Landley | 1193a02 | 2009-01-30 16:10:55 -0600 | [diff] [blame] | 32 | -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 Landley | d04dc1f | 2013-08-30 01:53:31 -0500 | [diff] [blame] | 44 | $KCONFIG_CONFIG > generated/config.h || exit 1 |
Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 45 | |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 46 | |
| 47 | echo "Extract configuration information from toys/*.c files..." |
| 48 | scripts/genconfig.sh |
| 49 | |
| 50 | echo "Generate headers from toys/*/*.c..." |
| 51 | |
Rob Landley | 3a489e4 | 2013-06-22 14:23:06 -0500 | [diff] [blame] | 52 | # 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 Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 55 | # for fast binary search. |
| 56 | |
Rob Landley | 46c8069 | 2013-12-28 17:06:55 -0600 | [diff] [blame] | 57 | echo -n "generated/newtoys.h " |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 58 | |
Rob Landley | d04dc1f | 2013-08-30 01:53:31 -0500 | [diff] [blame] | 59 | echo "USE_TOYBOX(NEWTOY(toybox, NULL, TOYFLAG_STAYROOT))" > generated/newtoys.h |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 60 | sed -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 Landley | 75a1815 | 2013-09-22 03:37:39 -0500 | [diff] [blame] | 63 | sed -n -e 's/.*(NEWTOY(\([^,]*\), *\(\("[^"]*"[^,]*\)*\),.*/#define OPTSTR_\1\t\2/p' \ |
Rob Landley | dd2d239 | 2013-08-12 01:48:27 -0500 | [diff] [blame] | 64 | generated/newtoys.h > generated/oldtoys.h |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 65 | |
Rob Landley | 86cafe1 | 2014-01-03 18:23:09 -0600 | [diff] [blame] | 66 | do_loudly $HOSTCC scripts/mkflags.c -o generated/mkflags || exit 1 |
Rob Landley | 6cf0a11 | 2012-11-26 14:14:29 -0600 | [diff] [blame] | 67 | |
Rob Landley | 46c8069 | 2013-12-28 17:06:55 -0600 | [diff] [blame] | 68 | echo -n "generated/flags.h " |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 69 | |
Rob Landley | e36a9dd | 2014-02-23 20:11:06 -0600 | [diff] [blame] | 70 | # 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 Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 75 | # Parse files through C preprocessor twice, once to get flags for current |
| 76 | # .config and once to get flags for allyesconfig |
| 77 | for I in A B |
| 78 | do |
| 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 Landley | 170c397 | 2014-02-28 20:46:16 -0600 | [diff] [blame] | 95 | # handle "" with nothing in it, and mkflags uses that). |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 96 | |
Rob Landley | 73fff3b | 2013-10-23 02:52:01 -0500 | [diff] [blame] | 97 | ) | ${CROSS_COMPILE}${CC} -E - | \ |
Rob Landley | 170c397 | 2014-02-28 20:46:16 -0600 | [diff] [blame] | 98 | sed -n -e 's/" *"//g;/^#/d;t clear;:clear;s/"/"/p;t;s/\( [AB] \).*/\1 " "/p' |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 99 | |
| 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 Landley | e36a9dd | 2014-02-23 20:11:06 -0600 | [diff] [blame] | 103 | # If no pair (because command's disabled in config), use " " for flags |
| 104 | # so allflags can define the appropriate zero macros. |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 105 | |
Rob Landley | e36a9dd | 2014-02-23 20:11:06 -0600 | [diff] [blame] | 106 | done | 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 Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 107 | generated/mkflags > generated/flags.h || exit 1 |
Rob Landley | 6cf0a11 | 2012-11-26 14:14:29 -0600 | [diff] [blame] | 108 | |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 109 | # Extract global structure definitions and flag definitions from toys/*/*.c |
| 110 | |
| 111 | function getglobals() |
| 112 | { |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 113 | for i in toys/*/*.c |
| 114 | do |
| 115 | NAME="$(echo $i | sed 's@.*/\(.*\)\.c@\1@')" |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 116 | DATA="$(sed -n -e '/^GLOBALS(/,/^)/b got;b;:got' \ |
| 117 | -e 's/^GLOBALS(/struct '"$NAME"'_data {/' \ |
| 118 | -e 's/^)/};/' -e 'p' $i)" |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 119 | |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 120 | [ ! -z "$DATA" ] && echo -e "// $i\n\n$DATA\n" |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 121 | done |
| 122 | } |
| 123 | |
Rob Landley | 46c8069 | 2013-12-28 17:06:55 -0600 | [diff] [blame] | 124 | echo -n "generated/globals.h " |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 125 | |
| 126 | GLOBSTRUCT="$(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 | |
| 135 | echo "generated/help.h" |
Rob Landley | 86cafe1 | 2014-01-03 18:23:09 -0600 | [diff] [blame] | 136 | do_loudly $HOSTCC scripts/config2help.c -I . lib/xwrap.c lib/llist.c lib/lib.c \ |
| 137 | -o generated/config2help && \ |
Rob Landley | 0e040df | 2014-02-04 06:14:30 -0600 | [diff] [blame] | 138 | generated/config2help Config.in $KCONFIG_CONFIG > generated/help.h || exit 1 |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 139 | |
Rob Landley | fb98c1e | 2012-05-23 21:54:16 -0500 | [diff] [blame] | 140 | echo "Library probe..." |
| 141 | |
Rob Landley | 3a9241a | 2012-08-25 14:25:22 -0500 | [diff] [blame] | 142 | # 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 Landley | 91b360a | 2014-08-09 14:33:34 -0500 | [diff] [blame] | 147 | > generated/optlibs.dat |
| 148 | for i in util crypt m resolv |
| 149 | do |
| 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 |
| 153 | done |
Rob Landley | fb98c1e | 2012-05-23 21:54:16 -0500 | [diff] [blame] | 154 | |
Rob Landley | 91b360a | 2014-08-09 14:33:34 -0500 | [diff] [blame] | 155 | echo -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 | |
| 164 | TOYFILES="^$(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 | |
| 172 | TOYFILES=$(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 Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 174 | |
Rob Landley | 1aa66ba | 2012-02-17 12:06:12 -0600 | [diff] [blame] | 175 | do_loudly() |
| 176 | { |
Rob Landley | 91b360a | 2014-08-09 14:33:34 -0500 | [diff] [blame] | 177 | [ ! -z "$V" ] && echo "$@" || echo -n . |
Rob Landley | 1aa66ba | 2012-02-17 12:06:12 -0600 | [diff] [blame] | 178 | "$@" |
| 179 | } |
| 180 | |
Rob Landley | 91b360a | 2014-08-09 14:33:34 -0500 | [diff] [blame] | 181 | BUILD="${CROSS_COMPILE}${CC} $CFLAGS -I . $OPTIMIZE" |
| 182 | FILES="$(ls lib/*.c) main.c $TOYFILES" |
| 183 | LINK="-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 | |
| 187 | rm -f generated/*.o |
Rob Landley | 5d16faa | 2014-08-24 22:42:47 -0500 | [diff] [blame] | 188 | PENDING= |
Rob Landley | 91b360a | 2014-08-09 14:33:34 -0500 | [diff] [blame] | 189 | for i in $FILES |
| 190 | do |
| 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 Landley | 5d16faa | 2014-08-24 22:42:47 -0500 | [diff] [blame] | 201 | 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 Landley | 91b360a | 2014-08-09 14:33:34 -0500 | [diff] [blame] | 206 | done |
| 207 | done |
| 208 | |
| 209 | # wait for all background jobs, detecting errors |
| 210 | |
Rob Landley | 5d16faa | 2014-08-24 22:42:47 -0500 | [diff] [blame] | 211 | for i in $PENDING |
Rob Landley | 91b360a | 2014-08-09 14:33:34 -0500 | [diff] [blame] | 212 | do |
| 213 | wait $i || exit 1 |
| 214 | done |
| 215 | |
| 216 | do_loudly $BUILD generated/*.o $LINK || exit 1 |
Rob Landley | 344c267 | 2012-03-03 10:56:11 -0600 | [diff] [blame] | 217 | do_loudly ${CROSS_COMPILE}${STRIP} toybox_unstripped -o toybox || exit 1 |
Rob Landley | b704ad2 | 2009-12-13 00:12:26 -0600 | [diff] [blame] | 218 | # 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 Landley | 1aa66ba | 2012-02-17 12:06:12 -0600 | [diff] [blame] | 220 | do_loudly chmod +x toybox || exit 1 |
Rob Landley | 91b360a | 2014-08-09 14:33:34 -0500 | [diff] [blame] | 221 | echo |