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 force_32bit_binaries
     28 
     29 # For now, this is the only toolchain that works reliably.
     30 TOOLCHAIN_NAME=
     31 register_var_option "--toolchain=<name>" TOOLCHAIN_NAME "Specify toolchain name"
     32 
     33 LLVM_VERSION=
     34 register_var_option "--llvm-ver=<vers>" LLVM_VERSION "List of LLVM release versions"
     35 
     36 ARCH=
     37 register_option "--arch=<name>" do_arch "Specify target architecture" "arm"
     38 do_arch () { ARCH=$1; }
     39 
     40 NDK_DIR=`dirname $0`
     41 NDK_DIR=`dirname $NDK_DIR`
     42 NDK_DIR=`dirname $NDK_DIR`
     43 register_var_option "--ndk-dir=<path>" NDK_DIR "Take source files from NDK at <path>"
     44 
     45 SYSTEM=$HOST_TAG
     46 register_var_option "--system=<name>" SYSTEM "Specify host system"
     47 
     48 PACKAGE_DIR=/tmp/ndk-$USER
     49 register_var_option "--package-dir=<path>" PACKAGE_DIR "Place package file in <path>"
     50 
     51 INSTALL_DIR=
     52 register_var_option "--install-dir=<path>" INSTALL_DIR "Don't create package, install files to <path> instead."
     53 
     54 PLATFORM=
     55 register_option "--platform=<name>" do_platform "Specify target Android platform/API level." "android-3"
     56 do_platform () { PLATFORM=$1; }
     57 
     58 extract_parameters "$@"
     59 
     60 # Check NDK_DIR
     61 if [ ! -d "$NDK_DIR/build/core" ] ; then
     62     echo "Invalid source NDK directory: $NDK_DIR"
     63     echo "Please use --ndk-dir=<path> to specify the path of an installed NDK."
     64     exit 1
     65 fi
     66 
     67 # Check ARCH
     68 if [ -z "$ARCH" ]; then
     69     case $TOOLCHAIN_NAME in
     70         arm-*)
     71             ARCH=arm
     72             ;;
     73         x86-*)
     74             ARCH=x86
     75             ;;
     76         mips*)
     77             ARCH=mips
     78             ;;
     79         *)
     80             ARCH=arm
     81             ;;
     82     esac
     83     log "Auto-config: --arch=$ARCH"
     84 fi
     85 
     86 # Check toolchain name
     87 if [ -z "$TOOLCHAIN_NAME" ]; then
     88     TOOLCHAIN_NAME=$(get_default_toolchain_name_for_arch $ARCH)
     89     echo "Auto-config: --toolchain=$TOOLCHAIN_NAME"
     90 fi
     91 
     92 # Check PLATFORM
     93 if [ -z "$PLATFORM" ]; then
     94     case $ARCH in
     95         arm) PLATFORM=android-3
     96             ;;
     97         x86)
     98             PLATFORM=android-9
     99             ;;
    100         mips)
    101             # Set it to android-9
    102             PLATFORM=android-9
    103             ;;
    104     esac
    105     log "Auto-config: --platform=$PLATFORM"
    106 fi
    107 
    108 if [ ! -d "$NDK_DIR/platforms/$PLATFORM" ] ; then
    109     echo "Invalid platform name: $PLATFORM"
    110     echo "Please use --platform=<name> with one of:" `(cd "$NDK_DIR/platforms" && ls)`
    111     exit 1
    112 fi
    113 
    114 # Check toolchain name
    115 TOOLCHAIN_PATH="$NDK_DIR/toolchains/$TOOLCHAIN_NAME"
    116 if [ ! -d "$TOOLCHAIN_PATH" ] ; then
    117     echo "Invalid toolchain name: $TOOLCHAIN_NAME"
    118     echo "Please use --toolchain=<name> with the name of a toolchain supported by the source NDK."
    119     echo "Try one of: " `(cd "$NDK_DIR/toolchains" && ls)`
    120     exit 1
    121 fi
    122 
    123 # Extract architecture from platform name
    124 parse_toolchain_name $TOOLCHAIN_NAME
    125 
    126 # Check that there are any platform files for it!
    127 (cd $NDK_DIR/platforms && ls -d */arch-${ARCH} >/dev/null 2>&1 )
    128 if [ $? != 0 ] ; then
    129     echo "Platform $PLATFORM doesn't have any files for this architecture: $ARCH"
    130     echo "Either use --platform=<name> or --toolchain=<name> to select a different"
    131     echo "platform or arch-dependent toolchain name (respectively)!"
    132     exit 1
    133 fi
    134 
    135 # Compute source sysroot
    136 SRC_SYSROOT="$NDK_DIR/platforms/$PLATFORM/arch-$ARCH"
    137 if [ ! -d "$SRC_SYSROOT" ] ; then
    138     echo "No platform files ($PLATFORM) for this architecture: $ARCH"
    139     exit 1
    140 fi
    141 
    142 # Check that we have any prebuilts GCC toolchain here
    143 if [ ! -d "$TOOLCHAIN_PATH/prebuilt" ] ; then
    144     echo "Toolchain is missing prebuilt files: $TOOLCHAIN_NAME"
    145     echo "You must point to a valid NDK release package!"
    146     exit 1
    147 fi
    148 
    149 if [ ! -d "$TOOLCHAIN_PATH/prebuilt/$SYSTEM" ] ; then
    150     echo "Host system '$SYSTEM' is not supported by the source NDK!"
    151     echo "Try --system=<name> with one of: " `(cd $TOOLCHAIN_PATH/prebuilt && ls) | grep -v gdbserver`
    152     exit 1
    153 fi
    154 
    155 TOOLCHAIN_PATH="$TOOLCHAIN_PATH/prebuilt/$SYSTEM"
    156 TOOLCHAIN_GCC=$TOOLCHAIN_PATH/bin/$ABI_CONFIGURE_TARGET-gcc
    157 
    158 if [ ! -f "$TOOLCHAIN_GCC" ] ; then
    159     echo "Toolchain $TOOLCHAIN_GCC is missing!"
    160     exit 1
    161 fi
    162 
    163 if [ -n "$LLVM_VERSION" ]; then
    164     LLVM_TOOLCHAIN_PATH="$NDK_DIR/toolchains/llvm-$LLVM_VERSION"
    165     # Check that we have any prebuilts LLVM toolchain here
    166     if [ ! -d "$LLVM_TOOLCHAIN_PATH/prebuilt" ] ; then
    167         echo "LLVM Toolchain is missing prebuilt files"
    168         echo "You must point to a valid NDK release package!"
    169         exit 1
    170     fi
    171 
    172     if [ ! -d "$LLVM_TOOLCHAIN_PATH/prebuilt/$SYSTEM" ] ; then
    173         echo "Host system '$SYSTEM' is not supported by the source NDK!"
    174         echo "Try --system=<name> with one of: " `(cd $LLVM_TOOLCHAIN_PATH/prebuilt && ls)`
    175         exit 1
    176     fi
    177     LLVM_TOOLCHAIN_PATH="$LLVM_TOOLCHAIN_PATH/prebuilt/$SYSTEM"
    178 fi
    179 
    180 # Get GCC_BASE_VERSION.  Note that GCC_BASE_VERSION may be slightly different from GCC_VERSION.
    181 # eg. In gcc4.6 GCC_BASE_VERSION is "4.6.x-google"
    182 LIBGCC_PATH=`$TOOLCHAIN_GCC -print-libgcc-file-name`
    183 LIBGCC_BASE_PATH=${LIBGCC_PATH%/libgcc.a}  # base path of libgcc.a
    184 GCC_BASE_VERSION=${LIBGCC_BASE_PATH##*/}   # stuff after the last /
    185 
    186 # Create temporary directory
    187 TMPDIR=$NDK_TMPDIR/standalone/$TOOLCHAIN_NAME
    188 
    189 dump "Copying prebuilt binaries..."
    190 # Now copy the GCC toolchain prebuilt binaries
    191 run copy_directory "$TOOLCHAIN_PATH" "$TMPDIR"
    192 
    193 if [ -n "$LLVM_VERSION" ]; then
    194   # Copy the clang/llvm toolchain prebuilt binaries
    195   run copy_directory "$LLVM_TOOLCHAIN_PATH" "$TMPDIR"
    196 fi
    197 
    198 dump "Copying sysroot headers and libraries..."
    199 # Copy the sysroot under $TMPDIR/sysroot. The toolchain was built to
    200 # expect the sysroot files to be placed there!
    201 run copy_directory_nolinks "$SRC_SYSROOT" "$TMPDIR/sysroot"
    202 
    203 dump "Copying libstdc++ headers and libraries..."
    204 
    205 GNUSTL_DIR=$NDK_DIR/$GNUSTL_SUBDIR/$GCC_VERSION
    206 GNUSTL_LIBS=$GNUSTL_DIR/libs
    207 
    208 ABI_STL="$TMPDIR/$ABI_CONFIGURE_TARGET"
    209 ABI_STL_INCLUDE="$TMPDIR/include/c++/$GCC_BASE_VERSION"
    210 
    211 copy_directory "$GNUSTL_DIR/include" "$ABI_STL_INCLUDE"
    212 ABI_STL_INCLUDE_TARGET="$ABI_STL_INCLUDE/$ABI_CONFIGURE_TARGET"
    213 mkdir -p "$ABI_STL_INCLUDE_TARGET"
    214 fail_panic "Can't create directory: $ABI_STL_INCLUDE_TARGET"
    215 case "$ARCH" in
    216     arm)
    217         copy_directory "$GNUSTL_LIBS/armeabi/include/bits" "$ABI_STL_INCLUDE_TARGET/bits"
    218         copy_file_list "$GNUSTL_LIBS/armeabi" "$ABI_STL/lib" "libgnustl_shared.so"
    219         copy_file_list "$GNUSTL_LIBS/armeabi" "$ABI_STL/lib" "libsupc++.a"
    220         cp -p "$GNUSTL_LIBS/armeabi/libgnustl_static.a" "$ABI_STL/lib/libstdc++.a"
    221 
    222         copy_directory "$GNUSTL_LIBS/armeabi/include/bits" "$ABI_STL_INCLUDE_TARGET/thumb/bits"
    223         copy_file_list "$GNUSTL_LIBS/armeabi" "$ABI_STL/lib/thumb" "libgnustl_shared.so"
    224         copy_file_list "$GNUSTL_LIBS/armeabi" "$ABI_STL/lib/thumb" "libsupc++.a"
    225         cp -p "$GNUSTL_LIBS/armeabi/libgnustl_static.a" "$ABI_STL/lib/thumb/libstdc++.a"
    226 
    227         copy_directory "$GNUSTL_LIBS/armeabi-v7a/include/bits" "$ABI_STL_INCLUDE_TARGET/armv7-a/bits"
    228         copy_file_list "$GNUSTL_LIBS/armeabi-v7a" "$ABI_STL/lib/armv7-a" "libgnustl_shared.so"
    229         copy_file_list "$GNUSTL_LIBS/armeabi-v7a" "$ABI_STL/lib/armv7-a" "libsupc++.a"
    230         cp -p "$GNUSTL_LIBS/armeabi-v7a/libgnustl_static.a" "$ABI_STL/lib/armv7-a/libstdc++.a"
    231 
    232         copy_directory "$GNUSTL_LIBS/armeabi-v7a/include/bits" "$ABI_STL_INCLUDE_TARGET/armv7-a/thumb/bits"
    233         copy_file_list "$GNUSTL_LIBS/armeabi-v7a" "$ABI_STL/lib/armv7-a/thumb/" "libgnustl_shared.so"
    234         copy_file_list "$GNUSTL_LIBS/armeabi-v7a" "$ABI_STL/lib/armv7-a/thumb/" "libsupc++.a"
    235         cp -p "$GNUSTL_LIBS/armeabi-v7a/libgnustl_static.a" "$ABI_STL/lib/armv7-a//thumb/libstdc++.a"
    236         ;;
    237     x86)
    238         copy_directory "$GNUSTL_LIBS/x86/include/bits" "$ABI_STL_INCLUDE_TARGET/bits"
    239         copy_file_list "$GNUSTL_LIBS/x86" "$ABI_STL/lib" "libgnustl_shared.so"
    240         copy_file_list "$GNUSTL_LIBS/x86" "$ABI_STL/lib" "libsupc++.a"
    241         cp -p "$GNUSTL_LIBS/x86/libgnustl_static.a" "$ABI_STL/lib/libstdc++.a"
    242         ;;
    243     mips)
    244         copy_directory "$GNUSTL_LIBS/mips/include/bits" "$ABI_STL_INCLUDE_TARGET/bits"
    245         copy_file_list "$GNUSTL_LIBS/mips" "$ABI_STL/lib" "libgnustl_shared.so"
    246         copy_file_list "$GNUSTL_LIBS/mips" "$ABI_STL/lib" "libsupc++.a"
    247         cp -p "$GNUSTL_LIBS/mips/libgnustl_static.a" "$ABI_STL/lib/libstdc++.a"
    248         ;;
    249     *)
    250         dump "ERROR: Unsupported NDK architecture!"
    251 esac
    252 
    253 # Install or Package
    254 if [ -n "$INSTALL_DIR" ] ; then
    255     dump "Copying files to: $INSTALL_DIR"
    256     run copy_directory "$TMPDIR" "$INSTALL_DIR"
    257 else
    258     PACKAGE_FILE="$PACKAGE_DIR/$TOOLCHAIN_NAME.tar.bz2"
    259     dump "Creating package file: $PACKAGE_FILE"
    260     pack_archive "$PACKAGE_FILE" "`dirname $TMPDIR`" "$TOOLCHAIN_NAME"
    261     fail_panic "Could not create tarball from $TMPDIR"
    262 fi
    263 dump "Cleaning up..."
    264 run rm -rf $TMPDIR
    265 
    266 dump "Done."
    267