blob: 5ed9df42ac23694494cb3a5f5193cac189025229 [file] [log] [blame]
Rob Landley28964802008-01-19 17:08:39 -06001#!/bin/bash
2
3# Grab default values for $CFLAGS and such.
4
Rob Landleyb3d4f0b2013-04-29 16:00:40 -05005export LANG=c
Rob Landley28964802008-01-19 17:08:39 -06006source ./configure
7
Rob Landleyd04dc1f2013-08-30 01:53:31 -05008[ -z "$KCONFIG_CONFIG" ] && KCONFIG_CONFIG=".config"
9
10if [ -z "$KCONFIG_CONFIG" ]
Rob Landley3a9241a2012-08-25 14:25:22 -050011then
Rob Landleyd04dc1f2013-08-30 01:53:31 -050012 echo "No $KCONFIG_CONFIG (see "make help" for configuration options)."
Rob Landley3a9241a2012-08-25 14:25:22 -050013 exit 1
14fi
15
Rob Landley86cafe12014-01-03 18:23:09 -060016# Respond to V= by echoing command lines as well as running them
17do_loudly()
18{
19 [ ! -z "$V" ] && echo "$@"
20 "$@"
21}
22
Rob Landleyd04dc1f2013-08-30 01:53:31 -050023echo "Make generated/config.h from $KCONFIG_CONFIG."
Rob Landley28964802008-01-19 17:08:39 -060024
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 Landley1193a022009-01-30 16:10:55 -060029sed -n \
30 -e 's/^# CONFIG_\(.*\) is not set.*/\1/' \
31 -e 't notset' \
32 -e 's/^CONFIG_\(.*\)=y.*/\1/' \
33 -e 't isset' \
Rob Landley687bea52009-01-30 16:17:35 -060034 -e 's/^CONFIG_\([^=]*\)=\(.*\)/#define CFG_\1 \2/p' \
Rob Landley1193a022009-01-30 16:10:55 -060035 -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 Landleyd04dc1f2013-08-30 01:53:31 -050047 $KCONFIG_CONFIG > generated/config.h || exit 1
Rob Landley28964802008-01-19 17:08:39 -060048
Rob Landleyc0e56ed2012-10-08 00:02:30 -050049
50echo "Extract configuration information from toys/*.c files..."
51scripts/genconfig.sh
52
53echo "Generate headers from toys/*/*.c..."
54
Rob Landley3a489e42013-06-22 14:23:06 -050055# 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 Landleyc0e56ed2012-10-08 00:02:30 -050058# for fast binary search.
59
Rob Landley46c80692013-12-28 17:06:55 -060060echo -n "generated/newtoys.h "
Rob Landleyc0e56ed2012-10-08 00:02:30 -050061
Rob Landleyd04dc1f2013-08-30 01:53:31 -050062echo "USE_TOYBOX(NEWTOY(toybox, NULL, TOYFLAG_STAYROOT))" > generated/newtoys.h
Rob Landleyc0e56ed2012-10-08 00:02:30 -050063sed -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 Landley75a18152013-09-22 03:37:39 -050066sed -n -e 's/.*(NEWTOY(\([^,]*\), *\(\("[^"]*"[^,]*\)*\),.*/#define OPTSTR_\1\t\2/p' \
Rob Landleydd2d2392013-08-12 01:48:27 -050067 generated/newtoys.h > generated/oldtoys.h
Rob Landleyc0e56ed2012-10-08 00:02:30 -050068
Rob Landley86cafe12014-01-03 18:23:09 -060069do_loudly $HOSTCC scripts/mkflags.c -o generated/mkflags || exit 1
Rob Landley6cf0a112012-11-26 14:14:29 -060070
Rob Landley46c80692013-12-28 17:06:55 -060071echo -n "generated/flags.h "
Rob Landley207cada2013-10-03 03:18:00 -050072
Rob Landleye36a9dd2014-02-23 20:11:06 -060073# 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 Landley207cada2013-10-03 03:18:00 -050078# Parse files through C preprocessor twice, once to get flags for current
79# .config and once to get flags for allyesconfig
80for I in A B
81do
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 Landley170c3972014-02-28 20:46:16 -060098 # handle "" with nothing in it, and mkflags uses that).
Rob Landley207cada2013-10-03 03:18:00 -050099
Rob Landley73fff3b2013-10-23 02:52:01 -0500100 ) | ${CROSS_COMPILE}${CC} -E - | \
Rob Landley170c3972014-02-28 20:46:16 -0600101 sed -n -e 's/" *"//g;/^#/d;t clear;:clear;s/"/"/p;t;s/\( [AB] \).*/\1 " "/p'
Rob Landley207cada2013-10-03 03:18:00 -0500102
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 Landleye36a9dd2014-02-23 20:11:06 -0600106# If no pair (because command's disabled in config), use " " for flags
107# so allflags can define the appropriate zero macros.
Rob Landley207cada2013-10-03 03:18:00 -0500108
Rob Landleye36a9dd2014-02-23 20:11:06 -0600109done | 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 Landley207cada2013-10-03 03:18:00 -0500110generated/mkflags > generated/flags.h || exit 1
Rob Landley6cf0a112012-11-26 14:14:29 -0600111
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500112# Extract global structure definitions and flag definitions from toys/*/*.c
113
114function getglobals()
115{
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500116 for i in toys/*/*.c
117 do
118 NAME="$(echo $i | sed 's@.*/\(.*\)\.c@\1@')"
Rob Landley207cada2013-10-03 03:18:00 -0500119 DATA="$(sed -n -e '/^GLOBALS(/,/^)/b got;b;:got' \
120 -e 's/^GLOBALS(/struct '"$NAME"'_data {/' \
121 -e 's/^)/};/' -e 'p' $i)"
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500122
Rob Landley207cada2013-10-03 03:18:00 -0500123 [ ! -z "$DATA" ] && echo -e "// $i\n\n$DATA\n"
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500124 done
125}
126
Rob Landley46c80692013-12-28 17:06:55 -0600127echo -n "generated/globals.h "
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500128
129GLOBSTRUCT="$(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
138echo "generated/help.h"
Rob Landley86cafe12014-01-03 18:23:09 -0600139do_loudly $HOSTCC scripts/config2help.c -I . lib/xwrap.c lib/llist.c lib/lib.c \
140 -o generated/config2help && \
Rob Landley0e040df2014-02-04 06:14:30 -0600141generated/config2help Config.in $KCONFIG_CONFIG > generated/help.h || exit 1
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500142
Rob Landleyd04dc1f2013-08-30 01:53:31 -0500143# Extract a list of toys/*/*.c files to compile from the data in $KCONFIG_CONFIG
Rob Landley28964802008-01-19 17:08:39 -0600144
Rob Landley658b97b2012-08-25 15:43:02 -0500145# 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).
147TOYFILES="^$(ls toys/*/*.c | sed -n 's@^.*/\(.*\)\.c$@\1@;s/-/_/g;H;${g;s/\n//;s/\n/$|^/gp}')\$"
Rob Landley28964802008-01-19 17:08:39 -0600148
Rob Landley658b97b2012-08-25 15:43:02 -0500149# 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 Landleyd04dc1f2013-08-30 01:53:31 -0500156TOYFILES=$(sed -nre 's/^CONFIG_(.*)=y/\1/p' < "$KCONFIG_CONFIG" \
Rob Landley658b97b2012-08-25 15:43:02 -0500157 | sort -u | tr A-Z a-z | grep -E "$TOYFILES" | sed 's@\(.*\)@toys/\*/\1.c@')
Rob Landley28964802008-01-19 17:08:39 -0600158
Rob Landleyfb98c1e2012-05-23 21:54:16 -0500159echo "Library probe..."
160
Rob Landley3a9241a2012-08-25 14:25:22 -0500161# 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 Landleyfb98c1e2012-05-23 21:54:16 -0500166OPTLIBS="$(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 Landley28964802008-01-19 17:08:39 -0600168echo "Compile toybox..."
169
Rob Landley1aa66ba2012-02-17 12:06:12 -0600170do_loudly()
171{
172 [ ! -z "$V" ] && echo "$@"
173 "$@"
174}
175
Rob Landley344c2672012-03-03 10:56:11 -0600176do_loudly ${CROSS_COMPILE}${CC} $CFLAGS -I . -o toybox_unstripped $OPTIMIZE \
Rob Landleyfb98c1e2012-05-23 21:54:16 -0500177 main.c lib/*.c $TOYFILES -Wl,--as-needed $OPTLIBS || exit 1
Rob Landley344c2672012-03-03 10:56:11 -0600178do_loudly ${CROSS_COMPILE}${STRIP} toybox_unstripped -o toybox || exit 1
Rob Landleyb704ad22009-12-13 00:12:26 -0600179# 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 Landley1aa66ba2012-02-17 12:06:12 -0600181do_loudly chmod +x toybox || exit 1