1#!/bin/sh
2#
3# Copyright (C) 2010 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# This script is used to build an NDK release package *from* scratch !!
18# While handy, this is *not* the best way to generate NDK release packages.
19# See docs/DEVELOPMENT.TXT for details.
20#
21
22. `dirname $0`/prebuilt-common.sh
23
24force_32bit_binaries
25
26# The default release name (use today's date)
27RELEASE=`date +%Y%m%d`
28register_var_option "--release=<name>" RELEASE "Specify release name"
29
30# The package prefix
31PREFIX=android-ndk
32register_var_option "--prefix=<name>" PREFIX "Specify package prefix"
33
34# Find the location of the platforms root directory
35DEVELOPMENT_ROOT=`dirname $ANDROID_NDK_ROOT`/development/ndk
36register_var_option "--development=<path>" DEVELOPMENT_ROOT "Path to development/ndk directory"
37
38# Default location for final packages
39OUT_DIR=/tmp/ndk-$USER/release
40register_var_option "--out-dir=<path>" OUT_DIR "Path to output directory"
41
42# Force the build
43FORCE=no
44register_var_option "--force" FORCE "Force build (do not ask initial question)"
45
46# Use --incremental to implement incremental release builds.
47# This is only useful to debug this script or the ones it calls.
48INCREMENTAL=no
49register_var_option "--incremental" INCREMENTAL "Enable incremental packaging (debug only)."
50
51DARWIN_SSH=
52if [ "$HOST_OS" = "linux" ] ; then
53register_var_option "--darwin-ssh=<hostname>" DARWIN_SSH "Specify Darwin hostname to ssh to for the build."
54fi
55
56# Determine the host platforms we can build for.
57# This is the current host platform, and eventually windows if
58# we are on Linux and have the mingw32 compiler installed and
59# in our path.
60#
61HOST_SYSTEMS="$HOST_TAG32"
62
63MINGW_GCC=
64CANADIAN_DARWIN_BUILD=no
65if [ "$HOST_TAG" = "linux-x86" ] ; then
66    find_mingw_toolchain
67    if [ -n "$MINGW_GCC" ] ; then
68        HOST_SYSTEMS="$HOST_SYSTEMS,windows"
69    fi
70    # If darwin toolchain exist, build darwin too
71    if [ -z "$DARWIN_SSH" -a -f "${DARWIN_TOOLCHAIN}-gcc" ]; then
72        HOST_SYSTEMS="$HOST_SYSTEMS,darwin-x86"
73        CANADIAN_DARWIN_BUILD=yes
74    fi
75fi
76if [ -n "$DARWIN_SSH" -a "$HOST_SYSTEMS" = "${HOST_SYSTEMS%darwin-x86*}" ]; then
77    HOST_SYSTEMS="$HOST_SYSTEMS,darwin-x86"
78fi
79
80register_var_option "--systems=<list>" HOST_SYSTEMS "List of host systems to build for"
81
82ALSO_64_FLAG=
83register_option "--also-64" do_ALSO_64 "Also build 64-bit host toolchain"
84do_ALSO_64 () { ALSO_64_FLAG=--also-64; }
85
86TOOLCHAIN_SRCDIR=
87register_var_option "--toolchain-src-dir=<path>" TOOLCHAIN_SRCDIR "Use toolchain sources from <path>"
88
89extract_parameters "$@"
90
91# Check if windows is specified w/o linux-x86
92if [ "$HOST_SYSTEMS" != "${HOST_SYSTEMS%windows*}" ] ; then
93    if [ -z "$MINGW_GCC" ]; then
94        echo "ERROR: Can't find mingw tool with --systems=windows"
95        exit 1
96    fi
97    if [ "$HOST_SYSTEMS" = "${HOST_SYSTEMS%linux-x86*}" ] ; then
98        echo "ERROR: Can't specify --systems=windows w/o also specifying linux-x86"
99        exit 1
100    fi
101fi
102HOST_FLAGS="--systems=$HOST_SYSTEMS $ALSO_64_FLAG"
103if [ -z "$CANADIAN_DARWIN_BUILD" ]; then
104    # Filter out darwin-x86 in $HOST_FLAGS, because
105    # 1) On linux when cross-compiling is done via "--darwin-ssh", keeping darwin-x86 in --systems list
106    #    actually disable --darwin-ssh later on.
107    # 2) On MacOSX, darwin-x86 is the default, no need to be explicit.
108    #
109    HOST_FLAGS=$(echo "$HOST_FLAGS" | sed -e 's/darwin-x86//')
110fi
111
112# Print a warning and ask the user if he really wants to do that !
113#
114if [ "$FORCE" = "no" -a "$INCREMENTAL" = "no" ] ; then
115    echo "IMPORTANT WARNING !!"
116    echo ""
117    echo "This script is used to generate an NDK release package from scratch"
118    echo "for the following host platforms: $HOST_SYSTEMS"
119    echo ""
120    echo "This process is EXTREMELY LONG and may take SEVERAL HOURS on a dual-core"
121    echo "machine. If you plan to do that often, please read docs/DEVELOPMENT.TXT"
122    echo "that provides instructions on how to do that more easily."
123    echo ""
124    echo "Are you sure you want to do that [y/N] "
125    read YESNO
126    case "$YESNO" in
127        y|Y|yes|YES)
128            ;;
129        *)
130            echo "Aborting !"
131            exit 0
132    esac
133fi
134
135PROGRAM_PARAMETERS=
136PROGRAM_DESCRIPTION=\
137"This script is used to generate an NDK release package from scratch.
138
139This process is EXTREMELY LONG and consists in the following steps:
140
141  - downloading toolchain sources from the Internet
142  - patching them appropriately (if needed)
143  - rebuilding the toolchain binaries for the host system
144  - rebuilding the platforms and samples directories from ../development/ndk
145  - packaging everything into a host-specific archive
146
147This can take several hours on a dual-core machine, even assuming a very
148nice Internet connection and plenty of RAM and disk space.
149
150Note that on Linux, if you have the 'mingw32' package installed, the script
151will also automatically generate a windows release package. You can prevent
152that by using the --platforms option.
153
154IMPORTANT:
155        If you intend to package NDK releases often, please read the
156        file named docs/DEVELOPMENT.TXT which provides ways to do that
157        more quickly, by preparing toolchain binary tarballs that can be
158        reused for each package operation. This will save you hours of
159        your time compared to using this script!
160"
161
162# Create directory where everything will be performed.
163RELEASE_DIR=$NDK_TMPDIR/release-$RELEASE
164unset NDK_TMPDIR  # prevent later script from reusing/removing it
165if [ "$INCREMENTAL" = "no" ] ; then
166    rm -rf $RELEASE_DIR && mkdir -p $RELEASE_DIR
167else
168    if [ ! -d "$RELEASE_DIR" ] ; then
169        echo "ERROR: Can't make incremental, missing release dir: $RELEASE_DIR"
170        exit 1
171    fi
172fi
173
174
175#
176# Timestamp management
177TIMESTAMP_DIR="$RELEASE_DIR/timestamps"
178mkdir -p "$TIMESTAMP_DIR"
179if [ "$INCREMENTAL" = "no" ] ; then
180    run rm -rf "$TIMESTAMP_DIR/*"
181fi
182
183timestamp_set ()
184{
185    touch "$TIMESTAMP_DIR/$1"
186}
187
188timestamp_clear ()
189{
190    rm -f "$TIMESTAMP_DIR/$1"
191}
192
193timestamp_check ()
194{
195    if [ -f "$TIMESTAMP_DIR/$1" ] ; then
196        return 1
197    else
198        return 0
199    fi
200}
201
202# Step 1, If needed, download toolchain sources into a temporary directory
203if [ -n "$TOOLCHAIN_SRCDIR" ] ; then
204    dump "Using toolchain source directory: $TOOLCHAIN_SRCDIR"
205    timestamp_set   toolchain-download-sources
206else
207    if timestamp_check toolchain-download-sources; then
208        dump "Downloading toolchain sources..."
209        TOOLCHAIN_SRCDIR="$RELEASE_DIR/toolchain-src"
210        log "Using toolchain source directory: $TOOLCHAIN_SRCDIR"
211        run $ANDROID_NDK_ROOT/build/tools/download-toolchain-sources.sh "$TOOLCHAIN_SRCDIR"
212        if [ "$?" != 0 ] ; then
213            dump "ERROR: Could not download toolchain sources"
214            exit 1
215        fi
216        timestamp_set   toolchain-download-sources
217        timestamp_clear build-prebuilts
218        timestamp_clear build-host-prebuilts
219        timestamp_clear build-darwin-prebuilts
220        timestamp_clear build-mingw-prebuilts
221    fi
222fi
223
224# Step 2, build the host toolchain binaries and package them
225if timestamp_check build-prebuilts; then
226    PREBUILT_DIR="$RELEASE_DIR/prebuilt"
227    if timestamp_check build-host-prebuilts; then
228        dump "Building host toolchain binaries..."
229        run $ANDROID_NDK_ROOT/build/tools/rebuild-all-prebuilt.sh --package-dir="$PREBUILT_DIR" --build-dir="$RELEASE_DIR/build" "$TOOLCHAIN_SRCDIR" $HOST_FLAGS
230        fail_panic "Can't build $HOST_SYSTEM binaries."
231        timestamp_set build-host-prebuilts
232    fi
233    if [ -n "$DARWIN_SSH" ] ; then
234        if timestamp_check build-darwin-prebuilts; then
235            dump "Building Darwin prebuilts through ssh to $DARWIN_SSH..."
236            run $ANDROID_NDK_ROOT/build/tools/rebuild-all-prebuilt.sh --package-dir="$PREBUILT_DIR" --darwin-ssh="$DARWIN_SSH" "$TOOLCHAIN_SRCDIR"
237            fail_panic "Can't build Darwin binaries!"
238            timestamp_set build-darwin-prebuilts
239        fi
240    fi
241    timestamp_set build-prebuilts
242    timestamp_clear make-packages
243fi
244
245# Step 3, package a release with everything
246if timestamp_check make-packages; then
247    dump "Generating NDK release packages"
248    run $ANDROID_NDK_ROOT/build/tools/package-release.sh --release=$RELEASE --prefix=$PREFIX --out-dir="$OUT_DIR" --prebuilt-dir="$PREBUILT_DIR" --systems="$HOST_SYSTEMS" --development-root="$DEVELOPMENT_ROOT"
249    if [ $? != 0 ] ; then
250        dump "ERROR: Can't generate proper release packages."
251        exit 1
252    fi
253    timestamp_set make-packages
254fi
255
256dump "All clear. Good work! See $OUT_DIR"
257