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=$DEFAULT_ARCHS
     46 register_var_option "--arch=<list>" ARCHS "List of target archs to build for"
     47 
     48 PACKAGE_DIR=
     49 register_var_option "--package-dir=<path>" PACKAGE_DIR "Package toolchain into this directory"
     50 
     51 DARWIN_SSH=
     52 register_var_option "--darwin-ssh=<hostname>" DARWIN_SSH "Generate darwin packages on remote host"
     53 
     54 NO_GEN_PLATFORMS=
     55 register_var_option "--no-gen-platforms" NO_GEN_PLATFORMS "Don't generate platforms/ directory, use existing one"
     56 
     57 LLVM_VERSION_LIST=$DEFAULT_LLVM_VERSION_LIST
     58 register_var_option "--llvm-version-list=<vers>" LLVM_VERSION_LIST "List of LLVM release versions"
     59 
     60 CHECK_FLAG=
     61 do_check_option () { CHECK_FLAG="--check"; }
     62 register_option "--check" do_check_option "Check host prebuilts"
     63 
     64 register_try64_option
     65 
     66 PROGRAM_PARAMETERS="<toolchain-src-dir>"
     67 PROGRAM_DESCRIPTION=\
     68 "This script can be used to rebuild all the host NDK toolchains at once.
     69 You need to give it the path to the toolchain source directory, as
     70 downloaded by the 'download-toolchain-sources.sh' dev-script."
     71 
     72 extract_parameters "$@"
     73 
     74 # Check toolchain source path
     75 SRC_DIR="$PARAMETERS"
     76 if [ -z "$SRC_DIR" ]; then
     77     echo "ERROR: Please provide the path to the toolchain source tree. See --help"
     78     exit 1
     79 fi
     80 
     81 if [ ! -d "$SRC_DIR" ]; then
     82     echo "ERROR: Not a directory: '$SRC_DIR'"
     83     exit 1
     84 fi
     85 
     86 if [ ! -f "$SRC_DIR/build/configure" -o ! -d "$SRC_DIR/gcc/gcc-$DEFAULT_GCC_VERSION" ]; then
     87     echo "ERROR: The file $SRC_DIR/build/configure or"
     88     echo "       the directory $SRC_DIR/gcc/gcc-$DEFAULT_GCC_VERSION does not exist"
     89     echo "This is not the top of a toolchain tree: $SRC_DIR"
     90     echo "You must give the path to a copy of the toolchain source directories"
     91     echo "created by 'download-toolchain-sources.sh."
     92     exit 1
     93 fi
     94 
     95 # Now we can do the build
     96 BUILDTOOLS=$ANDROID_NDK_ROOT/build/tools
     97 
     98 if [ -z "$NO_GEN_PLATFORMS" ]; then
     99     echo "Preparing the build..."
    100     run $BUILDTOOLS/gen-platforms.sh --minimal --dst-dir=$NDK_DIR --ndk-dir=$NDK_DIR --arch=$(spaces_to_commas $ARCHS)
    101     fail_panic "Could not generate minimal sysroot!"
    102 else
    103     if [ ! -d "$NDK_DIR/platforms" ]; then
    104         echo "ERROR: --no-gen-platforms used but directory missing: $NDK_DIR/platforms"
    105         exit 1
    106     fi
    107 fi
    108 
    109 SYSTEMS=$(commas_to_spaces $SYSTEMS)
    110 ARCHS=$(commas_to_spaces $ARCHS)
    111 LLVM_VERSION_LIST=$(commas_to_spaces $LLVM_VERSION_LIST)
    112 
    113 if [ "$DARWIN_SSH" -a -z "$CUSTOM_SYSTEMS" ]; then
    114     SYSTEMS=" darwin-x86"
    115 fi
    116 
    117 FLAGS=
    118 if [ "$VERBOSE" = "yes" ]; then
    119     FLAGS=$FLAGS" --verbose"
    120 fi
    121 if [ "$VERBOSE2" = "yes" ]; then
    122     FLAGS=$FLAGS" --verbose"
    123 fi
    124 if [ "$TRY64" = "yes" ]; then
    125     FLAGS=$FLAGS" --try-64"
    126 else
    127     force_32bit_binaries
    128 fi
    129 if [ "$PACKAGE_DIR" ]; then
    130     mkdir -p "$PACKAGE_DIR"
    131     fail_panic "Could not create package directory: $PACKAGE_DIR"
    132     FLAGS=$FLAGS" --package-dir=$PACKAGE_DIR"
    133 fi
    134 
    135 do_remote_host_build ()
    136 {
    137     local SYSTEM="$1"
    138     local ARCH="$2"
    139     local REMOTE_HOST="$3"
    140 
    141     # 1/ Copy the NDK toolchain build scripts
    142     # 2/ Copy the toolchain sources/package
    143     # 3/ Ssh to unpack the build scripts, and run them
    144     # 4/ Copy back the generated prebuilt binaries
    145     #
    146     dump "Preparing remote build on $REMOTE_HOST..."
    147 
    148     # First try to create a remote temp directory on the remote host
    149     # Do it first so we can fail fast, not after spending time preparing
    150     # large tarballs.
    151     dump "Creating remote temp directory"
    152     TMPREMOTE=/tmp/ndk-$USER/darwin-prebuild
    153     run ssh $REMOTE_HOST "mkdir -p $TMPREMOTE && rm -rf $TMPREMOTE/*"
    154     fail_panic "Could not create temporary build directory on $REMOTE_HOST"
    155 
    156     # Now, package all the stuff we're going to send in a temporary
    157     # directory here on the machine, except toolchain sources
    158     TMPDARWIN=$NDK_TMPDIR/darwin  # Where we're going to package stuff
    159     log "Using temporary work directory: $TMPDARWIN"
    160     mkdir -p "$TMPDARWIN"
    161     dump "Prepare NDK build scripts"
    162     copy_directory "$ANDROID_NDK_ROOT/build" "$TMPDARWIN/ndk/build"
    163     copy_file_list "$ANDROID_NDK_ROOT" "$TMPDARWIN/ndk" sources/android/libthread_db
    164     copy_file_list "$ANDROID_NDK_ROOT" "$TMPDARWIN/ndk" "$STLPORT_SUBDIR"
    165     copy_file_list "$ANDROID_NDK_ROOT" "$TMPDARWIN/ndk" "$GABIXX_SUBDIR"
    166     copy_file_list "$ANDROID_NDK_ROOT" "$TMPDARWIN/ndk" sources/host-tools
    167     dump "Prepare platforms files"
    168     copy_directory "$NDK_DIR/platforms" "$TMPDARWIN/ndk/platforms"
    169     dump "Copying NDK build scripts and platform files to remote..."
    170     (cd "$TMPDARWIN" && tar czf - ndk) | (ssh $REMOTE_HOST tar xzf - -C $TMPREMOTE)
    171     fail_panic "Could not copy!"
    172     rm -rf $TMPDARWIN
    173 
    174     # Copy the toolchain sources
    175     dump "Copy toolchain sources to remote"
    176     ssh $REMOTE_HOST mkdir -p $TMPREMOTE/toolchain &&
    177     (cd "$SRC_DIR" && tar czf - .) | (ssh $REMOTE_HOST tar xzf - -C $TMPREMOTE/toolchain)
    178     fail_panic "Could not copy toolchain!"
    179 
    180     # Time to run the show :-)
    181     for ARCH in $(commas_to_spaces $ARCHS); do
    182         dump "Running remote $ARCH toolchain build..."
    183         SYSROOT=$TMPREMOTE/ndk/platforms/android-$(get_default_api_level_for_arch $ARCH)/arch-$ARCH
    184         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"
    185         fail_panic "Could not build prebuilt $ARCH toolchain on Darwin!"
    186     done
    187     # Get the results
    188     dump "Copying back Darwin prebuilt packages..."
    189     mkdir -p $TMPDARWIN/packages && rm -rf $TMPDARWIN/packages/*
    190     run scp $REMOTE_HOST:$TMPREMOTE/packages/*-darwin-* $TMPDARWIN/packages
    191     fail_panic "Could not grab Darwin packages!"
    192 
    193     for PACK in $TMPDARWIN/packages/*; do
    194         if [ "$PACKAGE_DIR" ]; then
    195             echo "Copying $(basename $PACK) to $PACKAGE_DIR..."
    196             cp $PACK $PACKAGE_DIR/
    197         else
    198             echo "Unpacking $(basename $PACK) into $NDK_DIR..."
    199             unpack_archive $PACK $NDK_DIR
    200         fi
    201         fail_panic ""
    202     done
    203 
    204     dump "Cleaning up remote machine..."
    205     run ssh $REMOTE_HOST rm -rf $TMPREMOTE
    206 }
    207 
    208 for SYSTEM in $SYSTEMS; do
    209 
    210     # Add --mingw/--darwin flag
    211     TOOLCHAIN_FLAGS=$FLAGS
    212     CANADIAN_BUILD=no
    213     if [ "$HOST_TAG32" = "linux-x86" ]; then
    214         case "$SYSTEM" in
    215             windows)
    216                 TOOLCHAIN_FLAGS=$TOOLCHAIN_FLAGS" --mingw"
    217                 CANADIAN_BUILD=yes
    218                 ;;
    219             darwin-x86)
    220                 TOOLCHAIN_FLAGS=$TOOLCHAIN_FLAGS" --darwin"
    221                 CANADIAN_BUILD=yes
    222                 ;;
    223         esac
    224     fi
    225 
    226     # Should we do a remote build?
    227     if [ "$SYSTEM" != "$HOST_TAG32" -a "$CANADIAN_BUILD" != "yes" ]; then
    228         case $SYSTEM in
    229             darwin-*)
    230                 if [ "$DARWIN_SSH" ]; then
    231                     do_remote_host_build "$SYSTEM" "$ARCH" "$DARWIN_SSH"
    232                 else
    233                     echo "WARNING: Can't build $SYSTEM binaries on this system!"
    234                     echo "Consider using the --darwin-ssh option."
    235                 fi
    236                 continue
    237         esac
    238     fi
    239 
    240     # Determin the display system name
    241     SYSNAME=$SYSTEM
    242     if [ "$TRY64" = "yes" ]; then
    243         case $SYSTEM in
    244             darwin-x86|linux-x86)
    245                 SYSNAME=${SYSTEM%%x86}x86_64
    246                 ;;
    247             windows)
    248                 SYSNAME=windows-x86_64
    249                 ;;
    250         esac
    251     fi
    252 
    253     # First, ndk-stack
    254     echo "Building $SYSNAME ndk-stack"
    255     run $BUILDTOOLS/build-ndk-stack.sh $TOOLCHAIN_FLAGS
    256     fail_panic "ndk-stack build failure!"
    257 
    258     echo "Building $SYSNAME ndk-depends"
    259     run $BUILDTOOLS/build-ndk-depends.sh $TOOLCHAIN_FLAGS
    260     fail_panic "ndk-depends build failure!"
    261 
    262     echo "Building $SYSNAME ndk-make"
    263     run $BUILDTOOLS/build-host-make.sh $TOOLCHAIN_FLAGS
    264     fail_panic "make build failure!"
    265 
    266     echo "Building $SYSNAME ndk-awk"
    267     run $BUILDTOOLS/build-host-awk.sh $TOOLCHAIN_FLAGS
    268     fail_panic "awk build failure!"
    269 
    270     echo "Building $SYSNAME ndk-sed"
    271     run $BUILDTOOLS/build-host-sed.sh $TOOLCHAIN_FLAGS
    272     fail_panic "sed build failure!"
    273 
    274     # ToDo: perl in windows/darwin cross.
    275     MAKE_PERL=no
    276     case $SYSTEM in
    277         linux*)
    278             MAKE_PERL=yes
    279         ;;
    280         darwin*)
    281             # Only works if not cross compiling.
    282             if [ "$CANADIAN_BUILD" = "no" ] ; then
    283                 MAKE_PERL=yes
    284             fi
    285         ;;
    286         *)
    287         ;;
    288     esac
    289 
    290     if [ "$MAKE_PERL" = "yes" ] ; then
    291         echo "Building $SYSNAME ndk-perl"
    292         run $BUILDTOOLS/build-host-perl.sh $TOOLCHAIN_FLAGS "$SRC_DIR"
    293         fail_panic "perl build failure!"
    294     fi
    295 
    296     echo "Building $SYSNAME ndk-python"
    297     run $BUILDTOOLS/build-host-python.sh $TOOLCHAIN_FLAGS "--toolchain-src-dir=$SRC_DIR" "--systems=$SYSTEM" "--force"
    298     fail_panic "python build failure!"
    299 
    300     if [ "$SYSTEM" = "windows" ]; then
    301         echo "Building $SYSNAME toolbox"
    302         run $BUILDTOOLS/build-host-toolbox.sh $FLAGS
    303         fail_panic "Windows toolbox build failure!"
    304     fi
    305 
    306     # Then the toolchains
    307     for ARCH in $ARCHS; do
    308         TOOLCHAIN_NAMES=$(get_toolchain_name_list_for_arch $ARCH)
    309         if [ -z "$TOOLCHAIN_NAMES" ]; then
    310             echo "ERROR: Invalid architecture name: $ARCH"
    311             exit 1
    312         fi
    313 
    314         for TOOLCHAIN_NAME in $TOOLCHAIN_NAMES; do
    315             echo "Building $SYSNAME toolchain for $ARCH architecture: $TOOLCHAIN_NAME"
    316             run $BUILDTOOLS/build-gcc.sh "$SRC_DIR" "$NDK_DIR" $TOOLCHAIN_NAME $TOOLCHAIN_FLAGS --with-python=prebuilt
    317             fail_panic "Could not build $TOOLCHAIN_NAME-$SYSNAME!"
    318         done
    319     done
    320 
    321     # Build llvm and clang
    322     POLLY_FLAGS=
    323     if [ "$TRY64" != "yes" -a "$SYSTEM" != "windows" ]; then
    324         POLLY_FLAGS="--with-polly"
    325     fi
    326     for LLVM_VERSION in $LLVM_VERSION_LIST; do
    327         echo "Building $SYSNAME clang/llvm-$LLVM_VERSION"
    328         run $BUILDTOOLS/build-llvm.sh "$SRC_DIR" "$NDK_DIR" "llvm-$LLVM_VERSION" $TOOLCHAIN_FLAGS $POLLY_FLAGS $CHECK_FLAG
    329         fail_panic "Could not build llvm for $SYSNAME"
    330     done
    331 
    332     # Deploy ld.mcld
    333     $PROGDIR/deploy-host-mcld.sh --package-dir=$PACKAGE_DIR --systems=$SYSNAME
    334     fail_panic "Could not deploy ld.mcld for $SYSNAME"
    335 
    336     # We're done for this system
    337 done
    338 
    339 # Build tools common to all system
    340 run $BUILDTOOLS/build-analyzer.sh "$SRC_DIR" "$NDK_DIR" "llvm-$DEFAULT_LLVM_VERSION" --package-dir="$PACKAGE_DIR"
    341 
    342 if [ "$PACKAGE_DIR" ]; then
    343     echo "Done, please look at $PACKAGE_DIR"
    344 else
    345     echo "Done"
    346 fi
    347 
    348 exit 0
    349