OSDN Git Service

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