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