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.1)."
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
39PLATFORM=$DEFAULT_PLATFORM
40register_var_option "--platform=<name>"  PLATFORM "Specify platform name"
41
42PACKAGE_DIR=
43register_var_option "--package-dir=<path>" PACKAGE_DIR "Create archive tarball in specific directory"
44
45register_jobs_option
46register_mingw_option
47register_try64_option
48
49extract_parameters "$@"
50
51prepare_mingw_toolchain /tmp/ndk-$USER/build
52
53fix_option BUILD_OUT "$OPTION_BUILD_OUT" "build directory"
54setup_default_log_file $BUILD_OUT/config.log
55
56set_parameters ()
57{
58    SRC_DIR="$1"
59    NDK_DIR="$2"
60    TOOLCHAIN="$3"
61
62    # Check source directory
63    #
64    if [ -z "$SRC_DIR" ] ; then
65        echo "ERROR: Missing source directory parameter. See --help for details."
66        exit 1
67    fi
68
69    if [ ! -d "$SRC_DIR/llvm" ] ; then
70        echo "ERROR: Source directory does not contain llvm sources: $SRC_DIR"
71        exit 1
72    fi
73
74    log "Using source directory: $SRC_DIR"
75
76    # Check NDK installation directory
77    #
78    if [ -z "$NDK_DIR" ] ; then
79        echo "ERROR: Missing NDK directory parameter. See --help for details."
80        exit 1
81    fi
82
83    if [ ! -d "$NDK_DIR" ] ; then
84        mkdir -p $NDK_DIR
85        if [ $? != 0 ] ; then
86            echo "ERROR: Could not create target NDK installation path: $NDK_DIR"
87            exit 1
88        fi
89    fi
90
91    log "Using NDK directory: $NDK_DIR"
92
93    # Check toolchain name
94    #
95    if [ -z "$TOOLCHAIN" ] ; then
96        echo "ERROR: Missing toolchain name parameter. See --help for details."
97        exit 1
98    fi
99
100    if [ ! -d "$SRC_DIR/llvm/$TOOLCHAIN" ] ; then
101        echo "ERROR: Source directory does not contain llvm sources for $TOOLCHAIN"
102    fi
103}
104
105set_parameters $PARAMETERS
106
107prepare_target_build
108
109if [ "$PACKAGE_DIR" ]; then
110    mkdir -p "$PACKAGE_DIR"
111    fail_panic "Could not create package directory: $PACKAGE_DIR"
112fi
113
114set_toolchain_ndk $NDK_DIR $TOOLCHAIN
115
116dump "Using C compiler: $CC"
117dump "Using C++ compiler: $CXX"
118
119rm -rf $BUILD_OUT
120mkdir -p $BUILD_OUT
121
122TOOLCHAIN_BUILD_PREFIX=$BUILD_OUT/prefix
123
124# configure the toolchain
125dump "Configure: $TOOLCHAIN toolchain build"
126cd $BUILD_OUT
127export CC CXX CFLAGS CXXFLAGS
128run $SRC_DIR/llvm/$TOOLCHAIN/configure \
129    --prefix=$TOOLCHAIN_BUILD_PREFIX \
130    --host=$ABI_CONFIGURE_HOST \
131    --build=$ABI_CONFIGURE_BUILD \
132    --enable-targets=arm,mips,x86 \
133    $CONFIG_FLAGS
134fail_panic "Couldn't configure llvm toolchain"
135
136
137# build the toolchain
138dump "Building : llvm toolchain [this can take a long time]."
139cd $BUILD_OUT
140export CC CXX CFLAGS CXXFLAGS
141run make -j$NUM_JOBS
142fail_panic "Couldn't compile llvm toolchain"
143
144# install the toolchain to its final location
145dump "Install  : llvm toolchain binaries."
146cd $BUILD_OUT && run make install
147fail_panic "Couldn't install llvm toolchain to $TOOLCHAIN_BUILD_PREFIX"
148
149# clean static or shared libraries
150rm -rf $TOOLCHAIN_BUILD_PREFIX/docs
151rm -rf $TOOLCHAIN_BUILD_PREFIX/include
152rm -rf $TOOLCHAIN_BUILD_PREFIX/lib/*.a
153rm -rf $TOOLCHAIN_BUILD_PREFIX/lib/*.so
154rm -rf $TOOLCHAIN_BUILD_PREFIX/share
155
156UNUSED_LLVM_EXECUTABLES="
157bugpoint c-index-test clang-tblgen lli llvm-ar llvm-as llvm-bcanalyzer
158llvm-config llvm-cov llvm-diff llvm-dwarfdump llvm-extract llvm-ld llvm-mc
159llvm-nm llvm-objdump llvm-prof llvm-ranlib llvm-readobj llvm-rtdyld llvm-size
160llvm-stress llvm-stub llvm-tblgen macho-dump"
161
162for i in $UNUSED_LLVM_EXECUTABLES; do
163    rm -f $TOOLCHAIN_BUILD_PREFIX/bin/$i
164    rm -f $TOOLCHAIN_BUILD_PREFIX/bin/$i.exe
165done
166
167# copy to toolchain path
168run copy_directory "$TOOLCHAIN_BUILD_PREFIX" "$TOOLCHAIN_PATH"
169
170if [ "$PACKAGE_DIR" ]; then
171    ARCHIVE="$TOOLCHAIN-$HOST_TAG.tar.bz2"
172    SUBDIR=$(get_toolchain_install_subdir $TOOLCHAIN $HOST_TAG)
173    dump "Packaging $ARCHIVE"
174    pack_archive "$PACKAGE_DIR/$ARCHIVE" "$NDK_DIR" "$SUBDIR"
175fi
176
177dump "Done."
178if [ -z "$OPTION_BUILD_OUT" ] ; then
179    rm -rf $BUILD_OUT
180fi
181