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 the libportable 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 libportable.a for the Android NDK.
29
30This script builds libportable.a for app link with portable header.
31
32This requires a platform.git/development and a temporary NDK installation
33containing platforms and toolchain binaries for all target architectures.
34
35By default, this will try with the current NDK directory, unless
36you use the --ndk-dir=<path> option.
37
38The output will be placed in appropriate sub-directories of
39<ndk>/$LIBPORTABLE_SUBDIR, but you can override this with the --out-dir=<path>
40option.
41"
42
43PACKAGE_DIR=
44register_var_option "--package-dir=<path>" PACKAGE_DIR "Put prebuilt tarballs into <path>."
45
46NDK_DIR=
47register_var_option "--ndk-dir=<path>" NDK_DIR "Specify NDK root path for the build."
48
49BUILD_DIR=
50OPTION_BUILD_DIR=
51register_var_option "--build-dir=<path>" OPTION_BUILD_DIR "Specify temporary build dir."
52
53OUT_DIR=
54register_var_option "--out-dir=<path>" OUT_DIR "Specify output directory directly."
55
56ABIS="$PREBUILT_ABIS"
57register_var_option "--abis=<list>" ABIS "Specify list of target ABIs."
58
59NO_MAKEFILE=
60register_var_option "--no-makefile" NO_MAKEFILE "Do not use makefile to speed-up build"
61
62VISIBLE_LIBPORTABLE_STATIC=
63register_var_option "--visible-libportable-static" VISIBLE_LIBPORTABLE_STATIC "Do not use hidden visibility for libportable.a"
64
65GCC_VERSION=
66register_var_option "--gcc-version=<ver>" GCC_VERSION "Specify GCC version"
67
68LLVM_VERSION=
69register_var_option "--llvm-version=<ver>" LLVM_VERSION "Specify LLVM version"
70
71PLATFORM=
72register_var_option "--platform=<name>"  PLATFORM "Specify platform name"
73
74register_jobs_option
75
76register_try64_option
77
78extract_parameters "$@"
79
80ABIS=$(commas_to_spaces $ABIS)
81
82# Handle NDK_DIR
83if [ -z "$NDK_DIR" ] ; then
84    NDK_DIR=$ANDROID_NDK_ROOT
85    log "Auto-config: --ndk-dir=$NDK_DIR"
86else
87    if [ ! -d "$NDK_DIR" ] ; then
88        echo "ERROR: NDK directory does not exists: $NDK_DIR"
89        exit 1
90    fi
91fi
92
93if [ -z "$OPTION_BUILD_DIR" ]; then
94    BUILD_DIR=$NDK_TMPDIR/build-libportable
95else
96    BUILD_DIR=$OPTION_BUILD_DIR
97fi
98mkdir -p "$BUILD_DIR"
99fail_panic "Could not create build directory: $BUILD_DIR"
100
101# Location of the libportable source tree base
102LIBPORTABLE_SRCDIR_BASE=$ANDROID_NDK_ROOT/../development/ndk/$LIBPORTABLE_SUBDIR
103CPUFEATURE_SRCDIR=$ANDROID_NDK_ROOT/sources/android/cpufeatures
104
105# Compiler flags we want to use
106LIBPORTABLE_CFLAGS="-fPIC -O2 -DANDROID -D__ANDROID__ -ffunction-sections"
107LIBPORTABLE_CFLAGS=$LIBPORTABLE_CFLAGS" -I$LIBPORTABLE_SRCDIR_BASE/common/include -I$CPUFEATURE_SRCDIR -D__HOST__"
108LIBPORTABLE_CXXFLAGS="-fno-exceptions -fno-rtti"
109LIBPORTABLE_LDFLAGS=""
110
111# If the --no-makefile flag is not used, we're going to put all build
112# commands in a temporary Makefile that we will be able to invoke with
113# -j$NUM_JOBS to build stuff in parallel.
114#
115if [ -z "$NO_MAKEFILE" ]; then
116    MAKEFILE=$BUILD_DIR/Makefile
117else
118    MAKEFILE=
119fi
120
121# build_libportable_for_abi
122# $1: ABI
123# $2: build directory
124# $3: (optional) installation directory
125build_libportable_libs_for_abi ()
126{
127    local BINPREFIX
128    local ABI=$1
129    local BUILDDIR="$2"
130    local DSTDIR="$3"
131    local SRC OBJ OBJECTS CFLAGS CXXFLAGS
132
133    mkdir -p "$BUILDDIR"
134
135    # If the output directory is not specified, use default location
136    if [ -z "$DSTDIR" ]; then
137        DSTDIR=$NDK_DIR/$LIBPORTABLE_SUBDIR/libs/$ABI
138    fi
139
140    mkdir -p "$DSTDIR"
141    ARCH=$(convert_abi_to_arch $ABI)
142    if [ -n "$GCC_VERSION" ]; then
143        GCCVER=$GCC_VERSION
144    else
145        GCCVER=$(get_default_gcc_version_for_arch $ARCH)
146    fi
147
148    if [ -z "$PLATFORM" ]; then
149       PLATFORM="android-$FIRST_API64_LEVEL"
150    fi
151    builder_begin_android $ABI "$BUILDDIR" "$GCCVER" "$LLVM_VERSION" "$MAKEFILE" "$PLATFORM"
152    builder_set_srcdir "$LIBPORTABLE_SRCDIR"
153    builder_set_dstdir "$DSTDIR"
154
155    if [ -z "$VISIBLE_LIBLIBPORTABLE_STATIC" ]; then
156        # No -fvisibility-inlines-hidden because it is for C++, and there is
157        # no C++ code in libportable
158        builder_cflags "$LIBPORTABLE_CFLAGS" # ToDo: -fvisibility=hidden
159    else
160        builder_cflags "$LIBPORTABLE_CFLAGS"
161    fi
162    builder_ldflags "$LIBPORTABLE_LDFLAGS"
163    builder_sources $LIBPORTABLE_SOURCES
164
165    builder_set_srcdir "$CPUFEATURE_SRCDIR"
166    builder_sources $CPUFEATURE_SOURCES
167
168    log "Building $DSTDIR/libportable.a"
169    builder_static_library libportable
170
171    builder_end
172  # Extract __wrap functions and create a *.wrap file of "--wrap=symbol".
173  # This file will be passed to g++ doing the link in
174  #
175  #    g++ -Wl,@/path/to/libportable.wrap
176  #
177    nm -a $DSTDIR/libportable.a | grep __wrap_ | awk '{print $3}' | sed '/^$/d' | \
178        sed 's/_wrap_/|/' | awk -F'|' '{print $2}' | sort | uniq | \
179        awk '{printf "--wrap=%s\n",$1}' > "$DSTDIR/libportable.wrap"
180}
181
182for ABI in $ABIS; do
183    # List of sources to compile
184    ARCH=$(convert_abi_to_arch $ABI)
185    LIBPORTABLE_SRCDIR=$LIBPORTABLE_SRCDIR_BASE/arch-$ARCH
186    LIBPORTABLE_SOURCES=$(cd $LIBPORTABLE_SRCDIR && ls *.[cS])
187    CPUFEATURE_SOURCES=$(cd $CPUFEATURE_SRCDIR && ls *.[cS])
188    build_libportable_libs_for_abi $ABI "$BUILD_DIR/$ABI" "$OUT_DIR"
189done
190
191# If needed, package files into tarballs
192if [ -n "$PACKAGE_DIR" ] ; then
193    for ABI in $ABIS; do
194        FILES="$LIBPORTABLE_SUBDIR/libs/$ABI/libportable.*"
195        PACKAGE="$PACKAGE_DIR/libportable-libs-$ABI.tar.bz2"
196        log "Packaging: $PACKAGE"
197        pack_archive "$PACKAGE" "$NDK_DIR" "$FILES"
198        fail_panic "Could not package $ABI libportable binaries!"
199        dump "Packaging: $PACKAGE"
200    done
201fi
202
203if [ -z "$OPTION_BUILD_DIR" ]; then
204    log "Cleaning up..."
205    rm -rf $BUILD_DIR
206else
207    log "Don't forget to cleanup: $BUILD_DIR"
208fi
209
210log "Done!"
211