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