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