Home | History | Annotate | Download | only in tools
      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 #
     23 PROGDIR=$(dirname $0)
     24 . $PROGDIR/prebuilt-common.sh
     25 
     26 PROGRAM_PARAMETERS=""
     27 PROGRAM_DESCRIPTION=\
     28 "This script is used to rebuild the host ndk-stack binary program."
     29 
     30 register_jobs_option
     31 
     32 OPTION_BUILD_DIR=
     33 BUILD_DIR=
     34 register_var_option "--build-dir=<path>" BUILD_DIR "Specify build directory"
     35 
     36 NDK_DIR=$ANDROID_NDK_ROOT
     37 register_var_option "--ndk-dir=<path>" NDK_DIR "Place binary in NDK installation path"
     38 
     39 GNUMAKE=
     40 register_var_option "--make=<path>" GNUMAKE "Specify GNU Make program"
     41 
     42 DEBUG=
     43 register_var_option "--debug" DEBUG "Build debug version"
     44 
     45 WITH_LIBBFD=
     46 register_var_option "--with-libbfd" WITH_LIBBFD "Link with libbfd.a instead of elff/. Need to set --src-dir= too."
     47 
     48 SRC_DIR=
     49 register_var_option "--src-dir=<path>" SRC_DIR "Specify binutils source dir.  Must be set for --with-libbfd"
     50 
     51 PROGNAME=ndk-stack
     52 register_var_option "--program-name=<name>" PROGNAME "Alternate NDK tool program name"
     53 
     54 PACKAGE_DIR=
     55 register_var_option "--package-dir=<path>" PACKAGE_DIR "Archive binary into specific directory"
     56 
     57 register_canadian_option
     58 register_try64_option
     59 
     60 extract_parameters "$@"
     61 
     62 if [ "$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
     67 fi
     68 
     69 prepare_abi_configure_build
     70 prepare_host_build
     71 
     72 # Choose a build directory if not specified by --build-dir
     73 if [ -z "$BUILD_DIR" ]; then
     74     BUILD_DIR=$NDK_TMPDIR/build-$PROGNAME
     75     log "Auto-config: --build-dir=$BUILD_DIR"
     76 else
     77     OPTION_BUILD_DIR="yes"
     78 fi
     79 
     80 rm -rf $BUILD_DIR
     81 mkdir -p $BUILD_DIR
     82 
     83 prepare_canadian_toolchain $BUILD_DIR
     84 
     85 CFLAGS=$HOST_CFLAGS" -O2 -s -ffunction-sections -fdata-sections"
     86 LDFLAGS=$HOST_LDFLAGS
     87 EXTRA_CONFIG=
     88 
     89 if [ "$HOST_OS" != "darwin" -a "$DARWIN" != "yes" ]; then
     90     LDFLAGS=$LDFLAGS" -Wl,-gc-sections"
     91 else
     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"
     94 fi
     95 
     96 BINUTILS_BUILD_DIR=$BUILD_DIR/binutils
     97 BINUTILS_SRC_DIR=$SRC_DIR/binutils/binutils-$RECENT_BINUTILS_VERSION
     98 if [ "$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 
    116 fi
    117 
    118 OUT=$NDK_DIR/$(get_host_exec_name $PROGNAME)
    119 
    120 # GNU Make
    121 if [ -z "$GNUMAKE" ]; then
    122     GNUMAKE=make
    123     log "Auto-config: --make=$GNUMAKE"
    124 fi
    125 
    126 if [ "$PACKAGE_DIR" ]; then
    127     mkdir -p "$PACKAGE_DIR"
    128     fail_panic "Could not create package directory: $PACKAGE_DIR"
    129 fi
    130 
    131 # Create output directory
    132 mkdir -p $(dirname $OUT)
    133 if [ $? != 0 ]; then
    134     echo "ERROR: Could not create output directory: $(dirname $OUT)"
    135     exit 1
    136 fi
    137 
    138 SRCDIR=$ANDROID_NDK_ROOT/sources/host-tools/$PROGNAME
    139 
    140 # Let's roll
    141 if [ "$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
    161 fi
    162 
    163 export CFLAGS LDFLAGS
    164 run $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 
    173 if [ $? != 0 ]; then
    174     echo "ERROR: Could not build host program!"
    175     exit 1
    176 fi
    177 
    178 if [ "$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"
    184 fi
    185 
    186 if [ "$OPTION_BUILD_DIR" != "yes" ]; then
    187     log "Cleaning up..."
    188     rm -rf $BUILD_DIR
    189 else
    190     log "Don't forget to cleanup: $BUILD_DIR"
    191 fi
    192 
    193 log "Done!"
    194 exit 0
    195