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