• 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"
36    # If darwin toolchain exist, build darwin too
37    if [ -f "${DARWIN_TOOLCHAIN}-gcc" ]; then
38        SYSTEMS=$SYSTEMS",darwin-x86"
39    fi
40fi
41CUSTOM_SYSTEMS=
42register_option "--systems=<names>" do_SYSTEMS "List of host systems to build for"
43do_SYSTEMS () { CUSTOM_SYSTEMS=true; SYSTEMS=$1; }
44
45ARCHS=$(find_ndk_unknown_archs)
46ARCHS="$DEFAULT_ARCHS $ARCHS"
47register_var_option "--arch=<list>" ARCHS "List of target archs to build for"
48
49PACKAGE_DIR=
50register_var_option "--package-dir=<path>" PACKAGE_DIR "Package toolchain into this directory"
51
52DARWIN_SSH=
53register_var_option "--darwin-ssh=<hostname>" DARWIN_SSH "Generate darwin packages on remote host"
54
55NO_GEN_PLATFORMS=
56register_var_option "--no-gen-platforms" NO_GEN_PLATFORMS "Don't generate platforms/ directory, use existing one"
57
58GCC_VERSION_LIST="default" # it's arch defined by default so use default keyword
59register_var_option "--gcc-version-list=<vers>" GCC_VERSION_LIST "List of GCC release versions"
60
61LLVM_VERSION_LIST=$DEFAULT_LLVM_VERSION_LIST
62register_var_option "--llvm-version-list=<vers>" LLVM_VERSION_LIST "List of LLVM release versions"
63
64CHECK_FLAG=
65do_check_option () { CHECK_FLAG="--check"; }
66register_option "--check" do_check_option "Check host prebuilts"
67
68register_try64_option
69
70PROGRAM_PARAMETERS="<toolchain-src-dir>"
71PROGRAM_DESCRIPTION=\
72"This script can be used to rebuild all the host NDK toolchains at once.
73You need to give it the path to the toolchain source directory, as
74downloaded by the 'download-toolchain-sources.sh' dev-script."
75
76extract_parameters "$@"
77
78# Check toolchain source path
79SRC_DIR="$PARAMETERS"
80if [ -z "$SRC_DIR" ]; then
81    echo "ERROR: Please provide the path to the toolchain source tree. See --help"
82    exit 1
83fi
84
85if [ ! -d "$SRC_DIR" ]; then
86    echo "ERROR: Not a directory: '$SRC_DIR'"
87    exit 1
88fi
89
90if [ ! -f "$SRC_DIR/build/configure" -o ! -d "$SRC_DIR/gcc/gcc-$DEFAULT_GCC32_VERSION" -o ! -d "$SRC_DIR/gcc/gcc-$DEFAULT_GCC64_VERSION" ]; then
91    echo "ERROR: The file $SRC_DIR/build/configure or"
92    echo "       the directory $SRC_DIR/gcc/gcc-$DEFAULT_GCC32_VERSION or"
93    echo "       $SRC_DIR/gcc/gcc-$DEFAULT_GCC64_VERSION does not exist"
94    echo "This is not the top of a toolchain tree: $SRC_DIR"
95    echo "You must give the path to a copy of the toolchain source directories"
96    echo "created by 'download-toolchain-sources.sh."
97    exit 1
98fi
99
100# Now we can do the build
101BUILDTOOLS=$ANDROID_NDK_ROOT/build/tools
102
103if [ -z "$NO_GEN_PLATFORMS" ]; then
104    echo "Preparing the build..."
105    run $BUILDTOOLS/gen-platforms.sh --minimal --dst-dir=$NDK_DIR --ndk-dir=$NDK_DIR --arch=$(spaces_to_commas $ARCHS)
106    fail_panic "Could not generate minimal sysroot!"
107else
108    if [ ! -d "$NDK_DIR/platforms" ]; then
109        echo "ERROR: --no-gen-platforms used but directory missing: $NDK_DIR/platforms"
110        exit 1
111    fi
112fi
113
114SYSTEMS=$(commas_to_spaces $SYSTEMS)
115ARCHS=$(commas_to_spaces $ARCHS)
116
117# Detect unknown arch
118UNKNOWN_ARCH=$(filter_out "$DEFAULT_ARCHS" "$ARCHS")
119if [ ! -z "$UNKNOWN_ARCH" ]; then
120    ARCHS=$(filter_out "$UNKNOWN_ARCH" "$ARCHS")
121fi
122
123LLVM_VERSION_LIST=$(commas_to_spaces $LLVM_VERSION_LIST)
124
125if [ "$DARWIN_SSH" -a -z "$CUSTOM_SYSTEMS" ]; then
126    SYSTEMS=" darwin-x86"
127fi
128
129FLAGS=
130if [ "$DRYRUN" = "yes" ]; then
131    FLAGS=$FLAGS" --dryrun"
132fi
133if [ "$VERBOSE" = "yes" ]; then
134    FLAGS=$FLAGS" --verbose"
135fi
136if [ "$VERBOSE2" = "yes" ]; then
137    FLAGS=$FLAGS" --verbose"
138fi
139if [ "$TRY64" = "yes" ]; then
140    FLAGS=$FLAGS" --try-64"
141else
142    force_32bit_binaries
143fi
144if [ "$PACKAGE_DIR" ]; then
145    mkdir -p "$PACKAGE_DIR"
146    fail_panic "Could not create package directory: $PACKAGE_DIR"
147    FLAGS=$FLAGS" --package-dir=$PACKAGE_DIR"
148fi
149
150do_remote_host_build ()
151{
152    local SYSTEM="$1"
153    local ARCH="$2"
154    local REMOTE_HOST="$3"
155
156    # 1/ Copy the NDK toolchain build scripts
157    # 2/ Copy the toolchain sources/package
158    # 3/ Ssh to unpack the build scripts, and run them
159    # 4/ Copy back the generated prebuilt binaries
160    #
161    dump "Preparing remote build on $REMOTE_HOST..."
162
163    # First try to create a remote temp directory on the remote host
164    # Do it first so we can fail fast, not after spending time preparing
165    # large tarballs.
166    dump "Creating remote temp directory"
167    TMPREMOTE=/tmp/ndk-$USER/darwin-prebuild
168    run ssh $REMOTE_HOST "mkdir -p $TMPREMOTE && rm -rf $TMPREMOTE/*"
169    fail_panic "Could not create temporary build directory on $REMOTE_HOST"
170
171    # Now, package all the stuff we're going to send in a temporary
172    # directory here on the machine, except toolchain sources
173    TMPDARWIN=$NDK_TMPDIR/darwin  # Where we're going to package stuff
174    log "Using temporary work directory: $TMPDARWIN"
175    mkdir -p "$TMPDARWIN"
176    dump "Prepare NDK build scripts"
177    copy_directory "$ANDROID_NDK_ROOT/build" "$TMPDARWIN/ndk/build"
178    copy_file_list "$ANDROID_NDK_ROOT" "$TMPDARWIN/ndk" sources/android/libthread_db
179    copy_file_list "$ANDROID_NDK_ROOT" "$TMPDARWIN/ndk" "$STLPORT_SUBDIR"
180    copy_file_list "$ANDROID_NDK_ROOT" "$TMPDARWIN/ndk" "$GABIXX_SUBDIR"
181    copy_file_list "$ANDROID_NDK_ROOT" "$TMPDARWIN/ndk" sources/host-tools
182    dump "Prepare platforms files"
183    copy_directory "$NDK_DIR/platforms" "$TMPDARWIN/ndk/platforms"
184    dump "Copying NDK build scripts and platform files to remote..."
185    (cd "$TMPDARWIN" && tar czf - ndk) | (ssh $REMOTE_HOST tar xzf - -C $TMPREMOTE)
186    fail_panic "Could not copy!"
187    rm -rf $TMPDARWIN
188
189    # Copy the toolchain sources
190    dump "Copy toolchain sources to remote"
191    ssh $REMOTE_HOST mkdir -p $TMPREMOTE/toolchain &&
192    (cd "$SRC_DIR" && tar czf - .) | (ssh $REMOTE_HOST tar xzf - -C $TMPREMOTE/toolchain)
193    fail_panic "Could not copy toolchain!"
194
195    # Time to run the show :-)
196    for ARCH in $(commas_to_spaces $ARCHS $UNKNOWN_ARCH); do
197        dump "Running remote $ARCH toolchain build..."
198        SYSROOT=$TMPREMOTE/ndk/platforms/android-$(get_default_api_level_for_arch $ARCH)/arch-$ARCH
199        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"
200        fail_panic "Could not build prebuilt $ARCH toolchain on Darwin!"
201    done
202    # Get the results
203    dump "Copying back Darwin prebuilt packages..."
204    mkdir -p $TMPDARWIN/packages && rm -rf $TMPDARWIN/packages/*
205    run scp $REMOTE_HOST:$TMPREMOTE/packages/*-darwin-* $TMPDARWIN/packages
206    fail_panic "Could not grab Darwin packages!"
207
208    for PACK in $TMPDARWIN/packages/*; do
209        if [ "$PACKAGE_DIR" ]; then
210            echo "Copying $(basename $PACK) to $PACKAGE_DIR..."
211            cp $PACK $PACKAGE_DIR/
212        else
213            echo "Unpacking $(basename $PACK) into $NDK_DIR..."
214            unpack_archive $PACK $NDK_DIR
215        fi
216        fail_panic ""
217    done
218
219    dump "Cleaning up remote machine..."
220    run ssh $REMOTE_HOST rm -rf $TMPREMOTE
221}
222
223for SYSTEM in $SYSTEMS; do
224
225    # Add --mingw/--darwin flag
226    TOOLCHAIN_FLAGS=$FLAGS
227    CANADIAN_BUILD=no
228    if [ "$HOST_TAG32" = "linux-x86" ]; then
229        case "$SYSTEM" in
230            windows)
231                TOOLCHAIN_FLAGS=$TOOLCHAIN_FLAGS" --mingw"
232                CANADIAN_BUILD=yes
233                ;;
234            darwin-x86)
235                TOOLCHAIN_FLAGS=$TOOLCHAIN_FLAGS" --darwin"
236                CANADIAN_BUILD=yes
237                ;;
238        esac
239    fi
240
241    # Should we do a remote build?
242    if [ "$SYSTEM" != "$HOST_TAG32" -a "$CANADIAN_BUILD" != "yes" ]; then
243        case $SYSTEM in
244            darwin-*)
245                if [ "$DARWIN_SSH" ]; then
246                    do_remote_host_build "$SYSTEM" "$ARCH" "$DARWIN_SSH"
247                else
248                    echo "WARNING: Can't build $SYSTEM binaries on this system!"
249                    echo "Consider using the --darwin-ssh option."
250                fi
251                continue
252        esac
253    fi
254
255    # Determin the display system name
256    SYSNAME=$SYSTEM
257    if [ "$TRY64" = "yes" ]; then
258        case $SYSTEM in
259            darwin-x86|linux-x86)
260                SYSNAME=${SYSTEM%%x86}x86_64
261                ;;
262            windows)
263                SYSNAME=windows-x86_64
264                ;;
265        esac
266    fi
267
268    # First, ndk-stack
269    echo "Building $SYSNAME ndk-stack"
270    run $BUILDTOOLS/build-ndk-stack.sh $TOOLCHAIN_FLAGS --with-libbfd --src-dir=$SRC_DIR
271    fail_panic "ndk-stack build failure!"
272
273    echo "Building $SYSNAME ndk-depends"
274    run $BUILDTOOLS/build-ndk-depends.sh $TOOLCHAIN_FLAGS
275    fail_panic "ndk-depends build failure!"
276
277    echo "Building $SYSNAME ndk-make"
278    run $BUILDTOOLS/build-host-make.sh $TOOLCHAIN_FLAGS
279    fail_panic "make build failure!"
280
281    echo "Building $SYSNAME ndk-awk"
282    run $BUILDTOOLS/build-host-awk.sh $TOOLCHAIN_FLAGS
283    fail_panic "awk build failure!"
284
285    # ToDo: perl in windows/darwin cross.
286    MAKE_PERL=no
287    case $SYSTEM in
288        linux*)
289            MAKE_PERL=yes
290        ;;
291        darwin*)
292            # Only works if not cross compiling.
293            if [ "$CANADIAN_BUILD" = "no" ] ; then
294                MAKE_PERL=yes
295            fi
296        ;;
297        *)
298        ;;
299    esac
300
301    if [ "$MAKE_PERL" = "yes" ] ; then
302        echo "Building $SYSNAME ndk-perl"
303        run $BUILDTOOLS/build-host-perl.sh $TOOLCHAIN_FLAGS "$SRC_DIR"
304        fail_panic "perl build failure!"
305    fi
306
307    echo "Building $SYSNAME ndk-python"
308    run $BUILDTOOLS/build-host-python.sh $TOOLCHAIN_FLAGS "--toolchain-src-dir=$SRC_DIR" "--systems=$SYSTEM" "--force"
309    fail_panic "python build failure!"
310
311    echo "Building $SYSNAME ndk-yasm"
312    run $BUILDTOOLS/build-host-yasm.sh "$SRC_DIR" "$NDK_DIR" $TOOLCHAIN_FLAGS
313    fail_panic "yasm build failure!"
314
315    if [ "$SYSTEM" = "windows" ]; then
316        echo "Building $SYSNAME toolbox"
317        run $BUILDTOOLS/build-host-toolbox.sh $FLAGS
318        fail_panic "Windows toolbox build failure!"
319    fi
320
321    # Then the toolchains
322    for ARCH in $ARCHS; do
323        if [ "$GCC_VERSION_LIST" != "default" ]; then
324            VERSIONS=$(spaces_to_commas $GCC_VERSION_LIST)
325            TOOLCHAIN_NAMES=$(get_toolchain_name_list_for_arch $ARCH $VERSIONS)
326        else
327            TOOLCHAIN_NAMES=$(get_toolchain_name_list_for_arch $ARCH)
328        fi
329        if [ -z "$TOOLCHAIN_NAMES" ]; then
330            echo "ERROR: Toolchains: "$(spaces_to_commas $GCC_VERSION_LIST)" are not available for arch: $ARCH"
331            exit 1
332        fi
333
334        for TOOLCHAIN_NAME in $TOOLCHAIN_NAMES; do
335            echo "Building $SYSNAME toolchain for $ARCH architecture: $TOOLCHAIN_NAME"
336            run $BUILDTOOLS/build-gcc.sh "$SRC_DIR" "$NDK_DIR" $TOOLCHAIN_NAME $TOOLCHAIN_FLAGS --with-python=prebuilt -j$BUILD_NUM_CPUS
337            fail_panic "Could not build $TOOLCHAIN_NAME-$SYSNAME!"
338        done
339    done
340
341    # Build llvm and clang
342    POLLY_FLAGS=
343    if [ "$TRY64" != "yes" -a "$SYSTEM" != "windows" ]; then
344        POLLY_FLAGS="--with-polly"
345    fi
346    for LLVM_VERSION in $LLVM_VERSION_LIST; do
347        echo "Building $SYSNAME clang/llvm-$LLVM_VERSION"
348        run $BUILDTOOLS/build-llvm.sh "$SRC_DIR" "$NDK_DIR" "llvm-$LLVM_VERSION" $TOOLCHAIN_FLAGS $POLLY_FLAGS $CHECK_FLAG -j$BUILD_NUM_CPUS
349        fail_panic "Could not build llvm for $SYSNAME"
350    done
351
352    if [ ! -z "$LLVM_VERSION_LIST" ]; then
353        # Deploy ld.mcld
354        run $PROGDIR/deploy-host-mcld.sh --package-dir=$PACKAGE_DIR --systems=$SYSNAME
355        fail_panic "Could not deploy ld.mcld for $SYSNAME"
356    fi
357
358    # We're done for this system
359done
360
361# Build tools common to all system
362run $BUILDTOOLS/build-analyzer.sh "$SRC_DIR" "$NDK_DIR" "llvm-$DEFAULT_LLVM_VERSION" --package-dir="$PACKAGE_DIR"
363
364if [ "$PACKAGE_DIR" ]; then
365    echo "Done, please look at $PACKAGE_DIR"
366else
367    echo "Done"
368fi
369
370exit 0
371