1#!/bin/sh
2#
3# Copyright (C) 2011 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 the prebuilt GAbi++ binaries from
18#  their sources. It requires a working NDK installation.
19#
20
21# include common function and variable definitions
22. `dirname $0`/prebuilt-common.sh
23. `dirname $0`/builder-funcs.sh
24
25PROGRAM_PARAMETERS=""
26
27PROGRAM_DESCRIPTION=\
28"Rebuild the prebuilt GAbi++ binaries for the Android NDK.
29
30This script is called when packaging a new NDK release. It will simply
31rebuild the GAbi++ static and shared libraries from sources.
32
33This requires a temporary NDK installation containing platforms and
34toolchain binaries for all target architectures.
35
36By default, this will try with the current NDK directory, unless
37you use the --ndk-dir=<path> option.
38
39The output will be placed in appropriate sub-directories of
40<ndk>/$GABIXX_SUBDIR, but you can override this with the --out-dir=<path>
41option.
42"
43
44PACKAGE_DIR=
45register_var_option "--package-dir=<path>" PACKAGE_DIR "Put prebuilt tarballs into <path>."
46
47NDK_DIR=
48register_var_option "--ndk-dir=<path>" NDK_DIR "Specify NDK root path for the build."
49
50BUILD_DIR=
51OPTION_BUILD_DIR=
52register_var_option "--build-dir=<path>" OPTION_BUILD_DIR "Specify temporary build dir."
53
54OUT_DIR=
55register_var_option "--out-dir=<path>" OUT_DIR "Specify output directory directly."
56
57ABIS="$PREBUILT_ABIS"
58register_var_option "--abis=<list>" ABIS "Specify list of target ABIs."
59
60NO_MAKEFILE=
61register_var_option "--no-makefile" NO_MAKEFILE "Do not use makefile to speed-up build"
62
63register_jobs_option
64
65extract_parameters "$@"
66
67ABIS=$(commas_to_spaces $ABIS)
68
69# Handle NDK_DIR
70if [ -z "$NDK_DIR" ] ; then
71    NDK_DIR=$ANDROID_NDK_ROOT
72    log "Auto-config: --ndk-dir=$NDK_DIR"
73else
74    if [ ! -d "$NDK_DIR" ] ; then
75        echo "ERROR: NDK directory does not exists: $NDK_DIR"
76        exit 1
77    fi
78fi
79
80if [ -z "$OPTION_BUILD_DIR" ]; then
81    BUILD_DIR=$NDK_TMPDIR/build-gabi++
82else
83    BUILD_DIR=$OPTION_BUILD_DIR
84fi
85mkdir -p "$BUILD_DIR"
86fail_panic "Could not create build directory: $BUILD_DIR"
87
88# Location of the GAbi++ source tree
89GABIXX_SRCDIR=$ANDROID_NDK_ROOT/$GABIXX_SUBDIR
90
91# Compiler flags we want to use
92GABIXX_CFLAGS="-fPIC -O2 -DANDROID -D__ANDROID__"
93GABIXX_CFLAGS=$GABIXX_CFLAGS" -I$GABIXX_SRCDIR/include"
94GABIXX_CXXFLAGS="-fuse-cxa-atexit -fno-exceptions -frtti"
95GABIXX_LDFLAGS=
96
97# List of sources to compile
98GABIXX_SOURCES=$(cd $GABIXX_SRCDIR && ls src/*.cc)
99
100# If the --no-makefile flag is not used, we're going to put all build
101# commands in a temporary Makefile that we will be able to invoke with
102# -j$NUM_JOBS to build stuff in parallel.
103#
104if [ -z "$NO_MAKEFILE" ]; then
105    MAKEFILE=$BUILD_DIR/Makefile
106else
107    MAKEFILE=
108fi
109
110build_gabixx_libs_for_abi ()
111{
112    local ARCH BINPREFIX
113    local ABI=$1
114    local BUILDDIR="$2"
115    local DSTDIR="$3"
116    local SRC OBJ OBJECTS CFLAGS CXXFLAGS
117
118    mkdir -p "$BUILDDIR"
119
120    # If the output directory is not specified, use default location
121    if [ -z "$DSTDIR" ]; then
122        DSTDIR=$NDK_DIR/$GABIXX_SUBDIR/libs/$ABI
123    fi
124
125    mkdir -p "$DSTDIR"
126
127    builder_begin_android $ABI "$BUILDDIR" "$MAKEFILE"
128    builder_set_srcdir "$GABIXX_SRCDIR"
129    builder_set_dstdir "$DSTDIR"
130
131    builder_cflags "$GABIXX_CFLAGS"
132    builder_cxxflags "$GABIXX_CXXFLAGS"
133    builder_ldflags "$GABIXX_LDFLAGS"
134    builder_sources $GABIXX_SOURCES
135
136    log "Building $DSTDIR/libgabi++_static.a"
137    builder_static_library libgabi++_static
138
139    log "Building $DSTDIR/libgabi++_shared.so"
140    builder_shared_library libgabi++_shared
141    builder_end
142}
143
144for ABI in $ABIS; do
145    build_gabixx_libs_for_abi $ABI "$BUILD_DIR/$ABI"
146done
147
148# If needed, package files into tarballs
149if [ -n "$PACKAGE_DIR" ] ; then
150    for ABI in $ABIS; do
151        FILES=""
152        for LIB in libgabi++_static.a libgabi++_shared.so; do
153            FILES="$FILES $GABIXX_SUBDIR/libs/$ABI/$LIB"
154        done
155        PACKAGE="$PACKAGE_DIR/gabixx-libs-$ABI.tar.bz2"
156        log "Packaging: $PACKAGE"
157        pack_archive "$PACKAGE" "$NDK_DIR" "$FILES"
158        fail_panic "Could not package $ABI GAbi++ binaries!"
159        dump "Packaging: $PACKAGE"
160    done
161fi
162
163if [ -z "$OPTION_BUILD_DIR" ]; then
164    log "Cleaning up..."
165    rm -rf $BUILD_DIR
166else
167    log "Don't forget to cleanup: $BUILD_DIR"
168fi
169
170log "Done!"
171