1 #!/bin/sh 2 # 3 # This script is used to rebuild the x86 toolchain Linux 4 # (i686-android-linux-4.4.3) completely from scratch. 5 # 6 # CURRENTLY UNTESTED ON OSX!! 7 # 8 # You need to have a recent AOSP workspace, and have ANDROID_BUILD_TOP 9 # point to it. Usually, this variable is defined in your environment after 10 # you call "lunch <product>-<variant>" 11 # 12 13 PRODUCT_NAME=full_x86 14 ARCH=x86 15 GCC_VERSION=4.4.3 16 TOOLCHAIN_INSTALL_NAME=i686-android-linux-$GCC_VERSION 17 TOOLCHAIN_BIN_PREFIX=i686-android-linux- 18 TOOLCHAIN_BUILD_NAME=$ARCH-$GCC_VERSION 19 PLATFORM=android-9 20 21 MPFR_VERSION=2.4.1 22 GDB_VERSION=6.6 23 BINUTILS_VERSION=2.20.1 24 25 GIT_DATE=20110303 26 27 HOST_OS=`uname -s` 28 case "$HOST_OS" in 29 Darwin) 30 HOST_OS=darwin 31 HOST_TAG=darwin-x86 32 BUILD_NUM_CPUS=$(sysctl -n hw.ncpu) 33 ;; 34 Linux) 35 # note that building 32-bit binaries on x86_64 is handled later 36 HOST_OS=linux 37 HOST_TAG=linux-x86 38 BUILD_NUM_CPUS=$(grep -c processor /proc/cpuinfo) 39 ;; 40 *) 41 echo "ERROR: Unsupported OS: $HOST_OS" 42 exit 1 43 esac 44 45 PREBUILT_INSTALL_DIR=prebuilt/$HOST_TAG/toolchain/$TOOLCHAIN_INSTALL_NAME 46 #PREBUILT_INSTALL_DIR=/tmp/zoo 47 48 PREBUILT_GDB_DIR=prebuilt/android-$ARCH/gdbserver 49 50 # If 51 if [ -z "$ANDROID_BUILD_TOP" ]; then 52 if [ ! -f build/envsetup.sh ]; then 53 echo "Please run this script from the top of a valid AOSP platform source tree" 54 echo "or define ANDROID_BUILD_TOP in your environment to point to it. Note that" 55 echo "this variable is typically defined by running 'lunch <name>' too." 56 exit 1 57 fi 58 ANDROID_BUILD_TOP=. 59 fi 60 61 # Today's date, ISO format, only used to determine the temporary directory. 62 TODAY=`date '+%Y%m%d'` 63 64 VERBOSE_FLAG= 65 : ${V:=0} 66 if [ "$V" -gt 0 ]; then 67 VERBOSE_FLAG="--verbose" 68 fi 69 70 # A few useful functions 71 # 72 fail_panic () 73 { 74 if [ $? != 0 ]; then 75 echo "PANIC: $@" 76 exit 1 77 fi 78 } 79 80 run () 81 { 82 echo "### $@" 83 $@ 84 } 85 86 # Compute the number of parallel build jobs 87 # 88 JOBS=$(( $BUILD_NUM_CPUS * 2 )) 89 90 # Temporary directory where we're going to store every intermediate file. 91 # 92 # Normally, none of the generated executables should embed/use this path 93 # but select something complex to avoid problems if this is not the case 94 # 95 TMPDIR=/tmp/$TOOLCHAIN_INSTALL_NAME-$TODAY 96 mkdir -p $TMPDIR 97 fail_panic "Can't create temporary directory: $TMPDIR" 98 99 # System libraries needed to bootstrap the toolchain 100 SYSTEM_LIBS="libc libm libdl libstdc++" 101 102 # Where to download the toolchain sources from android.git.kernel.org 103 TOOLCHAIN_SRC_DIR=$TMPDIR/toolchain-sources 104 105 # Where to build the toolchain binaries 106 TOOLCHAIN_BUILD_DIR=$TMPDIR/toolchain-build 107 108 # Directory where we will copy the content of development/ndk 109 # and update it with the freshest libraries from the system. 110 DEVELOPMENT_DIR=$TMPDIR/devel 111 112 # Where to place the installed binaries (NDK installation-like) 113 TOOLCHAIN_INSTALL_DIR=$TMPDIR/ndk-install 114 115 build_system_libraries () 116 { 117 echo "Building $PRODUCT_NAME system libraries: $SYSTEM_LIBS" 118 cd $ANDROID_BUILD_TOP && 119 make $SYSTEM_LIBS -j$JOBS 120 fail_panic "Could not build $PRODUCT_NAME system libraries!" 121 } 122 123 download_toolchain_sources () 124 { 125 # We don't use ndk/build/tools/download-toolchain-sources.sh here 126 # because it removes all .info files, which breaks the ToT source 127 # build as of 2011-03-03 128 # 129 echo "Download toolchain patches to: $TOOLCHAIN_SRC_DIR" 130 GIT_PREFIX=git://android.git.kernel.org/toolchain 131 for PROJECT in build binutils gcc gdb mpfr gmp gold; do 132 PROJECT_DIR="$TOOLCHAIN_SRC_DIR/$PROJECT" 133 PROJECT_GIT="$GIT_PREFIX/$PROJECT.git" 134 echo "$PROJECT: Cleaning" 135 rm -rf $PROJECT_DIR 136 echo "$PROJECT: Downloading sources from: $PROJECT_GIT" 137 (git clone $PROJECT_GIT $PROJECT_DIR) > /dev/null 2>&1 138 fail_panic "$PROJECT: Could not git clone!!" 139 if [ -n "$GIT_DATE" ] ; then 140 echo "$PROJECT: Switching to sources for date '$GIT_DATE'" 141 (cd $PROJECT_DIR && 142 REVISION=`git rev-list -n 1 --until="$GIT_DATE" HEAD` && 143 run git checkout $REVISION) > /dev/null 2>&1 144 fail_panic "$PROJECT: Could not checkout?" 145 fi 146 done 147 fail_panic "Could not download toolchain sources!" 148 } 149 150 build_development_dir () 151 { 152 echo "Copy development/ndk to: $DEVELOPMENT_DIR" 153 rm -rf $DEVELOPMENT_DIR 154 cp -rf $ANDROID_BUILD_TOP/development/ndk $DEVELOPMENT_DIR 155 fail_panic "Could not copy development/ndk!" 156 } 157 158 build_sysroot () 159 { 160 echo "Generate platform files into: $TOOLCHAIN_INSTALL_DIR" 161 ndk/build/tools/build-platforms.sh \ 162 --abi=$ARCH --no-symlinks --no-samples \ 163 --src-dir=$DEVELOPMENT_DIR \ 164 --dst-dir=$TOOLCHAIN_INSTALL_DIR 165 fail_panic "Could not generate build sysroot" 166 167 echo "Copying system files into: $TOOLCHAIN_INSTALL_DIR/sysroot" 168 cp -r $TOOLCHAIN_INSTALL_DIR/platforms/$PLATFORM/arch-$ARCH $TOOLCHAIN_INSTALL_DIR/sysroot 169 fail_panic "Could not copy files to: $TOOLCHAIN_INSTALL_DIR/sysroot" 170 171 rm -rf $TOOLCHAIN_INSTALL_DIR/sysroot/usr/lib 172 } 173 174 build_gcc () 175 { 176 echo "Building $TOOLCHAIN_INSTALL_NAME now (this can be long)." 177 mkdir -p $TOOLCHAIN_BUILD_DIR && rm -rf $TOOLCHAIN_BUILD_DIR/* 178 run ndk/build/tools/build-gcc.sh \ 179 --sysroot=$TOOLCHAIN_INSTALL_DIR/sysroot \ 180 --build-out=$TOOLCHAIN_BUILD_DIR \ 181 --mpfr-version=$MPFR_VERSION \ 182 --gdb-version=$GDB_VERSION \ 183 --binutils-version=$BINUTILS_VERSION \ 184 "$TOOLCHAIN_SRC_DIR" "$TOOLCHAIN_INSTALL_DIR" $TOOLCHAIN_BUILD_NAME 185 186 fail_panic "Could not build compiler!" 187 } 188 189 install_gcc () 190 { 191 echo "Installing toolchain to $PREBUILT_INSTALL_DIR" 192 SRCDIR=$TOOLCHAIN_INSTALL_DIR/toolchains/$TOOLCHAIN_BUILD_NAME/prebuilt/$HOST_TAG 193 mkdir -p $PREBUILT_INSTALL_DIR && 194 rm -rf $PREBUILT_INSTALL_DIR/* && 195 (cd $SRCDIR && tar cf - --exclude=sysroot *) | tar xf - -C $PREBUILT_INSTALL_DIR 196 fail_panic "Can't install!" 197 # Copy toolchain license files too 198 # Plus this script as well! 199 cp ndk/build/tools/toolchain-licenses/* $PREBUILT_INSTALL_DIR && 200 cp $0 $PREBUILT_INSTALL_DIR 201 } 202 203 download_toolchain_sources 204 build_development_dir 205 build_sysroot 206 build_gcc 207 install_gcc 208 209 echo "Done!" 210