1#!/bin/sh 2# 3# Copyright (C) 2010 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 18# Download and rebuild all prebuilt binaries for the Android NDK 19# This includes: 20# - cross toolchains (gcc, ld, gdb, etc...) 21# - target-specific gdbserver 22# - host ccache 23# 24 25. `dirname $0`/prebuilt-common.sh 26PROGDIR=`dirname $0` 27 28NDK_DIR= 29register_var_option "--ndk-dir=<path>" NDK_DIR "Don't package, copy binaries to target NDK directory" 30 31BUILD_DIR=/tmp/ndk-$USER/build 32register_var_option "--build-dir=<path>" BUILD_DIR "Specify temporary build directory" 33 34GDB_VERSION=$DEFAULT_GDB_VERSION 35register_var_option "--gdb-version=<version>" GDB_VERSION "Specify gdb version" 36 37BINUTILS_VERSION=$DEFAULT_BINUTILS_VERSION 38register_var_option "--binutils-version=<version>" BINUTILS_VERSION "Specify binutils version" 39 40MPFR_VERSION=$DEFAULT_MPFR_VERSION 41register_var_option "--mpfr-version=<version>" MPFR_VERSION "Specify mpfr version" 42 43ARCH=arm 44register_var_option "--arch=<arch>" ARCH "Specify architecture" 45 46OPTION_SYSROOT= 47register_var_option "--sysroot=<dir>" OPTION_SYSROOT "Specify sysroot" 48 49OPTION_TOOLCHAIN_SRC_PKG= 50register_var_option "--toolchain-src-pkg=<file>" OPTION_TOOLCHAIN_SRC_PKG "Use toolchain source package." 51 52OPTION_TOOLCHAIN_SRC_DIR= 53register_var_option "--toolchain-src-dir=<path>" OPTION_TOOLCHAIN_SRC_DIR "Use toolchain source directory." 54 55RELEASE=`date +%Y%m%d` 56PACKAGE_DIR=/tmp/ndk-$USER/prebuilt-$RELEASE 57register_var_option "--package-dir=<path>" PACKAGE_DIR "Put prebuilt tarballs into <path>." 58 59OPTION_GIT_HTTP=no 60register_var_option "--git-http" OPTION_GIT_HTTP "Download sources with http." 61 62HOST_ONLY= 63register_var_option "--host-only" HOST_ONLY "Only rebuild host binaries." 64 65DARWIN_SSH= 66if [ "$HOST_OS" = "linux" ] ; then 67register_var_option "--darwin-ssh=<hostname>" DARWIN_SSH "Specify Darwin hostname to ssh to for the build." 68fi 69 70register_mingw_option 71 72PROGRAM_PARAMETERS= 73PROGRAM_DESCRIPTION=\ 74"Download and rebuild from scratch all prebuilt binaries for the Android NDK. 75By default, this will create prebuilt binary tarballs in: $PACKAGE_DIR 76 77You can however use --ndk-dir=<path> to instead copy the binaries directly 78to an existing NDK installation. 79 80By default, the script will download the toolchain sources from the Internet, 81but you can override this using either --toolchain-src-dir or 82--toolchain-src-pkg. 83 84Please read docs/DEVELOPMENT.TXT for more usage information about this 85script. 86" 87 88extract_parameters "$@" 89 90# Needed to set HOST_TAG to windows if --mingw is used. 91handle_host 92 93if [ -n "$PACKAGE_DIR" -a -n "$NDK_DIR" ] ; then 94 echo "ERROR: You cannot use both --package-dir and --ndk-dir at the same time!" 95 exit 1 96fi 97 98if [ -z "$NDK_DIR" ] ; then 99 mkdir -p "$PACKAGE_DIR" 100 fail_panic "Could not create directory: $PACKAGE_DIR" 101 NDK_DIR=$BUILD_DIR/install 102 mkdir -p $NDK_DIR 103else 104 if [ ! -d "$NDK_DIR" ] ; then 105 echo "ERROR: NDK directory does not exists: $NDK_DIR" 106 exit 1 107 fi 108 PACKAGE_DIR= 109fi 110 111if [ -n "$PARAMETERS" ]; then 112 dump "ERROR: Too many parameters. See --help for proper usage." 113 exit 1 114fi 115 116mkdir -p $BUILD_DIR 117if [ $? != 0 ] ; then 118 dump "ERROR: Could not create build directory: $BUILD_DIR" 119 exit 1 120fi 121setup_default_log_file "$BUILD_DIR/log.txt" 122 123if [ -n "$OPTION_TOOLCHAIN_SRC_DIR" ] ; then 124 if [ -n "$OPTION_TOOLCHAIN_SRC_PKG" ] ; then 125 dump "ERROR: You can't use both --toolchain-src-dir and --toolchain-src-pkg" 126 exi t1 127 fi 128 SRC_DIR="$OPTION_TOOLCHAIN_SRC_DIR" 129 if [ ! -d "$SRC_DIR/gcc" ] ; then 130 dump "ERROR: Invalid toolchain source directory: $SRC_DIR" 131 exit 1 132 fi 133else 134 SRC_DIR="$BUILD_DIR/src" 135 mkdir -p "$SRC_DIR" 136fi 137 138FLAGS="" 139if [ $VERBOSE = yes ] ; then 140 FLAGS="--verbose" 141fi 142if [ "$MINGW" = "yes" ] ; then 143 FLAGS="$FLAGS --mingw" 144fi 145 146# If --toolchain-src-dir is not given, get the toolchain sources, either 147# by downloading, or by extracting the, from a tarball given by the 148# --toolchain-src-pkg option. 149# 150get_toolchain_sources () 151{ 152 if [ -z "$OPTION_TOOLCHAIN_SRC_DIR" ] ; then 153 if [ -n "$OPTION_TOOLCHAIN_SRC_PKG" ] ; then 154 # Unpack the toolchain sources 155 if [ ! -f "$OPTION_TOOLCHAIN_SRC_PKG" ] ; then 156 dump "ERROR: Invalid toolchain source package: $OPTION_TOOLCHAIN_SRC_PKG" 157 exit 1 158 fi 159 unpack_archive "$OPTION_TOOLCHAIN_SRC_PKG" "$SRC_DIR" 160 fail_panic "Could not unpack toolchain sources!" 161 else 162 # Download the toolchain sources 163 dump "Download sources from android.git.kernel.org" 164 DOWNLOAD_FLAGS="$FLAGS" 165 if [ $OPTION_GIT_HTTP = "yes" ] ; then 166 DOWNLOAD_FLAGS="$DOWNLOAD_FLAGS --git-http" 167 fi 168 $PROGDIR/download-toolchain-sources.sh $DOWNLOAD_FLAGS $SRC_DIR 169 fail_panic "Could not download toolchain sources!" 170 fi 171 fi # ! $TOOLCHAIN_SRC_DIR 172} 173 174 175 176# Special treatment when building remotely on a Darwin machine through ssh 177# For now, we will have to prompt the user several times. 178# 179if [ -n "$DARWIN_SSH" ] ; then 180 # 1/ Copy the NDK toolchain build scripts 181 # 2/ Copy the toolchain sources/package 182 # 3/ Ssh to unpack the build scripts, and run them 183 # 4/ Copy back the generated prebuilt binaries 184 # 185 dump "Preparing remote build on $DARWIN_SSH..." 186 dump "Getting toolchain sources" 187 get_toolchain_sources 188 dump "Creating remote temp directory" 189 TMPREMOTE=/tmp/ndk-$USER/darwin-prebuild 190 run ssh $DARWIN_SSH "mkdir -p $TMPREMOTE && rm -rf $TMPREMOTE/*" 191 TMPDARWIN=$NDK_TMPDIR/darwin # Where we're going to package stuff 192 log "Using temporary work directory: $TMPDARWIN" 193 dump "Prepare NDK build scripts" 194 copy_directory "$ANDROID_NDK_ROOT/build" "$TMPDARWIN/ndk/build" 195 copy_file_list "$ANDROID_NDK_ROOT" "$TMPDARWIN/ndk" sources/android/libthread_db 196 copy_file_list "$ANDROID_NDK_ROOT" "$TMPDARWIN/ndk" "$STLPORT_SUBDIR" 197 copy_file_list "$ANDROID_NDK_ROOT" "$TMPDARWIN/ndk" "$GABIXX_SUBDIR" 198 copy_file_list "$ANDROID_NDK_ROOT" "$TMPDARWIN/ndk" sources/host-tools/ndk-stack 199 dump "Prepare platforms files" 200 $PROGDIR/build-platforms.sh --arch=$ARCH --no-samples --dst-dir="$TMPDARWIN/ndk" 201 dump "Copying NDK build scripts and platform files to remote..." 202 (cd "$TMPDARWIN" && tar czf - ndk) | (ssh $DARWIN_SSH tar xzf - -C $TMPREMOTE) 203 fail_panic "Could not copy!" 204 rm -rf $TMPDARWIN 205 dump "Copy toolchain sources to remote" 206 ssh $DARWIN_SSH mkdir -p $TMPREMOTE/toolchain && 207 (cd "$SRC_DIR" && tar czf - .) | (ssh $DARWIN_SSH tar xzf - -C $TMPREMOTE/toolchain) 208 fail_panic "Could not copy toolchain!" 209 dump "Running remote build..." 210 SYSROOT=$TMPREMOTE/ndk/platforms/android-$(get_default_api_level_for_arch $ARCH)/arch-$ARCH 211 run ssh $DARWIN_SSH "$TMPREMOTE/ndk/build/tools/rebuild-all-prebuilt.sh --toolchain-src-dir=$TMPREMOTE/toolchain --package-dir=$TMPREMOTE/packages --arch=$(spaces_to_commas $ARCH) --sysroot=$SYSROOT --host-only" 212 fail_panic "Could not build prebuilt packages on Darwin!" 213 dump "Copying back Darwin prebuilt packages..." 214 run scp $DARWIN_SSH:$TMPREMOTE/packages/*-darwin-* $PACKAGE_DIR/ 215 fail_panic "Could not grab Darwin packages!" 216 dump "Cleaning up remote machine..." 217 run ssh $DARWIN_SSH rm -rf $TMPREMOTE 218 exit 0 219fi 220 221# Package a directory in a .tar.bz2 archive 222# 223# $1: textual description 224# $2: final package name (without .tar.bz2 suffix) 225# $3: relative root path from $NDK_DIR 226# 227package_it () 228{ 229 if [ -n "$PACKAGE_DIR" ] ; then 230 dump "Packaging $1 ($2.tar.bz2) ..." 231 PREBUILT_PACKAGE="$PACKAGE_DIR/$2".tar.bz2 232 (cd $NDK_DIR && tar cjf $PREBUILT_PACKAGE $3) 233 fail_panic "Could not package $1!" 234 fi 235} 236 237 238# Rebuild the ndk-stack tool, and link to it 239dump "Building host ndk-stack tool from sources." 240NDK_STACK_FLAGS= 241NDK_STACK_PACKAGE=$(get_host_exec_name ndk-stack) 242if [ "$MINGW" = yes ]; then 243 NDK_STACK_FLAGS=$NDK_STACK_FLAGS" --mingw" 244fi 245if [ "$VERBOSE" = "yes" ]; then 246 NDK_STACK_FLAGS=$NDK_STACK_FLAGS" --verbose" 247fi 248if [ "$VERBOSE2" = "yes" ]; then 249 NDK_STACK_FLAGS=$NDK_STACK_FLAGS" --verbose" 250fi 251$ANDROID_NDK_ROOT/build/tools/build-ndk-stack.sh $NDK_STACK_FLAGS --out=$NDK_DIR/$NDK_STACK_PACKAGE && 252package_it "ndk-stack binary" ndk-stack-$HOST_TAG $NDK_STACK_PACKAGE 253 254 255# If no sysroot is specified, build one explicitely 256if [ -z "$OPTION_SYSROOT" ]; then 257 SYSROOT=$BUILD_DIR/platforms/android-9/arch-$ARCH 258 mkdir -p $SYSROOT 259 dump "Creating sysroot: $SYSROOT" 260 $PROGDIR/build-platforms.sh --arch=$ARCH --no-samples --no-symlinks --dst-dir=$BUILD_DIR 261 OPTION_SYSROOT=$SYSROOT 262fi 263 264# Build the toolchain from sources 265# $1: toolchain name (e.g. arm-linux-androideabi-4.4.3) 266# $2: extra flags, if needed 267build_toolchain () 268{ 269 dump "Building $1 toolchain... (this can be long)" 270 run $PROGDIR/build-gcc.sh $FLAGS $2 --sysroot=$OPTION_SYSROOT --mpfr-version=$MPFR_VERSION --binutils-version=$BINUTILS_VERSION --gdb-version=$GDB_VERSION --build-out=$BUILD_DIR/toolchain-$1 $SRC_DIR $NDK_DIR $1 271 fail_panic "Could not build $1 toolchain!" 272 package_it "$1 toolchain" "$1-$HOST_TAG" "toolchains/$1/prebuilt/$HOST_TAG" 273} 274 275build_gdbserver () 276{ 277 if [ "$MINGW" = yes ] ; then 278 dump "Skipping gdbserver build (--mingw option being used)." 279 return 280 fi 281 dump "Build $1 gdbserver..." 282 $PROGDIR/build-gdbserver.sh $FLAGS --sysroot=$OPTION_SYSROOT --build-out=$BUILD_DIR/gdbserver-$1 --gdb-version=$GDB_VERSION $SRC_DIR $NDK_DIR $1 283 fail_panic "Could not build $1 gdbserver!" 284 package_it "$1 gdbserver" "$1-gdbserver" "toolchains/$1/prebuilt/gdbserver" 285} 286 287get_toolchain_sources 288 289case "$ARCH" in 290arm ) 291 build_toolchain $DEFAULT_ARCH_TOOLCHAIN_NAME_arm 292 if [ -z "$HOST_ONLY" ]; then 293 build_gdbserver $DEFAULT_ARCH_TOOLCHAIN_NAME_arm 294 fi 295 ;; 296x86 ) 297 build_toolchain $DEFAULT_ARCH_TOOLCHAIN_NAME_x86 298 if [ -z "$HOST_ONLY" ]; then 299 build_gdbserver $DEFAULT_ARCH_TOOLCHAIN_NAME_x86 300 fi 301 ;; 302esac 303 304if [ -z "$HOST_ONLY" ]; then 305 # We need to package the libsupc++ binaries on Linux since the GCC build 306 # scripts cannot build them with --mingw option. 307 if [ "$HOST_OS" = "linux" ] ; then 308 case "$ARCH" in 309 arm ) 310 LIBSUPC_DIR="toolchains/$DEFAULT_ARCH_TOOLCHAIN_NAME_arm/prebuilt/$HOST_TAG/$DEFAULT_ARCH_TOOLCHAIN_PREFIX_arm/lib" 311 package_it "GNU libsupc++ armeabi libs" "gnu-libsupc++-armeabi" "$LIBSUPC_DIR/libsupc++.a $LIBSUPC_DIR/thumb/libsupc++.a" 312 package_it "GNU libsupc++ armeabi-v7a libs" "gnu-libsupc++-armeabi-v7a" "$LIBSUPC_DIR/armv7-a/libsupc++.a $LIBSUPC_DIR/armv7-a/thumb/libsupc++.a" 313 ;; 314 x86 ) 315 LIBSUPC_DIR="toolchains/$DEFAULT_ARCH_TOOLCHAIN_NAME_x86/prebuilt/$HOST_TAG/$DEFAULT_ARCH_TOOLCHAIN_PREFIX_x86/lib" 316 package_it "GNU libsupc++ x86 libs" "gnu-libsupc++-x86" "$LIBSUPC_DIR/libsupc++.a" 317 ;; 318 esac 319 fi 320 321 if [ "$MINGW" != "yes" ] ; then 322 package_it "GNU libstdc++ headers" "gnu-libstdc++-headers" "sources/cxx-stl/gnu-libstdc++/include" 323 324 case "$ARCH" in 325 arm ) 326 package_it "GNU libstdc++ armeabi libs" "gnu-libstdc++-libs-armeabi" "sources/cxx-stl/gnu-libstdc++/libs/armeabi" 327 package_it "GNU libstdc++ armeabi-v7a libs" "gnu-libstdc++-libs-armeabi-v7a" "sources/cxx-stl/gnu-libstdc++/libs/armeabi-v7a" 328 ;; 329 x86 ) 330 package_it "GNU libstdc++ x86 libs" "gnu-libstdc++-libs-x86" "sources/cxx-stl/gnu-libstdc++/libs/x86" 331 ;; 332 esac 333 fi 334 335 # Rebuild GAbi++ prebuilt libraries 336 if [ "$MINGW" != "yes" ]; then 337 dump "Building GAbi++ binaries" 338 BUILD_GABIXX_FLAGS="--ndk-dir=\"$NDK_DIR\" --package-dir=\"$PACKAGE_DIR\"" 339 if [ $VERBOSE = yes ] ; then 340 BUILD_GABIXX_FLAGS="$BUILD_GABIXX_FLAGS --verbose" 341 fi 342 $ANDROID_NDK_ROOT/build/tools/build-platforms.sh --no-symlinks --no-samples --arch=$ARCH --dst-dir="$NDK_DIR" 343 case "$ARCH" in 344 arm ) 345 BUILD_GABIXX_FLAGS=$BUILD_GABIXX_FLAGS" --abis=armeabi,armeabi-v7a" 346 ;; 347 x86 ) 348 BUILD_GABIXX_FLAGS=$BUILD_GABIXX_FLAGS" --abis=x86" 349 ;; 350 esac 351 $ANDROID_NDK_ROOT/build/tools/build-gabi++.sh $BUILD_GABIXX_FLAGS 352 else 353 dump "Skipping GAbi++ binaries build (--mingw option being used)" 354 fi 355 356 # Rebuild STLport prebuilt libraries 357 if [ "$MINGW" != "yes" ] ; then 358 dump "Building STLport binaries" 359 BUILD_STLPORT_FLAGS="--ndk-dir=\"$NDK_DIR\" --package-dir=\"$PACKAGE_DIR\"" 360 if [ $VERBOSE = yes ] ; then 361 BUILD_STLPORT_FLAGS="$BUILD_STLPORT_FLAGS --verbose" 362 fi 363 $ANDROID_NDK_ROOT/build/tools/build-platforms.sh --no-symlinks --no-samples --arch=$ARCH --dst-dir="$NDK_DIR" 364 case "$ARCH" in 365 arm ) 366 BUILD_STLPORT_FLAGS=$BUILD_STLPORT_FLAGS" --abis=armeabi,armeabi-v7a" 367 ;; 368 x86 ) 369 BUILD_STLPORT_FLAGS=$BUILD_STLPORT_FLAGS" --abis=x86" 370 ;; 371 esac 372 $ANDROID_NDK_ROOT/build/tools/build-stlport.sh $BUILD_STLPORT_FLAGS 373 else 374 dump "Skipping STLport binaries build (--mingw option being used)" 375 fi 376fi # !HOST_ONLY 377 378if [ -n "$PACKAGE_DIR" ] ; then 379 dump "Done! See $PACKAGE_DIR" 380else 381 dump "Done! See $NDK_DIR/toolchains" 382fi 383