1#!/bin/bash 2 3# Grab default values for $CFLAGS and such. 4set -o pipefail 5source scripts/portability.sh 6 7# Shell functions called by the build 8 9DASHN=-n 10true & wait -n 2>/dev/null || { wait; unset DASHN; } 11ratelimit() 12{ 13 if [ "$#" -eq 0 ] 14 then 15 [ -z "$DASHN" ] && PIDS="$PIDS$! " 16 [ $((++COUNT)) -lt $CPUS ] && return 0 17 fi 18 ((--COUNT)) 19 if [ -n "$DASHN" ] 20 then 21 wait -n 22 DONE=$(($DONE+$?)) 23 else 24 # MacOS uses an ancient version of bash which hasn't got "wait -n", and 25 # wait without arguments always returns 0 instead of process exit code. 26 # This keeps $CPUS less busy when jobs finish out of order. 27 wait ${PIDS%% *} 28 DONE=$(($DONE+$?)) 29 PIDS=${PIDS#* } 30 fi 31 32 return $DONE 33} 34 35# Respond to V= by echoing command lines as well as running them 36do_loudly() 37{ 38 { [ -n "$V" ] && echo "$@" || echo -n "$DOTPROG" ; } >&2 39 "$@" 40} 41 42# Is anything under directory $2 newer than generated/$1 (or does it not exist)? 43isnewer() 44{ 45 [ -e "$GENDIR/$1" ] && [ -z "$(find "${@:2}" -newer "$GENDIR/$1")" ] && 46 return 1 47 echo -n "${DIDNEWER:-$GENDIR/{}$1" 48 DIDNEWER=, 49} 50 51# Build a tool that runs on the host 52hostcomp() 53{ 54 if [ ! -f "$UNSTRIPPED"/$1 ] || [ "$UNSTRIPPED"/$1 -ot scripts/$1.c ] 55 then 56 do_loudly $HOSTCC scripts/$1.c -o "$UNSTRIPPED"/$1 || exit 1 57 fi 58} 59 60# --as-needed removes libraries we don't use any symbols out of, but the 61# compiler has no way to ignore a library that doesn't exist, so detect 62# and skip nonexistent libraries for it (probing in parallel). 63LIBRARIES=$( 64 [ -z "$V" ] && X=/dev/null || X=/dev/stderr 65 for i in util crypt m resolv selinux smack attr crypto z log iconv tls ssl 66 do 67 do_loudly ${CROSS_COMPILE}${CC} $CFLAGS $LDFLAGS -xc - -l$i >>$X 2>&1 \ 68 -o /dev/null <<<"int main(int argc,char*argv[]){return 0;}" && 69 echo -l$i & 70 done | sort | xargs 71) 72# Actually resolve dangling dependencies in extra libraries when static linking 73[ -n "$LIBRARIES" ] && [ "$LDFLAGS" != "${LDFLAGS/-static/}" ] && 74 LIBRARIES="-Wl,--start-group $LIBRARIES -Wl,--end-group" 75 76[ -z "$VERSION" ] && [ -d ".git" ] && [ -n "$(which git 2>/dev/null)" ] && 77 VERSION="$(git describe --tags --abbrev=12 2>/dev/null)" 78 79# Set/record build environment information 80compflags() 81{ 82 # The #d lines tag dependencies that force full rebuild if changed 83 echo '#!/bin/sh' 84 echo 85 echo "VERSION='$VERSION'" 86 echo "LIBRARIES='$LIBRARIES'" 87 echo "BUILD='${CROSS_COMPILE}${CC} $CFLAGS -I . $OPTIMIZE" \ 88 "'\"\${VERSION:+-DTOYBOX_VERSION=\\\"$VERSION\\\"}\" #d" 89 echo "LINK='$LDOPTIMIZE $LDFLAGS '\"\$LIBRARIES\" #d" 90 echo "#d Config was $KCONFIG_CONFIG" 91 echo "#d PATH was '$PATH'" 92} 93 94# Make sure rm -rf isn't gonna go funny 95B="$(readlink -f "$PWD")/" A="$(readlink -f "$GENDIR")" A="${A%/}"/ 96[ "$A" == "${B::${#A}}" ] && 97 { echo "\$GENDIR=$GENDIR cannot include \$PWD=$PWD"; exit 1; } 98unset A B DOTPROG DIDNEWER 99 100# Force full rebuild if our compiler/linker options changed 101cmp -s <(compflags | grep '#d') <(grep '%d' "$GENDIR"/build.sh 2>/dev/null) || 102 rm -rf "$GENDIR"/* # Keep symlink, delete contents 103mkdir -p "$UNSTRIPPED" "$(dirname $OUTNAME)" || exit 1 104 105# Extract a list of toys/*/*.c files to compile from the data in $KCONFIG_CONFIG 106# (First command names, then filenames with relevant {NEW,OLD}TOY() macro.) 107 108[ -n "$V" ] && echo -e "\nWhich C files to build..." 109TOYFILES="$($SED -n 's/^CONFIG_\([^=]*\)=.*/\1/p' "$KCONFIG_CONFIG" | xargs | tr ' ' '|')" 110TOYFILES="main.c $(egrep -l "^USE_($TOYFILES)[(]...TOY[(]" toys/*/*.c | xargs)" 111 112if [ "${TOYFILES/pending//}" != "$TOYFILES" ] 113then 114 echo -e "\n\033[1;31mwarning: using unfinished code from toys/pending\033[0m" 115fi 116 117# Write build variables (and set them locally), then append build invocation. 118compflags > "$GENDIR"/build.sh && source "$GENDIR/build.sh" && 119 { 120 echo FILES=$'"\n'"$(fold -s <<<"$TOYFILES")"$'\n"' && 121 echo && 122 echo -e "\$BUILD lib/*.c \$FILES \$LINK -o $OUTNAME" 123 } >> "$GENDIR"/build.sh && 124 chmod +x "$GENDIR"/build.sh || exit 1 125 126if isnewer Config.in toys || isnewer Config.in Config.in 127then 128 scripts/genconfig.sh 129fi 130 131# Does .config need dependency recalculation because toolchain changed? 132A="$($SED -n '/^config .*$/h;s/default \(.\)/\1/;T;H;g;s/config \([^\n]*\)[^yn]*\(.\)/\1=\2/p' "$GENDIR"/Config.probed | sort)" 133B="$(egrep "^CONFIG_($(echo "$A" | sed 's/=[yn]//' | xargs | tr ' ' '|'))=" "$KCONFIG_CONFIG" | $SED 's/^CONFIG_//' | sort)" 134A="$(echo "$A" | grep -v =n)" 135[ "$A" != "$B" ] && 136 { echo -e "\nWarning: Config.probed changed, run 'make oldconfig'" >&2; } 137unset A B 138 139# Create a list of all the commands toybox can provide. 140if isnewer newtoys.h toys 141then 142 # The multiplexer is the first element in the array 143 echo "USE_TOYBOX(NEWTOY(toybox, 0, TOYFLAG_STAYROOT|TOYFLAG_NOHELP))" \ 144 > "$GENDIR"/newtoys.h 145 # Sort rest by name for binary search (copy name to front, sort, remove copy) 146 $SED -n 's/^\(USE_[^(]*(.*TOY(\)\([^,]*\)\(,.*\)/\2 \1\2\3/p' toys/*/*.c \ 147 | sort -s -k 1,1 | $SED 's/[^ ]* //' >> "$GENDIR"/newtoys.h 148 [ $? -ne 0 ] && exit 1 149fi 150 151# Rebuild config.h from .config 152$SED -En $KCONFIG_CONFIG > "$GENDIR"/config.h \ 153 -e 's/^# CONFIG_(.*) is not set.*/#define CFG_\1 0\n#define USE_\1(...)/p;t' \ 154 -e 's/^CONFIG_(.*)=y.*/#define CFG_\1 1\n#define USE_\1(...) __VA_ARGS__/p;t'\ 155 -e 's/^CONFIG_(.*)=/#define CFG_\1 /p' || exit 1 156 157# Process config.h and newtoys.h to generate FLAG_x macros. Note we must 158# always #define the relevant macro, even when it's disabled, because we 159# allow multiple NEWTOY() in the same C file. (When disabled the FLAG is 0, 160# so flags&0 becomes a constant 0 allowing dead code elimination.) 161 162hostcomp mkflags 163if isnewer flags.h toys "$KCONFIG_CONFIG" 164then 165 # Parse files through C preprocessor twice, once to get flags for current 166 # .config and once to get flags for allyesconfig 167 for I in A B 168 do 169 ( 170 # define macros and select header files with option string data 171 172 echo "#define NEWTOY(aa,bb,cc) aa $I bb" 173 echo '#define OLDTOY(...)' 174 if [ "$I" == A ] 175 then 176 cat "$GENDIR"/config.h 177 else 178 $SED '/USE_.*([^)]*)$/s/$/ __VA_ARGS__/' "$GENDIR"/config.h 179 fi 180 echo '#include "lib/toyflags.h"' 181 cat "$GENDIR"/newtoys.h 182 183 # Run result through preprocessor, glue together " " gaps leftover from USE 184 # macros, delete comment lines, print any line with a quoted optstring, 185 # turn any non-quoted opstring (NULL or 0) into " " (because fscanf can't 186 # handle "" with nothing in it, and mkflags uses that). 187 188 ) | ${CROSS_COMPILE}${CC} -E - | \ 189 $SED -n -e 's/" *"//g;/^#/d;t clear;:clear;s/"/"/p;t;s/\( [AB] \).*/\1 " "/p' 190 191 # Sort resulting line pairs and glue them together into triplets of 192 # command "flags" "allflags" 193 # to feed into mkflags C program that outputs actual flag macros 194 # If no pair (because command's disabled in config), use " " for flags 195 # so allflags can define the appropriate zero macros. 196 197 done | sort -s | $SED -n -e 's/ A / /;t pair;h;s/\([^ ]*\).*/\1 " "/;x' \ 198 -e 'b single;:pair;h;n;:single;s/[^ ]* B //;H;g;s/\n/ /;p' | \ 199 tee "$GENDIR"/flags.raw | "$UNSTRIPPED"/mkflags > "$GENDIR"/flags.h || exit 1 200fi 201 202# Extract global structure definitions and flag definitions from toys/*/*.c 203 204{ 205 STRUX="$($SED -ne 's/^#define[ \t]*FOR_\([^ \t]*\).*/\1/;T s1;h;:s1' \ 206 -e '/^GLOBALS(/,/^)/{s/^GLOBALS(//;T s2;g;s/.*/struct &_data {/;:s2;s/^)/};\n/;p}' \ 207 $TOYFILES)" 208 echo "$STRUX" && 209 echo "extern union global_union {" && 210 $SED -n 's/^struct \(.*\)_data .*/\1/;T;s/.*/\tstruct &_data &;/p' \ 211 <<<"$STRUX" && 212 echo "} this;" 213} > "$GENDIR"/globals.h || exit 1 214 215# Recreate tags.h 216$SED -ne '/TAGGED_ARRAY(/,/^)/{s/.*TAGGED_ARRAY[(]\([^,]*\),/\1/p' \ 217 -e 's/[^{]*{"\([^"]*\)"[^{]*/ _\1/gp}' toys/*/*.c | tr '[:punct:]' _ | \ 218while read i; do 219 [ "$i" = "${i#_}" ] && { HEAD="$i"; X=0; LL=; continue;} 220 for j in $i; do 221 [ $X -eq 31 ] && LL=LL 222 NAME="$HEAD$j" 223 printf "#define $NAME %*s%s\n#define _$NAME %*s%s\n" \ 224 $((32-${#NAME})) "" "$X" $((31-${#NAME})) "" "(1$LL<<$((X++)))" || exit 1 225 done 226done > "$GENDIR"/tags.h || exit 1 227 228# Create help.h, and zhelp.h if zcat enabled 229hostcomp config2help 230if isnewer help.h "$GENDIR"/Config.in 231then 232 "$UNSTRIPPED"/config2help Config.in $KCONFIG_CONFIG > "$GENDIR"/help.h||exit 1 233fi 234 235if grep -qx 'CONFIG_TOYBOX_ZHELP=y' "$KCONFIG_CONFIG" 236then 237 do_loudly $HOSTCC -I . scripts/install.c -o "$UNSTRIPPED"/instlist || exit 1 238 { echo "#define ZHELP_LEN $("$UNSTRIPPED"/instlist --help | wc -c)" && 239 "$UNSTRIPPED"/instlist --help | gzip -9 | od -Anone -vtx1 | \ 240 sed 's/ /,0x/g;1s/^,/static char zhelp_data[] = {\n /;$s/.*/&};/' 241 } > "$GENDIR"/zhelp.h || exit 1 242else 243 rm -f "$GENDIR"/zhelp.h 244fi 245 246[ -z "$DIDNEWER" ] || echo } 247[ -n "$NOBUILD" ] && exit 0 248 249echo "Compile $OUTNAME" 250DOTPROG=. 251 252# This is a parallel version of: do_loudly $BUILD lib/*.c $TOYFILES $LINK 253 254# Build all if oldest generated/obj file isn't newer than all header files. 255X="$(ls -1t "$GENDIR"/obj/* 2>/dev/null | tail -n 1)" 256if [ ! -e "$X" ] || [ -n "$(find toys -name "*.h" -newer "$X")" ] 257then 258 rm -rf "$GENDIR"/obj && mkdir -p "$GENDIR"/obj || exit 1 259else 260 # always redo toy_list[] and help_data[] 261 rm -f "$GENDIR"/obj/main.o || exit 1 262fi 263 264# build each generated/obj/*.o file in parallel 265 266PENDING= LNKFILES= CLICK= DONE=0 COUNT=0 267for i in lib/*.c click $TOYFILES 268do 269 [ "$i" == click ] && CLICK=1 && continue 270 271 X=${i/lib\//lib_} 272 X=${X##*/} 273 OUT="$GENDIR/obj/${X%%.c}.o" 274 LNKFILES="$LNKFILES $OUT" 275 276 # Library files don't get rebuilt if older than .config, but commands do. 277 [ "$OUT" -nt "$i" ] && [ -z "$CLICK" -o "$OUT" -nt "$KCONFIG_CONFIG" ] && 278 continue 279 280 do_loudly $BUILD -c $i -o $OUT & 281 282 ratelimit || break 283done 284 285# wait for all background jobs, detecting errors 286while [ "$COUNT" -gt 0 ] 287do 288 ratelimit done 289done 290[ $DONE -ne 0 ] && exit 1 291 292UNSTRIPPED="$UNSTRIPPED/${OUTNAME/*\//}" 293do_loudly $BUILD $LNKFILES $LINK -o "$UNSTRIPPED" || exit 1 294if [ -n "$NOSTRIP" ] || 295 ! do_loudly ${CROSS_COMPILE}${STRIP} "$UNSTRIPPED" -o "$OUTNAME" 296then 297 [ -z "$NOSTRIP" ] && echo "strip failed, using unstripped" 298 rm -f "$OUTNAME" && 299 cp "$UNSTRIPPED" "$OUTNAME" || exit 1 300fi 301 302# Remove write bit set so buggy installs (like bzip's) don't overwrite the 303# multiplexer binary via truncate-and-write through a symlink. 304do_loudly chmod 555 "$OUTNAME" || exit 1 305 306# Ensure make wrapper sees success return code 307[ -z "$V" ] && echo >&2 || true 308