# Copyright (C) 2010 The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # Create a standalone toolchain package for Android. . `dirname $0`/prebuilt-common.sh PROGRAM_PARAMETERS="" PROGRAM_DESCRIPTION=\ "Generate a customized Android toolchain installation that includes a working sysroot. The result is something that can more easily be used as a standalone cross-compiler, e.g. to run configure and make scripts." # For now, this is the only toolchain that works reliably. TOOLCHAIN_NAME= register_var_option "--toolchain=" TOOLCHAIN_NAME "Specify toolchain name" LLVM_VERSION= register_var_option "--llvm-version=" LLVM_VERSION "Specify LLVM version" ARCH= register_option "--arch=" do_arch "Specify target architecture" "arm" do_arch () { ARCH=$1; } NDK_DIR=`dirname $0` NDK_DIR=`dirname $NDK_DIR` NDK_DIR=`dirname $NDK_DIR` register_var_option "--ndk-dir=" NDK_DIR "Take source files from NDK at " # Create 32-bit host toolchain by default SYSTEM=$HOST_TAG32 register_var_option "--system=" SYSTEM "Specify host system" PACKAGE_DIR=/tmp/ndk-$USER register_var_option "--package-dir=" PACKAGE_DIR "Place package file in " INSTALL_DIR= register_var_option "--install-dir=" INSTALL_DIR "Don't create package, install files to instead." PLATFORM= register_option "--platform=" do_platform "Specify target Android platform/API level." "android-3" do_platform () { PLATFORM=$1; } extract_parameters "$@" # Check NDK_DIR if [ ! -d "$NDK_DIR/build/core" ] ; then echo "Invalid source NDK directory: $NDK_DIR" echo "Please use --ndk-dir= to specify the path of an installed NDK." exit 1 fi # Check ARCH if [ -z "$ARCH" ]; then case $TOOLCHAIN_NAME in arm-*) ARCH=arm ;; x86-*) ARCH=x86 ;; mips*) ARCH=mips ;; *) ARCH=arm ;; esac log "Auto-config: --arch=$ARCH" fi # Check toolchain name if [ -z "$TOOLCHAIN_NAME" ]; then TOOLCHAIN_NAME=$(get_default_toolchain_name_for_arch $ARCH) echo "Auto-config: --toolchain=$TOOLCHAIN_NAME" fi # Detect LLVM version from toolchain name if [ -z "$LLVM_VERSION" ]; then LLVM_VERSION_EXTRACT=$(echo "$TOOLCHAIN_NAME" | grep 'clang[0-9]\.[0-9]$' | sed -e 's/.*-clang//') if [ -n "$LLVM_VERSION_EXTRACT" ]; then TOOLCHAIN_NAME=$(get_default_toolchain_name_for_arch $ARCH) LLVM_VERSION=$LLVM_VERSION_EXTRACT echo "Auto-config: --toolchain=$TOOLCHAIN_NAME, --llvm-version=$LLVM_VERSION" fi fi # Check PLATFORM if [ -z "$PLATFORM" ]; then case $ARCH in arm) PLATFORM=android-3 ;; x86) PLATFORM=android-9 ;; mips) # Set it to android-9 PLATFORM=android-9 ;; esac log "Auto-config: --platform=$PLATFORM" fi if [ ! -d "$NDK_DIR/platforms/$PLATFORM" ] ; then echo "Invalid platform name: $PLATFORM" echo "Please use --platform= with one of:" `(cd "$NDK_DIR/platforms" && ls)` exit 1 fi # Check toolchain name TOOLCHAIN_PATH="$NDK_DIR/toolchains/$TOOLCHAIN_NAME" if [ ! -d "$TOOLCHAIN_PATH" ] ; then echo "Invalid toolchain name: $TOOLCHAIN_NAME" echo "Please use --toolchain= with the name of a toolchain supported by the source NDK." echo "Try one of: " `(cd "$NDK_DIR/toolchains" && ls)` exit 1 fi # Extract architecture from platform name parse_toolchain_name $TOOLCHAIN_NAME # Check that there are any platform files for it! (cd $NDK_DIR/platforms && ls -d */arch-${ARCH} >/dev/null 2>&1 ) if [ $? != 0 ] ; then echo "Platform $PLATFORM doesn't have any files for this architecture: $ARCH" echo "Either use --platform= or --toolchain= to select a different" echo "platform or arch-dependent toolchain name (respectively)!" exit 1 fi # Compute source sysroot SRC_SYSROOT="$NDK_DIR/platforms/$PLATFORM/arch-$ARCH" if [ ! -d "$SRC_SYSROOT" ] ; then echo "No platform files ($PLATFORM) for this architecture: $ARCH" exit 1 fi # Check that we have any prebuilts GCC toolchain here if [ ! -d "$TOOLCHAIN_PATH/prebuilt" ] ; then echo "Toolchain is missing prebuilt files: $TOOLCHAIN_NAME" echo "You must point to a valid NDK release package!" exit 1 fi if [ ! -d "$TOOLCHAIN_PATH/prebuilt/$SYSTEM" ] ; then echo "Host system '$SYSTEM' is not supported by the source NDK!" echo "Try --system= with one of: " `(cd $TOOLCHAIN_PATH/prebuilt && ls) | grep -v gdbserver` exit 1 fi TOOLCHAIN_PATH="$TOOLCHAIN_PATH/prebuilt/$SYSTEM" TOOLCHAIN_GCC=$TOOLCHAIN_PATH/bin/$ABI_CONFIGURE_TARGET-gcc if [ ! -f "$TOOLCHAIN_GCC" ] ; then echo "Toolchain $TOOLCHAIN_GCC is missing!" exit 1 fi if [ -n "$LLVM_VERSION" ]; then LLVM_TOOLCHAIN_PATH="$NDK_DIR/toolchains/llvm-$LLVM_VERSION" # Check that we have any prebuilts LLVM toolchain here if [ ! -d "$LLVM_TOOLCHAIN_PATH/prebuilt" ] ; then echo "LLVM Toolchain is missing prebuilt files" echo "You must point to a valid NDK release package!" exit 1 fi if [ ! -d "$LLVM_TOOLCHAIN_PATH/prebuilt/$SYSTEM" ] ; then echo "Host system '$SYSTEM' is not supported by the source NDK!" echo "Try --system= with one of: " `(cd $LLVM_TOOLCHAIN_PATH/prebuilt && ls)` exit 1 fi LLVM_TOOLCHAIN_PATH="$LLVM_TOOLCHAIN_PATH/prebuilt/$SYSTEM" fi # Get GCC_BASE_VERSION. Note that GCC_BASE_VERSION may be slightly different from GCC_VERSION. # eg. In gcc4.6 GCC_BASE_VERSION is "4.6.x-google" LIBGCC_PATH=`$TOOLCHAIN_GCC -print-libgcc-file-name` LIBGCC_BASE_PATH=${LIBGCC_PATH%/*} # base path of libgcc.a GCC_BASE_VERSION=${LIBGCC_BASE_PATH##*/} # stuff after the last / # Create temporary directory TMPDIR=$NDK_TMPDIR/standalone/$TOOLCHAIN_NAME dump "Copying prebuilt binaries..." # Now copy the GCC toolchain prebuilt binaries run copy_directory "$TOOLCHAIN_PATH" "$TMPDIR" if [ -n "$LLVM_VERSION" ]; then # Copy the clang/llvm toolchain prebuilt binaries run copy_directory "$LLVM_TOOLCHAIN_PATH" "$TMPDIR" # Move clang and clang++ to clang${LLVM_VERSION} and clang${LLVM_VERSION}++, # then create scripts linking them with predefined -target flag. This is to # make clang/++ easier drop-in replacement for gcc/++ in NDK standalone mode. # Note that the file name of "clang" isn't important, and the trailing # "++" tells clang to compile in C++ mode LLVM_TARGET= case "$ARCH" in arm) # NOte: -target may change by clang based on the # presence of subsequent -march=armv7-a and/or -mthumb LLVM_TARGET=armv5te-none-linux-androideabi ;; x86) LLVM_TARGET=i686-none-linux-android ;; mips) LLVM_TARGET=mipsel-none-linux-android ;; *) dump "ERROR: Unsupported NDK architecture!" esac # Need to remove '.' from LLVM_VERSION when constructing new clang name, # otherwise clang3.1++ may still compile *.c code as C, not C++, which # is not consistent with g++ LLVM_VERSION_WITHOUT_DOT=$(echo "$LLVM_VERSION" | sed -e "s!\.!!") mv "$TMPDIR/bin/clang${HOST_EXE}" "$TMPDIR/bin/clang${LLVM_VERSION_WITHOUT_DOT}${HOST_EXE}" if [ -h "$TMPDIR/bin/clang++${HOST_EXE}" ] ; then ## clang++ is a link to clang. Remove it and reconstruct rm "$TMPDIR/bin/clang++${HOST_EXE}" ln -s "clang${LLVM_VERSION_WITHOUT_DOT}${HOST_EXE}" "$TMPDIR/bin/clang${LLVM_VERSION_WITHOUT_DOT}++${HOST_EXE}" else mv "$TMPDIR/bin/clang++${HOST_EXE}" "$TMPDIR/bin/clang$LLVM_VERSION_WITHOUT_DOT++${HOST_EXE}" fi cat > "$TMPDIR/bin/clang" < "$TMPDIR/bin/clang++" < "$TMPDIR/bin/clang.cmd" < "$TMPDIR/bin/clang++.cmd" <