comparison scripts/make.sh @ 674:7e846e281e38

New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
author Rob Landley <rob@landley.net>
date Mon, 08 Oct 2012 00:02:30 -0500
parents c60ac785784f
children 3e81cd0bad4b
comparison
equal deleted inserted replaced
673:c102f31a753e 674:7e846e281e38
6 6
7 if [ -z ".config" ] 7 if [ -z ".config" ]
8 then 8 then
9 echo "No .config (see "make help" for configuration options)." 9 echo "No .config (see "make help" for configuration options)."
10 exit 1 10 exit 1
11 fi
12
13 echo "Extract configuration information from toys/*.c files..."
14 scripts/genconfig.sh
15
16 echo "Generate headers from toys/*/*.c..."
17
18 # Create a list of all the applets toybox can provide. Note that the first
19 # entry is out of order on purpose (the toybox multiplexer applet must be the
20 # first element of the array). The rest must be sorted in alphabetical order
21 # for fast binary search.
22
23 echo "generated/newtoys.h"
24
25 function newtoys()
26 {
27 for i in toys/*/*.c
28 do
29 sed -n -e '1,/^config [A-Z]/s/^USE_/&/p' $i || exit 1
30 done
31 }
32 echo "NEWTOY(toybox, NULL, TOYFLAG_STAYROOT)" > generated/newtoys.h
33 newtoys | sed 's/\(.*TOY(\)\([^,]*\),\(.*\)/\2 \1\2,\3/' | sort -k 1,1 \
34 | sed 's/[^ ]* //' >> generated/newtoys.h
35
36 # Extract global structure definitions and flag definitions from toys/*/*.c
37
38 function getglobals()
39 {
40 for i in toys/*/*.c
41 do
42 NAME="$(echo $i | sed 's@.*/\(.*\)\.c@\1@')"
43
44 echo -e "// $i\n"
45 sed -n -e '/^DEFINE_GLOBALS(/,/^)/b got;b;:got' \
46 -e 's/^DEFINE_GLOBALS(/struct '"$NAME"'_data {/' \
47 -e 's/^)/};/' -e 'p' $i
48
49 # And get flag definitions
50 FLAGS="$(sed -n \
51 -e "s/.*TOY($NAME"',[ \t]*"\([^"]*\)"[ \t]*,.*)/\1/' \
52 -e 't keep;d;:keep' \
53 -e 's/[><=][0-9][0-9]*//g' \
54 -e 's/+.//g' \
55 -e 's/([^)]*)//g' \
56 -e 's/[-?^:&#|@*]//g' \
57 -e 'p' \
58 generated/newtoys.h)"
59 X=0
60 while [ $X -lt ${#FLAGS} ]
61 do
62 echo -ne "#define OPTFLAG_${NAME}_${FLAGS:$X:1}\t"
63 X=$(($X+1))
64 echo "(1<<$((${#FLAGS}-$X)))"
65 done
66 done
67 }
68
69 echo "generated/globals.h"
70
71 GLOBSTRUCT="$(getglobals)"
72 (
73 echo "$GLOBSTRUCT"
74 echo
75 echo "extern union global_union {"
76 echo "$GLOBSTRUCT" | sed -n 's/struct \(.*\)_data {/ struct \1_data \1;/p'
77 echo "} this;"
78 ) > generated/globals.h
79
80 echo "generated/help.h"
81 # Only recreate generated/help.h if python is installed
82 if [ ! -z "$(which python)" ] && [ ! -z "$(grep 'CONFIG_HELP=y' .config)" ]
83 then
84 echo "Extract help text from Config.in."
85 scripts/config2help.py Config.in > generated/help.h || exit 1
86 fi 11 fi
87 12
88 echo "Make generated/config.h from .config." 13 echo "Make generated/config.h from .config."
89 14
90 # This long and roundabout sed invocation is to make old versions of sed happy. 15 # This long and roundabout sed invocation is to make old versions of sed happy.
108 -e 'h' \ 33 -e 'h' \
109 -e 's/.*/#define CFG_& 1/p' \ 34 -e 's/.*/#define CFG_& 1/p' \
110 -e 'g' \ 35 -e 'g' \
111 -e 's/.*/#define USE_&(...) __VA_ARGS__/p' \ 36 -e 's/.*/#define USE_&(...) __VA_ARGS__/p' \
112 .config > generated/config.h || exit 1 37 .config > generated/config.h || exit 1
38
39
40 echo "Extract configuration information from toys/*.c files..."
41 scripts/genconfig.sh
42
43 echo "Generate headers from toys/*/*.c..."
44
45 # Create a list of all the applets toybox can provide. Note that the first
46 # entry is out of order on purpose (the toybox multiplexer applet must be the
47 # first element of the array). The rest must be sorted in alphabetical order
48 # for fast binary search.
49
50 echo "generated/newtoys.h"
51
52 echo "NEWTOY(toybox, NULL, TOYFLAG_STAYROOT)" > generated/newtoys.h
53 sed -n -e 's/^USE_[A-Z0-9_]*(/&/p' toys/*/*.c \
54 | sed 's/\(.*TOY(\)\([^,]*\),\(.*\)/\2 \1\2,\3/' | sort -k 1,1 \
55 | sed 's/[^ ]* //' >> generated/newtoys.h
56
57 # Extract global structure definitions and flag definitions from toys/*/*.c
58
59 function getglobals()
60 {
61 NEWTOYS="$(cat generated/config.h generated/newtoys.h | gcc -E - | sed 's/" *"//g')"
62 for i in toys/*/*.c
63 do
64 NAME="$(echo $i | sed 's@.*/\(.*\)\.c@\1@')"
65
66 echo -e "// $i\n"
67 sed -n -e '/^GLOBALS(/,/^)/b got;b;:got' \
68 -e 's/^GLOBALS(/struct '"$NAME"'_data {/' \
69 -e 's/^)/};/' -e 'p' $i
70
71 # And get flag definitions
72 FLAGS="$(echo "$NEWTOYS" | sed -n \
73 -e "s/.*TOY($NAME"',[ \t]*"\([^"]*\)"[ \t]*,.*)/\1/' \
74 -e 't keep;d;:keep' -e 's/^[<>=][0-9]//' -e 's/[?&^]//' \
75 -e 't keep' -e 's/[><=][0-9][0-9]*//g' -e 's/+.//g' \
76 -e 's/([^)]*)//g' -e 's/[-?^:&#|@*]//g' -e 'p')"
77 echo "#ifdef FOR_${NAME}"
78 X=0
79 while [ $X -lt ${#FLAGS} ]
80 do
81 echo -ne "#define FLAG_${FLAGS:$X:1}\t"
82 X=$(($X+1))
83 echo "(1<<$((${#FLAGS}-$X)))"
84 done
85 echo "#define TT this.${NAME}"
86 echo "#endif"
87 done
88 }
89
90 echo "generated/globals.h"
91
92 GLOBSTRUCT="$(getglobals)"
93 (
94 echo "$GLOBSTRUCT"
95 echo
96 echo "extern union global_union {"
97 echo "$GLOBSTRUCT" | sed -n 's/struct \(.*\)_data {/ struct \1_data \1;/p'
98 echo "} this;"
99 ) > generated/globals.h
100
101 echo "generated/help.h"
102 # Only recreate generated/help.h if python is installed
103 if [ ! -z "$(which python)" ] && [ ! -z "$(grep 'CONFIG_HELP=y' .config)" ]
104 then
105 echo "Extract help text from Config.in."
106 scripts/config2help.py Config.in > generated/help.h || exit 1
107 fi
113 108
114 # Extract a list of toys/*/*.c files to compile from the data in ".config": 109 # Extract a list of toys/*/*.c files to compile from the data in ".config":
115 110
116 # 1) Get a list of C files in toys/* and glue them together into a regex we can 111 # 1) Get a list of C files in toys/* and glue them together into a regex we can
117 # feed to grep that will match any one of them (whole word, not substring). 112 # feed to grep that will match any one of them (whole word, not substring).