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