OSDN Git Service

Work around increasingly insane compiler developers wanting to make everything
[android-x86/external-toybox.git] / scripts / make.sh
1 #!/bin/bash
2
3 # Grab default values for $CFLAGS and such.
4
5 export LANG=c
6 export LC_ALL=C
7 set -o pipefail
8 source ./configure
9
10 [ -z "$KCONFIG_CONFIG" ] && KCONFIG_CONFIG=.config
11 [ -z "$OUTNAME" ] && OUTNAME=toybox
12 UNSTRIPPED="generated/unstripped/$(basename "$OUTNAME")"
13
14 # Since each cc invocation is short, launch half again as many processes
15 # as we have processors so they don't exit faster than we can start them.
16 [ -z "$CPUS" ] &&
17   CPUS=$((($(echo /sys/devices/system/cpu/cpu[0-9]* | wc -w)*3)/2))
18
19 # Respond to V= by echoing command lines as well as running them
20 DOTPROG=
21 do_loudly()
22 {
23   [ ! -z "$V" ] && echo "$@" || echo -n "$DOTPROG"
24   "$@"
25 }
26
27 # Is anything under directory $2 newer than file $1
28 isnewer()
29 {
30  [ ! -z "$(find "$2" -newer "$1" 2>/dev/null || echo yes)" ]
31 }
32
33 echo "Generate headers from toys/*/*.c..."
34
35 mkdir -p generated/unstripped
36
37 if isnewer generated/Config.in toys
38 then
39   echo "Extract configuration information from toys/*.c files..."
40   scripts/genconfig.sh
41 fi
42
43 # Create a list of all the commands toybox can provide. Note that the first
44 # entry is out of order on purpose (the toybox multiplexer command must be the
45 # first element of the array). The rest must be sorted in alphabetical order
46 # for fast binary search.
47
48 if isnewer generated/newtoys.h toys
49 then
50   echo -n "generated/newtoys.h "
51
52   echo "USE_TOYBOX(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 -s -k 1,1 \
55         | sed 's/[^ ]* //'  >> generated/newtoys.h || exit 1
56 fi
57
58 [ ! -z "$V" ] && echo "Which C files to build..."
59
60 # Extract a list of toys/*/*.c files to compile from the data in $KCONFIG_CONFIG
61 # (First command names, then filenames with relevant {NEW,OLD}TOY() macro.)
62
63 GITHASH="$(git describe --tags --abbrev=12 2>/dev/null)"
64 [ ! -z "$GITHASH" ] && GITHASH="-DTOYBOX_VERSION=\"$GITHASH\""
65 TOYFILES="$(sed -n 's/^CONFIG_\([^=]*\)=.*/\1/p' "$KCONFIG_CONFIG" | xargs | tr ' [A-Z]' '|[a-z]')"
66 TOYFILES="$(egrep -l "TOY[(]($TOYFILES)[ ,]" toys/*/*.c)"
67 CFLAGS="$CFLAGS $(cat generated/cflags)"
68 BUILD="$(echo ${CROSS_COMPILE}${CC} $CFLAGS -I . $OPTIMIZE $GITHASH)"
69 FILES="$(echo lib/*.c main.c $TOYFILES)"
70
71 if [ "${TOYFILES/pending//}" != "$TOYFILES" ]
72 then
73   echo -e "\n\033[1;31mwarning: using unfinished code from toys/pending\033[0m"
74 fi
75
76 genbuildsh()
77 {
78   # Write a canned build line for use on crippled build machines.
79
80   echo "#!/bin/sh"
81   echo
82   echo "BUILD=\"$BUILD\""
83   echo
84   echo "FILES=\"$FILES\""
85   echo
86   echo "LINK=\"$LINK\""
87   echo
88   echo
89   echo '$BUILD $FILES $LINK'
90 }
91
92 if ! cmp -s <(genbuildsh | head -n 3) \
93           <(head -n 3 generated/build.sh 2>/dev/null)
94 then
95   echo -n "Library probe"
96
97   # We trust --as-needed to remove each library if we don't use any symbols
98   # out of it, this loop is because the compiler has no way to ignore a library
99   # that doesn't exist, so we have to detect and skip nonexistent libraries
100   # for it.
101
102   > generated/optlibs.dat
103   for i in util crypt m resolv selinux smack attr rt
104   do
105     echo "int main(int argc, char *argv[]) {return 0;}" | \
106     ${CROSS_COMPILE}${CC} $CFLAGS -xc - -o generated/libprobe -Wl,--as-needed -l$i > /dev/null 2>/dev/null &&
107     echo -l$i >> generated/optlibs.dat
108     echo -n .
109   done
110   rm -f generated/libprobe
111   echo
112 fi
113
114 # LINK needs optlibs.dat, above
115
116 LINK="$(echo $LDOPTIMIZE $LDFLAGS -o "$UNSTRIPPED" -Wl,--as-needed $(cat generated/optlibs.dat))"
117 genbuildsh > generated/build.sh && chmod +x generated/build.sh || exit 1
118
119 echo "Make generated/config.h from $KCONFIG_CONFIG."
120
121 # This long and roundabout sed invocation is to make old versions of sed happy.
122 # New ones have '\n' so can replace one line with two without all the branches
123 # and tedious mucking about with hold space.
124
125 sed -n \
126   -e 's/^# CONFIG_\(.*\) is not set.*/\1/' \
127   -e 't notset' \
128   -e 's/^CONFIG_\(.*\)=y.*/\1/' \
129   -e 't isset' \
130   -e 's/^CONFIG_\([^=]*\)=\(.*\)/#define CFG_\1 \2/p' \
131   -e 'd' \
132   -e ':notset' \
133   -e 'h' \
134   -e 's/.*/#define CFG_& 0/p' \
135   -e 'g' \
136   -e 's/.*/#define USE_&(...)/p' \
137   -e 'd' \
138   -e ':isset' \
139   -e 'h' \
140   -e 's/.*/#define CFG_& 1/p' \
141   -e 'g' \
142   -e 's/.*/#define USE_&(...) __VA_ARGS__/p' \
143   $KCONFIG_CONFIG > generated/config.h || exit 1
144
145 if [ generated/mkflags -ot scripts/mkflags.c ]
146 then
147   do_loudly $HOSTCC scripts/mkflags.c -o generated/mkflags || exit 1
148 fi
149
150 echo -n "generated/flags.h "
151
152 # Process config.h and newtoys.h to generate FLAG_x macros. Note we must
153 # always #define the relevant macro, even when it's disabled, because we
154 # allow multiple NEWTOY() in the same C file. (When disabled the FLAG is 0,
155 # so flags&0 becomes a constant 0 allowing dead code elimination.)
156
157 # Parse files through C preprocessor twice, once to get flags for current
158 # .config and once to get flags for allyesconfig
159 for I in A B
160 do
161   (
162   # define macros and select header files with option string data
163
164   echo "#define NEWTOY(aa,bb,cc) aa $I bb"
165   echo '#define OLDTOY(...)'
166   if [ "$I" == A ]
167   then
168     cat generated/config.h
169   else
170     sed '/USE_.*([^)]*)$/s/$/ __VA_ARGS__/' generated/config.h
171   fi
172   cat generated/newtoys.h
173
174   # Run result through preprocessor, glue together " " gaps leftover from USE
175   # macros, delete comment lines, print any line with a quoted optstring,
176   # turn any non-quoted opstring (NULL or 0) into " " (because fscanf can't
177   # handle "" with nothing in it, and mkflags uses that).
178
179   ) | ${CROSS_COMPILE}${CC} -E - | \
180     sed -n -e 's/" *"//g;/^#/d;t clear;:clear;s/"/"/p;t;s/\( [AB] \).*/\1 " "/p'
181
182 # Sort resulting line pairs and glue them together into triplets of
183 #   command "flags" "allflags"
184 # to feed into mkflags C program that outputs actual flag macros
185 # If no pair (because command's disabled in config), use " " for flags
186 # so allflags can define the appropriate zero macros.
187
188 done | sort -s | sed -n 's/ A / /;t pair;h;s/\([^ ]*\).*/\1 " "/;x;b single;:pair;h;n;:single;s/[^ ]* B //;H;g;s/\n/ /;p' | tee generated/flags.raw | \
189 generated/mkflags > generated/flags.h || exit 1
190
191 # Extract global structure definitions and flag definitions from toys/*/*.c
192
193 function getglobals()
194 {
195   for i in toys/*/*.c
196   do
197     NAME="$(echo $i | sed 's@.*/\(.*\)\.c@\1@')"
198     DATA="$(sed -n -e '/^GLOBALS(/,/^)/b got;b;:got' \
199             -e 's/^GLOBALS(/struct '"$NAME"'_data {/' \
200             -e 's/^)/};/' -e 'p' $i)"
201
202     [ ! -z "$DATA" ] && echo -e "// $i\n\n$DATA\n"
203   done
204 }
205
206 if isnewer generated/globals.h toys
207 then
208   echo -n "generated/globals.h "
209   GLOBSTRUCT="$(getglobals)"
210   (
211     echo "$GLOBSTRUCT"
212     echo
213     echo "extern union global_union {"
214     echo "$GLOBSTRUCT" | \
215       sed -n 's/struct \(.*\)_data {/   struct \1_data \1;/p'
216     echo "} this;"
217   ) > generated/globals.h
218 fi
219
220 if [ generated/mktags -ot scripts/mktags.c ]
221 then
222   do_loudly $HOSTCC scripts/mktags.c -o generated/mktags || exit 1
223 fi
224
225 if isnewer generated/tags.h toys
226 then
227   echo -n "generated/tags.h "
228
229   sed -n '/TAGGED_ARRAY(/,/^)/{s/.*TAGGED_ARRAY[(]\([^,]*\),/\1/;p}' \
230     toys/*/*.c lib/*.c | generated/mktags > generated/tags.h
231 fi
232
233 echo "generated/help.h"
234 if [ generated/config2help -ot scripts/config2help.c ]
235 then
236   do_loudly $HOSTCC scripts/config2help.c -I . lib/xwrap.c lib/llist.c \
237     lib/lib.c lib/portability.c -o generated/config2help || exit 1
238 fi
239 generated/config2help Config.in $KCONFIG_CONFIG > generated/help.h || exit 1
240
241 [ ! -z "$NOBUILD" ] && exit 0
242
243 echo -n "Compile toybox"
244 [ ! -z "$V" ] && echo
245 DOTPROG=.
246
247 # This is a parallel version of: do_loudly $BUILD $FILES $LINK || exit 1
248
249 X="$(ls -1t generated/obj/* 2>/dev/null | tail -n 1)"
250 if [ ! -e "$X" ] || [ ! -z "$(find toys -name "*.h" -newer "$X")" ]
251 then
252   rm -rf generated/obj && mkdir -p generated/obj || exit 1
253 else
254   rm -f generated/obj/{main,lib_help}.o || exit 1
255 fi
256 PENDING=
257 LFILES=
258 DONE=0
259 COUNT=0
260 for i in $FILES
261 do
262   # build each generated/obj/*.o file in parallel
263
264   X=${i/lib\//lib_}
265   X=${X##*/}
266   OUT="generated/obj/${X%%.c}.o"
267   LFILES="$LFILES $OUT"
268   [ "$OUT" -nt "$i" ] && continue
269   do_loudly $BUILD -c $i -o $OUT &
270   PENDING="$PENDING $!"
271   COUNT=$(($COUNT+1))
272
273   # ratelimit to $CPUS many parallel jobs, detecting errors
274
275   for j in $PENDING
276   do
277     [ "$COUNT" -lt "$CPUS" ] && break;
278
279     wait $j
280     DONE=$(($DONE+$?))
281     COUNT=$(($COUNT-1))
282     PENDING="${PENDING## $j}"
283   done
284   [ $DONE -ne 0 ] && break
285 done
286
287 # wait for all background jobs, detecting errors
288
289 for i in $PENDING
290 do
291   wait $i
292   DONE=$(($DONE+$?))
293 done
294
295 [ $DONE -ne 0 ] && exit 1
296
297 do_loudly $BUILD $LFILES $LINK || exit 1
298 if [ ! -z "$NOSTRIP" ] ||
299   ! do_loudly ${CROSS_COMPILE}strip "$UNSTRIPPED" -o "$OUTNAME"
300 then
301   echo "strip failed, using unstripped" && cp "$UNSTRIPPED" "$OUTNAME" ||
302   exit 1
303 fi
304
305 # gcc 4.4's strip command is buggy, and doesn't set the executable bit on
306 # its output the way SUSv4 suggests it do so. While we're at it, make sure
307 # we don't have the "w" bit set so things like bzip2's "cp -f" install don't
308 # overwrite our binary through the symlink.
309 do_loudly chmod 555 "$OUTNAME" || exit 1
310
311 echo