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