1#!/bin/sh
2#
3# Copyright (C) 2012 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 shell script is used to rebuild the llvm and clang binaries
18#  for the Android NDK.
19#
20
21# include common function and variable definitions
22. `dirname $0`/prebuilt-common.sh
23
24PROGRAM_PARAMETERS="<src-dir> <ndk-dir> <toolchain>"
25
26PROGRAM_DESCRIPTION=\
27"Rebuild the LLVM prebuilt binaries for the Android NDK.
28
29Where <src-dir> is the location of toolchain sources, <ndk-dir> is
30the top-level NDK installation path and <toolchain> is the name of
31the toolchain to use (e.g. llvm-3.4)."
32
33RELEASE=`date +%Y%m%d`
34BUILD_OUT=/tmp/ndk-$USER/build/toolchain
35OPTION_BUILD_OUT=
36register_var_option "--build-out=<path>" OPTION_BUILD_OUT "Set temporary build directory"
37
38# Note: platform API level 9 or higher is needed for proper C++ support
39register_var_option "--platform=<name>"  PLATFORM "Specify platform name"
40
41GMP_VERSION=$DEFAULT_GMP_VERSION
42register_var_option "--gmp-version=<version>" GMP_VERSION "Specify gmp version"
43
44PACKAGE_DIR=
45register_var_option "--package-dir=<path>" PACKAGE_DIR "Create archive tarball in specific directory"
46
47POLLY=no
48do_polly_option () { POLLY=yes; }
49register_option "--with-polly" do_polly_option "Enable Polyhedral optimizations for LLVM"
50
51CHECK=no
52do_check_option () { CHECK=yes; }
53register_option "--check" do_check_option "Check LLVM"
54
55USE_PYTHON=no
56do_use_python_option () { USE_PYTHON=yes; }
57register_option "--use-python" do_use_python_option "Use python bc2native instead of integrated one"
58
59register_jobs_option
60register_canadian_option
61register_try64_option
62
63extract_parameters "$@"
64
65prepare_canadian_toolchain /tmp/ndk-$USER/build
66
67fix_option BUILD_OUT "$OPTION_BUILD_OUT" "build directory"
68setup_default_log_file $BUILD_OUT/config.log
69
70set_parameters ()
71{
72    SRC_DIR="$1"
73    NDK_DIR="$2"
74    TOOLCHAIN="$3"
75
76    # Check source directory
77    #
78    if [ -z "$SRC_DIR" ] ; then
79        echo "ERROR: Missing source directory parameter. See --help for details."
80        exit 1
81    fi
82
83    if [ ! -d "$SRC_DIR/$TOOLCHAIN/llvm" ] ; then
84        echo "ERROR: Source directory does not contain llvm sources: $SRC_DIR/$TOOLCHAIN/llvm"
85        exit 1
86    fi
87
88    if [ -e "$SRC_DIR/$TOOLCHAIN/llvm/tools/polly" -a ! -h "$SRC_DIR/$TOOLCHAIN/llvm/tools/polly" ] ; then
89        echo "ERROR: polly, if exist, needs to be a symbolic link: $SRC_DIR/$TOOLCHAIN/llvm/tools/polly"
90        exit 1
91    fi
92
93    GMP_SOURCE=$SRC_DIR/gmp/gmp-$GMP_VERSION.tar.bz2
94    if [ ! -f "$GMP_SOURCE" ] ; then
95        echo "ERROR: Source directory does not contain gmp: $GMP_SOURCE"
96        exit 1
97    fi
98    SRC_DIR=`cd $SRC_DIR; pwd`
99    log "Using source directory: $SRC_DIR"
100
101    # Check NDK installation directory
102    #
103    if [ -z "$NDK_DIR" ] ; then
104        echo "ERROR: Missing NDK directory parameter. See --help for details."
105        exit 1
106    fi
107
108    if [ ! -d "$NDK_DIR" ] ; then
109        mkdir -p $NDK_DIR
110        if [ $? != 0 ] ; then
111            echo "ERROR: Could not create target NDK installation path: $NDK_DIR"
112            exit 1
113        fi
114    fi
115    NDK_DIR=`cd $NDK_DIR; pwd`
116    log "Using NDK directory: $NDK_DIR"
117
118    # Check toolchain name
119    #
120    if [ -z "$TOOLCHAIN" ] ; then
121        echo "ERROR: Missing toolchain name parameter. See --help for details."
122        exit 1
123    fi
124}
125
126set_parameters $PARAMETERS
127
128if [ -z "$PLATFORM" ]; then
129   PLATFORM="android-"$(get_default_api_level_for_arch $ARCH)
130fi
131
132prepare_target_build
133
134if [ "$PACKAGE_DIR" ]; then
135    mkdir -p "$PACKAGE_DIR"
136    fail_panic "Could not create package directory: $PACKAGE_DIR"
137fi
138
139set_toolchain_ndk $NDK_DIR $TOOLCHAIN
140
141if [ "$MINGW" != "yes" -a "$DARWIN" != "yes" ] ; then
142    dump "Using C compiler: $CC"
143    dump "Using C++ compiler: $CXX"
144fi
145
146rm -rf $BUILD_OUT
147mkdir -p $BUILD_OUT
148
149MAKE_FLAGS=
150if [ "$VERBOSE" = "yes" ]; then
151    MAKE_FLAGS="VERBOSE=1"
152fi
153
154TOOLCHAIN_BUILD_PREFIX=$BUILD_OUT/prefix
155
156ARCH=$HOST_ARCH
157
158# Disable futimens@GLIBC_2.6 not available in system on server with very old libc.so
159CFLAGS_FOR_BUILD="-O2 -I$TOOLCHAIN_BUILD_PREFIX/include -DDISABLE_FUTIMENS"
160LDFLAGS_FOR_BUILD="-L$TOOLCHAIN_BUILD_PREFIX/lib"
161
162# Statically link stdc++ to eliminate dependency on outdated libctdc++.so in old 32-bit
163# linux system, and libgcc_s_sjlj-1.dll and libstdc++-6.dll on windows
164if [ "$MINGW" = "yes" -o "$HOST_TAG" = "linux-x86" ]; then
165    LDFLAGS_FOR_BUILD=$LDFLAGS_FOR_BUILD" -static-libgcc -static-libstdc++"
166fi
167
168CFLAGS="$CFLAGS $CFLAGS_FOR_BUILD $HOST_CFLAGS"
169CXXFLAGS="$CXXFLAGS $CFLAGS_FOR_BUILD $HOST_CFLAGS"  # polly doesn't look at CFLAGS !
170LDFLAGS="$LDFLAGS $LDFLAGS_FOR_BUILD $HOST_LDFLAGS"
171export CC CXX CFLAGS CXXFLAGS LDFLAGS CFLAGS_FOR_BUILD LDFLAGS_FOR_BUILD REQUIRES_RTTI=1 ARCH
172
173if [ "$DARWIN" = "yes" ]; then
174    # To stop /usr/bin/install -s calls strip on darwin binary
175    export KEEP_SYMBOLS=1
176    # Disable polly for now
177    POLLY=no
178fi
179
180if [ "$POLLY" = "yes" -a ! -d "$SRC_DIR/$TOOLCHAIN/polly" ] ; then
181    dump "Disable polly because $SRC_DIR/$TOOLCHAIN/polly doesn't exist"
182    POLLY=no
183fi
184
185EXTRA_CONFIG_FLAGS=
186rm -rf $SRC_DIR/$TOOLCHAIN/llvm/tools/polly
187if [ "$POLLY" = "yes" ]; then
188    # crate symbolic link
189    ln -s ../../polly $SRC_DIR/$TOOLCHAIN/llvm/tools
190
191    # build polly dependencies
192    unpack_archive "$GMP_SOURCE" "$BUILD_OUT"
193    fail_panic "Couldn't unpack $SRC_DIR/gmp/gmp-$GMP_VERSION to $BUILD_OUT"
194
195    GMP_BUILD_OUT=$BUILD_OUT/gmp-$GMP_VERSION
196    cd $GMP_BUILD_OUT
197    fail_panic "Couldn't cd into gmp build path: $GMP_BUILD_OUT"
198
199    OLD_ABI="${ABI}"
200    export ABI=$HOST_GMP_ABI  # needed to build 32-bit on 64-bit host
201    run ./configure \
202        --prefix=$TOOLCHAIN_BUILD_PREFIX \
203        --host=$ABI_CONFIGURE_HOST \
204        --build=$ABI_CONFIGURE_BUILD \
205        --disable-shared \
206        --disable-nls \
207        --enable-cxx
208    fail_panic "Couldn't configure gmp"
209    run make -j$NUM_JOBS
210    fail_panic "Couldn't compile gmp"
211    run make install
212    fail_panic "Couldn't install gmp to $TOOLCHAIN_BUILD_PREFIX"
213    ABI="$OLD_ABI"
214
215    CLOOG_BUILD_OUT=$BUILD_OUT/cloog
216    mkdir -p $CLOOG_BUILD_OUT && cd $CLOOG_BUILD_OUT
217    fail_panic "Couldn't create cloog build path: $CLOOG_BUILD_OUT"
218
219    run $SRC_DIR/$TOOLCHAIN/llvm/tools/polly/utils/cloog_src/configure \
220        --prefix=$TOOLCHAIN_BUILD_PREFIX \
221        --host=$ABI_CONFIGURE_HOST \
222        --build=$ABI_CONFIGURE_BUILD \
223        --with-gmp-prefix=$TOOLCHAIN_BUILD_PREFIX \
224        --disable-shared \
225        --disable-nls
226    fail_panic "Couldn't configure cloog"
227    run make -j$NUM_JOBS
228    fail_panic "Couldn't compile cloog"
229    run make install
230    fail_panic "Couldn't install cloog to $TOOLCHAIN_BUILD_PREFIX"
231
232    EXTRA_CONFIG_FLAGS="--with-cloog=$TOOLCHAIN_BUILD_PREFIX --with-isl=$TOOLCHAIN_BUILD_PREFIX"
233
234    # Allow text relocs when linking LLVMPolly.dylib against statically linked libgmp.a
235    if [ "$HOST_TAG32" = "darwin-x86" -o "$DARWIN" = "yes" ]; then   # -a "$HOST_ARCH" = "x86"
236        LDFLAGS="$LDFLAGS -read_only_relocs suppress"
237        export LDFLAGS
238    fi
239fi # POLLY = yes
240
241# configure the toolchain
242dump "Configure: $TOOLCHAIN toolchain build"
243LLVM_BUILD_OUT=$BUILD_OUT/llvm
244mkdir -p $LLVM_BUILD_OUT && cd $LLVM_BUILD_OUT
245fail_panic "Couldn't cd into llvm build path: $LLVM_BUILD_OUT"
246
247# Only start using integrated bc2native source >= 3.3 by default
248LLVM_VERSION="`echo $TOOLCHAIN | tr '-' '\n' | tail -n 1`"
249LLVM_VERSION_MAJOR=`echo $LLVM_VERSION | tr '.' '\n' | head -n 1`
250LLVM_VERSION_MINOR=`echo $LLVM_VERSION | tr '.' '\n' | tail -n 1`
251if [ $LLVM_VERSION_MAJOR -lt 3 ]; then
252    USE_PYTHON=yes
253elif [ $LLVM_VERSION_MAJOR -eq 3 ] && [ $LLVM_VERSION_MINOR -lt 3 ]; then
254    USE_PYTHON=yes
255fi
256
257if [ "$USE_PYTHON" != "yes" ]; then
258    # Refresh intermediate source
259    rm -f $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native/*.cpp
260    rm -f $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native/*.c
261    rm -f $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native/*.h
262    run cp -a $NDK_DIR/tests/abcc/jni/*.cpp $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native
263    run cp -a $NDK_DIR/tests/abcc/jni/*.h $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native
264    run cp -a $NDK_DIR/tests/abcc/jni/host/*.cpp $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native
265    run cp -a $NDK_DIR/tests/abcc/jni/host/*.h $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native
266    run cp -a $NDK_DIR/tests/abcc/jni/mman-win32/mman.[ch] $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native
267    export LLVM_TOOLS_FILTER="PARALLEL_DIRS:=\$\$(PARALLEL_DIRS:%=% ndk-bc2native)"
268fi
269
270BINUTILS_VERSION=$(get_default_binutils_version_for_llvm $TOOLCHAIN)
271
272run $SRC_DIR/$TOOLCHAIN/llvm/configure \
273    --prefix=$TOOLCHAIN_BUILD_PREFIX \
274    --host=$ABI_CONFIGURE_HOST \
275    --build=$ABI_CONFIGURE_BUILD \
276    --with-bug-report-url=$DEFAULT_ISSUE_TRACKER_URL \
277    --enable-targets=arm,mips,x86,aarch64 \
278    --enable-optimized \
279    --with-binutils-include=$SRC_DIR/binutils/binutils-$BINUTILS_VERSION/include \
280    $EXTRA_CONFIG_FLAGS
281fail_panic "Couldn't configure llvm toolchain"
282
283# build llvm/clang
284dump "Building : llvm toolchain [this can take a long time]."
285cd $LLVM_BUILD_OUT
286run make -j$NUM_JOBS $MAKE_FLAGS
287fail_panic "Couldn't compile llvm toolchain"
288
289if [ "$CHECK" = "yes" -a "$MINGW" != "yes" -a "$DARWIN" != "yes" ] ; then
290    # run the regression test
291    dump "Running  : llvm toolchain regression test"
292    cd $LLVM_BUILD_OUT
293    run make check-all
294    fail_warning "Couldn't pass all llvm regression test"  # change to fail_panic later
295    if [ "$POLLY" = "yes" ]; then
296        dump "Running  : polly toolchain regression test"
297        cd $LLVM_BUILD_OUT
298        run make polly-test -C tools/polly/test
299        fail_warning "Couldn't pass all polly regression test"  # change to fail_panic later
300    fi
301fi
302
303# install the toolchain to its final location
304dump "Install  : llvm toolchain binaries"
305cd $LLVM_BUILD_OUT && run make install $MAKE_FLAGS
306fail_panic "Couldn't install llvm toolchain to $TOOLCHAIN_BUILD_PREFIX"
307
308# copy arm_neon_x86.h from GCC
309GCC_SRC_DIR=$SRC_DIR/gcc/gcc-$DEFAULT_GCC32_VERSION
310cp -a $GCC_SRC_DIR/gcc/config/i386/arm_neon.h $TOOLCHAIN_BUILD_PREFIX/lib/clang/$LLVM_VERSION/include/arm_neon_x86.h
311
312# Since r156448, llvm installs a separate llvm-config-host when cross-compiling. Use llvm-config-host if this
313# exists otherwise llvm-config.
314# Note, llvm-config-host should've really been called llvm-config-build and the following changes fix this by
315# doing this rename and also making a proper llvm-config-host;
316# https://android-review.googlesource.com/#/c/64261/
317# https://android-review.googlesource.com/#/c/64263/
318# .. with these fixes in place, Darwin mclinker can be cross-compiled and Wine is not needed for Windows cross
319# To my mind, llvm-config-host is a misnomer and it should be llvm-config-build.
320LLVM_CONFIG=$TOOLCHAIN_BUILD_PREFIX/bin/llvm-config
321if [ -f $TOOLCHAIN_BUILD_PREFIX/bin/llvm-config-host ] ; then
322    LLVM_CONFIG=$TOOLCHAIN_BUILD_PREFIX/bin/llvm-config-host
323fi
324
325# build mclinker only against default the LLVM version, once
326if [ "$TOOLCHAIN" = "llvm-$DEFAULT_LLVM_VERSION" ] ; then
327    dump "Copy     : mclinker source"
328    MCLINKER_SRC_DIR=$BUILD_OUT/mclinker
329    mkdir -p $MCLINKER_SRC_DIR
330    fail_panic "Couldn't create mclinker source directory: $MCLINKER_SRC_DIR"
331
332    run copy_directory "$SRC_DIR/mclinker" "$MCLINKER_SRC_DIR"
333    fail_panic "Couldn't copy mclinker source: $MCLINKER_SRC_DIR"
334
335    CXXFLAGS="$CXXFLAGS -fexceptions"  # optimized/ScriptParser.cc needs it
336    export CXXFLAGS
337
338    cd $MCLINKER_SRC_DIR && run ./autogen.sh
339    fail_panic "Couldn't run autogen.sh in $MCLINKER_SRC_DIR"
340
341    dump "Configure: mclinker against $TOOLCHAIN"
342    MCLINKER_BUILD_OUT=$MCLINKER_SRC_DIR/build
343    mkdir -p $MCLINKER_BUILD_OUT && cd $MCLINKER_BUILD_OUT
344    fail_panic "Couldn't cd into mclinker build path: $MCLINKER_BUILD_OUT"
345
346    run $MCLINKER_SRC_DIR/configure \
347        --prefix=$TOOLCHAIN_BUILD_PREFIX \
348        --with-llvm-config=$LLVM_CONFIG \
349        --host=$ABI_CONFIGURE_HOST \
350        --build=$ABI_CONFIGURE_BUILD
351    fail_panic "Couldn't configure mclinker"
352
353    dump "Building : mclinker"
354    if [ "$MINGW" = "yes" ]; then
355        MAKE_FLAGS="$MAKE_FLAGS LIBS=-lshlwapi" # lib/Object/SectionMap.cpp needs PathMatchSpec to replace fnmatch()
356    fi
357    cd $MCLINKER_BUILD_OUT
358    run make -j$NUM_JOBS $MAKE_FLAGS CXXFLAGS="$CXXFLAGS"
359    fail_panic "Couldn't compile mclinker"
360
361    dump "Install  : mclinker"
362    cd $MCLINKER_BUILD_OUT && run make install $MAKE_FLAGS
363    fail_panic "Couldn't install mclinker to $TOOLCHAIN_BUILD_PREFIX"
364
365    if [ "$CHECK" = "yes" -a "$MINGW" != "yes" -a "$DARWIN" != "yes" ] ; then
366        # run the regression test
367        dump "Running  : mclinker regression test"
368        cd $MCLINKER_BUILD_OUT
369        run make check
370        fail_warning "Couldn't pass all mclinker regression test"  # change to fail_panic later
371    fi
372fi
373
374# remove redundant bits
375rm -rf $TOOLCHAIN_BUILD_PREFIX/docs
376rm -rf $TOOLCHAIN_BUILD_PREFIX/include
377rm -rf $TOOLCHAIN_BUILD_PREFIX/lib/*.a
378rm -rf $TOOLCHAIN_BUILD_PREFIX/lib/*.la
379rm -rf $TOOLCHAIN_BUILD_PREFIX/lib/pkgconfig
380rm -rf $TOOLCHAIN_BUILD_PREFIX/lib/lib[cp]*.so
381rm -rf $TOOLCHAIN_BUILD_PREFIX/lib/lib[cp]*.dylib
382rm -rf $TOOLCHAIN_BUILD_PREFIX/lib/B*.so
383rm -rf $TOOLCHAIN_BUILD_PREFIX/lib/B*.dylib
384rm -rf $TOOLCHAIN_BUILD_PREFIX/lib/LLVMH*.so
385rm -rf $TOOLCHAIN_BUILD_PREFIX/lib/LLVMH*.dylib
386if [ -f $TOOLCHAIN_BUILD_PREFIX/bin/ld.lite${HOST_EXE} ]; then
387    # rename ld.lite to ld.mcld
388    rm -rf $TOOLCHAIN_BUILD_PREFIX/bin/ld.[bm]*
389    mv -f $TOOLCHAIN_BUILD_PREFIX/bin/ld.lite${HOST_EXE} $TOOLCHAIN_BUILD_PREFIX/bin/ld.mcld${HOST_EXE}
390else
391    rm -rf $TOOLCHAIN_BUILD_PREFIX/bin/ld.bcc
392fi
393rm -rf $TOOLCHAIN_BUILD_PREFIX/share
394
395UNUSED_LLVM_EXECUTABLES="
396bugpoint c-index-test clang-check clang-format clang-tblgen lli llvm-bcanalyzer
397llvm-config llvm-config-host llvm-cov llvm-diff llvm-dwarfdump llvm-extract llvm-ld
398llvm-mc llvm-nm llvm-mcmarkup llvm-objdump llvm-prof llvm-ranlib llvm-readobj llvm-rtdyld
399llvm-size llvm-stress llvm-stub llvm-symbolizer llvm-tblgen macho-dump cloog lli-child-target"
400
401for i in $UNUSED_LLVM_EXECUTABLES; do
402    rm -f $TOOLCHAIN_BUILD_PREFIX/bin/$i
403    rm -f $TOOLCHAIN_BUILD_PREFIX/bin/$i.exe
404done
405
406test -z "$STRIP" && STRIP=strip
407find $TOOLCHAIN_BUILD_PREFIX/bin -maxdepth 1 -type f -exec $STRIP {} \;
408# Note that MacOSX strip generate the follow error on .dylib:
409# "symbols referenced by indirect symbol table entries that can't be stripped "
410find $TOOLCHAIN_BUILD_PREFIX/lib -maxdepth 1 -type f \( -name "*.dll" -o -name "*.so" \) -exec $STRIP {} \;
411
412# For now, le64-tools is just like le32 ones
413if [ -f "$TOOLCHAIN_BUILD_PREFIX/bin/ndk-link${HOST_EXE}" ]; then
414    run ln -s ndk-link${HOST_EXE} $TOOLCHAIN_BUILD_PREFIX/bin/le32-none-ndk-link${HOST_EXE}
415    run ln -s ndk-link${HOST_EXE} $TOOLCHAIN_BUILD_PREFIX/bin/le64-none-ndk-link${HOST_EXE}
416fi
417if [ -f "$TOOLCHAIN_BUILD_PREFIX/bin/ndk-strip${HOST_EXE}" ]; then
418    run ln -s ndk-strip${HOST_EXE} $TOOLCHAIN_BUILD_PREFIX/bin/le32-none-ndk-strip${HOST_EXE}
419    run ln -s ndk-strip${HOST_EXE} $TOOLCHAIN_BUILD_PREFIX/bin/le64-none-ndk-strip${HOST_EXE}
420fi
421if [ -f "$TOOLCHAIN_BUILD_PREFIX/bin/ndk-translate${HOST_EXE}" ]; then
422    run ln -s ndk-translate${HOST_EXE} $TOOLCHAIN_BUILD_PREFIX/bin/le32-none-ndk-translate${HOST_EXE}
423    run ln -s ndk-translate${HOST_EXE} $TOOLCHAIN_BUILD_PREFIX/bin/le64-none-ndk-translate${HOST_EXE}
424fi
425
426# install script
427if [ "$USE_PYTHON" != "yes" ]; then
428    # Remove those intermediate cpp
429    rm -f $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native/*.cpp
430    rm -f $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native/*.c
431    rm -f $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native/*.h
432else
433    cp -p "$SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native/ndk-bc2native.py" "$TOOLCHAIN_BUILD_PREFIX/bin/"
434fi
435
436# copy to toolchain path
437run copy_directory "$TOOLCHAIN_BUILD_PREFIX" "$TOOLCHAIN_PATH"
438
439# create analyzer/++ scripts
440ABIS=$PREBUILT_ABIS
441# temp hack before 64-bit ABIs are part of PREBUILT_ABIS
442if [ "$ABIS" != "${ABIS%%64*}" ]; then
443    ABIS="$PREBUILT_ABIS arm64-v8a x86_64 mips64"
444fi
445ABIS=$ABIS
446for ABI in $ABIS; do
447    ANALYZER_PATH="$TOOLCHAIN_PATH/bin/$ABI"
448    ANALYZER="$ANALYZER_PATH/analyzer"
449    mkdir -p "$ANALYZER_PATH"
450    case "$ABI" in
451      armeabi)
452          LLVM_TARGET=armv5te-none-linux-androideabi
453          ;;
454      armeabi-v7a|armeabi-v7a-hard)
455          LLVM_TARGET=armv7-none-linux-androideabi
456          ;;
457      arm64-v8a)
458          LLVM_TARGET=aarch64-none-linux-android
459          ;;
460      x86)
461          LLVM_TARGET=i686-none-linux-android
462          ;;
463      x86_64)
464          LLVM_TARGET=x86_64-none-linux-android
465          ;;
466      mips)
467          LLVM_TARGET=mipsel-none-linux-android
468          ;;
469      mips64)
470          LLVM_TARGET=mips64el-none-linux-android
471          ;;
472      *)
473        dump "ERROR: Unsupported NDK ABI: $ABI"
474        exit 1
475    esac
476
477    cat > "${ANALYZER}" <<EOF
478if [ "\$1" != "-cc1" ]; then
479    \`dirname \$0\`/../clang -target $LLVM_TARGET "\$@"
480else
481    # target/triple already spelled out.
482    \`dirname \$0\`/../clang "\$@"
483fi
484EOF
485    cat > "${ANALYZER}++" <<EOF
486if [ "\$1" != "-cc1" ]; then
487    \`dirname \$0\`/../clang++ -target $LLVM_TARGET "\$@"
488else
489    # target/triple already spelled out.
490    \`dirname \$0\`/../clang++ "\$@"
491fi
492EOF
493    chmod 0755 "${ANALYZER}" "${ANALYZER}++"
494
495    if [ -n "$HOST_EXE" ] ; then
496        cat > "${ANALYZER}.cmd" <<EOF
497@echo off
498if "%1" == "-cc1" goto :L
499%~dp0\\..\\clang${HOST_EXE} -target $LLVM_TARGET %*
500if ERRORLEVEL 1 exit /b 1
501goto :done
502:L
503rem target/triple already spelled out.
504%~dp0\\..\\clang${HOST_EXE} %*
505if ERRORLEVEL 1 exit /b 1
506:done
507EOF
508        cat > "${ANALYZER}++.cmd" <<EOF
509@echo off
510if "%1" == "-cc1" goto :L
511%~dp0\\..\\clang++${HOST_EXE} -target $LLVM_TARGET %*
512if ERRORLEVEL 1 exit /b 1
513goto :done
514:L
515rem target/triple already spelled out.
516%~dp0\\..\\clang++${HOST_EXE} %*
517if ERRORLEVEL 1 exit /b 1
518:done
519EOF
520        chmod 0755 "${ANALYZER}.cmd" "${ANALYZER}++.cmd"
521    fi
522done
523
524# copy SOURCES file if present
525if [ -f "$SRC_DIR/SOURCES" ]; then
526    cp "$SRC_DIR/SOURCES" "$TOOLCHAIN_PATH/SOURCES"
527fi
528
529if [ "$PACKAGE_DIR" ]; then
530    ARCHIVE="$TOOLCHAIN-$HOST_TAG.tar.bz2"
531    SUBDIR=$(get_toolchain_install_subdir $TOOLCHAIN $HOST_TAG)
532    dump "Packaging $ARCHIVE"
533    pack_archive "$PACKAGE_DIR/$ARCHIVE" "$NDK_DIR" "$SUBDIR"
534fi
535
536dump "Done."
537if [ -z "$OPTION_BUILD_OUT" ] ; then
538    rm -rf $BUILD_OUT
539fi
540