Home | History | Annotate | Download | only in ndk-stack
      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 #
     22 PROGDIR=$(dirname $0)
     23 . $NDK_BUILDTOOLS_PATH/prebuilt-common.sh
     24 
     25 PROGRAM_PARAMETERS=""
     26 PROGRAM_DESCRIPTION=\
     27 "This script is used to rebuild the host ndk-stack binary program."
     28 
     29 register_jobs_option
     30 
     31 OPTION_BUILD_DIR=
     32 BUILD_DIR=
     33 register_var_option "--build-dir=<path>" BUILD_DIR "Specify build directory"
     34 
     35 NDK_DIR=$ANDROID_NDK_ROOT
     36 register_var_option "--ndk-dir=<path>" NDK_DIR "Place binary in NDK installation path"
     37 
     38 GNUMAKE=
     39 register_var_option "--make=<path>" GNUMAKE "Specify GNU Make program"
     40 
     41 DEBUG=
     42 register_var_option "--debug" DEBUG "Build debug version"
     43 
     44 SRC_DIR=
     45 register_var_option "--src-dir=<path>" SRC_DIR "Specify binutils source dir.  Must be set for --with-libbfd"
     46 
     47 PACKAGE_DIR=
     48 register_var_option "--package-dir=<path>" PACKAGE_DIR "Archive binary into specific directory"
     49 
     50 register_canadian_option
     51 register_try64_option
     52 
     53 extract_parameters "$@"
     54 
     55 if [ -z "$SRC_DIR" ]; then
     56     echo "ERROR: Missing source directory parameter. See --help for details."
     57     exit 1
     58 fi
     59 
     60 prepare_abi_configure_build
     61 prepare_host_build
     62 
     63 # Choose a build directory if not specified by --build-dir
     64 if [ -z "$BUILD_DIR" ]; then
     65     BUILD_DIR=$NDK_TMPDIR/build-ndk-stack
     66     log "Auto-config: --build-dir=$BUILD_DIR"
     67 else
     68     OPTION_BUILD_DIR="yes"
     69 fi
     70 
     71 rm -rf $BUILD_DIR
     72 mkdir -p $BUILD_DIR
     73 
     74 prepare_canadian_toolchain $BUILD_DIR
     75 
     76 CFLAGS=$HOST_CFLAGS" -O2 -s -ffunction-sections -fdata-sections"
     77 LDFLAGS=$HOST_LDFLAGS
     78 EXTRA_CONFIG=
     79 
     80 if [ "$HOST_OS" != "darwin" -a "$DARWIN" != "yes" ]; then
     81     LDFLAGS=$LDFLAGS" -Wl,-gc-sections"
     82 else
     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"
     86 fi
     87 
     88 BINUTILS_BUILD_DIR=$BUILD_DIR/binutils
     89 BINUTILS_SRC_DIR=$SRC_DIR/binutils/binutils-$DEFAULT_BINUTILS_VERSION
     90 
     91 # build binutils first
     92 if [ -z "$ABI_CONFIGURE_HOST" ]; then
     93     ABI_CONFIGURE_HOST=$ABI_CONFIGURE_BUILD
     94 fi
     95 run mkdir -p $BINUTILS_BUILD_DIR
     96 run export CC CFLAGS LDFLAGS
     97 run cd $BINUTILS_BUILD_DIR && \
     98 run $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
    104 fail_panic "Can't configure $BINUTILS_SRC_DIR"
    105 run make -j$NUM_JOBS
    106 fail_panic "Can't build $BINUTILS_SRC_DIR"
    107 
    108 NAME=$(get_host_exec_name ndk-stack)
    109 INSTALL_ROOT=$(mktemp -d $NDK_TMPDIR/ndk-stack-XXXXXX)
    110 INSTALL_SUBDIR=host-tools/bin
    111 INSTALL_PATH=$INSTALL_ROOT/$INSTALL_SUBDIR
    112 OUT=$INSTALL_PATH/$NAME
    113 mkdir $INSTALL_PATH
    114 
    115 # GNU Make
    116 if [ -z "$GNUMAKE" ]; then
    117     GNUMAKE=make
    118     log "Auto-config: --make=$GNUMAKE"
    119 fi
    120 
    121 if [ "$PACKAGE_DIR" ]; then
    122     mkdir -p "$PACKAGE_DIR"
    123     fail_panic "Could not create package directory: $PACKAGE_DIR"
    124 fi
    125 
    126 # Create output directory
    127 mkdir -p $(dirname $OUT)
    128 if [ $? != 0 ]; then
    129     echo "ERROR: Could not create output directory: $(dirname $OUT)"
    130     exit 1
    131 fi
    132 
    133 SRCDIR=$ANDROID_NDK_ROOT/sources/host-tools/ndk-stack
    134 
    135 # Let's roll
    136 CFLAGS="$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    "
    144 LDFLAGS="$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    "
    151 if [ "$MINGW" != "yes" ]; then
    152     LDFLAGS="$LDFLAGS -ldl -lz"
    153 fi
    154 
    155 export CFLAGS LDFLAGS
    156 run $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 
    164 if [ $? != 0 ]; then
    165     echo "ERROR: Could not build host program!"
    166     exit 1
    167 fi
    168 
    169 if [ "$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"
    174 fi
    175 
    176 if [ "$OPTION_BUILD_DIR" != "yes" ]; then
    177     log "Cleaning up..."
    178     rm -rf $BUILD_DIR
    179 else
    180     log "Don't forget to cleanup: $BUILD_DIR"
    181 fi
    182 
    183 log "Done!"
    184 exit 0
    185