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 script is used to rebuild the host 'ndk-stack' tool from the
18# sources under sources/host-tools/ndk-stack.
19#
20# Note: The tool is installed under prebuilt/$HOST_TAG/bin/ndk-stack
21#       by default.
22#
23PROGDIR=$(dirname $0)
24. $PROGDIR/prebuilt-common.sh
25
26PROGRAM_PARAMETERS=""
27PROGRAM_DESCRIPTION=\
28"This script is used to rebuild the host ndk-stack binary program."
29
30register_jobs_option
31
32OPTION_BUILD_DIR=
33BUILD_DIR=
34register_var_option "--build-dir=<path>" BUILD_DIR "Specify build directory"
35
36NDK_DIR=$ANDROID_NDK_ROOT
37register_var_option "--ndk-dir=<path>" NDK_DIR "Place binary in NDK installation path"
38
39GNUMAKE=
40register_var_option "--make=<path>" GNUMAKE "Specify GNU Make program"
41
42DEBUG=
43register_var_option "--debug" DEBUG "Build debug version"
44
45WITH_LIBBFD=
46register_var_option "--with-libbfd" WITH_LIBBFD "Link with libbfd.a instead of elff/. Need to set --src-dir= too."
47
48SRC_DIR=
49register_var_option "--src-dir=<path>" SRC_DIR "Specify binutils source dir.  Must be set for --with-libbfd"
50
51PROGNAME=ndk-stack
52register_var_option "--program-name=<name>" PROGNAME "Alternate NDK tool program name"
53
54PACKAGE_DIR=
55register_var_option "--package-dir=<path>" PACKAGE_DIR "Archive binary into specific directory"
56
57register_canadian_option
58register_try64_option
59
60extract_parameters "$@"
61
62if [ "$WITH_LIBBFD" ]; then
63    if [ -z "$SRC_DIR" ]; then
64        echo "ERROR: Missing source directory parameter. See --help for details."
65        exit 1
66    fi
67fi
68
69prepare_abi_configure_build
70prepare_host_build
71
72# Choose a build directory if not specified by --build-dir
73if [ -z "$BUILD_DIR" ]; then
74    BUILD_DIR=$NDK_TMPDIR/build-$PROGNAME
75    log "Auto-config: --build-dir=$BUILD_DIR"
76else
77    OPTION_BUILD_DIR="yes"
78fi
79
80rm -rf $BUILD_DIR
81mkdir -p $BUILD_DIR
82
83prepare_canadian_toolchain $BUILD_DIR
84
85CFLAGS=$HOST_CFLAGS" -O2 -s -ffunction-sections -fdata-sections"
86LDFLAGS=$HOST_LDFLAGS
87EXTRA_CONFIG=
88
89if [ "$HOST_OS" != "darwin" -a "$DARWIN" != "yes" ]; then
90    LDFLAGS=$LDFLAGS" -Wl,-gc-sections"
91else
92    # In darwin libbfd has to be built with some *linux* target or it won't understand ELF
93    EXTRA_CONFIG="-target=arm-linux-androideabi"
94fi
95
96BINUTILS_BUILD_DIR=$BUILD_DIR/binutils
97BINUTILS_SRC_DIR=$SRC_DIR/binutils/binutils-$RECENT_BINUTILS_VERSION
98if [ "$WITH_LIBBFD" ]; then
99    # build binutils first
100    if [ -z "$ABI_CONFIGURE_HOST" ]; then
101        ABI_CONFIGURE_HOST=$ABI_CONFIGURE_BUILD
102    fi
103    run mkdir -p $BINUTILS_BUILD_DIR
104    run export CC CFLAGS LDFLAGS
105    run cd $BINUTILS_BUILD_DIR && \
106    run $BINUTILS_SRC_DIR/configure \
107        --host=$ABI_CONFIGURE_HOST \
108        --build=$ABI_CONFIGURE_BUILD \
109        --disable-nls \
110        --with-bug-report-url=$DEFAULT_ISSUE_TRACKER_URL \
111	$EXTRA_CONFIG
112    fail_panic "Can't configure $BINUTILS_SRC_DIR"
113    run make -j$NUM_JOBS
114    fail_panic "Can't build $BINUTILS_SRC_DIR"
115
116fi
117
118OUT=$NDK_DIR/$(get_host_exec_name $PROGNAME)
119
120# GNU Make
121if [ -z "$GNUMAKE" ]; then
122    GNUMAKE=make
123    log "Auto-config: --make=$GNUMAKE"
124fi
125
126if [ "$PACKAGE_DIR" ]; then
127    mkdir -p "$PACKAGE_DIR"
128    fail_panic "Could not create package directory: $PACKAGE_DIR"
129fi
130
131# Create output directory
132mkdir -p $(dirname $OUT)
133if [ $? != 0 ]; then
134    echo "ERROR: Could not create output directory: $(dirname $OUT)"
135    exit 1
136fi
137
138SRCDIR=$ANDROID_NDK_ROOT/sources/host-tools/$PROGNAME
139
140# Let's roll
141if [ "$WITH_LIBBFD" ]; then
142    CFLAGS="$CFLAGS \
143       -DWITH_LIBBFD=1 \
144       -DHAVE_CONFIG_H \
145       -I$BINUTILS_BUILD_DIR/binutils \
146       -I$BINUTILS_SRC_DIR/binutils \
147       -I$BINUTILS_BUILD_DIR/bfd \
148       -I$BINUTILS_SRC_DIR/bfd \
149       -I$BINUTILS_SRC_DIR/include \
150       "
151    LDFLAGS="$LDFLAGS \
152       $BINUTILS_BUILD_DIR/binutils/bucomm.o \
153       $BINUTILS_BUILD_DIR/binutils/version.o \
154       $BINUTILS_BUILD_DIR/binutils/filemode.o \
155       $BINUTILS_BUILD_DIR/bfd/libbfd.a \
156       $BINUTILS_BUILD_DIR/libiberty/libiberty.a \
157       "
158    if [ "$MINGW" != "yes" ]; then
159        LDFLAGS="$LDFLAGS -ldl -lz"
160    fi
161fi
162
163export CFLAGS LDFLAGS
164run $GNUMAKE -C $SRCDIR -f $SRCDIR/GNUmakefile \
165    -B -j$NUM_JOBS \
166    PROGNAME="$OUT" \
167    WITH_LIBBFD=$WITH_LIBBFD \
168    BUILD_DIR="$BUILD_DIR" \
169    CC="$CC" CXX="$CXX" \
170    STRIP="$STRIP" \
171    DEBUG=$DEBUG
172
173if [ $? != 0 ]; then
174    echo "ERROR: Could not build host program!"
175    exit 1
176fi
177
178if [ "$PACKAGE_DIR" ]; then
179    ARCHIVE=$PROGNAME-$HOST_TAG.tar.bz2
180    SUBDIR=$(get_host_exec_name $PROGNAME $HOST_TAG)
181    dump "Packaging: $ARCHIVE"
182    pack_archive "$PACKAGE_DIR/$ARCHIVE" "$NDK_DIR" "$SUBDIR"
183    fail_panic "Could not create package: $PACKAGE_DIR/$ARCHIVE from $OUT"
184fi
185
186if [ "$OPTION_BUILD_DIR" != "yes" ]; then
187    log "Cleaning up..."
188    rm -rf $BUILD_DIR
189else
190    log "Don't forget to cleanup: $BUILD_DIR"
191fi
192
193log "Done!"
194exit 0
195