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