• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
28CXX_STL_LIST="gabi++ stlport libc++"
29
30PROGRAM_PARAMETERS=""
31
32PROGRAM_DESCRIPTION=\
33"Rebuild one of the following NDK C++ runtimes: $CXX_STL_LIST.
34
35This script is called when pacakging a new NDK release. It will simply
36rebuild the static and shared libraries of a given C++ runtime from
37sources.
38
39Use the --stl=<name> option to specify which runtime you want to rebuild.
40
41This requires a temporary NDK installation containing platforms and
42toolchain binaries for all target architectures.
43
44By default, this will try with the current NDK directory, unless
45you use the --ndk-dir=<path> option.
46
47If you want to use clang to rebuild the binaries, please use
48--llvm-version=<ver> option.
49
50The output will be placed in appropriate sub-directories of
51<ndk>/sources/cxx-stl/$CXX_STL_SUBDIR, but you can override this with
52the --out-dir=<path> option.
53"
54
55CXX_STL=
56register_var_option "--stl=<name>" CXX_STL "Select C++ runtime to rebuild."
57
58PACKAGE_DIR=
59register_var_option "--package-dir=<path>" PACKAGE_DIR "Put prebuilt tarballs into <path>."
60
61NDK_DIR=
62register_var_option "--ndk-dir=<path>" NDK_DIR "Specify NDK root path for the build."
63
64BUILD_DIR=
65OPTION_BUILD_DIR=
66register_var_option "--build-dir=<path>" OPTION_BUILD_DIR "Specify temporary build dir."
67
68OUT_DIR=
69register_var_option "--out-dir=<path>" OUT_DIR "Specify output directory directly."
70
71ABIS="$PREBUILT_ABIS"
72register_var_option "--abis=<list>" ABIS "Specify list of target ABIs."
73
74NO_MAKEFILE=
75register_var_option "--no-makefile" NO_MAKEFILE "Do not use makefile to speed-up build"
76
77VISIBLE_STATIC=
78register_var_option "--visible-static" VISIBLE_STATIC "Do not use hidden visibility for the static library"
79
80WITH_DEBUG_INFO=
81register_var_option "--with-debug-info" WITH_DEBUG_INFO "Build with -g.  STL is still built with optimization but with debug info"
82
83GCC_VERSION=
84register_var_option "--gcc-version=<ver>" GCC_VERSION "Specify GCC version"
85
86LLVM_VERSION=
87register_var_option "--llvm-version=<ver>" LLVM_VERSION "Specify LLVM version"
88
89register_jobs_option
90
91register_try64_option
92
93extract_parameters "$@"
94
95if [ -n "${LLVM_VERSION}" -a -n "${GCC_VERSION}" ]; then
96    panic "Cannot set both LLVM_VERSION and GCC_VERSION. Make up your mind!"
97fi
98
99ABIS=$(commas_to_spaces $ABIS)
100UNKNOWN_ABIS=
101if [ "$ABIS" = "${ABIS%%64*}" ]; then
102    UNKNOWN_ABIS="$(filter_out "$PREBUILT_ABIS mips32r6" "$ABIS" )"
103    if [ -n "$UNKNOWN_ABIS" ] && [ -n "$(find_ndk_unknown_archs)" ]; then
104        ABIS="$(filter_out "$UNKNOWN_ABIS" "$ABIS")"
105        ABIS="$ABIS $(find_ndk_unknown_archs)"
106    fi
107fi
108
109# Handle NDK_DIR
110if [ -z "$NDK_DIR" ] ; then
111    NDK_DIR=$ANDROID_NDK_ROOT
112    log "Auto-config: --ndk-dir=$NDK_DIR"
113else
114  if [ ! -d "$NDK_DIR" ]; then
115    panic "NDK directory does not exist: $NDK_DIR"
116  fi
117fi
118
119# Handle OUT_DIR
120if [ -z "$OUT_DIR" ] ; then
121  OUT_DIR=$ANDROID_NDK_ROOT
122  log "Auto-config: --out-dir=$OUT_DIR"
123else
124  mkdir -p "$OUT_DIR"
125  fail_panic "Could not create directory: $OUT_DIR"
126fi
127
128# Check that --stl=<name> is used with one of the supported runtime names.
129if [ -z "$CXX_STL" ]; then
130  panic "Please use --stl=<name> to select a C++ runtime to rebuild."
131fi
132
133# Derive runtime, and normalize CXX_STL
134CXX_SUPPORT_LIB=gabi++
135case $CXX_STL in
136  gabi++)
137    ;;
138  stlport)
139    ;;
140  libc++)
141    CXX_SUPPORT_LIB=gabi++  # libc++abi
142    ;;
143  libc++-libc++abi)
144    CXX_SUPPORT_LIB=libc++abi
145    CXX_STL=libc++
146    ;;
147  libc++-gabi++)
148    CXX_SUPPORT_LIB=gabi++
149    CXX_STL=libc++
150    ;;
151  *)
152    panic "Invalid --stl value ('$CXX_STL'), please use one of: $CXX_STL_LIST."
153    ;;
154esac
155
156if [ -z "$OPTION_BUILD_DIR" ]; then
157    BUILD_DIR=$NDK_TMPDIR/build-$CXX_STL
158else
159    BUILD_DIR=$OPTION_BUILD_DIR
160fi
161rm -rf "$BUILD_DIR"
162mkdir -p "$BUILD_DIR"
163fail_panic "Could not create build directory: $BUILD_DIR"
164
165# Location of the various C++ runtime source trees.  Use symlink from
166# $BUILD_DIR instead of $NDK which may contain full path of builder's working dir
167
168rm -f $BUILD_DIR/ndk
169ln -sf $ANDROID_NDK_ROOT $BUILD_DIR/ndk
170
171GABIXX_SRCDIR=$BUILD_DIR/ndk/$GABIXX_SUBDIR
172STLPORT_SRCDIR=$BUILD_DIR/ndk/$STLPORT_SUBDIR
173LIBCXX_SRCDIR=$BUILD_DIR/ndk/$LIBCXX_SUBDIR
174LIBCXXABI_SRCDIR=$BUILD_DIR/ndk/$LIBCXXABI_SUBDIR
175
176if [ "$CXX_SUPPORT_LIB" = "gabi++" ]; then
177    LIBCXX_INCLUDES="-I$LIBCXX_SRCDIR/libcxx/include -I$ANDROID_NDK_ROOT/sources/android/support/include -I$GABIXX_SRCDIR/include"
178elif [ "$CXX_SUPPORT_LIB" = "libc++abi" ]; then
179    LIBCXX_INCLUDES="-I$LIBCXX_SRCDIR/libcxx/include -I$ANDROID_NDK_ROOT/sources/android/support/include -I$LIBCXXABI_SRCDIR/include"
180else
181    panic "Unknown CXX_SUPPORT_LIB: $CXX_SUPPORT_LIB"
182fi
183
184COMMON_C_CXX_FLAGS="-fPIC -O2 -ffunction-sections -fdata-sections"
185COMMON_CXXFLAGS="-fexceptions -frtti -fuse-cxa-atexit"
186
187if [ "$WITH_DEBUG_INFO" ]; then
188    COMMON_C_CXX_FLAGS="$COMMON_C_CXX_FLAGS -g"
189fi
190
191if [ "$CXX_STL" = "libc++" ]; then
192    # Use clang to build libc++ by default.
193    if [ -z "$LLVM_VERSION" -a -z "$GCC_VERSION" ]; then
194        LLVM_VERSION=$DEFAULT_LLVM_VERSION
195    fi
196fi
197
198# Determine GAbi++ build parameters. Note that GAbi++ is also built as part
199# of STLport and Libc++, in slightly different ways.
200if [ "$CXX_SUPPORT_LIB" = "gabi++" ]; then
201    if [ "$CXX_STL" = "libc++" ]; then
202        GABIXX_INCLUDES="$LIBCXX_INCLUDES"
203        GABIXX_CXXFLAGS="$GABIXX_CXXFLAGS -DLIBCXXABI=1"
204    else
205        GABIXX_INCLUDES="-I$GABIXX_SRCDIR/include"
206    fi
207    GABIXX_CFLAGS="$COMMON_C_CXX_FLAGS $GABIXX_INCLUDES"
208    GABIXX_CXXFLAGS="$GABIXX_CXXFLAGS $GABIXX_CFLAGS $COMMON_CXXFLAGS"
209    GABIXX_SOURCES=$(cd $ANDROID_NDK_ROOT/$GABIXX_SUBDIR && ls src/*.cc)
210    GABIXX_LDFLAGS="-ldl"
211fi
212
213# Determine STLport build parameters
214STLPORT_CFLAGS="$COMMON_C_CXX_FLAGS -DGNU_SOURCE -I$STLPORT_SRCDIR/stlport $GABIXX_INCLUDES"
215STLPORT_CXXFLAGS="$STLPORT_CFLAGS $COMMON_CXXFLAGS"
216STLPORT_SOURCES=\
217"src/dll_main.cpp \
218src/fstream.cpp \
219src/strstream.cpp \
220src/sstream.cpp \
221src/ios.cpp \
222src/stdio_streambuf.cpp \
223src/istream.cpp \
224src/ostream.cpp \
225src/iostream.cpp \
226src/codecvt.cpp \
227src/collate.cpp \
228src/ctype.cpp \
229src/monetary.cpp \
230src/num_get.cpp \
231src/num_put.cpp \
232src/num_get_float.cpp \
233src/num_put_float.cpp \
234src/numpunct.cpp \
235src/time_facets.cpp \
236src/messages.cpp \
237src/locale.cpp \
238src/locale_impl.cpp \
239src/locale_catalog.cpp \
240src/facets_byname.cpp \
241src/complex.cpp \
242src/complex_io.cpp \
243src/complex_trig.cpp \
244src/string.cpp \
245src/bitset.cpp \
246src/allocators.cpp \
247src/c_locale.c \
248src/cxa.c"
249
250# Determine Libc++ build parameters
251LIBCXX_LINKER_SCRIPT=export_symbols.txt
252LIBCXX_CFLAGS="$COMMON_C_CXX_FLAGS $LIBCXX_INCLUDES -Drestrict=__restrict__"
253LIBCXX_CXXFLAGS="$LIBCXX_CFLAGS -DLIBCXXABI=1 -std=c++11"
254if [ -f "$_BUILD_SRCDIR/$LIBCXX_LINKER_SCRIPT" ]; then
255    LIBCXX_LDFLAGS="-Wl,--version-script,\$_BUILD_SRCDIR/$LIBCXX_LINKER_SCRIPT"
256fi
257LIBCXX_SOURCES=\
258"libcxx/src/algorithm.cpp \
259libcxx/src/bind.cpp \
260libcxx/src/chrono.cpp \
261libcxx/src/condition_variable.cpp \
262libcxx/src/debug.cpp \
263libcxx/src/exception.cpp \
264libcxx/src/future.cpp \
265libcxx/src/hash.cpp \
266libcxx/src/ios.cpp \
267libcxx/src/iostream.cpp \
268libcxx/src/locale.cpp \
269libcxx/src/memory.cpp \
270libcxx/src/mutex.cpp \
271libcxx/src/new.cpp \
272libcxx/src/optional.cpp \
273libcxx/src/random.cpp \
274libcxx/src/regex.cpp \
275libcxx/src/shared_mutex.cpp \
276libcxx/src/stdexcept.cpp \
277libcxx/src/string.cpp \
278libcxx/src/strstream.cpp \
279libcxx/src/system_error.cpp \
280libcxx/src/thread.cpp \
281libcxx/src/typeinfo.cpp \
282libcxx/src/utility.cpp \
283libcxx/src/valarray.cpp \
284libcxx/src/support/android/locale_android.cpp \
285"
286
287LIBCXXABI_SOURCES=\
288"../llvm-libc++abi/libcxxabi/src/abort_message.cpp \
289../llvm-libc++abi/libcxxabi/src/cxa_aux_runtime.cpp \
290../llvm-libc++abi/libcxxabi/src/cxa_default_handlers.cpp \
291../llvm-libc++abi/libcxxabi/src/cxa_demangle.cpp \
292../llvm-libc++abi/libcxxabi/src/cxa_exception.cpp \
293../llvm-libc++abi/libcxxabi/src/cxa_exception_storage.cpp \
294../llvm-libc++abi/libcxxabi/src/cxa_guard.cpp \
295../llvm-libc++abi/libcxxabi/src/cxa_handlers.cpp \
296../llvm-libc++abi/libcxxabi/src/cxa_new_delete.cpp \
297../llvm-libc++abi/libcxxabi/src/cxa_personality.cpp \
298../llvm-libc++abi/libcxxabi/src/cxa_unexpected.cpp \
299../llvm-libc++abi/libcxxabi/src/cxa_vector.cpp \
300../llvm-libc++abi/libcxxabi/src/cxa_virtual.cpp \
301../llvm-libc++abi/libcxxabi/src/exception.cpp \
302../llvm-libc++abi/libcxxabi/src/private_typeinfo.cpp \
303../llvm-libc++abi/libcxxabi/src/stdexcept.cpp \
304../llvm-libc++abi/libcxxabi/src/typeinfo.cpp \
305../llvm-libc++abi/libcxxabi/src/Unwind/libunwind.cpp \
306../llvm-libc++abi/libcxxabi/src/Unwind/Unwind-EHABI.cpp \
307../llvm-libc++abi/libcxxabi/src/Unwind/UnwindLevel1.c \
308../llvm-libc++abi/libcxxabi/src/Unwind/UnwindLevel1-gcc-ext.c \
309../llvm-libc++abi/libcxxabi/src/Unwind/UnwindRegistersRestore.S \
310../llvm-libc++abi/libcxxabi/src/Unwind/UnwindRegistersSave.S \
311"
312
313# android/support files for libc++
314SUPPORT32_SOURCES=\
315"../../android/support/src/libdl_support.c \
316../../android/support/src/locale_support.c \
317../../android/support/src/math_support.c \
318../../android/support/src/stdlib_support.c \
319../../android/support/src/wchar_support.c \
320../../android/support/src/locale/duplocale.c \
321../../android/support/src/locale/freelocale.c \
322../../android/support/src/locale/localeconv.c \
323../../android/support/src/locale/newlocale.c \
324../../android/support/src/locale/uselocale.c \
325../../android/support/src/stdio/stdio_impl.c \
326../../android/support/src/stdio/strtod.c \
327../../android/support/src/stdio/vfprintf.c \
328../../android/support/src/stdio/vfwprintf.c \
329../../android/support/src/msun/e_log2.c \
330../../android/support/src/msun/e_log2f.c \
331../../android/support/src/msun/s_nan.c \
332../../android/support/src/musl-multibyte/btowc.c \
333../../android/support/src/musl-multibyte/internal.c \
334../../android/support/src/musl-multibyte/mblen.c \
335../../android/support/src/musl-multibyte/mbrlen.c \
336../../android/support/src/musl-multibyte/mbrtowc.c \
337../../android/support/src/musl-multibyte/mbsinit.c \
338../../android/support/src/musl-multibyte/mbsnrtowcs.c \
339../../android/support/src/musl-multibyte/mbsrtowcs.c \
340../../android/support/src/musl-multibyte/mbstowcs.c \
341../../android/support/src/musl-multibyte/mbtowc.c \
342../../android/support/src/musl-multibyte/wcrtomb.c \
343../../android/support/src/musl-multibyte/wcsnrtombs.c \
344../../android/support/src/musl-multibyte/wcsrtombs.c \
345../../android/support/src/musl-multibyte/wcstombs.c \
346../../android/support/src/musl-multibyte/wctob.c \
347../../android/support/src/musl-multibyte/wctomb.c \
348../../android/support/src/musl-ctype/iswalnum.c \
349../../android/support/src/musl-ctype/iswalpha.c \
350../../android/support/src/musl-ctype/iswblank.c \
351../../android/support/src/musl-ctype/iswcntrl.c \
352../../android/support/src/musl-ctype/iswctype.c \
353../../android/support/src/musl-ctype/iswdigit.c \
354../../android/support/src/musl-ctype/iswgraph.c \
355../../android/support/src/musl-ctype/iswlower.c \
356../../android/support/src/musl-ctype/iswprint.c \
357../../android/support/src/musl-ctype/iswpunct.c \
358../../android/support/src/musl-ctype/iswspace.c \
359../../android/support/src/musl-ctype/iswupper.c \
360../../android/support/src/musl-ctype/iswxdigit.c \
361../../android/support/src/musl-ctype/isxdigit.c \
362../../android/support/src/musl-ctype/towctrans.c \
363../../android/support/src/musl-ctype/wcswidth.c \
364../../android/support/src/musl-ctype/wctrans.c \
365../../android/support/src/musl-ctype/wcwidth.c \
366../../android/support/src/musl-locale/catclose.c \
367../../android/support/src/musl-locale/catgets.c \
368../../android/support/src/musl-locale/catopen.c \
369../../android/support/src/musl-locale/iconv.c \
370../../android/support/src/musl-locale/intl.c \
371../../android/support/src/musl-locale/isalnum_l.c \
372../../android/support/src/musl-locale/isalpha_l.c \
373../../android/support/src/musl-locale/isblank_l.c \
374../../android/support/src/musl-locale/iscntrl_l.c \
375../../android/support/src/musl-locale/isdigit_l.c \
376../../android/support/src/musl-locale/isgraph_l.c \
377../../android/support/src/musl-locale/islower_l.c \
378../../android/support/src/musl-locale/isprint_l.c \
379../../android/support/src/musl-locale/ispunct_l.c \
380../../android/support/src/musl-locale/isspace_l.c \
381../../android/support/src/musl-locale/isupper_l.c \
382../../android/support/src/musl-locale/iswalnum_l.c \
383../../android/support/src/musl-locale/iswalpha_l.c \
384../../android/support/src/musl-locale/iswblank_l.c \
385../../android/support/src/musl-locale/iswcntrl_l.c \
386../../android/support/src/musl-locale/iswctype_l.c \
387../../android/support/src/musl-locale/iswdigit_l.c \
388../../android/support/src/musl-locale/iswgraph_l.c \
389../../android/support/src/musl-locale/iswlower_l.c \
390../../android/support/src/musl-locale/iswprint_l.c \
391../../android/support/src/musl-locale/iswpunct_l.c \
392../../android/support/src/musl-locale/iswspace_l.c \
393../../android/support/src/musl-locale/iswupper_l.c \
394../../android/support/src/musl-locale/iswxdigit_l.c \
395../../android/support/src/musl-locale/isxdigit_l.c \
396../../android/support/src/musl-locale/langinfo.c \
397../../android/support/src/musl-locale/strcasecmp_l.c \
398../../android/support/src/musl-locale/strcoll.c \
399../../android/support/src/musl-locale/strerror_l.c \
400../../android/support/src/musl-locale/strfmon.c \
401../../android/support/src/musl-locale/strftime_l.c \
402../../android/support/src/musl-locale/strncasecmp_l.c \
403../../android/support/src/musl-locale/strxfrm.c \
404../../android/support/src/musl-locale/tolower_l.c \
405../../android/support/src/musl-locale/toupper_l.c \
406../../android/support/src/musl-locale/towctrans_l.c \
407../../android/support/src/musl-locale/towlower_l.c \
408../../android/support/src/musl-locale/towupper_l.c \
409../../android/support/src/musl-locale/wcscoll.c \
410../../android/support/src/musl-locale/wcsxfrm.c \
411../../android/support/src/musl-locale/wctrans_l.c \
412../../android/support/src/musl-locale/wctype_l.c \
413../../android/support/src/musl-math/frexpf.c \
414../../android/support/src/musl-math/frexpl.c \
415../../android/support/src/musl-math/frexp.c \
416../../android/support/src/musl-stdio/swprintf.c \
417../../android/support/src/musl-stdio/vwprintf.c \
418../../android/support/src/musl-stdio/wprintf.c \
419../../android/support/src/musl-stdio/printf.c \
420../../android/support/src/musl-stdio/snprintf.c \
421../../android/support/src/musl-stdio/sprintf.c \
422../../android/support/src/musl-stdio/vprintf.c \
423../../android/support/src/musl-stdio/vsprintf.c \
424../../android/support/src/wcstox/intscan.c \
425../../android/support/src/wcstox/floatscan.c \
426../../android/support/src/wcstox/shgetc.c \
427../../android/support/src/wcstox/wcstod.c \
428../../android/support/src/wcstox/wcstol.c \
429"
430# Replaces broken implementations in x86 libm.so
431SUPPORT32_SOURCES_x86=\
432"../../android/support/src/musl-math/scalbln.c \
433../../android/support/src/musl-math/scalblnf.c \
434../../android/support/src/musl-math/scalblnl.c \
435../../android/support/src/musl-math/scalbnl.c \
436"
437
438# android/support files for libc++
439SUPPORT64_SOURCES=\
440"../../android/support/src/musl-locale/catclose.c \
441../../android/support/src/musl-locale/catgets.c \
442../../android/support/src/musl-locale/catopen.c \
443"
444
445# If the --no-makefile flag is not used, we're going to put all build
446# commands in a temporary Makefile that we will be able to invoke with
447# -j$NUM_JOBS to build stuff in parallel.
448#
449if [ -z "$NO_MAKEFILE" ]; then
450    MAKEFILE=$BUILD_DIR/Makefile
451else
452    MAKEFILE=
453fi
454
455# Define a few common variables based on parameters.
456case $CXX_STL in
457  gabi++)
458    CXX_STL_LIB=libgabi++
459    CXX_STL_SUBDIR=$GABIXX_SUBDIR
460    CXX_STL_SRCDIR=$GABIXX_SRCDIR
461    CXX_STL_CFLAGS=$GABIXX_CFLAGS
462    CXX_STL_CXXFLAGS=$GABIXX_CXXFLAGS
463    CXX_STL_LDFLAGS=$GABIXX_LDFLAGS
464    CXX_STL_SOURCES=$GABIXX_SOURCES
465    CXX_STL_PACKAGE=gabixx
466    ;;
467  stlport)
468    CXX_STL_LIB=libstlport
469    CXX_STL_SUBDIR=$STLPORT_SUBDIR
470    CXX_STL_SRCDIR=$STLPORT_SRCDIR
471    CXX_STL_CFLAGS=$STLPORT_CFLAGS
472    CXX_STL_CXXFLAGS=$STLPORT_CXXFLAGS
473    CXX_STL_LDFLAGS=$STLPORT_LDFLAGS
474    CXX_STL_SOURCES=$STLPORT_SOURCES
475    CXX_STL_PACKAGE=stlport
476    ;;
477  libc++)
478    CXX_STL_LIB=libc++
479    CXX_STL_SUBDIR=$LIBCXX_SUBDIR
480    CXX_STL_SRCDIR=$LIBCXX_SRCDIR
481    CXX_STL_CFLAGS=$LIBCXX_CFLAGS
482    CXX_STL_CXXFLAGS=$LIBCXX_CXXFLAGS
483    CXX_STL_LDFLAGS=$LIBCXX_LDFLAGS
484    CXX_STL_SOURCES=$LIBCXX_SOURCES
485    CXX_STL_PACKAGE=libcxx
486    ;;
487  *)
488    panic "Internal error: Unknown STL name '$CXX_STL'"
489    ;;
490esac
491
492HIDDEN_VISIBILITY_FLAGS="-fvisibility=hidden -fvisibility-inlines-hidden"
493
494# By default, all static libraries include hidden ELF symbols, except
495# if one uses the --visible-static option.
496if [ -z "$VISIBLE_STATIC" ]; then
497    STATIC_CONLYFLAGS="$HIDDEN_VISIBILITY_FLAGS"
498    STATIC_CXXFLAGS="$HIDDEN_VISIBILITY_FLAGS"
499else
500    STATIC_CONLYFLAGS=
501    STATIC_CXXFLAGS=
502fi
503SHARED_CONLYFLAGS="$HIDDEN_VISIBILITY_FLAGS"
504SHARED_CXXFLAGS=
505
506
507# build_stl_libs_for_abi
508# $1: ABI
509# $2: build directory
510# $3: build type: "static" or "shared"
511# $4: installation directory
512# $5: (optional) thumb
513build_stl_libs_for_abi ()
514{
515    local ARCH BINPREFIX SYSROOT
516    local ABI=$1
517    local THUMB="$5"
518    local BUILDDIR="$2"/$THUMB
519    local TYPE="$3"
520    local DSTDIR="$4"
521    local FLOAT_ABI=""
522    local DEFAULT_CFLAGS DEFAULT_CXXFLAGS
523    local SRC OBJ OBJECTS EXTRA_CFLAGS EXTRA_CXXFLAGS EXTRA_LDFLAGS LIB_SUFFIX GCCVER
524
525    EXTRA_CFLAGS=""
526    EXTRA_CXXFLAGS=""
527    EXTRA_LDFLAGS=""
528
529    case $ABI in
530        armeabi-v7a-hard)
531            EXTRA_CFLAGS="-mhard-float -D_NDK_MATH_NO_SOFTFP=1"
532            EXTRA_CXXFLAGS="-mhard-float -D_NDK_MATH_NO_SOFTFP=1"
533            EXTRA_LDFLAGS="-Wl,--no-warn-mismatch -lm_hard"
534            FLOAT_ABI="hard"
535            ;;
536        arm64-v8a)
537            EXTRA_CFLAGS="-mfix-cortex-a53-835769"
538            EXTRA_CXXFLAGS="-mfix-cortex-a53-835769"
539            ;;
540        x86|x86_64)
541            # ToDo: remove the following once all x86-based device call JNI function with
542            #       stack aligned to 16-byte
543            EXTRA_CFLAGS="-mstackrealign"
544            EXTRA_CXXFLAGS="-mstackrealign"
545            ;;
546        mips32r6)
547            EXTRA_CFLAGS="-mips32r6"
548            EXTRA_CXXFLAGS="-mips32r6"
549            EXTRA_LDFLAGS="-mips32r6"
550            ;;
551    esac
552
553    if [ -n "$THUMB" ]; then
554        EXTRA_CFLAGS="$EXTRA_CFLAGS -mthumb"
555        EXTRA_CXXFLAGS="$EXTRA_CXXFLAGS -mthumb"
556    fi
557
558    if [ "$TYPE" = "static" ]; then
559        EXTRA_CFLAGS="$EXTRA_CFLAGS $STATIC_CONLYFLAGS"
560        EXTRA_CXXFLAGS="$EXTRA_CXXFLAGS $STATIC_CXXFLAGS"
561    else
562        EXTRA_CFLAGS="$EXTRA_CFLAGS $SHARED_CONLYFLAGS"
563        EXTRA_CXXFLAGS="$EXTRA_CXXFLAGS $SHARED_CXXFLAGS"
564    fi
565
566    DSTDIR=$DSTDIR/$CXX_STL_SUBDIR/libs/$ABI/$THUMB
567    LIB_SUFFIX="$(get_lib_suffix_for_abi $ABI)"
568
569    mkdir -p "$BUILDDIR"
570    mkdir -p "$DSTDIR"
571
572    if [ -n "$GCC_VERSION" ]; then
573        GCCVER=$GCC_VERSION
574        EXTRA_CFLAGS="$EXTRA_CFLAGS -std=c99"
575    else
576        ARCH=$(convert_abi_to_arch $ABI)
577        GCCVER=$(get_default_gcc_version_for_arch $ARCH)
578    fi
579
580    # libc++ built with clang (for ABI armeabi-only) produces
581    # libc++_shared.so and libc++_static.a with undefined __atomic_fetch_add_4
582    # Add -latomic.
583    if [ -n "$LLVM_VERSION" -a "$CXX_STL_LIB" = "libc++" ]; then
584        # clang3.5 use integrated-as as default, which has trouble compiling
585        # llvm-libc++abi/libcxxabi/src/Unwind/UnwindRegistersRestore.S
586        if [ "$LLVM_VERSION" = "3.5" ]; then
587            EXTRA_CFLAGS="${EXTRA_CFLAGS} -no-integrated-as"
588            EXTRA_CXXFLAGS="${EXTRA_CXXFLAGS} -no-integrated-as"
589        fi
590        if [ "$ABI" = "armeabi" ]; then
591            # EHABI tables were added as experimental flags in llvm 3.4. In 3.5, these
592            # are now the defaults and the flags have been removed. Add these flags
593            # explicitly only for llvm 3.4.
594            if [ "$LLVM_VERSION" = "3.4" ]; then
595                EXTRA_CFLAGS="${EXTRA_CFLAGS} -mllvm -arm-enable-ehabi-descriptors \
596                              -mllvm -arm-enable-ehabi"
597                EXTRA_CXXFLAGS="${EXTRA_CXXFLAGS} -mllvm -arm-enable-ehabi-descriptors \
598                                -mllvm -arm-enable-ehabi"
599            fi
600            EXTRA_LDFLAGS="$EXTRA_LDFLAGS -latomic"
601        fi
602    fi
603
604    builder_begin_android $ABI "$BUILDDIR" "$GCCVER" "$LLVM_VERSION" "$MAKEFILE"
605
606    builder_set_dstdir "$DSTDIR"
607    builder_reset_cflags DEFAULT_CFLAGS
608    builder_reset_cxxflags DEFAULT_CXXFLAGS
609
610    if [ "$CXX_SUPPORT_LIB" = "gabi++" ]; then
611        builder_set_srcdir "$GABIXX_SRCDIR"
612        builder_cflags "$DEFAULT_CFLAGS $GABIXX_CFLAGS $EXTRA_CFLAGS"
613        builder_cxxflags "$DEFAULT_CXXFLAGS $GABIXX_CXXFLAGS $EXTRA_CXXFLAGS"
614        builder_ldflags "$GABIXX_LDFLAGS $EXTRA_LDFLAGS"
615        if [ "$(find_ndk_unknown_archs)" != "$ABI" ]; then
616            builder_sources $GABIXX_SOURCES
617        elif [ "$CXX_STL" = "gabi++" ]; then
618            log "Could not build gabi++ with unknown arch!"
619            exit 1
620        else
621            builder_sources src/delete.cc src/new.cc
622        fi
623    fi
624
625    # Build the runtime sources, except if we're only building GAbi++
626    if [ "$CXX_STL" != "gabi++" ]; then
627      builder_set_srcdir "$CXX_STL_SRCDIR"
628      builder_reset_cflags
629      builder_cflags "$DEFAULT_CFLAGS $CXX_STL_CFLAGS $EXTRA_CFLAGS"
630      builder_reset_cxxflags
631      builder_cxxflags "$DEFAULT_CXXFLAGS $CXX_STL_CXXFLAGS $EXTRA_CXXFLAGS"
632      builder_ldflags "$CXX_STL_LDFLAGS $EXTRA_LDFLAGS"
633      builder_sources $CXX_STL_SOURCES
634      if [ "$CXX_SUPPORT_LIB" = "libc++abi" ]; then
635          builder_sources $LIBCXXABI_SOURCES
636          builder_ldflags "-ldl"
637      fi
638      if [ "$CXX_STL" = "libc++" ]; then
639        if [ "$ABI" = "${ABI%%64*}" ]; then
640          if [ "$ABI" = "x86" ]; then
641            builder_sources $SUPPORT32_SOURCES $SUPPORT32_SOURCES_x86
642          else
643            builder_sources $SUPPORT32_SOURCES
644	  fi
645        else
646          builder_sources $SUPPORT64_SOURCES
647        fi
648      fi
649    fi
650
651    if [ "$TYPE" = "static" ]; then
652        log "Building $DSTDIR/${CXX_STL_LIB}_static.a"
653        builder_static_library ${CXX_STL_LIB}_static
654    else
655        log "Building $DSTDIR/${CXX_STL_LIB}_shared${LIB_SUFFIX}"
656        if [ "$(find_ndk_unknown_archs)" != "$ABI" ]; then
657            builder_shared_library ${CXX_STL_LIB}_shared $LIB_SUFFIX "$FLOAT_ABI"
658        else
659            builder_ldflags "-lm -lc"
660            builder_nostdlib_shared_library ${CXX_STL_LIB}_shared $LIB_SUFFIX # Don't use libgcc
661        fi
662    fi
663
664    builder_end
665}
666
667for ABI in $ABIS; do
668    build_stl_libs_for_abi $ABI "$BUILD_DIR/$ABI/static" "static" "$OUT_DIR"
669    build_stl_libs_for_abi $ABI "$BUILD_DIR/$ABI/shared" "shared" "$OUT_DIR"
670    # build thumb version of libraries for 32-bit arm
671    if [ "$ABI" != "${ABI%%arm*}" -a "$ABI" = "${ABI%%64*}" ] ; then
672        build_stl_libs_for_abi $ABI "$BUILD_DIR/$ABI/static" "static" "$OUT_DIR" thumb
673        build_stl_libs_for_abi $ABI "$BUILD_DIR/$ABI/shared" "shared" "$OUT_DIR" thumb
674    fi
675done
676
677# If needed, package files into tarballs
678if [ -n "$PACKAGE_DIR" ] ; then
679    for ABI in $ABIS; do
680        FILES=""
681        LIB_SUFFIX="$(get_lib_suffix_for_abi $ABI)"
682        for LIB in ${CXX_STL_LIB}_static.a ${CXX_STL_LIB}_shared${LIB_SUFFIX}; do
683	    if [ -d "$CXX_STL_SUBDIR/libs/$ABI/thumb" ]; then
684                FILES="$FILES $CXX_STL_SUBDIR/libs/$ABI/thumb/$LIB"
685            fi
686            FILES="$FILES $CXX_STL_SUBDIR/libs/$ABI/$LIB"
687        done
688        PACKAGE="$PACKAGE_DIR/${CXX_STL_PACKAGE}-libs-$ABI"
689        if [ "$WITH_DEBUG_INFO" ]; then
690            PACKAGE="${PACKAGE}-g"
691        fi
692        PACKAGE="${PACKAGE}.tar.bz2"
693        log "Packaging: $PACKAGE"
694        pack_archive "$PACKAGE" "$OUT_DIR" "$FILES"
695        fail_panic "Could not package $ABI $CXX_STL binaries!"
696        dump "Packaging: $PACKAGE"
697    done
698fi
699
700if [ -z "$OPTION_BUILD_DIR" ]; then
701    log "Cleaning up..."
702    rm -rf $BUILD_DIR
703else
704    log "Don't forget to cleanup: $BUILD_DIR"
705fi
706
707log "Done!"
708