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