10f66f451Sopenharmony_ci#!/bin/bash
20f66f451Sopenharmony_ci
30f66f451Sopenharmony_ci# Grab default values for $CFLAGS and such.
40f66f451Sopenharmony_ci
50f66f451Sopenharmony_cisource ./configure
60f66f451Sopenharmony_ci
70f66f451Sopenharmony_ci[ -z "$PREFIX" ] && PREFIX="/usr/toybox"
80f66f451Sopenharmony_ci
90f66f451Sopenharmony_ci# Parse command line arguments.
100f66f451Sopenharmony_ci
110f66f451Sopenharmony_ciLONG_PATH=""
120f66f451Sopenharmony_ciwhile [ ! -z "$1" ]
130f66f451Sopenharmony_cido
140f66f451Sopenharmony_ci  # Create symlinks instead of hardlinks?
150f66f451Sopenharmony_ci  [ "$1" == "--symlink" ] && LINK_TYPE="-s"
160f66f451Sopenharmony_ci
170f66f451Sopenharmony_ci  # Uninstall?
180f66f451Sopenharmony_ci  [ "$1" == "--uninstall" ] && UNINSTALL=Uninstall
190f66f451Sopenharmony_ci
200f66f451Sopenharmony_ci  # Delete destination command if it exists?
210f66f451Sopenharmony_ci  [ "$1" == "--force" ] && DO_FORCE="-f"
220f66f451Sopenharmony_ci
230f66f451Sopenharmony_ci  # Use {,usr}/{bin,sbin} paths instead of all files in one directory?
240f66f451Sopenharmony_ci  [ "$1" == "--long" ] && LONG_PATH="bin/"
250f66f451Sopenharmony_ci
260f66f451Sopenharmony_ci  # Symlink host toolchain binaries to destination to create cross compile $PATH
270f66f451Sopenharmony_ci  [ "$1" == "--airlock" ] && AIRLOCK=1
280f66f451Sopenharmony_ci
290f66f451Sopenharmony_ci  shift
300f66f451Sopenharmony_cidone
310f66f451Sopenharmony_ci
320f66f451Sopenharmony_ciecho "Compile instlist..."
330f66f451Sopenharmony_ci
340f66f451Sopenharmony_ciNOBUILD=1 scripts/make.sh
350f66f451Sopenharmony_ci$DEBUG $HOSTCC -I . scripts/install.c -o generated/instlist || exit 1
360f66f451Sopenharmony_ciCOMMANDS="$(generated/instlist $LONG_PATH)"
370f66f451Sopenharmony_ci
380f66f451Sopenharmony_ciecho "${UNINSTALL:-Install} commands..."
390f66f451Sopenharmony_ci
400f66f451Sopenharmony_ci# Copy toybox itself
410f66f451Sopenharmony_ci
420f66f451Sopenharmony_ciif [ -z "$UNINSTALL" ]
430f66f451Sopenharmony_cithen
440f66f451Sopenharmony_ci  mkdir -p "${PREFIX}/${LONG_PATH}" &&
450f66f451Sopenharmony_ci  rm -f "${PREFIX}/${LONG_PATH}/toybox" &&
460f66f451Sopenharmony_ci  cp toybox ${PREFIX}/${LONG_PATH} || exit 1
470f66f451Sopenharmony_cielse
480f66f451Sopenharmony_ci  rm -f "${PREFIX}/${LONG_PATH}/toybox" 2>/dev/null
490f66f451Sopenharmony_cifi
500f66f451Sopenharmony_cicd "$PREFIX" || exit 1
510f66f451Sopenharmony_ci
520f66f451Sopenharmony_ci# Make links to toybox
530f66f451Sopenharmony_ci
540f66f451Sopenharmony_ciEXIT=0
550f66f451Sopenharmony_ci
560f66f451Sopenharmony_cifor i in $COMMANDS
570f66f451Sopenharmony_cido
580f66f451Sopenharmony_ci  # Figure out target of link
590f66f451Sopenharmony_ci
600f66f451Sopenharmony_ci  if [ -z "$LONG_PATH" ]
610f66f451Sopenharmony_ci  then
620f66f451Sopenharmony_ci    DOTPATH=""
630f66f451Sopenharmony_ci  else
640f66f451Sopenharmony_ci    # Create subdirectory for command to go in (if necessary)
650f66f451Sopenharmony_ci
660f66f451Sopenharmony_ci    DOTPATH="$(dirname "$i")"/
670f66f451Sopenharmony_ci    if [ -z "$UNINSTALL" ]
680f66f451Sopenharmony_ci    then
690f66f451Sopenharmony_ci      mkdir -p "$DOTPATH" || exit 1
700f66f451Sopenharmony_ci    fi
710f66f451Sopenharmony_ci
720f66f451Sopenharmony_ci    if [ -z "$LINK_TYPE" ]
730f66f451Sopenharmony_ci    then
740f66f451Sopenharmony_ci      DOTPATH="bin/"
750f66f451Sopenharmony_ci    else
760f66f451Sopenharmony_ci      if [ "$DOTPATH" != "$LONG_PATH" ]
770f66f451Sopenharmony_ci      then
780f66f451Sopenharmony_ci        # For symlinks we need ../../bin style relative paths
790f66f451Sopenharmony_ci        DOTPATH="$(echo $DOTPATH | sed -e 's@[^/]*/@../@g')"$LONG_PATH
800f66f451Sopenharmony_ci      else
810f66f451Sopenharmony_ci        DOTPATH=""
820f66f451Sopenharmony_ci      fi
830f66f451Sopenharmony_ci    fi
840f66f451Sopenharmony_ci  fi
850f66f451Sopenharmony_ci
860f66f451Sopenharmony_ci  # Create link
870f66f451Sopenharmony_ci  if [ -z "$UNINSTALL" ]
880f66f451Sopenharmony_ci  then
890f66f451Sopenharmony_ci    ln $DO_FORCE $LINK_TYPE ${DOTPATH}toybox $i || EXIT=1
900f66f451Sopenharmony_ci  else
910f66f451Sopenharmony_ci    rm -f $i || EXIT=1
920f66f451Sopenharmony_ci  fi
930f66f451Sopenharmony_cidone
940f66f451Sopenharmony_ci
950f66f451Sopenharmony_ci[ -z "$AIRLOCK" ] && exit 0
960f66f451Sopenharmony_ci
970f66f451Sopenharmony_ci# --airlock creates a single directory you can point the $PATH to for cross
980f66f451Sopenharmony_ci# compiling, which contains just toybox and symlinks to toolchain binaries.
990f66f451Sopenharmony_ci
1000f66f451Sopenharmony_ci# This not only means you're building with a known set of tools (insulated from
1010f66f451Sopenharmony_ci# variations in the host distro), but that everything else is NOT in your PATH
1020f66f451Sopenharmony_ci# and thus various configure stages won't find things on thie host that won't
1030f66f451Sopenharmony_ci# be there on the target (such as the distcc build noticing the host has
1040f66f451Sopenharmony_ci# python and deciding to #include Python.h).
1050f66f451Sopenharmony_ci
1060f66f451Sopenharmony_ci# The following are commands toybox should provide, but doesn't yet.
1070f66f451Sopenharmony_ci# For now symlink the host version. This list must go away by 1.0.
1080f66f451Sopenharmony_ci
1090f66f451Sopenharmony_ciPENDING="dd diff expr ftpd less tr vi wget awk sh sha512sum sha256sum unxz xzcat bc bison flex make nm ar gzip"
1100f66f451Sopenharmony_ci
1110f66f451Sopenharmony_ci# "gcc" should go away for llvm, but some things still hardwire it
1120f66f451Sopenharmony_ciTOOLCHAIN="as cc ld gcc objdump"
1130f66f451Sopenharmony_ci
1140f66f451Sopenharmony_ciif [ ! -z "$AIRLOCK" ]
1150f66f451Sopenharmony_cithen
1160f66f451Sopenharmony_ci
1170f66f451Sopenharmony_ci  # Tools needed to build packages
1180f66f451Sopenharmony_ci  for i in $TOOLCHAIN $PENDING $HOST_EXTRA
1190f66f451Sopenharmony_ci  do
1200f66f451Sopenharmony_ci    if [ ! -f "$i" ]
1210f66f451Sopenharmony_ci  then
1220f66f451Sopenharmony_ci    # Loop through each instance, populating fallback directories (used by
1230f66f451Sopenharmony_ci    # things like distcc, which require multiple instances of the same binary
1240f66f451Sopenharmony_ci    # in a known order in the $PATH).
1250f66f451Sopenharmony_ci
1260f66f451Sopenharmony_ci    X=0
1270f66f451Sopenharmony_ci    FALLBACK="$PREFIX"
1280f66f451Sopenharmony_ci    which -a "$i" | while read j
1290f66f451Sopenharmony_ci    do
1300f66f451Sopenharmony_ci      if [ ! -e "$FALLBACK/$i" ]
1310f66f451Sopenharmony_ci      then
1320f66f451Sopenharmony_ci        mkdir -p "$FALLBACK" &&
1330f66f451Sopenharmony_ci        ln -sf "$j" "$FALLBACK/$i" || exit 1
1340f66f451Sopenharmony_ci      fi
1350f66f451Sopenharmony_ci
1360f66f451Sopenharmony_ci      X=$[$X+1]
1370f66f451Sopenharmony_ci      FALLBACK="$PREFIX/fallback-$X"
1380f66f451Sopenharmony_ci    done
1390f66f451Sopenharmony_ci
1400f66f451Sopenharmony_ci    if [ ! -f "$PREFIX/$i" ]
1410f66f451Sopenharmony_ci    then
1420f66f451Sopenharmony_ci      echo "Toolchain component missing: $i" >&2
1430f66f451Sopenharmony_ci      [ -z "$PEDANTIC" ] || EXIT=1
1440f66f451Sopenharmony_ci    fi
1450f66f451Sopenharmony_ci  fi
1460f66f451Sopenharmony_cidone
1470f66f451Sopenharmony_ci
1480f66f451Sopenharmony_ci
1490f66f451Sopenharmony_ci
1500f66f451Sopenharmony_cifi
1510f66f451Sopenharmony_ci
1520f66f451Sopenharmony_ciexit $EXIT
153