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 21 PROGRAM_PARAMETERS="" 22 23 PROGRAM_DESCRIPTION=\ 24 "Rebuild compiler-rt for the Android NDK. 25 26 This requires a temporary NDK installation containing 27 toolchain binaries for all target architectures. 28 29 By default, this will try with the current NDK directory, unless 30 you use the --ndk-dir=<path> option. 31 32 The output will be placed in appropriate sub-directories of 33 <ndk>/$COMPILER_RT_SUBDIR, but you can override this with the --out-dir=<path> 34 option. 35 " 36 37 PACKAGE_DIR= 38 register_var_option "--package-dir=<path>" PACKAGE_DIR "Put prebuilt tarballs into <path>." 39 40 NDK_DIR= 41 register_var_option "--ndk-dir=<path>" NDK_DIR "Specify NDK root path for the build." 42 43 SRC_DIR= 44 register_var_option "--src-dir=<path>" SRC_DIR "Specify compiler-rt source dir." 45 46 BUILD_DIR= 47 OPTION_BUILD_DIR= 48 register_var_option "--build-dir=<path>" OPTION_BUILD_DIR "Specify temporary build dir." 49 50 OUT_DIR= 51 register_var_option "--out-dir=<path>" OUT_DIR "Specify output directory directly." 52 53 ABIS="$PREBUILT_ABIS" 54 register_var_option "--abis=<list>" ABIS "Specify list of target ABIs." 55 56 NO_MAKEFILE= 57 register_var_option "--no-makefile" NO_MAKEFILE "Do not use makefile to speed-up build" 58 59 GCC_VERSION= 60 register_var_option "--gcc-version=<ver>" GCC_VERSION "Specify GCC version" 61 62 LLVM_VERSION= 63 register_var_option "--llvm-version=<ver>" LLVM_VERSION "Specify LLVM version" 64 65 register_jobs_option 66 67 extract_parameters "$@" 68 69 ABIS=$(commas_to_spaces $ABIS) 70 71 # Handle NDK_DIR 72 if [ -z "$NDK_DIR" ] ; then 73 NDK_DIR=$ANDROID_NDK_ROOT 74 log "Auto-config: --ndk-dir=$NDK_DIR" 75 else 76 if [ ! -d "$NDK_DIR" ] ; then 77 echo "ERROR: NDK directory does not exists: $NDK_DIR" 78 exit 1 79 fi 80 fi 81 82 if [ -z "$OPTION_BUILD_DIR" ]; then 83 BUILD_DIR=$NDK_TMPDIR/build-compiler-rt 84 else 85 BUILD_DIR=$OPTION_BUILD_DIR 86 fi 87 rm -rf "$BUILD_DIR" 88 mkdir -p "$BUILD_DIR" 89 fail_panic "Could not create build directory: $BUILD_DIR" 90 91 if [ -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 95 fi 96 97 # Compiler flags we want to use 98 COMPILER_RT_CFLAGS="-fPIC -O2 -DANDROID -D__ANDROID__ -ffunction-sections" 99 COMPILER_RT_CFLAGS=$COMPILER_RT_CFLAGS" -I$SRC_DIR/include -I$SRC_DIR/lib" 100 COMPILER_RT_LDFLAGS="-nostdlib" 101 102 # List of sources to compile 103 COMPILER_RT_GENERIC_SOURCES=$(cd $SRC_DIR && ls lib/*.c) 104 105 # filter out the sources we don't need 106 UNUSED_SOURCES="lib/apple_versioning.c lib/gcc_personality_v0.c" 107 COMPILER_RT_GENERIC_SOURCES=$(filter_out "$UNUSED_SOURCES" "$COMPILER_RT_GENERIC_SOURCES") 108 109 # ARM specific 110 COMPILER_RT_ARM_SOURCES=" 111 lib/arm/aeabi_dcmp.S \ 112 lib/arm/aeabi_fcmp.S \ 113 lib/arm/aeabi_idivmod.S \ 114 lib/arm/aeabi_ldivmod.S \ 115 lib/arm/aeabi_memcmp.S \ 116 lib/arm/aeabi_memcpy.S \ 117 lib/arm/aeabi_memmove.S \ 118 lib/arm/aeabi_memset.S \ 119 lib/arm/aeabi_uidivmod.S \ 120 lib/arm/aeabi_uldivmod.S \ 121 lib/arm/comparesf2.S 122 lib/arm/divmodsi4.S 123 lib/arm/divsi3.S 124 lib/arm/modsi3.S 125 lib/arm/udivmodsi4.S 126 lib/arm/udivsi3.S 127 lib/arm/umodsi3.S" 128 129 # X86 specific 130 COMPILER_RT_X86_SOURCES=" 131 lib/i386/ashldi3.S \ 132 lib/i386/ashrdi3.S \ 133 lib/i386/divdi3.S \ 134 lib/i386/floatdidf.S \ 135 lib/i386/floatdisf.S \ 136 lib/i386/floatdixf.S \ 137 lib/i386/floatundidf.S \ 138 lib/i386/floatundisf.S \ 139 lib/i386/floatundixf.S \ 140 lib/i386/lshrdi3.S \ 141 lib/i386/moddi3.S \ 142 lib/i386/muldi3.S \ 143 lib/i386/udivdi3.S \ 144 lib/i386/umoddi3.S" 145 146 # Mips specific 147 COMPILER_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 # 153 if [ -z "$NO_MAKEFILE" ]; then 154 MAKEFILE=$BUILD_DIR/Makefile 155 else 156 MAKEFILE= 157 fi 158 159 # prepare_compiler_rt_source_for_abi 160 # $1: ABI 161 prepare_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 192 build_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 251 for 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" 254 done 255 256 # If needed, package files into tarballs 257 if [ -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 269 fi 270 271 if [ -z "$OPTION_BUILD_DIR" ]; then 272 log "Cleaning up..." 273 rm -rf $BUILD_DIR 274 else 275 log "Don't forget to cleanup: $BUILD_DIR" 276 fi 277 278 log "Done!" 279