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 # This shell script is used to rebuild one of the NDK C++ STL 18 # implementations from sources. To use it: 19 # 20 # - Define CXX_STL to one of 'gabi++', 'stlport' or 'libc++' 21 # - Run it. 22 # 23 24 # include common function and variable definitions 25 . `dirname $0`/prebuilt-common.sh 26 . `dirname $0`/builder-funcs.sh 27 28 CXX_STL_LIST="gabi++ stlport libc++" 29 30 PROGRAM_PARAMETERS="" 31 32 PROGRAM_DESCRIPTION=\ 33 "Rebuild one of the following NDK C++ runtimes: $CXX_STL_LIST. 34 35 This script is called when pacakging a new NDK release. It will simply 36 rebuild the static and shared libraries of a given C++ runtime from 37 sources. 38 39 Use the --stl=<name> option to specify which runtime you want to rebuild. 40 41 This requires a temporary NDK installation containing platforms and 42 toolchain binaries for all target architectures. 43 44 By default, this will try with the current NDK directory, unless 45 you use the --ndk-dir=<path> option. 46 47 If you want to use clang to rebuild the binaries, please use 48 --llvm-version=<ver> option. 49 50 The output will be placed in appropriate sub-directories of 51 <ndk>/sources/cxx-stl/$CXX_STL_SUBDIR, but you can override this with 52 the --out-dir=<path> option. 53 " 54 55 CXX_STL= 56 register_var_option "--stl=<name>" CXX_STL "Select C++ runtime to rebuild." 57 58 PACKAGE_DIR= 59 register_var_option "--package-dir=<path>" PACKAGE_DIR "Put prebuilt tarballs into <path>." 60 61 NDK_DIR= 62 register_var_option "--ndk-dir=<path>" NDK_DIR "Specify NDK root path for the build." 63 64 BUILD_DIR= 65 OPTION_BUILD_DIR= 66 register_var_option "--build-dir=<path>" OPTION_BUILD_DIR "Specify temporary build dir." 67 68 OUT_DIR= 69 register_var_option "--out-dir=<path>" OUT_DIR "Specify output directory directly." 70 71 ABIS="$PREBUILT_ABIS" 72 register_var_option "--abis=<list>" ABIS "Specify list of target ABIs." 73 74 NO_MAKEFILE= 75 register_var_option "--no-makefile" NO_MAKEFILE "Do not use makefile to speed-up build" 76 77 VISIBLE_STATIC= 78 register_var_option "--visible-static" VISIBLE_STATIC "Do not use hidden visibility for the static library" 79 80 GCC_VERSION=$DEFAULT_GCC_VERSION 81 register_var_option "--gcc-version=<ver>" GCC_VERSION "Specify GCC version" 82 83 LLVM_VERSION= 84 register_var_option "--llvm-version=<ver>" LLVM_VERSION "Specify LLVM version" 85 86 register_jobs_option 87 88 extract_parameters "$@" 89 90 ABIS=$(commas_to_spaces $ABIS) 91 92 # Handle NDK_DIR 93 if [ -z "$NDK_DIR" ] ; then 94 NDK_DIR=$ANDROID_NDK_ROOT 95 log "Auto-config: --ndk-dir=$NDK_DIR" 96 else 97 if [ ! -d "$NDK_DIR" ]; then 98 panic "NDK directory does not exist: $NDK_DIR" 99 fi 100 fi 101 102 # Handle OUT_DIR 103 if [ -z "$OUT_DIR" ] ; then 104 OUT_DIR=$ANDROID_NDK_ROOT 105 log "Auto-config: --out-dir=$OUT_DIR" 106 else 107 mkdir -p "$OUT_DIR" 108 fail_panic "Could not create directory: $OUT_DIR" 109 fi 110 111 # Check that --stl=<name> is used with one of the supported runtime names. 112 if [ -z "$CXX_STL" ]; then 113 panic "Please use --stl=<name> to select a C++ runtime to rebuild." 114 fi 115 FOUND= 116 for STL in $CXX_STL_LIST; do 117 if [ "$STL" = "$CXX_STL" ]; then 118 FOUND=true 119 break 120 fi 121 done 122 if [ -z "$FOUND" ]; then 123 panic "Invalid --stl value ('$CXX_STL'), please use one of: $CXX_STL_LIST." 124 fi 125 126 if [ -z "$OPTION_BUILD_DIR" ]; then 127 BUILD_DIR=$NDK_TMPDIR/build-$CXX_STL 128 else 129 BUILD_DIR=$OPTION_BUILD_DIR 130 fi 131 mkdir -p "$BUILD_DIR" 132 fail_panic "Could not create build directory: $BUILD_DIR" 133 134 # Location of the various C++ runtime source trees. 135 GABIXX_SRCDIR=$ANDROID_NDK_ROOT/$GABIXX_SUBDIR 136 STLPORT_SRCDIR=$ANDROID_NDK_ROOT/$STLPORT_SUBDIR 137 LIBCXX_SRCDIR=$ANDROID_NDK_ROOT/$LIBCXX_SUBDIR 138 139 LIBCXX_INCLUDES="-I$LIBCXX_SRCDIR/libcxx/include -I$ANDROID_NDK_ROOT/sources/android/support/include -I$GABIXX_SRCDIR/include" 140 141 COMMON_CFLAGS="-fPIC -O2 -ffunction-sections -fdata-sections" 142 COMMON_CXXFLAGS="-fexceptions -frtti -fuse-cxa-atexit" 143 144 # Determine GAbi++ build parameters. Note that GAbi++ is also built as part 145 # of STLport and Libc++, in slightly different ways. 146 if [ "$CXX_STL" = "libc++" ]; then 147 GABIXX_INCLUDES=$LIBCXX_INCLUDES 148 else 149 GABIXX_INCLUDES="-I$GABIXX_SRCDIR/include" 150 fi 151 GABIXX_CFLAGS="$COMMON_CFLAGS $GABIXX_INCLUDES" 152 GABIXX_CXXFLAGS="$COMMON_CXXFLAGS" 153 GABIXX_SOURCES=$(cd $ANDROID_NDK_ROOT/$GABIXX_SUBDIR && ls src/*.cc) 154 GABIXX_LDFLAGS="-ldl" 155 if [ "$CXX_STL" = "libc++" ]; then 156 GABIXX_CXXFLAGS="$GABIXX_CXXFLAGS -DGABIXX_LIBCXX=1" 157 fi 158 159 # Determine STLport build parameters 160 STLPORT_CFLAGS="$COMMON_CFLAGS -DGNU_SOURCE -I$STLPORT_SRCDIR/stlport $GABIXX_INCLUDES" 161 STLPORT_CXXFLAGS="$COMMON_CXXFLAGS" 162 STLPORT_SOURCES=\ 163 "src/dll_main.cpp \ 164 src/fstream.cpp \ 165 src/strstream.cpp \ 166 src/sstream.cpp \ 167 src/ios.cpp \ 168 src/stdio_streambuf.cpp \ 169 src/istream.cpp \ 170 src/ostream.cpp \ 171 src/iostream.cpp \ 172 src/codecvt.cpp \ 173 src/collate.cpp \ 174 src/ctype.cpp \ 175 src/monetary.cpp \ 176 src/num_get.cpp \ 177 src/num_put.cpp \ 178 src/num_get_float.cpp \ 179 src/num_put_float.cpp \ 180 src/numpunct.cpp \ 181 src/time_facets.cpp \ 182 src/messages.cpp \ 183 src/locale.cpp \ 184 src/locale_impl.cpp \ 185 src/locale_catalog.cpp \ 186 src/facets_byname.cpp \ 187 src/complex.cpp \ 188 src/complex_io.cpp \ 189 src/complex_trig.cpp \ 190 src/string.cpp \ 191 src/bitset.cpp \ 192 src/allocators.cpp \ 193 src/c_locale.c \ 194 src/cxa.c" 195 196 # Determine Libc++ build parameters 197 LIBCXX_CFLAGS="$COMMON_CFLAGS $LIBCXX_INCLUDES -Drestrict=__restrict__" 198 LIBCXX_CXXFLAGS="$COMMON_CXXFLAGS -DLIBCXXRT=1 -DGABIXX_LIBCXX=1 -std=c++11" 199 LIBCXX_SOURCES=\ 200 "libcxx/src/algorithm.cpp \ 201 libcxx/src/bind.cpp \ 202 libcxx/src/chrono.cpp \ 203 libcxx/src/condition_variable.cpp \ 204 libcxx/src/debug.cpp \ 205 libcxx/src/exception.cpp \ 206 libcxx/src/future.cpp \ 207 libcxx/src/hash.cpp \ 208 libcxx/src/ios.cpp \ 209 libcxx/src/iostream.cpp \ 210 libcxx/src/locale.cpp \ 211 libcxx/src/memory.cpp \ 212 libcxx/src/mutex.cpp \ 213 libcxx/src/new.cpp \ 214 libcxx/src/random.cpp \ 215 libcxx/src/regex.cpp \ 216 libcxx/src/stdexcept.cpp \ 217 libcxx/src/string.cpp \ 218 libcxx/src/strstream.cpp \ 219 libcxx/src/system_error.cpp \ 220 libcxx/src/thread.cpp \ 221 libcxx/src/typeinfo.cpp \ 222 libcxx/src/utility.cpp \ 223 libcxx/src/valarray.cpp \ 224 libcxx/src/support/android/locale_android.cpp \ 225 ../../android/support/src/locale_support.c \ 226 ../../android/support/src/stdlib_support.c \ 227 ../../android/support/src/wchar_support.c \ 228 ../../android/support/src/locale/duplocale.c \ 229 ../../android/support/src/locale/freelocale.c \ 230 ../../android/support/src/locale/localeconv.c \ 231 ../../android/support/src/locale/newlocale.c \ 232 ../../android/support/src/locale/uselocale.c \ 233 ../../android/support/src/stdio/vfwprintf.c \ 234 ../../android/support/src/musl-multibyte/btowc.c \ 235 ../../android/support/src/musl-multibyte/internal.c \ 236 ../../android/support/src/musl-multibyte/mblen.c \ 237 ../../android/support/src/musl-multibyte/mbrlen.c \ 238 ../../android/support/src/musl-multibyte/mbrtowc.c \ 239 ../../android/support/src/musl-multibyte/mbsinit.c \ 240 ../../android/support/src/musl-multibyte/mbsnrtowcs.c \ 241 ../../android/support/src/musl-multibyte/mbsrtowcs.c \ 242 ../../android/support/src/musl-multibyte/mbstowcs.c \ 243 ../../android/support/src/musl-multibyte/mbtowc.c \ 244 ../../android/support/src/musl-multibyte/wcrtomb.c \ 245 ../../android/support/src/musl-multibyte/wcsnrtombs.c \ 246 ../../android/support/src/musl-multibyte/wcsrtombs.c \ 247 ../../android/support/src/musl-multibyte/wcstombs.c \ 248 ../../android/support/src/musl-multibyte/wctob.c \ 249 ../../android/support/src/musl-multibyte/wctomb.c \ 250 ../../android/support/src/musl-ctype/iswalnum.c \ 251 ../../android/support/src/musl-ctype/iswalpha.c \ 252 ../../android/support/src/musl-ctype/iswblank.c \ 253 ../../android/support/src/musl-ctype/iswcntrl.c \ 254 ../../android/support/src/musl-ctype/iswctype.c \ 255 ../../android/support/src/musl-ctype/iswdigit.c \ 256 ../../android/support/src/musl-ctype/iswgraph.c \ 257 ../../android/support/src/musl-ctype/iswlower.c \ 258 ../../android/support/src/musl-ctype/iswprint.c \ 259 ../../android/support/src/musl-ctype/iswpunct.c \ 260 ../../android/support/src/musl-ctype/iswspace.c \ 261 ../../android/support/src/musl-ctype/iswupper.c \ 262 ../../android/support/src/musl-ctype/iswxdigit.c \ 263 ../../android/support/src/musl-ctype/isxdigit.c \ 264 ../../android/support/src/musl-ctype/towctrans.c \ 265 ../../android/support/src/musl-ctype/wcswidth.c \ 266 ../../android/support/src/musl-ctype/wctrans.c \ 267 ../../android/support/src/musl-ctype/wcwidth.c \ 268 ../../android/support/src/musl-locale/catclose.c \ 269 ../../android/support/src/musl-locale/catgets.c \ 270 ../../android/support/src/musl-locale/catopen.c \ 271 ../../android/support/src/musl-locale/iconv.c \ 272 ../../android/support/src/musl-locale/intl.c \ 273 ../../android/support/src/musl-locale/isalnum_l.c \ 274 ../../android/support/src/musl-locale/isalpha_l.c \ 275 ../../android/support/src/musl-locale/isblank_l.c \ 276 ../../android/support/src/musl-locale/iscntrl_l.c \ 277 ../../android/support/src/musl-locale/isdigit_l.c \ 278 ../../android/support/src/musl-locale/isgraph_l.c \ 279 ../../android/support/src/musl-locale/islower_l.c \ 280 ../../android/support/src/musl-locale/isprint_l.c \ 281 ../../android/support/src/musl-locale/ispunct_l.c \ 282 ../../android/support/src/musl-locale/isspace_l.c \ 283 ../../android/support/src/musl-locale/isupper_l.c \ 284 ../../android/support/src/musl-locale/iswalnum_l.c \ 285 ../../android/support/src/musl-locale/iswalpha_l.c \ 286 ../../android/support/src/musl-locale/iswblank_l.c \ 287 ../../android/support/src/musl-locale/iswcntrl_l.c \ 288 ../../android/support/src/musl-locale/iswctype_l.c \ 289 ../../android/support/src/musl-locale/iswdigit_l.c \ 290 ../../android/support/src/musl-locale/iswgraph_l.c \ 291 ../../android/support/src/musl-locale/iswlower_l.c \ 292 ../../android/support/src/musl-locale/iswprint_l.c \ 293 ../../android/support/src/musl-locale/iswpunct_l.c \ 294 ../../android/support/src/musl-locale/iswspace_l.c \ 295 ../../android/support/src/musl-locale/iswupper_l.c \ 296 ../../android/support/src/musl-locale/iswxdigit_l.c \ 297 ../../android/support/src/musl-locale/isxdigit_l.c \ 298 ../../android/support/src/musl-locale/langinfo.c \ 299 ../../android/support/src/musl-locale/nl_langinfo_l.c \ 300 ../../android/support/src/musl-locale/strcasecmp_l.c \ 301 ../../android/support/src/musl-locale/strcoll.c \ 302 ../../android/support/src/musl-locale/strcoll_l.c \ 303 ../../android/support/src/musl-locale/strerror_l.c \ 304 ../../android/support/src/musl-locale/strfmon.c \ 305 ../../android/support/src/musl-locale/strftime_l.c \ 306 ../../android/support/src/musl-locale/strncasecmp_l.c \ 307 ../../android/support/src/musl-locale/strxfrm.c \ 308 ../../android/support/src/musl-locale/strxfrm_l.c \ 309 ../../android/support/src/musl-locale/tolower_l.c \ 310 ../../android/support/src/musl-locale/toupper_l.c \ 311 ../../android/support/src/musl-locale/towctrans_l.c \ 312 ../../android/support/src/musl-locale/towlower_l.c \ 313 ../../android/support/src/musl-locale/towupper_l.c \ 314 ../../android/support/src/musl-locale/wcscoll.c \ 315 ../../android/support/src/musl-locale/wcscoll_l.c \ 316 ../../android/support/src/musl-locale/wcsxfrm.c \ 317 ../../android/support/src/musl-locale/wcsxfrm_l.c \ 318 ../../android/support/src/musl-locale/wctrans_l.c \ 319 ../../android/support/src/musl-locale/wctype_l.c \ 320 ../../android/support/src/musl-stdio/swprintf.c \ 321 ../../android/support/src/musl-stdio/vwprintf.c \ 322 ../../android/support/src/musl-stdio/wprintf.c \ 323 " 324 325 # If the --no-makefile flag is not used, we're going to put all build 326 # commands in a temporary Makefile that we will be able to invoke with 327 # -j$NUM_JOBS to build stuff in parallel. 328 # 329 if [ -z "$NO_MAKEFILE" ]; then 330 MAKEFILE=$BUILD_DIR/Makefile 331 else 332 MAKEFILE= 333 fi 334 335 # Define a few common variables based on parameters. 336 case $CXX_STL in 337 gabi++) 338 CXX_STL_LIB=libgabi++ 339 CXX_STL_SUBDIR=$GABIXX_SUBDIR 340 CXX_STL_SRCDIR=$GABIXX_SRCDIR 341 CXX_STL_CFLAGS=$GABIXX_CFLAGS 342 CXX_STL_CXXFLAGS=$GABIXX_CXXFLAGS 343 CXX_STL_LDFLAGS=$GABIXX_LDFLAGS 344 CXX_STL_SOURCES=$GABIXX_SOURCES 345 CXX_STL_PACKAGE=gabixx 346 ;; 347 stlport) 348 CXX_STL_LIB=libstlport 349 CXX_STL_SUBDIR=$STLPORT_SUBDIR 350 CXX_STL_SRCDIR=$STLPORT_SRCDIR 351 CXX_STL_CFLAGS=$STLPORT_CFLAGS 352 CXX_STL_CXXFLAGS=$STLPORT_CXXFLAGS 353 CXX_STL_LDFLAGS=$STLPORT_LDFLAGS 354 CXX_STL_SOURCES=$STLPORT_SOURCES 355 CXX_STL_PACKAGE=stlport 356 ;; 357 libc++) 358 CXX_STL_LIB=libc++ 359 CXX_STL_SUBDIR=$LIBCXX_SUBDIR 360 CXX_STL_SRCDIR=$LIBCXX_SRCDIR 361 CXX_STL_CFLAGS=$LIBCXX_CFLAGS 362 CXX_STL_CXXFLAGS=$LIBCXX_CXXFLAGS 363 CXX_STL_LDFLAGS=$LIBCXX_LDFLAGS 364 CXX_STL_SOURCES=$LIBCXX_SOURCES 365 CXX_STL_PACKAGE=libcxx 366 ;; 367 *) 368 panic "Internal error: Unknown STL name '$CXX_STL'" 369 ;; 370 esac 371 372 # By default, all static libraries include hidden ELF symbols, except 373 # if one uses the --visible-static option. 374 if [ -z "$VISIBLE_STATIC" ]; then 375 STATIC_CXXFLAGS="-fvisibility=hidden -fvisibility-inlines-hidden" 376 else 377 STATIC_CXXFLAGS= 378 fi 379 SHARED_CXXFLAGS= 380 381 UNKNOWN_ABIS="$(filter_out "$PREBUILT_ABIS" "$ABIS")" 382 if [ -n "$UNKNOWN_ABIS" ] && [ -n $(find_ndk_unknown_archs) ]; then 383 ABIS="$(filter_out "$UNKNOWN_ABIS" "$ABIS")" 384 ABIS="$ABIS $(find_ndk_unknown_archs)" 385 fi 386 387 # build_stl_libs_for_abi 388 # $1: ABI 389 # $2: build directory 390 # $3: build type: "static" or "shared" 391 # $4: (optional) installation directory 392 build_stl_libs_for_abi () 393 { 394 local ARCH BINPREFIX SYSROOT 395 local ABI=$1 396 local BUILDDIR="$2" 397 local TYPE="$3" 398 local DSTDIR="$4" 399 local DEFAULT_CFLAGS DEFAULT_CXXFLAGS 400 local SRC OBJ OBJECTS EXTRA_CXXFLAGS 401 402 mkdir -p "$BUILDDIR" 403 404 DSTDIR=$DSTDIR/$CXX_STL_SUBDIR/libs/$ABI 405 406 if [ "$TYPE" = "static" -a -z "$VISIBLE_STATIC" ]; then 407 EXTRA_CXXFLAGS="$STATIC_CXXFLAGS" 408 else 409 EXTRA_CXXFLAGS="$SHARED_CXXFLAGS" 410 fi 411 412 mkdir -p "$DSTDIR" 413 414 builder_begin_android $ABI "$BUILDDIR" "$GCC_VERSION" "$LLVM_VERSION" "$MAKEFILE" 415 416 builder_set_dstdir "$DSTDIR" 417 418 # Always rebuild GAbi++, except for unknown archs. 419 builder_set_srcdir "$GABIXX_SRCDIR" 420 builder_reset_cflags DEFAULT_CFLAGS 421 builder_cflags "$DEFAULT_CFLAGS $GABIXX_CFLAGS" 422 423 builder_reset_cxxflags DEFAULT_CXXFLAGS 424 builder_cxxflags "$DEFAULT_CXXFLAGS $GABIXX_CXXFLAGS $EXTRA_CXXFLAGS" 425 builder_ldflags "$GABIXX_LDFLAGS" 426 if [ "$(find_ndk_unknown_archs)" != "$ABI" ]; then 427 builder_sources $GABIXX_SOURCES 428 elif [ "$CXX_STL" = "gabi++" ]; then 429 log "Could not build gabi++ with unknown arch!" 430 exit 1 431 else 432 builder_sources src/delete.cc src/new.cc 433 fi 434 435 # Build the runtime sources, except if we're only building GAbi++ 436 if [ "$CXX_STL" != "gabi++" ]; then 437 builder_set_srcdir "$CXX_STL_SRCDIR" 438 builder_reset_cflags 439 builder_cflags "$DEFAULT_CFLAGS $CXX_STL_CFLAGS" 440 builder_reset_cxxflags DEFAULT_CXXFLAGS 441 builder_cxxflags "$DEFAULT_CXXFLAGS $CXX_STL_CXXFLAGS $EXTRA_CXXFLAGS" 442 builder_ldflags "$CXX_STL_LDFLAGS" 443 builder_sources $CXX_STL_SOURCES 444 fi 445 446 if [ "$TYPE" = "static" ]; then 447 log "Building $DSTDIR/${CXX_STL_LIB}_static.a" 448 builder_static_library ${CXX_STL_LIB}_static 449 else 450 log "Building $DSTDIR/${CXX_STL_LIB}_shared.so" 451 if [ "$(find_ndk_unknown_archs)" != "$ABI" ]; then 452 builder_shared_library ${CXX_STL_LIB}_shared 453 else 454 builder_ldflags "-lc -lm" 455 builder_nostdlib_shared_library ${CXX_STL_LIB}_shared # Don't use libgcc 456 fi 457 fi 458 459 builder_end 460 } 461 462 for ABI in $ABIS; do 463 build_stl_libs_for_abi $ABI "$BUILD_DIR/$ABI/shared" "shared" "$OUT_DIR" 464 build_stl_libs_for_abi $ABI "$BUILD_DIR/$ABI/static" "static" "$OUT_DIR" 465 done 466 467 # If needed, package files into tarballs 468 if [ -n "$PACKAGE_DIR" ] ; then 469 for ABI in $ABIS; do 470 FILES="" 471 for LIB in ${CXX_STL_LIB}_static.a ${CXX_STL_LIB}_shared.so; do 472 FILES="$FILES $CXX_STL_SUBDIR/libs/$ABI/$LIB" 473 done 474 PACKAGE="$PACKAGE_DIR/${CXX_STL_PACKAGE}-libs-$ABI.tar.bz2" 475 log "Packaging: $PACKAGE" 476 pack_archive "$PACKAGE" "$OUT_DIR" "$FILES" 477 fail_panic "Could not package $ABI $CXX_STL binaries!" 478 dump "Packaging: $PACKAGE" 479 done 480 fi 481 482 if [ -z "$OPTION_BUILD_DIR" ]; then 483 log "Cleaning up..." 484 rm -rf $BUILD_DIR 485 else 486 log "Don't forget to cleanup: $BUILD_DIR" 487 fi 488 489 log "Done!" 490