• 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
80GCC_VERSION=$DEFAULT_GCC_VERSION
81register_var_option "--gcc-version=<ver>" GCC_VERSION "Specify GCC version"
82
83LLVM_VERSION=
84register_var_option "--llvm-version=<ver>" LLVM_VERSION "Specify LLVM version"
85
86register_jobs_option
87
88extract_parameters "$@"
89
90ABIS=$(commas_to_spaces $ABIS)
91
92# Handle NDK_DIR
93if [ -z "$NDK_DIR" ] ; then
94    NDK_DIR=$ANDROID_NDK_ROOT
95    log "Auto-config: --ndk-dir=$NDK_DIR"
96else
97  if [ ! -d "$NDK_DIR" ]; then
98    panic "NDK directory does not exist: $NDK_DIR"
99  fi
100fi
101
102# Handle OUT_DIR
103if [ -z "$OUT_DIR" ] ; then
104  OUT_DIR=$ANDROID_NDK_ROOT
105  log "Auto-config: --out-dir=$OUT_DIR"
106else
107  mkdir -p "$OUT_DIR"
108  fail_panic "Could not create directory: $OUT_DIR"
109fi
110
111# Check that --stl=<name> is used with one of the supported runtime names.
112if [ -z "$CXX_STL" ]; then
113  panic "Please use --stl=<name> to select a C++ runtime to rebuild."
114fi
115FOUND=
116for STL in $CXX_STL_LIST; do
117  if [ "$STL" = "$CXX_STL" ]; then
118    FOUND=true
119    break
120  fi
121done
122if [ -z "$FOUND" ]; then
123  panic "Invalid --stl value ('$CXX_STL'), please use one of: $CXX_STL_LIST."
124fi
125
126if [ -z "$OPTION_BUILD_DIR" ]; then
127    BUILD_DIR=$NDK_TMPDIR/build-$CXX_STL
128else
129    BUILD_DIR=$OPTION_BUILD_DIR
130fi
131mkdir -p "$BUILD_DIR"
132fail_panic "Could not create build directory: $BUILD_DIR"
133
134# Location of the various C++ runtime source trees.
135GABIXX_SRCDIR=$ANDROID_NDK_ROOT/$GABIXX_SUBDIR
136STLPORT_SRCDIR=$ANDROID_NDK_ROOT/$STLPORT_SUBDIR
137LIBCXX_SRCDIR=$ANDROID_NDK_ROOT/$LIBCXX_SUBDIR
138
139LIBCXX_INCLUDES="-I$LIBCXX_SRCDIR/libcxx/include -I$ANDROID_NDK_ROOT/sources/android/support/include -I$GABIXX_SRCDIR/include"
140
141COMMON_CFLAGS="-fPIC -O2 -ffunction-sections -fdata-sections"
142COMMON_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.
146if [ "$CXX_STL" = "libc++" ]; then
147  GABIXX_INCLUDES=$LIBCXX_INCLUDES
148else
149  GABIXX_INCLUDES="-I$GABIXX_SRCDIR/include"
150fi
151GABIXX_CFLAGS="$COMMON_CFLAGS $GABIXX_INCLUDES"
152GABIXX_CXXFLAGS="$COMMON_CXXFLAGS"
153GABIXX_SOURCES=$(cd $ANDROID_NDK_ROOT/$GABIXX_SUBDIR && ls src/*.cc)
154GABIXX_LDFLAGS="-ldl"
155if [ "$CXX_STL" = "libc++" ]; then
156  GABIXX_CXXFLAGS="$GABIXX_CXXFLAGS -DGABIXX_LIBCXX=1"
157fi
158
159# Determine STLport build parameters
160STLPORT_CFLAGS="$COMMON_CFLAGS -DGNU_SOURCE -I$STLPORT_SRCDIR/stlport $GABIXX_INCLUDES"
161STLPORT_CXXFLAGS="$COMMON_CXXFLAGS"
162STLPORT_SOURCES=\
163"src/dll_main.cpp \
164src/fstream.cpp \
165src/strstream.cpp \
166src/sstream.cpp \
167src/ios.cpp \
168src/stdio_streambuf.cpp \
169src/istream.cpp \
170src/ostream.cpp \
171src/iostream.cpp \
172src/codecvt.cpp \
173src/collate.cpp \
174src/ctype.cpp \
175src/monetary.cpp \
176src/num_get.cpp \
177src/num_put.cpp \
178src/num_get_float.cpp \
179src/num_put_float.cpp \
180src/numpunct.cpp \
181src/time_facets.cpp \
182src/messages.cpp \
183src/locale.cpp \
184src/locale_impl.cpp \
185src/locale_catalog.cpp \
186src/facets_byname.cpp \
187src/complex.cpp \
188src/complex_io.cpp \
189src/complex_trig.cpp \
190src/string.cpp \
191src/bitset.cpp \
192src/allocators.cpp \
193src/c_locale.c \
194src/cxa.c"
195
196# Determine Libc++ build parameters
197LIBCXX_CFLAGS="$COMMON_CFLAGS $LIBCXX_INCLUDES -Drestrict=__restrict__"
198LIBCXX_CXXFLAGS="$COMMON_CXXFLAGS -DLIBCXXRT=1 -DGABIXX_LIBCXX=1 -std=c++11"
199LIBCXX_SOURCES=\
200"libcxx/src/algorithm.cpp \
201libcxx/src/bind.cpp \
202libcxx/src/chrono.cpp \
203libcxx/src/condition_variable.cpp \
204libcxx/src/debug.cpp \
205libcxx/src/exception.cpp \
206libcxx/src/future.cpp \
207libcxx/src/hash.cpp \
208libcxx/src/ios.cpp \
209libcxx/src/iostream.cpp \
210libcxx/src/locale.cpp \
211libcxx/src/memory.cpp \
212libcxx/src/mutex.cpp \
213libcxx/src/new.cpp \
214libcxx/src/random.cpp \
215libcxx/src/regex.cpp \
216libcxx/src/stdexcept.cpp \
217libcxx/src/string.cpp \
218libcxx/src/strstream.cpp \
219libcxx/src/system_error.cpp \
220libcxx/src/thread.cpp \
221libcxx/src/typeinfo.cpp \
222libcxx/src/utility.cpp \
223libcxx/src/valarray.cpp \
224libcxx/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#
329if [ -z "$NO_MAKEFILE" ]; then
330    MAKEFILE=$BUILD_DIR/Makefile
331else
332    MAKEFILE=
333fi
334
335# Define a few common variables based on parameters.
336case $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    ;;
370esac
371
372# By default, all static libraries include hidden ELF symbols, except
373# if one uses the --visible-static option.
374if [ -z "$VISIBLE_STATIC" ]; then
375  STATIC_CXXFLAGS="-fvisibility=hidden -fvisibility-inlines-hidden"
376else
377  STATIC_CXXFLAGS=
378fi
379SHARED_CXXFLAGS=
380
381UNKNOWN_ABIS="$(filter_out "$PREBUILT_ABIS" "$ABIS")"
382if [ -n "$UNKNOWN_ABIS" ] && [ -n $(find_ndk_unknown_archs) ]; then
383  ABIS="$(filter_out "$UNKNOWN_ABIS" "$ABIS")"
384  ABIS="$ABIS $(find_ndk_unknown_archs)"
385fi
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
392build_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
462for 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"
465done
466
467# If needed, package files into tarballs
468if [ -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
480fi
481
482if [ -z "$OPTION_BUILD_DIR" ]; then
483    log "Cleaning up..."
484    rm -rf $BUILD_DIR
485else
486    log "Don't forget to cleanup: $BUILD_DIR"
487fi
488
489log "Done!"
490