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 #
     18 # This script is used internally to re-build the toolchains and NDK.
     19 # This is an engineer's tool... definitely not a production tool. Don't expect it to be
     20 # flawelss... at least at this revision.
     21 
     22 # Revamp of the 'ndk-buil.sh' based on feedback and NDK build
     23 # processes used by Google to generate the NDK>
     24 
     25 # Comment out the remote Mac OS X builds as it is currently
     26 # non-functional due to a change in AOSP
     27 # 3592767ce5ca9431eea728370c99d97aadb0800e
     28 
     29 # include common function and variable definitions
     30 . `dirname $0`/prebuilt-common.sh
     31 PROGDIR=`dirname $0`
     32 PROGDIR=`cd $PROGDIR && pwd`
     33 
     34 
     35 # Name of this NDK release
     36 OPTION_NDK_RELEASE=`date +%Y%m%d`
     37 register_var_option "--release=<rel_name>" OPTION_NDK_RELEASE "Version of release"
     38 
     39 # Should we only Build for Linux platform?
     40 OPTION_QUICK_BUILD="no"
     41 register_var_option "--quick" OPTION_QUICK_BUILD "Only build the Linux basics"
     42 
     43 # List of toolchains to package
     44 OPTION_TOOLCHAINS="$DEFAULT_ARCH_TOOLCHAIN_NAME_arm,$DEFAULT_ARCH_TOOLCHAIN_NAME_x86,$DEFAULT_ARCH_TOOLCHAIN_NAME_mips"
     45 register_var_option "--toolchains=<toolchain[,toolchain]>" OPTION_TOOLCHAINS "Toolchain(s) to package"
     46 
     47 OPTION_TRY_64=
     48 register_try64_option
     49 
     50 OPTION_ALSO_64=
     51 register_option "--also-64" do_ALSO_64 "Also build 64-bit host toolchain"
     52 do_ALSO_64 () { OPTION_ALSO_64=" --also-64"; }
     53 
     54 OPTION_SEPARATE_64=
     55 register_option "--separate-64" do_SEPARATE_64 "Separate 64-bit host toolchain to its own package"
     56 do_SEPARATE_64 ()
     57 {
     58     if [ "$TRY64" = "yes" ]; then
     59         echo "ERROR: You cannot use both --try-64 and --separate-64 at the same time."
     60         exit 1
     61     fi
     62     OPTION_SEPARATE_64=" --separate-64";
     63 }
     64 # # Name of the Mac OS Build host
     65 # MAC_BUILD_HOST="macdroid"
     66 # register_var_option "--mac-host=<name>" MAC_BUILD_HOST "Hostname of the Mac OS X system"
     67 
     68 PROGRAM_PARAMETERS=""
     69 PROGRAM_DESCRIPTION=\
     70 "Generate the NDK toolchain package."
     71 
     72 extract_parameters "$@"
     73 
     74 if [ "$TRY64" = "yes" ]; then
     75     OPTION_TRY_64=" --try-64"
     76 fi
     77 
     78 TOP=$PWD
     79 TODAY=`date '+%Y%m%d'`
     80 PACKAGE_DIR=$TOP/ndk-release-$TODAY
     81 HOST_OS=`uname -s | tr '[:upper:]' '[:lower:]'`
     82 # Set the list of Build Targets based on this Host OS
     83 case "$HOST_OS" in
     84 linux )
     85     # Build for Local Linux and Cross-compile for Windows (MINGW)
     86     if [ "$OPTION_QUICK_BUILD" = "yes" ]; then
     87        BUILD_TARGET_PLATFORMS="linux-x86"
     88     else
     89        BUILD_TARGET_PLATFORMS="linux-x86 windows"
     90     fi
     91     ;;
     92 darwin )
     93     # Build for Local Mac OS X
     94     BUILD_TARGET_PLATFORMS="darwin-x86"
     95     ;;
     96 esac
     97 
     98 #export ANDROID_NDK_ROOT=$TOP/ndk
     99 #export PATH=$PATH:$ANDROID_NDK_ROOT/build/tools
    100 export VERBOSE=--verbose
    101 
    102 # If BUILD_NUM_CPUS is not already defined in your environment,
    103 # define it as the double of HOST_NUM_CPUS. This is used to
    104 # run make commands in parallel, as in 'make -j$BUILD_NUM_CPUS'
    105 #
    106 if [ -z "$BUILD_NUM_CPUS" ] ; then
    107     if [ -e /proc/cpuinfo ] ; then
    108         HOST_NUM_CPUS=`cat /proc/cpuinfo | grep -c processor`
    109         export BUILD_NUM_CPUS=$(($HOST_NUM_CPUS * 2 * 8 / 10))
    110     elif [ -e /usr/sbin/sysctl ] ; then
    111         HOST_NUM_CPUS=`/usr/sbin/sysctl -n hw.ncpu`
    112         export BUILD_NUM_CPUS=$(($HOST_NUM_CPUS * 2 * 8 / 10))
    113     else
    114         export BUILD_NUM_CPUS=1
    115         echo "WARNING: Could not find Host Number CPUs; defaulting to BUILD_NUM_CPUS=${BUILD_NUM_CPUS}"
    116     fi
    117 fi
    118 
    119 # CLEAN
    120 rm -rf /tmp/ndk-$USER/{build,tmp}
    121 
    122 
    123 #######################################
    124 # Get the Toolchain sources
    125 #######################################
    126 
    127 # Create a sha1 for any additional patches
    128 PATCHES_SHA1=""
    129 if [ -d "$PROGDIR/toolchain-patches" ]
    130 then
    131     PATCHES_SHA1=`( find $PROGDIR/toolchain-patches -type f -print ) | \
    132         sort -f | xargs cat | git hash-object --stdin | cut -c1-7`
    133     PATCHES_SHA1="+${PATCHES_SHA1}"
    134 fi
    135 
    136 echo
    137 echo "Checking for Toolchain sources"
    138 NDK_SRC_DIR=/tmp/ndk-$USER/src/android-ndk-src-${TOOLCHAIN_GIT_DATE}${PATCHES_SHA1}
    139 if [ ! -d $NDK_SRC_DIR ]
    140 then
    141     echo "  Downloading Toolchain sources"
    142     mkdir -p `dirname $NDK_SRC_DIR`
    143     logfile="$TOP/download-toolchain-sources.log"
    144     rotate_log $logfile
    145     $PROGDIR/download-toolchain-sources.sh $NDK_SRC_DIR \
    146         > $logfile 2>&1
    147     fail_panic "Could not download toolchain sources!"
    148 else
    149     echo "  Found existing $NDK_SRC_DIR"
    150 fi
    151 
    152 
    153 ARCHS=$DEFAULT_ARCHS
    154 
    155 # Build the platform
    156 echo
    157 echo "Build the ndk/platforms directory"
    158 logfile="$TOP/build-platforms.log"
    159 rotate_log $logfile
    160 $PROGDIR/gen-platforms.sh \
    161     $VERBOSE \
    162     --arch=$(spaces_to_commas $ARCHS)  \
    163     --minimal \
    164     --fast-copy > $logfile 2>&1
    165 fail_panic "build-platforms.sh failed. Logfile in $logfile"
    166 
    167 logfile="$TOP/rebuild-all.log"
    168 rotate_log $logfile
    169 
    170 # Rebuild all prebuilts for archs and platforms
    171 for TARGET_PLATFORM in ${BUILD_TARGET_PLATFORMS}
    172 do
    173     # Set the Arch specific variables
    174     case "$TARGET_PLATFORM" in
    175     linux-x86 )
    176         TARGET_PLATFORM_OS="Linux"
    177         TARGET_PLATFORM_FLAGS="--systems=$TARGET_PLATFORM"
    178         ;;
    179     windows )
    180         TARGET_PLATFORM_OS="Windows"
    181         TARGET_PLATFORM_FLAGS="--systems=$TARGET_PLATFORM"
    182         # Skip this Target Platform in Quick Build Mode
    183         if [ "$OPTION_QUICK_BUILD" = "yes" ]; then break ; fi
    184         ;;
    185     darwin-x86 )
    186         TARGET_PLATFORM_OS="Mac OS X"
    187         TARGET_PLATFORM_FLAGS=""
    188 #        TARGET_PLATFORM_FLAGS="--darwin-ssh=$MAC_BUILD_HOST"
    189 #        # Skip this Target Platform in Quick Build Mode
    190 #        if [ "$OPTION_QUICK_BUILD" = "yes" ]; then break ; fi
    191         ;;
    192     esac
    193 
    194     # Rebuilt all prebuilts for the arch type
    195     echo
    196     echo "Rebuilding the toolchain and prebuilt tarballs for $TARGET_PLATFORM_OS"
    197     cd $TOP
    198     $PROGDIR/rebuild-all-prebuilt.sh \
    199         --arch=$(spaces_to_commas $ARCHS)  \
    200         --package-dir=$PACKAGE_DIR \
    201         $MPFR_VERSION $GDB_VERSION $BINUTILS_VERSION \
    202         $TARGET_PLATFORM_FLAGS \
    203         $VERBOSE \
    204         $OPTION_TRY_64 \
    205         $OPTION_ALSO_64 \
    206         $NDK_SRC_DIR >> $logfile 2>&1
    207     fail_panic "rebuild-all-prebuilt.sh failed. Logfile in $logfile"
    208 done # with TARGET_PLATFORM
    209 
    210 ALL_SYSTEMS=`echo ${BUILD_TARGET_PLATFORMS}`
    211 
    212 echo
    213 echo "Building the NDK release"
    214 cd $TOP
    215 logfile="$TOP/package-release.log"
    216 rotate_log $logfile
    217 $PROGDIR/package-release.sh \
    218     --prebuilt-dir=$PACKAGE_DIR \
    219     --systems="$ALL_SYSTEMS" \
    220     --out-dir=$PACKAGE_DIR \
    221     --arch=$(spaces_to_commas $ARCHS)  \
    222     --prefix=android-ndk-${OPTION_NDK_RELEASE} \
    223     $OPTION_TRY_64 \
    224     $OPTION_SEPARATE_64 \
    225     --no-git \
    226     $VERBOSE > $logfile 2>&1
    227 fail_panic "package-release.sh failed. Logfile in $logfile"
    228 
    229 echo
    230 echo "Packaging the NDK Toolchain sources"
    231 NDK_TOOLCHAIN_PKG=${PACKAGE_DIR}/toolchain-src.tar.bz2
    232 (cd $NDK_SRC_DIR && tar cjf $NDK_TOOLCHAIN_PKG .)
    233 fail_panic "Could not package NDK Toolchain sources!"
    234 
    235 exit 0
    236 
    237