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