Home | History | Annotate | Download | only in tools
      1 # Copyright (C) 2010 The Android Open Source Project
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
      4 # you may not use this file except in compliance with the License.
      5 # You may obtain a copy of the License at
      6 #
      7 #      http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 # Unless required by applicable law or agreed to in writing, software
     10 # distributed under the License is distributed on an "AS IS" BASIS,
     11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 #
     15 
     16 # Create a standalone toolchain package for Android.
     17 
     18 . `dirname $0`/prebuilt-common.sh
     19 
     20 PROGRAM_PARAMETERS=""
     21 PROGRAM_DESCRIPTION=\
     22 "Generate a customized Android toolchain installation that includes
     23 a working sysroot. The result is something that can more easily be
     24 used as a standalone cross-compiler, e.g. to run configure and
     25 make scripts."
     26 
     27 # $1: Source directory
     28 # $2: Destination directory
     29 copy_directory () {
     30     if [ ! -d "$1" ] ; then
     31         dump "Error: Trying to copy from non-existing directory: $1"
     32         exit 1
     33     fi
     34     mkdir -p "$2"
     35     if [ $? != 0 ] ; then
     36         dump "Error: Could not create target directory: $2"
     37         exit 1
     38     fi
     39     (cd "$1" && tar cf - *) | (tar xpf - -C "$2")
     40     if [ $? != 0 ] ; then
     41         dump "Error: Could not copy files from '$1' to '$2'"
     42         exit 1
     43     fi
     44 }
     45 
     46 force_32bit_binaries
     47 
     48 # For now, this is the only toolchain that works reliably.
     49 TOOLCHAIN_NAME=arm-linux-androideabi-4.4.3
     50 register_var_option "--toolchain=<name>" TOOLCHAIN_NAME "Specify toolchain name"
     51 
     52 NDK_DIR=`dirname $0`
     53 NDK_DIR=`dirname $NDK_DIR`
     54 NDK_DIR=`dirname $NDK_DIR`
     55 register_var_option "--ndk-dir=<path>" NDK_DIR "Take source files from NDK at <path>"
     56 
     57 SYSTEM=$HOST_TAG
     58 register_var_option "--system=<name>" SYSTEM "Specify host system"
     59 
     60 PACKAGE_DIR=/tmp/ndk
     61 register_var_option "--package-dir=<path>" PACKAGE_DIR "Place package file in <path>"
     62 
     63 INSTALL_DIR=
     64 register_var_option "--install-dir=<path>" INSTALL_DIR "Don't create package, install files to <path> instead."
     65 
     66 PLATFORM=android-3
     67 register_var_option "--platform=<name>" PLATFORM "Specify target Android platform/API level."
     68 
     69 extract_parameters "$@"
     70 
     71 # Check NDK_DIR
     72 if [ ! -d "$NDK_DIR/build/core" ] ; then
     73     echo "Invalid source NDK directory: $NDK_DIR"
     74     echo "Please use --ndk-dir=<path> to specify the path of an installed NDK."
     75     exit 1
     76 fi
     77 
     78 # Check PLATFORM
     79 if [ ! -d "$NDK_DIR/platforms/$PLATFORM" ] ; then
     80     echo "Invalid platform name: $PLATFORM"
     81     echo "Please use --platform=<name> with one of:" `(cd "$NDK_DIR/platforms" && ls)`
     82     exit 1
     83 fi
     84 
     85 # Check toolchain name
     86 TOOLCHAIN_PATH="$NDK_DIR/toolchains/$TOOLCHAIN_NAME"
     87 if [ ! -d "$TOOLCHAIN_PATH" ] ; then
     88     echo "Invalid toolchain name: $TOOLCHAIN_NAME"
     89     echo "Please use --toolchain=<name> with the name of a toolchain supported by the source NDK."
     90     echo "Try one of: " `(cd "$NDK_DIR/toolchains" && ls)`
     91     exit 1
     92 fi
     93 
     94 # Extract architecture from platform name
     95 case "$TOOLCHAIN_NAME" in
     96     arm-*)
     97         ARCH=arm
     98         ;;
     99     x86-*)
    100         ARCH=x86
    101         ;;
    102     *)
    103         echo "Unsupported toolchain name: $TOOLCHAIN_NAME"
    104         echo "Name must start with arm- or x86- !"
    105         exit 1
    106         ;;
    107 esac
    108 
    109 # Check that there are any platform files for it!
    110 (cd $NDK_DIR/platforms && ls -d */arch-${ARCH} >/dev/null 2>&1 )
    111 if [ $? != 0 ] ; then
    112     echo "Platform $PLATFORM doesn't have any files for this architecture: $ARCH"
    113     echo "Either use --platform=<name> or --toolchain=<name> to select a different"
    114     echo "platform or arch-dependent toolchain name (respectively)!"
    115     exit 1
    116 fi
    117 
    118 # Compute source sysroot
    119 SRC_SYSROOT="$NDK_DIR/platforms/$PLATFORM/arch-$ARCH"
    120 if [ ! -d "$SRC_SYSROOT" ] ; then
    121     echo "No platform files ($PLATFORM) for this architecture: $ARCH"
    122     exit 1
    123 fi
    124 
    125 # Check that we have any prebuilts here
    126 if [ ! -d "$TOOLCHAIN_PATH/prebuilt" ] ; then
    127     echo "Toolchain is missing prebuilt files: $TOOLCHAIN_NAME"
    128     echo "You must point to a valid NDK release package!"
    129     exit 1
    130 fi
    131 
    132 if [ ! -d "$TOOLCHAIN_PATH/prebuilt/$SYSTEM" ] ; then
    133     echo "Host system '$SYSTEM' is not supported by the source NDK!"
    134     echo "Try --system=<name> with one of: " `(cd $TOOLCHAIN_PATH/prebuilt && ls) | grep -v gdbserver`
    135     exit 1
    136 fi
    137 
    138 TOOLCHAIN_PATH="$TOOLCHAIN_PATH/prebuilt/$SYSTEM"
    139 
    140 # Create temporary directory
    141 TMPDIR=`random_temp_directory`/$TOOLCHAIN_NAME
    142 
    143 dump "Copying prebuilt binaries..."
    144 # Now copy the toolchain prebuilt binaries
    145 run copy_directory "$TOOLCHAIN_PATH" "$TMPDIR"
    146 
    147 dump "Copying sysroot headers and libraries..."
    148 # Copy the sysroot under $TMPDIR/sysroot. The toolchain was built to
    149 # expect the sysroot files to be placed there!
    150 run copy_directory "$SRC_SYSROOT" "$TMPDIR/sysroot"
    151 
    152 # Install or Package
    153 if [ -n "$INSTALL_DIR" ] ; then
    154     dump "Copying files to: $INSTALL_DIR"
    155     run copy_directory "$TMPDIR" "$INSTALL_DIR"
    156 else
    157     PACKAGE_FILE="$PACKAGE_DIR/$TOOLCHAIN_NAME.tar.bz2"
    158     dump "Creating package file: $PACKAGE_FILE"
    159     (cd "`dirname $TMPDIR`" && run tar cjf "$PACKAGE_FILE" "$TOOLCHAIN_NAME")
    160     if [ $? != 0 ] ; then
    161         dump "Error: Could not create tarball from $TMPDIR"
    162         exit 1
    163     fi
    164 fi
    165 dump "Cleaning up..."
    166 run rm -rf $TMPDIR
    167 
    168 dump "Done."
    169