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
31PROGDIR=`dirname $0`
32PROGDIR=`cd $PROGDIR && pwd`
33
34
35# Name of this NDK release
36OPTION_NDK_RELEASE=`date +%Y%m%d`
37register_var_option "--release=<rel_name>" OPTION_NDK_RELEASE "Version of release"
38
39# Should we only Build for Linux platform?
40OPTION_QUICK_BUILD="no"
41register_var_option "--quick" OPTION_QUICK_BUILD "Only build the Linux basics"
42
43# List of toolchains to package
44OPTION_TOOLCHAINS="$DEFAULT_ARCH_TOOLCHAIN_NAME_arm,$DEFAULT_ARCH_TOOLCHAIN_NAME_x86,$DEFAULT_ARCH_TOOLCHAIN_NAME_mips"
45register_var_option "--toolchains=<toolchain[,toolchain]>" OPTION_TOOLCHAINS "Toolchain(s) to package"
46
47OPTION_TRY_64=
48register_try64_option
49
50OPTION_ALSO_64=
51register_option "--also-64" do_ALSO_64 "Also build 64-bit host toolchain"
52do_ALSO_64 () { OPTION_ALSO_64=" --also-64"; }
53
54OPTION_SEPARATE_64=
55register_option "--separate-64" do_SEPARATE_64 "Separate 64-bit host toolchain to its own package"
56do_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
68PROGRAM_PARAMETERS=""
69PROGRAM_DESCRIPTION=\
70"Generate the NDK toolchain package."
71
72extract_parameters "$@"
73
74if [ "$TRY64" = "yes" ]; then
75    OPTION_TRY_64=" --try-64"
76fi
77
78TOP=$PWD
79TODAY=`date '+%Y%m%d'`
80PACKAGE_DIR=$TOP/ndk-release-$TODAY
81HOST_OS=`uname -s | tr '[:upper:]' '[:lower:]'`
82# Set the list of Build Targets based on this Host OS
83case "$HOST_OS" in
84linux )
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    ;;
92darwin )
93    # Build for Local Mac OS X
94    BUILD_TARGET_PLATFORMS="darwin-x86"
95    ;;
96esac
97
98#export ANDROID_NDK_ROOT=$TOP/ndk
99#export PATH=$PATH:$ANDROID_NDK_ROOT/build/tools
100export 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#
106if [ -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
117fi
118
119# CLEAN
120rm -rf /tmp/ndk-$USER/{build,tmp}
121
122
123#######################################
124# Get the Toolchain sources
125#######################################
126
127# Create a sha1 for any additional patches
128PATCHES_SHA1=""
129if [ -d "$PROGDIR/toolchain-patches" ]
130then
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}"
134fi
135
136echo
137echo "Checking for Toolchain sources"
138NDK_SRC_DIR=/tmp/ndk-$USER/src/android-ndk-src-${TOOLCHAIN_GIT_DATE}${PATCHES_SHA1}
139if [ ! -d $NDK_SRC_DIR ]
140then
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!"
148else
149    echo "  Found existing $NDK_SRC_DIR"
150fi
151
152
153ARCHS=$DEFAULT_ARCHS
154
155# Build the platform
156echo
157echo "Build the ndk/platforms directory"
158logfile="$TOP/build-platforms.log"
159rotate_log $logfile
160$PROGDIR/gen-platforms.sh \
161    $VERBOSE \
162    --arch=$(spaces_to_commas $ARCHS)  \
163    --minimal \
164    --fast-copy > $logfile 2>&1
165fail_panic "build-platforms.sh failed. Logfile in $logfile"
166
167logfile="$TOP/rebuild-all.log"
168rotate_log $logfile
169
170# Rebuild all prebuilts for archs and platforms
171for TARGET_PLATFORM in ${BUILD_TARGET_PLATFORMS}
172do
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"
208done # with TARGET_PLATFORM
209
210ALL_SYSTEMS=`echo ${BUILD_TARGET_PLATFORMS}`
211
212echo
213echo "Building the NDK release"
214cd $TOP
215logfile="$TOP/package-release.log"
216rotate_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
227fail_panic "package-release.sh failed. Logfile in $logfile"
228
229echo
230echo "Packaging the NDK Toolchain sources"
231NDK_TOOLCHAIN_PKG=${PACKAGE_DIR}/toolchain-src.tar.bz2
232(cd $NDK_SRC_DIR && tar cjf $NDK_TOOLCHAIN_PKG .)
233fail_panic "Could not package NDK Toolchain sources!"
234
235exit 0
236
237