• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
20PROGDIR=$(dirname $0)
21. $PROGDIR/prebuilt-common.sh
22
23NDK_DIR=$ANDROID_NDK_ROOT
24register_var_option "--ndk-dir=<path>" NDK_DIR "NDK installation directory"
25
26SYSTEMS=$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
29if [ "${SYSTEMS%%x86_64}" != "$SYSTEMS" ]; then
30    SYSTEMS=${SYSTEMS%%x86_64}x86
31fi
32# If we are on Linux, we are able to generate the Windows binaries
33# with the mingw32 cross-toolchain.
34if [ "$SYSTEMS" = "linux-x86" ]; then
35    SYSTEMS=$SYSTEMS",windows"
36fi
37CUSTOM_SYSTEMS=
38register_option "--systems=<names>" do_SYSTEMS "List of host systems to build for"
39do_SYSTEMS () { CUSTOM_SYSTEMS=true; SYSTEMS=$1; }
40
41ARCHS=$DEFAULT_ARCHS
42register_var_option "--arch=<list>" ARCHS "List of target archs to build for"
43
44PACKAGE_DIR=
45register_var_option "--package-dir=<path>" PACKAGE_DIR "Package toolchain into this directory"
46
47DARWIN_SSH=
48register_var_option "--darwin-ssh=<hostname>" DARWIN_SSH "Generate darwin packages on remote host"
49
50NO_GEN_PLATFORMS=
51register_var_option "--no-gen-platforms" NO_GEN_PLATFORMS "Don't generate platforms/ directory, use existing one"
52
53register_try64_option
54
55PROGRAM_PARAMETERS="<toolchain-src-dir>"
56PROGRAM_DESCRIPTION=\
57"This script can be used to rebuild all the host NDK toolchains at once.
58You need to give it the path to the toolchain source directory, as
59downloaded by the 'download-toolchain-sources.sh' dev-script."
60
61extract_parameters "$@"
62
63# Check toolchain source path
64SRC_DIR="$PARAMETERS"
65if [ -z "$SRC_DIR" ]; then
66    echo "ERROR: Please provide the path to the toolchain source tree. See --help"
67    exit 1
68fi
69
70if [ ! -d "$SRC_DIR" ]; then
71    echo "ERROR: Not a directory: '$SRC_DIR'"
72    exit 1
73fi
74
75if [ ! -f "$SRC_DIR/build/configure" -o ! -d "$SRC_DIR/gcc/gcc-$DEFAULT_GCC_VERSION" ]; then
76    echo "ERROR: The file $SRC_DIR/build/configure or"
77    echo "       the directory $SRC_DIR/gcc/gcc-$DEFAULT_GCC_VERSION does not exist"
78    echo "This is not the top of a toolchain tree: $SRC_DIR"
79    echo "You must give the path to a copy of the toolchain source directories"
80    echo "created by 'download-toolchain-sources.sh."
81    exit 1
82fi
83
84# Now we can do the build
85BUILDTOOLS=$ANDROID_NDK_ROOT/build/tools
86
87if [ -z "$NO_GEN_PLATFORMS" ]; then
88    echo "Preparing the build..."
89    run $BUILDTOOLS/gen-platforms.sh --minimal --dst-dir=$NDK_DIR --ndk-dir=$NDK_DIR --arch=$(spaces_to_commas $ARCHS)
90    fail_panic "Could not generate minimal sysroot!"
91else
92    if [ ! -d "$NDK_DIR/platforms" ]; then
93        echo "ERROR: --no-gen-platforms used but directory missing: $NDK_DIR/platforms"
94        exit 1
95    fi
96fi
97
98SYSTEMS=$(commas_to_spaces $SYSTEMS)
99ARCHS=$(commas_to_spaces $ARCHS)
100
101if [ "$DARWIN_SSH" -a -z "$CUSTOM_SYSTEMS" ]; then
102    SYSTEMS=" darwin-x86"
103fi
104
105FLAGS=
106if [ "$VERBOSE" = "yes" ]; then
107    FLAGS=$FLAGS" --verbose"
108fi
109if [ "$VERBOSE2" = "yes" ]; then
110    FLAGS=$FLAGS" --verbose"
111fi
112if [ "$TRY64" = "yes" ]; then
113    FLAGS=$FLAGS" --try64"
114else
115    force_32bit_binaries
116fi
117if [ "$PACKAGE_DIR" ]; then
118    mkdir -p "$PACKAGE_DIR"
119    fail_panic "Could not create package directory: $PACKAGE_DIR"
120    FLAGS=$FLAGS" --package-dir=$PACKAGE_DIR"
121fi
122
123do_remote_host_build ()
124{
125    local SYSTEM="$1"
126    local ARCH="$2"
127    local REMOTE_HOST="$3"
128
129    # 1/ Copy the NDK toolchain build scripts
130    # 2/ Copy the toolchain sources/package
131    # 3/ Ssh to unpack the build scripts, and run them
132    # 4/ Copy back the generated prebuilt binaries
133    #
134    dump "Preparing remote build on $REMOTE_HOST..."
135
136    # First try to create a remote temp directory on the remote host
137    # Do it first so we can fail fast, not after spending time preparing
138    # large tarballs.
139    dump "Creating remote temp directory"
140    TMPREMOTE=/tmp/ndk-$USER/darwin-prebuild
141    run ssh $REMOTE_HOST "mkdir -p $TMPREMOTE && rm -rf $TMPREMOTE/*"
142    fail_panic "Could not create temporary build directory on $REMOTE_HOST"
143
144    # Now, package all the stuff we're going to send in a temporary
145    # directory here on the machine, except toolchain sources
146    TMPDARWIN=$NDK_TMPDIR/darwin  # Where we're going to package stuff
147    log "Using temporary work directory: $TMPDARWIN"
148    mkdir -p "$TMPDARWIN"
149    dump "Prepare NDK build scripts"
150    copy_directory "$ANDROID_NDK_ROOT/build" "$TMPDARWIN/ndk/build"
151    copy_file_list "$ANDROID_NDK_ROOT" "$TMPDARWIN/ndk" sources/android/libthread_db
152    copy_file_list "$ANDROID_NDK_ROOT" "$TMPDARWIN/ndk" "$STLPORT_SUBDIR"
153    copy_file_list "$ANDROID_NDK_ROOT" "$TMPDARWIN/ndk" "$GABIXX_SUBDIR"
154    copy_file_list "$ANDROID_NDK_ROOT" "$TMPDARWIN/ndk" sources/host-tools
155    dump "Prepare platforms files"
156    copy_directory "$NDK_DIR/platforms" "$TMPDARWIN/ndk/platforms"
157    dump "Copying NDK build scripts and platform files to remote..."
158    (cd "$TMPDARWIN" && tar czf - ndk) | (ssh $REMOTE_HOST tar xzf - -C $TMPREMOTE)
159    fail_panic "Could not copy!"
160    rm -rf $TMPDARWIN
161
162    # Copy the toolchain sources
163    dump "Copy toolchain sources to remote"
164    ssh $REMOTE_HOST mkdir -p $TMPREMOTE/toolchain &&
165    (cd "$SRC_DIR" && tar czf - .) | (ssh $REMOTE_HOST tar xzf - -C $TMPREMOTE/toolchain)
166    fail_panic "Could not copy toolchain!"
167
168    # Time to run the show :-)
169    for ARCH in $(commas_to_spaces $ARCHS); do
170        dump "Running remote $ARCH toolchain build..."
171        SYSROOT=$TMPREMOTE/ndk/platforms/android-$(get_default_api_level_for_arch $ARCH)/arch-$ARCH
172        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"
173        fail_panic "Could not build prebuilt $ARCH toolchain on Darwin!"
174    done
175    # Get the results
176    dump "Copying back Darwin prebuilt packages..."
177    mkdir -p $TMPDARWIN/packages && rm -rf $TMPDARWIN/packages/*
178    run scp $REMOTE_HOST:$TMPREMOTE/packages/*-darwin-* $TMPDARWIN/packages
179    fail_panic "Could not grab Darwin packages!"
180
181    for PACK in $TMPDARWIN/packages/*; do
182        if [ "$PACKAGE_DIR" ]; then
183            echo "Copying $(basename $PACK) to $PACKAGE_DIR..."
184            cp $PACK $PACKAGE_DIR/
185        else
186            echo "Unpacking $(basename $PACK) into $NDK_DIR..."
187            unpack_archive $PACK $NDK_DIR
188        fi
189        fail_panic ""
190    done
191
192    dump "Cleaning up remote machine..."
193    run ssh $REMOTE_HOST rm -rf $TMPREMOTE
194}
195
196for SYSTEM in $SYSTEMS; do
197
198    # First, build the toolchains
199    TOOLCHAIN_FLAGS=$FLAGS
200    if [ "$HOST_TAG" = "linux-x86" -a "$SYSTEM" = "windows" ]; then
201        TOOLCHAIN_FLAGS=$TOOLCHAIN_FLAGS" --mingw"
202    fi
203
204     # Should we do a remote build?
205    if [ "$SYSTEM" != "$HOST_TAG" ]; then
206        case $SYSTEM in
207            darwin-*)
208                if [ "$DARWIN_SSH" ]; then
209                    do_remote_host_build "$SYSTEM" "$ARCH" "$DARWIN_SSH"
210                else
211                    echo "WARNING: Can't build $SYSTEM binaries on this system!"
212                    echo "Consider using the --darwin-ssh option."
213                fi
214                continue
215        esac
216    fi
217
218    # First, ndk-stack
219    echo "Building $SYSTEM ndk-stack"
220    run $BUILDTOOLS/build-ndk-stack.sh $TOOLCHAIN_FLAGS
221    fail_panic "ndk-stack build failure!"
222
223    echo "Building $SYSTEM ndk-make"
224    run $BUILDTOOLS/build-host-make.sh $TOOLCHAIN_FLAGS
225    fail_panic "make build failure!"
226
227    echo "Building $SYSTEM ndk-awk"
228    run $BUILDTOOLS/build-host-awk.sh $TOOLCHAIN_FLAGS
229    fail_panic "awk build failure!"
230
231    echo "Building $SYSTEM ndk-sed"
232    run $BUILDTOOLS/build-host-sed.sh $TOOLCHAIN_FLAGS
233    fail_panic "sed build failure!"
234
235    if [ "$SYSTEM" = "windows" ]; then
236        run $BUILDTOOLS/build-host-toolbox.sh $FLAGS
237        fail_panic "Windows toolbox build failure!"
238    fi
239
240    # Then the toolchains
241    for ARCH in $ARCHS; do
242        TOOLCHAIN_NAMES=$(get_toolchain_name_list_for_arch $ARCH)
243        if [ -z "$TOOLCHAIN_NAMES" ]; then
244            echo "ERROR: Invalid architecture name: $ARCH"
245            exit 1
246        fi
247
248        for TOOLCHAIN_NAME in $TOOLCHAIN_NAMES; do
249            echo "Building $SYSTEM toolchain for $ARCH architecture: $TOOLCHAIN_NAME"
250            run $BUILDTOOLS/build-gcc.sh "$SRC_DIR" "$NDK_DIR" $TOOLCHAIN_NAME $TOOLCHAIN_FLAGS
251            fail_panic "Could not build $TOOLCHAIN_NAME-$SYSTEM!"
252        done
253    done
254
255    # We're done for this system
256done
257
258if [ "$PACKAGE_DIR" ]; then
259    echo "Done, please look at $PACKAGE_DIR"
260else
261    echo "Done"
262fi
263
264exit 0