Home | History | Annotate | Download | only in tools
      1 #!/bin/sh
      2 #
      3 # Copyright (C) 2009 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 #  This shell script is used to download the sources of the Android NDK toolchain
     18 #  from the git server at android.git.kernel.org and package them in a nice tarball
     19 #  that can later be used with the 'built-toolchain.sh' script.
     20 #
     21 
     22 # include common function and variable definitions
     23 . `dirname $0`/prebuilt-common.sh
     24 PROGDIR=`dirname $0`
     25 PROGDIR=`cd $PROGDIR && pwd`
     26 
     27 # the default branch to use
     28 BRANCH=master
     29 register_option "--branch=<name>" BRANCH "Specify release branch"
     30 
     31 # the default release name (use today's date)
     32 RELEASE=`date +%Y%m%d`
     33 register_var_option "--release=<name>" RELEASE "Specify release name"
     34 
     35 GITCMD=git
     36 register_var_option "--git=<executable>" GITCMD "Use this version of the git tool"
     37 
     38 OPTION_GIT_HTTP=no
     39 register_var_option "--git-http" OPTION_GIT_HTTP "Use http to download sources from git"
     40 
     41 OPTION_PACKAGE=no
     42 register_var_option "--package" OPTION_PACKAGE "Create source package in /tmp"
     43 
     44 PROGRAM_PARAMETERS="<src-dir>"
     45 PROGRAM_DESCRIPTION=\
     46 "Download the NDK toolchain sources from android.git.kernel.org into <src-dir>.
     47 You will need to run this script before being able to rebuilt the NDK toolchain
     48 binaries from scratch with build/tools/build-gcc.sh."
     49 
     50 extract_parameters $@
     51 
     52 # Check that 'git' works
     53 $GITCMD --version > /dev/null 2>&1
     54 if [ $? != 0 ] ; then
     55     echo "The git tool doesn't seem to work. Please check $GITCMD"
     56     exit 1
     57 fi
     58 log "Git seems to work ok."
     59 
     60 SRC_DIR="$PARAMETERS"
     61 if [ -z "$SRC_DIR" -a "$OPTION_PACKAGE" = no ] ; then
     62     echo "ERROR: You need to provide a <src-dir> parameter or use the --package option!"
     63     exit 1
     64 fi
     65 
     66 if [ -n "$SRC_DIR" ] ; then
     67     mkdir -p $SRC_DIR
     68     SRC_DIR=`cd $SRC_DIR && pwd`
     69     log "Using target source directory: $SRC_DIR"
     70 fi
     71 
     72 # Create temp directory where everything will be copied first
     73 #
     74 PKGNAME=android-ndk-toolchain-$RELEASE
     75 TMPDIR=/tmp/$PKGNAME
     76 log "Creating temporary directory $TMPDIR"
     77 rm -rf $TMPDIR && mkdir $TMPDIR
     78 if [ $? != 0 ] ; then
     79     echo "Could not create temporary directory: $TMPDIR"
     80 fi
     81 
     82 # prefix used for all clone operations
     83 GITPROTO=git
     84 if [ "$OPTION_GIT_HTTP" = "yes" ] ; then
     85   GITPROTO=http
     86 fi
     87 GITPREFIX=${GITPROTO}://android.git.kernel.org/toolchain
     88 
     89 toolchain_clone ()
     90 {
     91     dump "downloading sources for toolchain/$1"
     92     log "cloning $GITPREFIX/$1.git"
     93     run git clone $GITPREFIX/$1.git $1
     94     if [ $? != 0 ] ; then
     95         dump "Could not clone $GITPREFIX/$1.git ?"
     96         exit 1
     97     fi
     98     log "checking out $BRANCH branch of $1.git"
     99     cd $1
    100     if [ "$BRANCH" != "master" ] ; then
    101         run git checkout -b $BRANCH origin/$BRANCH
    102         if [ $? != 0 ] ; then
    103             dump "Could not checkout $1 ?"
    104             exit 1
    105         fi
    106     fi
    107     # get rid of .git directory, we won't need it.
    108     cd ..
    109     log "getting rid of .git directory for $1."
    110     run rm -rf $1/.git
    111 }
    112 
    113 cd $TMPDIR
    114 toolchain_clone binutils
    115 toolchain_clone build
    116 toolchain_clone gcc
    117 toolchain_clone gdb
    118 toolchain_clone gmp
    119 toolchain_clone gold  # not sure about this one !
    120 toolchain_clone mpfr
    121 
    122 # Patch the toolchain sources
    123 PATCHES_DIR="$PROGDIR/toolchain-patches"
    124 if [ -d "$PATCHES_DIR" ] ; then
    125     dump "Patching toolchain sources"
    126     run $PROGDIR/patch-sources.sh $FLAGS $TMPDIR $PATCHES_DIR
    127     if [ $? != 0 ] ; then
    128         dump "ERROR: Could not patch sources."
    129         exit 1
    130     fi
    131 fi
    132 
    133 
    134 # We only keep one version of gcc and binutils
    135 
    136 # we clearly don't need this
    137 log "getting rid of obsolete sources: gcc-4.3.1 gdb-6.8 binutils-2.17"
    138 rm -rf $TMPDIR/gcc/gcc-4.3.1
    139 rm -rf $TMPDIR/gcc/gdb-6.8
    140 rm -rf $TMPDIR/binutils/binutils-2.17
    141 
    142 # remove all info files from the toolchain sources
    143 # they create countless little problems during the build
    144 # if you don't have exactly the configuration expected by
    145 # the scripts.
    146 #
    147 find $TMPDIR -type f -a -name "*.info" -print0 | xargs -0 rm -f
    148 
    149 if [ $OPTION_PACKAGE = "yes" ] ; then
    150     # create the package
    151     PACKAGE=/tmp/$PKGNAME.tar.bz2
    152     dump "Creating package archive $PACKAGE"
    153     cd `dirname $TMPDIR`
    154     TARFLAGS="cjf"
    155     if [ $VERBOSE = yes ] ; then
    156         TARFLAGS="${TARFLAGS}v"
    157     fi
    158     run tar $TARFLAGS $PACKAGE -C /tmp/$PKGNAME .
    159     if [ $? != 0 ] ; then
    160         dump "Could not package toolchain source archive ?. See $TMPLOG"
    161         exit 1
    162     fi
    163     dump "Toolchain sources downloaded and packaged succesfully at $PACKAGE"
    164 else
    165     # copy sources to <src-dir>
    166     SRC_DIR=`cd $SRC_DIR && pwd`
    167     rm -rf $SRC_DIR && mkdir -p $SRC_DIR
    168     if [ $? != 0 ] ; then
    169         dump "ERROR: Could not create target source directory: $SRC_DIR"
    170         exit 1
    171     fi
    172     run cd $TMPDIR && run cp -rp * $SRC_DIR
    173     if [ $? != 0 ] ; then
    174         dump "ERROR: Could not copy downloaded sources to: $SRC_DIR"
    175         exit 1
    176     fi
    177     dump "Toolchain sources downloaded and copied to $SRC_DIR"
    178 fi
    179 
    180 dump "Cleaning up..."
    181 rm -rf $TMPDIR
    182 rm -f $TMPLOG
    183 dump "Done."
    184