1#!/bin/sh
2#
3# Copyright (C) 2013 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# include common function and variable definitions
18. `dirname $0`/prebuilt-common.sh
19. `dirname $0`/builder-funcs.sh
20
21PROGRAM_PARAMETERS=""
22
23PROGRAM_DESCRIPTION=\
24"Rebuild compiler-rt for the Android NDK.
25
26This requires a temporary NDK installation containing
27toolchain binaries for all target architectures.
28
29By default, this will try with the current NDK directory, unless
30you use the --ndk-dir=<path> option.
31
32The output will be placed in appropriate sub-directories of
33<ndk>/$COMPILER_RT_SUBDIR, but you can override this with the --out-dir=<path>
34option.
35"
36
37PACKAGE_DIR=
38register_var_option "--package-dir=<path>" PACKAGE_DIR "Put prebuilt tarballs into <path>."
39
40NDK_DIR=
41register_var_option "--ndk-dir=<path>" NDK_DIR "Specify NDK root path for the build."
42
43SRC_DIR=
44register_var_option "--src-dir=<path>" SRC_DIR "Specify compiler-rt source dir."
45
46BUILD_DIR=
47OPTION_BUILD_DIR=
48register_var_option "--build-dir=<path>" OPTION_BUILD_DIR "Specify temporary build dir."
49
50OUT_DIR=
51register_var_option "--out-dir=<path>" OUT_DIR "Specify output directory directly."
52
53ABIS="$PREBUILT_ABIS"
54register_var_option "--abis=<list>" ABIS "Specify list of target ABIs."
55
56NO_MAKEFILE=
57register_var_option "--no-makefile" NO_MAKEFILE "Do not use makefile to speed-up build"
58
59GCC_VERSION=
60register_var_option "--gcc-version=<ver>" GCC_VERSION "Specify GCC version"
61
62LLVM_VERSION=
63register_var_option "--llvm-version=<ver>" LLVM_VERSION "Specify LLVM version"
64
65register_jobs_option
66
67extract_parameters "$@"
68
69ABIS=$(commas_to_spaces $ABIS)
70
71# Handle NDK_DIR
72if [ -z "$NDK_DIR" ] ; then
73    NDK_DIR=$ANDROID_NDK_ROOT
74    log "Auto-config: --ndk-dir=$NDK_DIR"
75else
76    if [ ! -d "$NDK_DIR" ] ; then
77        echo "ERROR: NDK directory does not exists: $NDK_DIR"
78        exit 1
79    fi
80fi
81
82if [ -z "$OPTION_BUILD_DIR" ]; then
83    BUILD_DIR=$NDK_TMPDIR/build-compiler-rt
84else
85    BUILD_DIR=$OPTION_BUILD_DIR
86fi
87rm -rf "$BUILD_DIR"
88mkdir -p "$BUILD_DIR"
89fail_panic "Could not create build directory: $BUILD_DIR"
90
91if [ -z "$SRC_DIR" -o ! -d "$SRC_DIR" ]; then
92    dump "Could not found compiler-rt source directory: $SRC_DIR"
93    dump "Use --src-dir=<dir> to specify source directory."
94    exit 1
95fi
96
97# Compiler flags we want to use
98COMPILER_RT_CFLAGS="-fPIC -O2 -DANDROID -D__ANDROID__ -ffunction-sections"
99COMPILER_RT_CFLAGS=$COMPILER_RT_CFLAGS" -I$SRC_DIR/include -I$SRC_DIR/lib"
100COMPILER_RT_LDFLAGS="-nostdlib"
101
102# List of sources to compile
103COMPILER_RT_GENERIC_SOURCES=$(cd $SRC_DIR && ls lib/*.c)
104
105# filter out the sources we don't need
106UNUSED_SOURCES="lib/apple_versioning.c lib/gcc_personality_v0.c"
107COMPILER_RT_GENERIC_SOURCES=$(filter_out "$UNUSED_SOURCES" "$COMPILER_RT_GENERIC_SOURCES")
108
109# ARM specific
110COMPILER_RT_ARM_SOURCES="
111lib/arm/aeabi_dcmp.S \
112lib/arm/aeabi_fcmp.S \
113lib/arm/aeabi_idivmod.S \
114lib/arm/aeabi_ldivmod.S \
115lib/arm/aeabi_memcmp.S \
116lib/arm/aeabi_memcpy.S \
117lib/arm/aeabi_memmove.S \
118lib/arm/aeabi_memset.S \
119lib/arm/aeabi_uidivmod.S \
120lib/arm/aeabi_uldivmod.S \
121lib/arm/comparesf2.S
122lib/arm/divmodsi4.S
123lib/arm/divsi3.S
124lib/arm/modsi3.S
125lib/arm/udivmodsi4.S
126lib/arm/udivsi3.S
127lib/arm/umodsi3.S"
128
129# X86 specific
130COMPILER_RT_X86_SOURCES="
131lib/i386/ashldi3.S \
132lib/i386/ashrdi3.S \
133lib/i386/divdi3.S \
134lib/i386/floatdidf.S \
135lib/i386/floatdisf.S \
136lib/i386/floatdixf.S \
137lib/i386/floatundidf.S \
138lib/i386/floatundisf.S \
139lib/i386/floatundixf.S \
140lib/i386/lshrdi3.S \
141lib/i386/moddi3.S \
142lib/i386/muldi3.S \
143lib/i386/udivdi3.S \
144lib/i386/umoddi3.S"
145
146# Mips specific
147COMPILER_RT_MIPS_SOURCES=
148
149# If the --no-makefile flag is not used, we're going to put all build
150# commands in a temporary Makefile that we will be able to invoke with
151# -j$NUM_JOBS to build stuff in parallel.
152#
153if [ -z "$NO_MAKEFILE" ]; then
154    MAKEFILE=$BUILD_DIR/Makefile
155else
156    MAKEFILE=
157fi
158
159# prepare_compiler_rt_source_for_abi
160# $1: ABI
161prepare_compiler_rt_source_for_abi ()
162{
163    local ABI=$1
164    local ARCH_SOURCES GENERIC_SOURCES FOUND
165
166    if [ $ABI == "armeabi" -o $ABI == "armeabi-v7a" -o $ABI == "armeabi-v7a-hard" ]; then
167        ARCH_SOURCES="$COMPILER_RT_ARM_SOURCES"
168    elif [ $ABI == "x86" ]; then
169        ARCH_SOURCES="$COMPILER_RT_X86_SOURCES"
170    elif [ $ABI == "mips" ]; then
171        ARCH_SOURCES="$COMPILER_RT_MIPS_SOURCES"
172    fi
173
174    for SOURCE in $COMPILER_RT_GENERIC_SOURCES; do
175        FILENAME=`basename $SOURCE`
176        FILENAME=$"${FILENAME/\.c/}"
177        # if we have lib/$ABI/*.S, skip lib/*.c
178        FOUND=$(echo $ARCH_SOURCES | grep $FILENAME)
179        if [ -z "$FOUND" ]; then
180            GENERIC_SOURCES="$GENERIC_SOURCES $SOURCE"
181        fi
182    done
183
184    echo "$ARCH_SOURCES $GENERIC_SOURCES"
185}
186
187# build_compiler_rt_libs_for_abi
188# $1: ABI
189# $2: build directory
190# $3: build type: "static" or "shared"
191# $4: (optional) installation directory
192build_compiler_rt_libs_for_abi ()
193{
194    local ARCH BINPREFIX
195    local ABI=$1
196    local BUILDDIR="$2"
197    local TYPE="$3"
198    local DSTDIR="$4"
199    local GCCVER
200
201    mkdir -p "$BUILDDIR"
202
203    # If the output directory is not specified, use default location
204    if [ -z "$DSTDIR" ]; then
205        DSTDIR=$NDK_DIR/$COMPILER_RT_SUBDIR/libs/$ABI
206    fi
207
208    mkdir -p "$DSTDIR"
209
210    if [ -n "$GCC_VERSION" ]; then
211        GCCVER=$GCC_VERSION
212    else
213        ARCH=$(convert_abi_to_arch $ABI)
214        GCCVER=$(get_default_gcc_version_for_arch $ARCH)
215    fi
216
217    builder_begin_android $ABI "$BUILDDIR" "$GCCVER" "$LLVM_VERSION" "$MAKEFILE"
218    builder_set_srcdir "$SRC_DIR"
219    builder_set_dstdir "$DSTDIR"
220
221    builder_cflags "$COMPILER_RT_CFLAGS"
222
223    if [ $ABI == "armeabi" -o $ABI == "armeabi-v7a" -o $ABI == "armeabi-v7a-hard" ]; then
224        builder_cflags "-D__ARM_EABI__"
225        if [ $ABI == "armeabi-v7a-hard" ]; then
226            builder_cflags "-mhard-float -D_NDK_MATH_NO_SOFTFP=1"
227        fi
228    fi
229
230    builder_ldflags "$COMPILER_RT_LDFLAGS"
231    if [ $ABI == "armeabi-v7a-hard" ]; then
232        builder_cflags "-Wl,--no-warn-mismatch -lm_hard"
233    fi
234
235    builder_sources $(prepare_compiler_rt_source_for_abi $ABI)
236
237    if [ "$TYPE" = "static" ]; then
238        log "Building $DSTDIR/libcompiler_rt_static.a"
239        builder_static_library libcompiler_rt_static
240    else
241        log "Building $DSTDIR/libcompiler_rt_shared.so"
242        builder_ldflags "-lc"
243        if [ $ABI != "armeabi-v7a-hard" ]; then
244            builder_ldflags "-lm"
245        fi
246        builder_nostdlib_shared_library libcompiler_rt_shared
247    fi
248    builder_end
249}
250
251for ABI in $ABIS; do
252    build_compiler_rt_libs_for_abi $ABI "$BUILD_DIR/$ABI/shared" "shared" "$OUT_DIR"
253    build_compiler_rt_libs_for_abi $ABI "$BUILD_DIR/$ABI/static" "static" "$OUT_DIR"
254done
255
256# If needed, package files into tarballs
257if [ -n "$PACKAGE_DIR" ] ; then
258    for ABI in $ABIS; do
259        FILES=""
260        for LIB in libcompiler_rt_static.a libcompiler_rt_shared.so; do
261            FILES="$FILES $COMPILER_RT_SUBDIR/libs/$ABI/$LIB"
262        done
263        PACKAGE="$PACKAGE_DIR/compiler-rt-libs-$ABI.tar.bz2"
264        log "Packaging: $PACKAGE"
265        pack_archive "$PACKAGE" "$NDK_DIR" "$FILES"
266        fail_panic "Could not package $ABI compiler-rt binaries!"
267        dump "Packaging: $PACKAGE"
268    done
269fi
270
271if [ -z "$OPTION_BUILD_DIR" ]; then
272    log "Cleaning up..."
273    rm -rf $BUILD_DIR
274else
275    log "Don't forget to cleanup: $BUILD_DIR"
276fi
277
278log "Done!"
279