Home | History | Annotate | Download | only in tools
      1 #!/bin/sh
      2 #
      3 # Copyright (C) 2011 The Android Open Source Project
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 #      http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 #
     17 # Rebuild all host toolchains and programs
     18 #
     19 
     20 PROGDIR=$(dirname $0)
     21 . $PROGDIR/prebuilt-common.sh
     22 
     23 NDK_DIR=$ANDROID_NDK_ROOT
     24 register_var_option "--ndk-dir=<path>" NDK_DIR "NDK installation directory"
     25 
     26 SYSTEMS=$HOST_TAG
     27 # Replace x86_64 by x86 at the end of SYSTEMS since we will produce
     28 # 32-bit toolchains by default, i.e. unless you use the --try-64 flag
     29 if [ "${SYSTEMS%%x86_64}" != "$SYSTEMS" ]; then
     30     SYSTEMS=${SYSTEMS%%x86_64}x86
     31 fi
     32 # If we are on Linux, we are able to generate the Windows binaries
     33 # with the mingw32 cross-toolchain.
     34 if [ "$SYSTEMS" = "linux-x86" ]; then
     35     SYSTEMS=$SYSTEMS",windows"
     36     # If darwin toolchain exist, build darwin too
     37     if [ -f "${DARWIN_TOOLCHAIN}-gcc" ]; then
     38         SYSTEMS=$SYSTEMS",darwin-x86"
     39     fi
     40 fi
     41 CUSTOM_SYSTEMS=
     42 register_option "--systems=<names>" do_SYSTEMS "List of host systems to build for"
     43 do_SYSTEMS () { CUSTOM_SYSTEMS=true; SYSTEMS=$1; }
     44 
     45 ARCHS=$(find_ndk_unknown_archs)
     46 ARCHS="$DEFAULT_ARCHS $ARCHS"
     47 register_var_option "--arch=<list>" ARCHS "List of target archs to build for"
     48 
     49 PACKAGE_DIR=
     50 register_var_option "--package-dir=<path>" PACKAGE_DIR "Package toolchain into this directory"
     51 
     52 DARWIN_SSH=
     53 register_var_option "--darwin-ssh=<hostname>" DARWIN_SSH "Generate darwin packages on remote host"
     54 
     55 NO_GEN_PLATFORMS=
     56 register_var_option "--no-gen-platforms" NO_GEN_PLATFORMS "Don't generate platforms/ directory, use existing one"
     57 
     58 GCC_VERSION_LIST="default" # it's arch defined by default so use default keyword
     59 register_var_option "--gcc-version-list=<vers>" GCC_VERSION_LIST "List of GCC release versions"
     60 
     61 LLVM_VERSION_LIST=$DEFAULT_LLVM_VERSION_LIST
     62 register_var_option "--llvm-version-list=<vers>" LLVM_VERSION_LIST "List of LLVM release versions"
     63 
     64 CHECK_FLAG=
     65 do_check_option () { CHECK_FLAG="--check"; }
     66 register_option "--check" do_check_option "Check host prebuilts"
     67 
     68 register_try64_option
     69 
     70 PROGRAM_PARAMETERS="<toolchain-src-dir>"
     71 PROGRAM_DESCRIPTION=\
     72 "This script can be used to rebuild all the host NDK toolchains at once.
     73 You need to give it the path to the toolchain source directory, as
     74 downloaded by the 'download-toolchain-sources.sh' dev-script."
     75 
     76 extract_parameters "$@"
     77 
     78 # Check toolchain source path
     79 SRC_DIR="$PARAMETERS"
     80 if [ -z "$SRC_DIR" ]; then
     81     echo "ERROR: Please provide the path to the toolchain source tree. See --help"
     82     exit 1
     83 fi
     84 
     85 if [ ! -d "$SRC_DIR" ]; then
     86     echo "ERROR: Not a directory: '$SRC_DIR'"
     87     exit 1
     88 fi
     89 
     90 if [ ! -f "$SRC_DIR/build/configure" -o ! -d "$SRC_DIR/gcc/gcc-$DEFAULT_GCC32_VERSION" -o ! -d "$SRC_DIR/gcc/gcc-$DEFAULT_GCC64_VERSION" ]; then
     91     echo "ERROR: The file $SRC_DIR/build/configure or"
     92     echo "       the directory $SRC_DIR/gcc/gcc-$DEFAULT_GCC32_VERSION or"
     93     echo "       $SRC_DIR/gcc/gcc-$DEFAULT_GCC64_VERSION does not exist"
     94     echo "This is not the top of a toolchain tree: $SRC_DIR"
     95     echo "You must give the path to a copy of the toolchain source directories"
     96     echo "created by 'download-toolchain-sources.sh."
     97     exit 1
     98 fi
     99 
    100 # Now we can do the build
    101 BUILDTOOLS=$ANDROID_NDK_ROOT/build/tools
    102 
    103 if [ -z "$NO_GEN_PLATFORMS" ]; then
    104     echo "Preparing the build..."
    105     run $BUILDTOOLS/gen-platforms.sh --minimal --dst-dir=$NDK_DIR --ndk-dir=$NDK_DIR --arch=$(spaces_to_commas $ARCHS)
    106     fail_panic "Could not generate minimal sysroot!"
    107 else
    108     if [ ! -d "$NDK_DIR/platforms" ]; then
    109         echo "ERROR: --no-gen-platforms used but directory missing: $NDK_DIR/platforms"
    110         exit 1
    111     fi
    112 fi
    113 
    114 SYSTEMS=$(commas_to_spaces $SYSTEMS)
    115 ARCHS=$(commas_to_spaces $ARCHS)
    116 
    117 # Detect unknown arch
    118 UNKNOWN_ARCH=$(filter_out "$DEFAULT_ARCHS" "$ARCHS")
    119 if [ ! -z "$UNKNOWN_ARCH" ]; then
    120     ARCHS=$(filter_out "$UNKNOWN_ARCH" "$ARCHS")
    121 fi
    122 
    123 LLVM_VERSION_LIST=$(commas_to_spaces $LLVM_VERSION_LIST)
    124 
    125 if [ "$DARWIN_SSH" -a -z "$CUSTOM_SYSTEMS" ]; then
    126     SYSTEMS=" darwin-x86"
    127 fi
    128 
    129 FLAGS=
    130 if [ "$VERBOSE" = "yes" ]; then
    131     FLAGS=$FLAGS" --verbose"
    132 fi
    133 if [ "$VERBOSE2" = "yes" ]; then
    134     FLAGS=$FLAGS" --verbose"
    135 fi
    136 if [ "$TRY64" = "yes" ]; then
    137     FLAGS=$FLAGS" --try-64"
    138 else
    139     force_32bit_binaries
    140 fi
    141 if [ "$PACKAGE_DIR" ]; then
    142     mkdir -p "$PACKAGE_DIR"
    143     fail_panic "Could not create package directory: $PACKAGE_DIR"
    144     FLAGS=$FLAGS" --package-dir=$PACKAGE_DIR"
    145 fi
    146 
    147 do_remote_host_build ()
    148 {
    149     local SYSTEM="$1"
    150     local ARCH="$2"
    151     local REMOTE_HOST="$3"
    152 
    153     # 1/ Copy the NDK toolchain build scripts
    154     # 2/ Copy the toolchain sources/package
    155     # 3/ Ssh to unpack the build scripts, and run them
    156     # 4/ Copy back the generated prebuilt binaries
    157     #
    158     dump "Preparing remote build on $REMOTE_HOST..."
    159 
    160     # First try to create a remote temp directory on the remote host
    161     # Do it first so we can fail fast, not after spending time preparing
    162     # large tarballs.
    163     dump "Creating remote temp directory"
    164     TMPREMOTE=/tmp/ndk-$USER/darwin-prebuild
    165     run ssh $REMOTE_HOST "mkdir -p $TMPREMOTE && rm -rf $TMPREMOTE/*"
    166     fail_panic "Could not create temporary build directory on $REMOTE_HOST"
    167 
    168     # Now, package all the stuff we're going to send in a temporary
    169     # directory here on the machine, except toolchain sources
    170     TMPDARWIN=$NDK_TMPDIR/darwin  # Where we're going to package stuff
    171     log "Using temporary work directory: $TMPDARWIN"
    172     mkdir -p "$TMPDARWIN"
    173     dump "Prepare NDK build scripts"
    174     copy_directory "$ANDROID_NDK_ROOT/build" "$TMPDARWIN/ndk/build"
    175     copy_file_list "$ANDROID_NDK_ROOT" "$TMPDARWIN/ndk" sources/android/libthread_db
    176     copy_file_list "$ANDROID_NDK_ROOT" "$TMPDARWIN/ndk" "$STLPORT_SUBDIR"
    177     copy_file_list "$ANDROID_NDK_ROOT" "$TMPDARWIN/ndk" "$GABIXX_SUBDIR"
    178     copy_file_list "$ANDROID_NDK_ROOT" "$TMPDARWIN/ndk" sources/host-tools
    179     dump "Prepare platforms files"
    180     copy_directory "$NDK_DIR/platforms" "$TMPDARWIN/ndk/platforms"
    181     dump "Copying NDK build scripts and platform files to remote..."
    182     (cd "$TMPDARWIN" && tar czf - ndk) | (ssh $REMOTE_HOST tar xzf - -C $TMPREMOTE)
    183     fail_panic "Could not copy!"
    184     rm -rf $TMPDARWIN
    185 
    186     # Copy the toolchain sources
    187     dump "Copy toolchain sources to remote"
    188     ssh $REMOTE_HOST mkdir -p $TMPREMOTE/toolchain &&
    189     (cd "$SRC_DIR" && tar czf - .) | (ssh $REMOTE_HOST tar xzf - -C $TMPREMOTE/toolchain)
    190     fail_panic "Could not copy toolchain!"
    191 
    192     # Time to run the show :-)
    193     for ARCH in $(commas_to_spaces $ARCHS $UNKNOWN_ARCH); do
    194         dump "Running remote $ARCH toolchain build..."
    195         SYSROOT=$TMPREMOTE/ndk/platforms/android-$(get_default_api_level_for_arch $ARCH)/arch-$ARCH
    196         run ssh $REMOTE_HOST "$TMPREMOTE/ndk/build/tools/build-host-prebuilts.sh $TMPREMOTE/toolchain --package-dir=$TMPREMOTE/packages --arch=$ARCH --ndk-dir=$TMPREMOTE/ndk --no-gen-platforms"
    197         fail_panic "Could not build prebuilt $ARCH toolchain on Darwin!"
    198     done
    199     # Get the results
    200     dump "Copying back Darwin prebuilt packages..."
    201     mkdir -p $TMPDARWIN/packages && rm -rf $TMPDARWIN/packages/*
    202     run scp $REMOTE_HOST:$TMPREMOTE/packages/*-darwin-* $TMPDARWIN/packages
    203     fail_panic "Could not grab Darwin packages!"
    204 
    205     for PACK in $TMPDARWIN/packages/*; do
    206         if [ "$PACKAGE_DIR" ]; then
    207             echo "Copying $(basename $PACK) to $PACKAGE_DIR..."
    208             cp $PACK $PACKAGE_DIR/
    209         else
    210             echo "Unpacking $(basename $PACK) into $NDK_DIR..."
    211             unpack_archive $PACK $NDK_DIR
    212         fi
    213         fail_panic ""
    214     done
    215 
    216     dump "Cleaning up remote machine..."
    217     run ssh $REMOTE_HOST rm -rf $TMPREMOTE
    218 }
    219 
    220 for SYSTEM in $SYSTEMS; do
    221 
    222     # Add --mingw/--darwin flag
    223     TOOLCHAIN_FLAGS=$FLAGS
    224     CANADIAN_BUILD=no
    225     if [ "$HOST_TAG32" = "linux-x86" ]; then
    226         case "$SYSTEM" in
    227             windows)
    228                 TOOLCHAIN_FLAGS=$TOOLCHAIN_FLAGS" --mingw"
    229                 CANADIAN_BUILD=yes
    230                 ;;
    231             darwin-x86)
    232                 TOOLCHAIN_FLAGS=$TOOLCHAIN_FLAGS" --darwin"
    233                 CANADIAN_BUILD=yes
    234                 ;;
    235         esac
    236     fi
    237 
    238     # Should we do a remote build?
    239     if [ "$SYSTEM" != "$HOST_TAG32" -a "$CANADIAN_BUILD" != "yes" ]; then
    240         case $SYSTEM in
    241             darwin-*)
    242                 if [ "$DARWIN_SSH" ]; then
    243                     do_remote_host_build "$SYSTEM" "$ARCH" "$DARWIN_SSH"
    244                 else
    245                     echo "WARNING: Can't build $SYSTEM binaries on this system!"
    246                     echo "Consider using the --darwin-ssh option."
    247                 fi
    248                 continue
    249         esac
    250     fi
    251 
    252     # Determin the display system name
    253     SYSNAME=$SYSTEM
    254     if [ "$TRY64" = "yes" ]; then
    255         case $SYSTEM in
    256             darwin-x86|linux-x86)
    257                 SYSNAME=${SYSTEM%%x86}x86_64
    258                 ;;
    259             windows)
    260                 SYSNAME=windows-x86_64
    261                 ;;
    262         esac
    263     fi
    264 
    265     # First, ndk-stack
    266     echo "Building $SYSNAME ndk-stack"
    267     run $BUILDTOOLS/build-ndk-stack.sh $TOOLCHAIN_FLAGS --with-libbfd --src-dir=$SRC_DIR
    268     fail_panic "ndk-stack build failure!"
    269 
    270     echo "Building $SYSNAME ndk-depends"
    271     run $BUILDTOOLS/build-ndk-depends.sh $TOOLCHAIN_FLAGS
    272     fail_panic "ndk-depends build failure!"
    273 
    274     echo "Building $SYSNAME ndk-make"
    275     run $BUILDTOOLS/build-host-make.sh $TOOLCHAIN_FLAGS
    276     fail_panic "make build failure!"
    277 
    278     echo "Building $SYSNAME ndk-awk"
    279     run $BUILDTOOLS/build-host-awk.sh $TOOLCHAIN_FLAGS
    280     fail_panic "awk build failure!"
    281 
    282     echo "Building $SYSNAME ndk-sed"
    283     run $BUILDTOOLS/build-host-sed.sh $TOOLCHAIN_FLAGS
    284     fail_panic "sed build failure!"
    285 
    286     # ToDo: perl in windows/darwin cross.
    287     MAKE_PERL=no
    288     case $SYSTEM in
    289         linux*)
    290             MAKE_PERL=yes
    291         ;;
    292         darwin*)
    293             # Only works if not cross compiling.
    294             if [ "$CANADIAN_BUILD" = "no" ] ; then
    295                 MAKE_PERL=yes
    296             fi
    297         ;;
    298         *)
    299         ;;
    300     esac
    301 
    302     if [ "$MAKE_PERL" = "yes" ] ; then
    303         echo "Building $SYSNAME ndk-perl"
    304         run $BUILDTOOLS/build-host-perl.sh $TOOLCHAIN_FLAGS "$SRC_DIR"
    305         fail_panic "perl build failure!"
    306     fi
    307 
    308     echo "Building $SYSNAME ndk-python"
    309     run $BUILDTOOLS/build-host-python.sh $TOOLCHAIN_FLAGS "--toolchain-src-dir=$SRC_DIR" "--systems=$SYSTEM" "--force"
    310     fail_panic "python build failure!"
    311 
    312     echo "Building $SYSNAME ndk-yasm"
    313     run $BUILDTOOLS/build-host-yasm.sh "$SRC_DIR" "$NDK_DIR" $TOOLCHAIN_FLAGS
    314     fail_panic "yasm build failure!"
    315 
    316     if [ "$SYSTEM" = "windows" ]; then
    317         echo "Building $SYSNAME toolbox"
    318         run $BUILDTOOLS/build-host-toolbox.sh $FLAGS
    319         fail_panic "Windows toolbox build failure!"
    320     fi
    321 
    322     # Then the toolchains
    323     for ARCH in $ARCHS; do
    324         TOOLCHAIN_NAMES=$(get_toolchain_name_list_for_arch $ARCH)
    325         if [ "$GCC_VERSION_LIST" != "default" ]; then
    326            TOOLCHAINS=
    327            for VERSION in $(commas_to_spaces $GCC_VERSION_LIST); do
    328               for TOOLCHAIN in $TOOLCHAIN_NAMES; do
    329                  if [ $TOOLCHAIN != ${TOOLCHAIN%%$VERSION} ]; then
    330                     TOOLCHAINS="$TOOLCHAIN $TOOLCHAINS"
    331                  fi
    332               done
    333            done
    334            TOOLCHAIN_NAMES=$TOOLCHAINS
    335         fi
    336         if [ -z "$TOOLCHAIN_NAMES" ]; then
    337             echo "ERROR: Toolchains: "$(spaces_to_commas $GCC_VERSION_LIST)" are not available for arch: $ARCH"
    338             exit 1
    339         fi
    340 
    341         for TOOLCHAIN_NAME in $TOOLCHAIN_NAMES; do
    342             echo "Building $SYSNAME toolchain for $ARCH architecture: $TOOLCHAIN_NAME"
    343             run $BUILDTOOLS/build-gcc.sh "$SRC_DIR" "$NDK_DIR" $TOOLCHAIN_NAME $TOOLCHAIN_FLAGS --with-python=prebuilt -j$BUILD_NUM_CPUS
    344             fail_panic "Could not build $TOOLCHAIN_NAME-$SYSNAME!"
    345         done
    346     done
    347 
    348     # Build llvm and clang
    349     POLLY_FLAGS=
    350     if [ "$TRY64" != "yes" -a "$SYSTEM" != "windows" ]; then
    351         POLLY_FLAGS="--with-polly"
    352     fi
    353     for LLVM_VERSION in $LLVM_VERSION_LIST; do
    354         echo "Building $SYSNAME clang/llvm-$LLVM_VERSION"
    355         run $BUILDTOOLS/build-llvm.sh "$SRC_DIR" "$NDK_DIR" "llvm-$LLVM_VERSION" $TOOLCHAIN_FLAGS $POLLY_FLAGS $CHECK_FLAG -j$BUILD_NUM_CPUS
    356         fail_panic "Could not build llvm for $SYSNAME"
    357     done
    358 
    359     # Deploy ld.mcld
    360     $PROGDIR/deploy-host-mcld.sh --package-dir=$PACKAGE_DIR --systems=$SYSNAME
    361     fail_panic "Could not deploy ld.mcld for $SYSNAME"
    362 
    363     # We're done for this system
    364 done
    365 
    366 # Build tools common to all system
    367 run $BUILDTOOLS/build-analyzer.sh "$SRC_DIR" "$NDK_DIR" "llvm-$DEFAULT_LLVM_VERSION" --package-dir="$PACKAGE_DIR"
    368 
    369 if [ "$PACKAGE_DIR" ]; then
    370     echo "Done, please look at $PACKAGE_DIR"
    371 else
    372     echo "Done"
    373 fi
    374 
    375 exit 0
    376