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 | 7a07c6b | 2014-09-09 20:13:03 -0500 | [diff] [blame] | 7 | set -o pipefail |
Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 8 | source ./configure |
| 9 | |
Rob Landley | d04dc1f | 2013-08-30 01:53:31 -0500 | [diff] [blame] | 10 | [ -z "$KCONFIG_CONFIG" ] && KCONFIG_CONFIG=".config" |
| 11 | |
Rob Landley | d3df423 | 2014-09-20 13:07:53 -0500 | [diff] [blame] | 12 | # Since each cc invocation is short, launch half again as many processes |
| 13 | # as we have processors so they don't exit faster than we can start them. |
| 14 | [ -z "$CPUS" ] && |
| 15 | CPUS=$((($(echo /sys/devices/system/cpu/cpu[0-9]* | wc -w)*3)/2)) |
| 16 | |
Rob Landley | 86cafe1 | 2014-01-03 18:23:09 -0600 | [diff] [blame] | 17 | # Respond to V= by echoing command lines as well as running them |
| 18 | do_loudly() |
| 19 | { |
| 20 | [ ! -z "$V" ] && echo "$@" |
| 21 | "$@" |
| 22 | } |
| 23 | |
Rob Landley | 94a4603 | 2014-09-20 14:20:28 -0500 | [diff] [blame^] | 24 | mkdir -p generated |
Rob Landley | d04dc1f | 2013-08-30 01:53:31 -0500 | [diff] [blame] | 25 | echo "Make generated/config.h from $KCONFIG_CONFIG." |
Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 26 | |
| 27 | # This long and roundabout sed invocation is to make old versions of sed happy. |
| 28 | # New ones have '\n' so can replace one line with two without all the branches |
| 29 | # and tedious mucking about with hold space. |
| 30 | |
Rob Landley | 1193a02 | 2009-01-30 16:10:55 -0600 | [diff] [blame] | 31 | sed -n \ |
| 32 | -e 's/^# CONFIG_\(.*\) is not set.*/\1/' \ |
| 33 | -e 't notset' \ |
| 34 | -e 's/^CONFIG_\(.*\)=y.*/\1/' \ |
| 35 | -e 't isset' \ |
Rob Landley | 687bea5 | 2009-01-30 16:17:35 -0600 | [diff] [blame] | 36 | -e 's/^CONFIG_\([^=]*\)=\(.*\)/#define CFG_\1 \2/p' \ |
Rob Landley | 1193a02 | 2009-01-30 16:10:55 -0600 | [diff] [blame] | 37 | -e 'd' \ |
| 38 | -e ':notset' \ |
| 39 | -e 'h' \ |
| 40 | -e 's/.*/#define CFG_& 0/p' \ |
| 41 | -e 'g' \ |
| 42 | -e 's/.*/#define USE_&(...)/p' \ |
| 43 | -e 'd' \ |
| 44 | -e ':isset' \ |
| 45 | -e 'h' \ |
| 46 | -e 's/.*/#define CFG_& 1/p' \ |
| 47 | -e 'g' \ |
| 48 | -e 's/.*/#define USE_&(...) __VA_ARGS__/p' \ |
Rob Landley | d04dc1f | 2013-08-30 01:53:31 -0500 | [diff] [blame] | 49 | $KCONFIG_CONFIG > generated/config.h || exit 1 |
Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 50 | |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 51 | |
| 52 | echo "Extract configuration information from toys/*.c files..." |
| 53 | scripts/genconfig.sh |
| 54 | |
| 55 | echo "Generate headers from toys/*/*.c..." |
| 56 | |
Rob Landley | 3a489e4 | 2013-06-22 14:23:06 -0500 | [diff] [blame] | 57 | # Create a list of all the commands toybox can provide. Note that the first |
| 58 | # entry is out of order on purpose (the toybox multiplexer command must be the |
| 59 | # 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] | 60 | # for fast binary search. |
| 61 | |
Rob Landley | 46c8069 | 2013-12-28 17:06:55 -0600 | [diff] [blame] | 62 | echo -n "generated/newtoys.h " |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 63 | |
Rob Landley | d04dc1f | 2013-08-30 01:53:31 -0500 | [diff] [blame] | 64 | echo "USE_TOYBOX(NEWTOY(toybox, NULL, TOYFLAG_STAYROOT))" > generated/newtoys.h |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 65 | sed -n -e 's/^USE_[A-Z0-9_]*(/&/p' toys/*/*.c \ |
| 66 | | sed 's/\(.*TOY(\)\([^,]*\),\(.*\)/\2 \1\2,\3/' | sort -k 1,1 \ |
| 67 | | sed 's/[^ ]* //' >> generated/newtoys.h |
Rob Landley | 75a1815 | 2013-09-22 03:37:39 -0500 | [diff] [blame] | 68 | sed -n -e 's/.*(NEWTOY(\([^,]*\), *\(\("[^"]*"[^,]*\)*\),.*/#define OPTSTR_\1\t\2/p' \ |
Rob Landley | dd2d239 | 2013-08-12 01:48:27 -0500 | [diff] [blame] | 69 | generated/newtoys.h > generated/oldtoys.h |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 70 | |
Rob Landley | 86cafe1 | 2014-01-03 18:23:09 -0600 | [diff] [blame] | 71 | do_loudly $HOSTCC scripts/mkflags.c -o generated/mkflags || exit 1 |
Rob Landley | 6cf0a11 | 2012-11-26 14:14:29 -0600 | [diff] [blame] | 72 | |
Rob Landley | 46c8069 | 2013-12-28 17:06:55 -0600 | [diff] [blame] | 73 | echo -n "generated/flags.h " |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 74 | |
Rob Landley | e36a9dd | 2014-02-23 20:11:06 -0600 | [diff] [blame] | 75 | # Process config.h and newtoys.h to generate FLAG_x macros. Note we must |
| 76 | # always #define the relevant macro, even when it's disabled, because we |
| 77 | # allow multiple NEWTOY() in the same C file. (When disabled the FLAG is 0, |
| 78 | # so flags&0 becomes a constant 0 allowing dead code elimination.) |
| 79 | |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 80 | # Parse files through C preprocessor twice, once to get flags for current |
| 81 | # .config and once to get flags for allyesconfig |
| 82 | for I in A B |
| 83 | do |
| 84 | ( |
| 85 | # define macros and select header files with option string data |
| 86 | |
| 87 | echo "#define NEWTOY(aa,bb,cc) aa $I bb" |
| 88 | echo '#define OLDTOY(...)' |
| 89 | if [ "$I" == A ] |
| 90 | then |
| 91 | cat generated/config.h |
| 92 | else |
| 93 | sed '/USE_.*([^)]*)$/s/$/ __VA_ARGS__/' generated/config.h |
| 94 | fi |
| 95 | cat generated/newtoys.h |
| 96 | |
| 97 | # Run result through preprocessor, glue together " " gaps leftover from USE |
| 98 | # macros, delete comment lines, print any line with a quoted optstring, |
| 99 | # 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] | 100 | # handle "" with nothing in it, and mkflags uses that). |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 101 | |
Rob Landley | 73fff3b | 2013-10-23 02:52:01 -0500 | [diff] [blame] | 102 | ) | ${CROSS_COMPILE}${CC} -E - | \ |
Rob Landley | 170c397 | 2014-02-28 20:46:16 -0600 | [diff] [blame] | 103 | 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] | 104 | |
| 105 | # Sort resulting line pairs and glue them together into triplets of |
| 106 | # command "flags" "allflags" |
| 107 | # to feed into mkflags C program that outputs actual flag macros |
Rob Landley | e36a9dd | 2014-02-23 20:11:06 -0600 | [diff] [blame] | 108 | # If no pair (because command's disabled in config), use " " for flags |
| 109 | # so allflags can define the appropriate zero macros. |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 110 | |
Rob Landley | e36a9dd | 2014-02-23 20:11:06 -0600 | [diff] [blame] | 111 | 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] | 112 | generated/mkflags > generated/flags.h || exit 1 |
Rob Landley | 6cf0a11 | 2012-11-26 14:14:29 -0600 | [diff] [blame] | 113 | |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 114 | # Extract global structure definitions and flag definitions from toys/*/*.c |
| 115 | |
| 116 | function getglobals() |
| 117 | { |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 118 | for i in toys/*/*.c |
| 119 | do |
| 120 | NAME="$(echo $i | sed 's@.*/\(.*\)\.c@\1@')" |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 121 | DATA="$(sed -n -e '/^GLOBALS(/,/^)/b got;b;:got' \ |
| 122 | -e 's/^GLOBALS(/struct '"$NAME"'_data {/' \ |
| 123 | -e 's/^)/};/' -e 'p' $i)" |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 124 | |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 125 | [ ! -z "$DATA" ] && echo -e "// $i\n\n$DATA\n" |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 126 | done |
| 127 | } |
| 128 | |
Rob Landley | 46c8069 | 2013-12-28 17:06:55 -0600 | [diff] [blame] | 129 | echo -n "generated/globals.h " |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 130 | |
| 131 | GLOBSTRUCT="$(getglobals)" |
| 132 | ( |
| 133 | echo "$GLOBSTRUCT" |
| 134 | echo |
| 135 | echo "extern union global_union {" |
| 136 | echo "$GLOBSTRUCT" | sed -n 's/struct \(.*\)_data {/ struct \1_data \1;/p' |
| 137 | echo "} this;" |
| 138 | ) > generated/globals.h |
| 139 | |
| 140 | echo "generated/help.h" |
Rob Landley | 86cafe1 | 2014-01-03 18:23:09 -0600 | [diff] [blame] | 141 | do_loudly $HOSTCC scripts/config2help.c -I . lib/xwrap.c lib/llist.c lib/lib.c \ |
| 142 | -o generated/config2help && \ |
Rob Landley | 0e040df | 2014-02-04 06:14:30 -0600 | [diff] [blame] | 143 | generated/config2help Config.in $KCONFIG_CONFIG > generated/help.h || exit 1 |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 144 | |
Rob Landley | d3df423 | 2014-09-20 13:07:53 -0500 | [diff] [blame] | 145 | echo -n "Library probe" |
Rob Landley | fb98c1e | 2012-05-23 21:54:16 -0500 | [diff] [blame] | 146 | |
Rob Landley | 3a9241a | 2012-08-25 14:25:22 -0500 | [diff] [blame] | 147 | # We trust --as-needed to remove each library if we don't use any symbols |
| 148 | # out of it, this loop is because the compiler has no way to ignore a library |
| 149 | # that doesn't exist, so we have to detect and skip nonexistent libraries |
| 150 | # for it. |
| 151 | |
Rob Landley | 91b360a | 2014-08-09 14:33:34 -0500 | [diff] [blame] | 152 | > generated/optlibs.dat |
| 153 | for i in util crypt m resolv |
| 154 | do |
| 155 | echo "int main(int argc, char *argv[]) {return 0;}" | \ |
| 156 | ${CROSS_COMPILE}${CC} $CFLAGS -xc - -o /dev/null -Wl,--as-needed -l$i > /dev/null 2>/dev/null && |
| 157 | echo -l$i >> generated/optlibs.dat |
Rob Landley | d3df423 | 2014-09-20 13:07:53 -0500 | [diff] [blame] | 158 | echo -n . |
Rob Landley | 91b360a | 2014-08-09 14:33:34 -0500 | [diff] [blame] | 159 | done |
Rob Landley | d3df423 | 2014-09-20 13:07:53 -0500 | [diff] [blame] | 160 | echo |
Rob Landley | fb98c1e | 2012-05-23 21:54:16 -0500 | [diff] [blame] | 161 | |
Rob Landley | 91b360a | 2014-08-09 14:33:34 -0500 | [diff] [blame] | 162 | echo -n "Compile toybox" |
| 163 | [ ! -z "$V" ] && echo |
| 164 | |
| 165 | # Extract a list of toys/*/*.c files to compile from the data in $KCONFIG_CONFIG |
| 166 | |
Rob Landley | 7a07c6b | 2014-09-09 20:13:03 -0500 | [diff] [blame] | 167 | TOYFILES="$(egrep -l "TOY[(]($(sed -n 's/^CONFIG_\([^=]*\)=.*/\1/p' "$KCONFIG_CONFIG" | xargs | tr ' [A-Z]' '|[a-z]'))[ ,]" toys/*/*.c)" |
Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 168 | |
Rob Landley | 1aa66ba | 2012-02-17 12:06:12 -0600 | [diff] [blame] | 169 | do_loudly() |
| 170 | { |
Rob Landley | 91b360a | 2014-08-09 14:33:34 -0500 | [diff] [blame] | 171 | [ ! -z "$V" ] && echo "$@" || echo -n . |
Rob Landley | 1aa66ba | 2012-02-17 12:06:12 -0600 | [diff] [blame] | 172 | "$@" |
| 173 | } |
| 174 | |
Rob Landley | 8aa87ab | 2014-09-11 20:50:10 -0500 | [diff] [blame] | 175 | BUILD="$(echo ${CROSS_COMPILE}${CC} $CFLAGS -I . $OPTIMIZE)" |
| 176 | FILES="$(echo lib/*.c main.c $TOYFILES)" |
| 177 | LINK="$(echo $LDOPTIMIZE -o toybox_unstripped -Wl,--as-needed $(cat generated/optlibs.dat))" |
Rob Landley | 91b360a | 2014-08-09 14:33:34 -0500 | [diff] [blame] | 178 | |
| 179 | # This is a parallel version of: do_loudly $BUILD $FILES $LINK || exit 1 |
| 180 | |
Rob Landley | 8aa87ab | 2014-09-11 20:50:10 -0500 | [diff] [blame] | 181 | # Write a canned build line for use on crippled build machines. |
| 182 | ( |
| 183 | echo "#!/bin/sh" |
| 184 | echo |
| 185 | echo "BUILD=\"$BUILD\"" |
| 186 | echo |
| 187 | echo "LINK=\"$LINK\"" |
| 188 | echo |
| 189 | echo "FILES=\"$FILES\"" |
| 190 | echo |
| 191 | echo '$BUILD $FILES $LINK' |
| 192 | ) > generated/build.sh && chmod +x generated/build.sh || echo 1 |
| 193 | |
Rob Landley | 09af6a7 | 2014-09-11 00:04:37 -0500 | [diff] [blame] | 194 | rm -rf generated/obj && mkdir -p generated/obj || exit 1 |
Rob Landley | 5d16faa | 2014-08-24 22:42:47 -0500 | [diff] [blame] | 195 | PENDING= |
Rob Landley | 91b360a | 2014-08-09 14:33:34 -0500 | [diff] [blame] | 196 | for i in $FILES |
| 197 | do |
Rob Landley | 7a07c6b | 2014-09-09 20:13:03 -0500 | [diff] [blame] | 198 | # build each generated/obj/*.o file in parallel |
Rob Landley | 91b360a | 2014-08-09 14:33:34 -0500 | [diff] [blame] | 199 | |
| 200 | X=${i/lib\//lib_} |
| 201 | X=${X##*/} |
Rob Landley | 7a07c6b | 2014-09-09 20:13:03 -0500 | [diff] [blame] | 202 | do_loudly $BUILD -c $i -o generated/obj/${X%%.c}.o & |
Rob Landley | 91b360a | 2014-08-09 14:33:34 -0500 | [diff] [blame] | 203 | |
| 204 | # ratelimit to $CPUS many parallel jobs, detecting errors |
| 205 | |
| 206 | while true |
| 207 | do |
Rob Landley | 5d16faa | 2014-08-24 22:42:47 -0500 | [diff] [blame] | 208 | PENDING="$(echo $PENDING $(jobs -rp) | tr ' ' '\n' | sort -u)" |
Rob Landley | 9bb73ad | 2014-09-04 00:23:51 -0500 | [diff] [blame] | 209 | [ $(echo -n "$PENDING" | wc -l) -lt "$CPUS" ] && break; |
Rob Landley | 5d16faa | 2014-08-24 22:42:47 -0500 | [diff] [blame] | 210 | |
Rob Landley | 658887a | 2014-08-31 22:07:43 -0500 | [diff] [blame] | 211 | wait $(echo "$PENDING" | head -n 1) || exit 1 |
Rob Landley | 5d16faa | 2014-08-24 22:42:47 -0500 | [diff] [blame] | 212 | PENDING="$(echo "$PENDING" | tail -n +2)" |
Rob Landley | 91b360a | 2014-08-09 14:33:34 -0500 | [diff] [blame] | 213 | done |
| 214 | done |
| 215 | |
| 216 | # wait for all background jobs, detecting errors |
| 217 | |
Rob Landley | 5d16faa | 2014-08-24 22:42:47 -0500 | [diff] [blame] | 218 | for i in $PENDING |
Rob Landley | 91b360a | 2014-08-09 14:33:34 -0500 | [diff] [blame] | 219 | do |
| 220 | wait $i || exit 1 |
| 221 | done |
| 222 | |
Rob Landley | 7a07c6b | 2014-09-09 20:13:03 -0500 | [diff] [blame] | 223 | do_loudly $BUILD generated/obj/*.o $LINK || exit 1 |
Rob Landley | 344c267 | 2012-03-03 10:56:11 -0600 | [diff] [blame] | 224 | do_loudly ${CROSS_COMPILE}${STRIP} toybox_unstripped -o toybox || exit 1 |
Rob Landley | b704ad2 | 2009-12-13 00:12:26 -0600 | [diff] [blame] | 225 | # gcc 4.4's strip command is buggy, and doesn't set the executable bit on |
| 226 | # its output the way SUSv4 suggests it do so. |
Rob Landley | 1aa66ba | 2012-02-17 12:06:12 -0600 | [diff] [blame] | 227 | do_loudly chmod +x toybox || exit 1 |
Rob Landley | 91b360a | 2014-08-09 14:33:34 -0500 | [diff] [blame] | 228 | echo |