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