• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
20PROGRAM_PARAMETERS=""
21PROGRAM_DESCRIPTION=\
22"Generate a customized Android toolchain installation that includes
23a working sysroot. The result is something that can more easily be
24used as a standalone cross-compiler, e.g. to run configure and
25make scripts."
26
27force_32bit_binaries
28
29# For now, this is the only toolchain that works reliably.
30TOOLCHAIN_NAME=
31register_var_option "--toolchain=<name>" TOOLCHAIN_NAME "Specify toolchain name"
32
33LLVM_VERSION=
34register_var_option "--llvm-ver=<vers>" LLVM_VERSION "List of LLVM release versions"
35
36ARCH=
37register_option "--arch=<name>" do_arch "Specify target architecture" "arm"
38do_arch () { ARCH=$1; }
39
40NDK_DIR=`dirname $0`
41NDK_DIR=`dirname $NDK_DIR`
42NDK_DIR=`dirname $NDK_DIR`
43register_var_option "--ndk-dir=<path>" NDK_DIR "Take source files from NDK at <path>"
44
45SYSTEM=$HOST_TAG
46register_var_option "--system=<name>" SYSTEM "Specify host system"
47
48PACKAGE_DIR=/tmp/ndk-$USER
49register_var_option "--package-dir=<path>" PACKAGE_DIR "Place package file in <path>"
50
51INSTALL_DIR=
52register_var_option "--install-dir=<path>" INSTALL_DIR "Don't create package, install files to <path> instead."
53
54PLATFORM=
55register_option "--platform=<name>" do_platform "Specify target Android platform/API level." "android-3"
56do_platform () { PLATFORM=$1; }
57
58extract_parameters "$@"
59
60# Check NDK_DIR
61if [ ! -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
65fi
66
67# Check ARCH
68if [ -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"
84fi
85
86# Check toolchain name
87if [ -z "$TOOLCHAIN_NAME" ]; then
88    TOOLCHAIN_NAME=$(get_default_toolchain_name_for_arch $ARCH)
89    echo "Auto-config: --toolchain=$TOOLCHAIN_NAME"
90fi
91
92# Check PLATFORM
93if [ -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"
106fi
107
108if [ ! -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
112fi
113
114# Check toolchain name
115TOOLCHAIN_PATH="$NDK_DIR/toolchains/$TOOLCHAIN_NAME"
116if [ ! -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
121fi
122
123# Extract architecture from platform name
124parse_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 )
128if [ $? != 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
133fi
134
135# Compute source sysroot
136SRC_SYSROOT="$NDK_DIR/platforms/$PLATFORM/arch-$ARCH"
137if [ ! -d "$SRC_SYSROOT" ] ; then
138    echo "No platform files ($PLATFORM) for this architecture: $ARCH"
139    exit 1
140fi
141
142# Check that we have any prebuilts GCC toolchain here
143if [ ! -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
147fi
148
149if [ ! -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
153fi
154
155TOOLCHAIN_PATH="$TOOLCHAIN_PATH/prebuilt/$SYSTEM"
156TOOLCHAIN_GCC=$TOOLCHAIN_PATH/bin/$ABI_CONFIGURE_TARGET-gcc
157
158if [ ! -f "$TOOLCHAIN_GCC" ] ; then
159    echo "Toolchain $TOOLCHAIN_GCC is missing!"
160    exit 1
161fi
162
163if [ -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"
178fi
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"
182LIBGCC_PATH=`$TOOLCHAIN_GCC -print-libgcc-file-name`
183LIBGCC_BASE_PATH=${LIBGCC_PATH%/libgcc.a}  # base path of libgcc.a
184GCC_BASE_VERSION=${LIBGCC_BASE_PATH##*/}   # stuff after the last /
185
186# Create temporary directory
187TMPDIR=$NDK_TMPDIR/standalone/$TOOLCHAIN_NAME
188
189dump "Copying prebuilt binaries..."
190# Now copy the GCC toolchain prebuilt binaries
191run copy_directory "$TOOLCHAIN_PATH" "$TMPDIR"
192
193if [ -n "$LLVM_VERSION" ]; then
194  # Copy the clang/llvm toolchain prebuilt binaries
195  run copy_directory "$LLVM_TOOLCHAIN_PATH" "$TMPDIR"
196fi
197
198dump "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!
201run copy_directory_nolinks "$SRC_SYSROOT" "$TMPDIR/sysroot"
202
203dump "Copying libstdc++ headers and libraries..."
204
205GNUSTL_DIR=$NDK_DIR/$GNUSTL_SUBDIR/$GCC_VERSION
206GNUSTL_LIBS=$GNUSTL_DIR/libs
207
208ABI_STL="$TMPDIR/$ABI_CONFIGURE_TARGET"
209ABI_STL_INCLUDE="$TMPDIR/include/c++/$GCC_BASE_VERSION"
210
211copy_directory "$GNUSTL_DIR/include" "$ABI_STL_INCLUDE"
212ABI_STL_INCLUDE_TARGET="$ABI_STL_INCLUDE/$ABI_CONFIGURE_TARGET"
213mkdir -p "$ABI_STL_INCLUDE_TARGET"
214fail_panic "Can't create directory: $ABI_STL_INCLUDE_TARGET"
215case "$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!"
251esac
252
253# Install or Package
254if [ -n "$INSTALL_DIR" ] ; then
255    dump "Copying files to: $INSTALL_DIR"
256    run copy_directory "$TMPDIR" "$INSTALL_DIR"
257else
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"
262fi
263dump "Cleaning up..."
264run rm -rf $TMPDIR
265
266dump "Done."
267