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
71
72register_jobs_option
73
74extract_parameters "$@"
75
76ABIS=$(commas_to_spaces $ABIS)
77
78# Handle NDK_DIR
79if [ -z "$NDK_DIR" ] ; then
80    NDK_DIR=$ANDROID_NDK_ROOT
81    log "Auto-config: --ndk-dir=$NDK_DIR"
82else
83    if [ ! -d "$NDK_DIR" ] ; then
84        echo "ERROR: NDK directory does not exists: $NDK_DIR"
85        exit 1
86    fi
87fi
88
89if [ -z "$OPTION_BUILD_DIR" ]; then
90    BUILD_DIR=$NDK_TMPDIR/build-libportable
91else
92    BUILD_DIR=$OPTION_BUILD_DIR
93fi
94mkdir -p "$BUILD_DIR"
95fail_panic "Could not create build directory: $BUILD_DIR"
96
97# Location of the libportable source tree base
98LIBPORTABLE_SRCDIR_BASE=$ANDROID_NDK_ROOT/../development/ndk/$LIBPORTABLE_SUBDIR
99CPUFEATURE_SRCDIR=$ANDROID_NDK_ROOT/sources/android/cpufeatures
100
101# Compiler flags we want to use
102LIBPORTABLE_CFLAGS="-fPIC -O2 -DANDROID -D__ANDROID__ -ffunction-sections"
103LIBPORTABLE_CFLAGS=$LIBPORTABLE_CFLAGS" -I$LIBPORTABLE_SRCDIR_BASE/common/include -I$CPUFEATURE_SRCDIR -D__HOST__"
104LIBPORTABLE_CXXFLAGS="-fno-exceptions -fno-rtti"
105LIBPORTABLE_LDFLAGS=""
106
107# If the --no-makefile flag is not used, we're going to put all build
108# commands in a temporary Makefile that we will be able to invoke with
109# -j$NUM_JOBS to build stuff in parallel.
110#
111if [ -z "$NO_MAKEFILE" ]; then
112    MAKEFILE=$BUILD_DIR/Makefile
113else
114    MAKEFILE=
115fi
116
117# build_libportable_for_abi
118# $1: ABI
119# $2: build directory
120# $3: (optional) installation directory
121build_libportable_libs_for_abi ()
122{
123    local BINPREFIX
124    local ABI=$1
125    local BUILDDIR="$2"
126    local DSTDIR="$3"
127    local SRC OBJ OBJECTS CFLAGS CXXFLAGS
128
129    mkdir -p "$BUILDDIR"
130
131    # If the output directory is not specified, use default location
132    if [ -z "$DSTDIR" ]; then
133        DSTDIR=$NDK_DIR/$LIBPORTABLE_SUBDIR/libs/$ABI
134    fi
135
136    mkdir -p "$DSTDIR"
137    ARCH=$(convert_abi_to_arch $ABI)
138    if [ -n "$GCC_VERSION" ]; then
139        GCCVER=$GCC_VERSION
140    else
141        GCCVER=$(get_default_gcc_version_for_arch $ARCH)
142    fi
143
144    builder_begin_android $ABI "$BUILDDIR" "$GCCVER" "$LLVM_VERSION" "$MAKEFILE"
145    builder_set_srcdir "$LIBPORTABLE_SRCDIR"
146    builder_set_dstdir "$DSTDIR"
147
148    if [ -z "$VISIBLE_LIBLIBPORTABLE_STATIC" ]; then
149        # No -fvisibility-inlines-hidden because it is for C++, and there is
150        # no C++ code in libportable
151        builder_cflags "$LIBPORTABLE_CFLAGS" # ToDo: -fvisibility=hidden
152    else
153        builder_cflags "$LIBPORTABLE_CFLAGS"
154    fi
155    builder_ldflags "$LIBPORTABLE_LDFLAGS"
156    builder_sources $LIBPORTABLE_SOURCES
157
158    builder_set_srcdir "$CPUFEATURE_SRCDIR"
159    builder_sources $CPUFEATURE_SOURCES
160
161    log "Building $DSTDIR/libportable.a"
162    builder_static_library libportable
163
164    builder_end
165  # Extract __wrap functions and create a *.wrap file of "--wrap=symbol".
166  # This file will be passed to g++ doing the link in
167  #
168  #    g++ -Wl,@/path/to/libportable.wrap
169  #
170    nm -a $DSTDIR/libportable.a | grep __wrap_ | awk '{print $3}' | sed '/^$/d' | \
171        sed 's/_wrap_/|/' | awk -F'|' '{print $2}' | sort | uniq | \
172        awk '{printf "--wrap=%s\n",$1}' > "$DSTDIR/libportable.wrap"
173}
174
175for ABI in $ABIS; do
176    # List of sources to compile
177    ARCH=$(convert_abi_to_arch $ABI)
178    LIBPORTABLE_SRCDIR=$LIBPORTABLE_SRCDIR_BASE/arch-$ARCH
179    LIBPORTABLE_SOURCES=$(cd $LIBPORTABLE_SRCDIR && ls *.[cS])
180    CPUFEATURE_SOURCES=$(cd $CPUFEATURE_SRCDIR && ls *.[cS])
181    build_libportable_libs_for_abi $ABI "$BUILD_DIR/$ABI" "$OUT_DIR"
182done
183
184# If needed, package files into tarballs
185if [ -n "$PACKAGE_DIR" ] ; then
186    for ABI in $ABIS; do
187        FILES="$LIBPORTABLE_SUBDIR/libs/$ABI/libportable.*"
188        PACKAGE="$PACKAGE_DIR/libportable-libs-$ABI.tar.bz2"
189        log "Packaging: $PACKAGE"
190        pack_archive "$PACKAGE" "$NDK_DIR" "$FILES"
191        fail_panic "Could not package $ABI libportable binaries!"
192        dump "Packaging: $PACKAGE"
193    done
194fi
195
196if [ -z "$OPTION_BUILD_DIR" ]; then
197    log "Cleaning up..."
198    rm -rf $BUILD_DIR
199else
200    log "Don't forget to cleanup: $BUILD_DIR"
201fi
202
203log "Done!"
204