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/builtins/*.c)
104
105# filter out the sources we don't need
106UNUSED_SOURCES="lib/builtins/apple_versioning.c lib/builtins/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/builtins/arm/aeabi_dcmp.S \
112lib/builtins/arm/aeabi_fcmp.S \
113lib/builtins/arm/aeabi_idivmod.S \
114lib/builtins/arm/aeabi_ldivmod.S \
115lib/builtins/arm/aeabi_memcmp.S \
116lib/builtins/arm/aeabi_memcpy.S \
117lib/builtins/arm/aeabi_memmove.S \
118lib/builtins/arm/aeabi_memset.S \
119lib/builtins/arm/aeabi_uidivmod.S \
120lib/builtins/arm/aeabi_uldivmod.S \
121lib/builtins/arm/comparesf2.S
122lib/builtins/arm/divmodsi4.S
123lib/builtins/arm/divsi3.S
124lib/builtins/arm/modsi3.S
125lib/builtins/arm/udivmodsi4.S
126lib/builtins/arm/udivsi3.S
127lib/builtins/arm/umodsi3.S
128lib/builtins/arm/idiv0.c"
129
130# X86 specific
131COMPILER_RT_X86_SOURCES="
132lib/builtins/i386/ashldi3.S \
133lib/builtins/i386/ashrdi3.S \
134lib/builtins/i386/divdi3.S \
135lib/builtins/i386/floatdidf.S \
136lib/builtins/i386/floatdisf.S \
137lib/builtins/i386/floatdixf.S \
138lib/builtins/i386/floatundidf.S \
139lib/builtins/i386/floatundisf.S \
140lib/builtins/i386/floatundixf.S \
141lib/builtins/i386/lshrdi3.S \
142lib/builtins/i386/moddi3.S \
143lib/builtins/i386/muldi3.S \
144lib/builtins/i386/udivdi3.S \
145lib/builtins/i386/umoddi3.S"
146
147# Mips specific
148COMPILER_RT_MIPS_SOURCES=
149
150# X86_64 specific
151COMPILER_RT_X86_64_SOURCES="
152lib/builtins/x86_64/floatdidf.c \
153lib/builtins/x86_64/floatdisf.c \
154lib/builtins/x86_64/floatdixf.c \
155lib/builtins/x86_64/floatundidf.S \
156lib/builtins/x86_64/floatundisf.S \
157lib/builtins/x86_64/floatundixf.S
158"
159
160# If the --no-makefile flag is not used, we're going to put all build
161# commands in a temporary Makefile that we will be able to invoke with
162# -j$NUM_JOBS to build stuff in parallel.
163#
164if [ -z "$NO_MAKEFILE" ]; then
165    MAKEFILE=$BUILD_DIR/Makefile
166else
167    MAKEFILE=
168fi
169
170# prepare_compiler_rt_source_for_abi
171# $1: ABI
172prepare_compiler_rt_source_for_abi ()
173{
174    local ABI=$1
175    local ARCH_SOURCES GENERIC_SOURCES FOUND
176
177    if [ $ABI = "armeabi" -o $ABI = "armeabi-v7a" -o $ABI = "armeabi-v7a-hard" ]; then
178        ARCH_SOURCES="$COMPILER_RT_ARM_SOURCES"
179    elif [ $ABI = "x86" ]; then
180        ARCH_SOURCES="$COMPILER_RT_X86_SOURCES"
181    elif [ $ABI = "mips" -o $ABI = "mips32r6" ]; then
182        ARCH_SOURCES="$COMPILER_RT_MIPS_SOURCES"
183    elif [ $ABI = "x86_64" ]; then
184        ARCH_SOURCES="$COMPILER_RT_X86_64_SOURCES"
185    fi
186
187    for SOURCE in $COMPILER_RT_GENERIC_SOURCES; do
188        FILENAME=`basename $SOURCE`
189        FILENAME=$"${FILENAME/\.c/}"
190        # if we have lib/$ABI/*.S, skip lib/*.c
191        FOUND=$(echo $ARCH_SOURCES | grep $FILENAME)
192        if [ -z "$FOUND" ]; then
193            GENERIC_SOURCES="$GENERIC_SOURCES $SOURCE"
194        fi
195    done
196
197    echo "$ARCH_SOURCES $GENERIC_SOURCES"
198}
199
200# build_compiler_rt_libs_for_abi
201# $1: ABI
202# $2: build directory
203# $3: build type: "static" or "shared"
204# $4: (optional) installation directory
205build_compiler_rt_libs_for_abi ()
206{
207    local ARCH BINPREFIX
208    local ABI=$1
209    local BUILDDIR="$2"
210    local TYPE="$3"
211    local DSTDIR="$4"
212    local GCCVER
213
214    mkdir -p "$BUILDDIR"
215
216    # If the output directory is not specified, use default location
217    if [ -z "$DSTDIR" ]; then
218        DSTDIR=$NDK_DIR/$COMPILER_RT_SUBDIR/libs/$ABI
219    fi
220
221    mkdir -p "$DSTDIR"
222
223    if [ -n "$GCC_VERSION" ]; then
224        GCCVER=$GCC_VERSION
225    else
226        ARCH=$(convert_abi_to_arch $ABI)
227        GCCVER=$(get_default_gcc_version_for_arch $ARCH)
228        if [ "$LLVM_VERSION" \> "3.4" ]; then
229            # Turn on integrated-as for clang >= 3.5 otherwise file like
230            # can't be compiled
231            COMPILER_RT_CFLAGS="$COMPILER_RT_CFLAGS -fintegrated-as"
232        fi
233    fi
234
235    builder_begin_android $ABI "$BUILDDIR" "$GCCVER" "$LLVM_VERSION" "$MAKEFILE" "android-$FIRST_API64_LEVEL"
236    builder_set_srcdir "$SRC_DIR"
237    builder_set_dstdir "$DSTDIR"
238
239    builder_cflags "$COMPILER_RT_CFLAGS"
240
241    if [ $ABI = "armeabi" -o $ABI = "armeabi-v7a" -o $ABI = "armeabi-v7a-hard" ]; then
242        builder_cflags "-D__ARM_EABI__"
243        if [ $ABI = "armeabi-v7a-hard" ]; then
244            builder_cflags "-mhard-float -D_NDK_MATH_NO_SOFTFP=1"
245        fi
246    fi
247
248    builder_ldflags "$COMPILER_RT_LDFLAGS"
249    if [ $ABI = "armeabi-v7a-hard" ]; then
250        builder_cflags "-Wl,--no-warn-mismatch -lm_hard"
251    fi
252
253    builder_sources $(prepare_compiler_rt_source_for_abi $ABI)
254
255    if [ "$TYPE" = "static" ]; then
256        log "Building $DSTDIR/libcompiler_rt_static.a"
257        builder_static_library libcompiler_rt_static
258    else
259        log "Building $DSTDIR/libcompiler_rt_shared.so"
260        builder_ldflags "-lc"
261        if [ $ABI != "armeabi-v7a-hard" ]; then
262            builder_ldflags "-lm"
263        fi
264        builder_nostdlib_shared_library libcompiler_rt_shared
265    fi
266    builder_end
267}
268
269for ABI in $ABIS; do
270    build_compiler_rt_libs_for_abi $ABI "$BUILD_DIR/$ABI/shared" "shared" "$OUT_DIR"
271    build_compiler_rt_libs_for_abi $ABI "$BUILD_DIR/$ABI/static" "static" "$OUT_DIR"
272done
273
274# If needed, package files into tarballs
275if [ -n "$PACKAGE_DIR" ] ; then
276    for ABI in $ABIS; do
277        FILES=""
278        for LIB in libcompiler_rt_static.a libcompiler_rt_shared.so; do
279            FILES="$FILES $COMPILER_RT_SUBDIR/libs/$ABI/$LIB"
280        done
281        PACKAGE="$PACKAGE_DIR/compiler-rt-libs-$ABI.tar.bz2"
282        log "Packaging: $PACKAGE"
283        pack_archive "$PACKAGE" "$NDK_DIR" "$FILES"
284        fail_panic "Could not package $ABI compiler-rt binaries!"
285        dump "Packaging: $PACKAGE"
286    done
287fi
288
289if [ -z "$OPTION_BUILD_DIR" ]; then
290    log "Cleaning up..."
291    rm -rf $BUILD_DIR
292else
293    log "Don't forget to cleanup: $BUILD_DIR"
294fi
295
296log "Done!"
297