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.googlesource.com 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 if [ "$TOOLCHAIN_GIT_DATE" -a "$TOOLCHAIN_GIT_DATE" != "now" ] ; then
     33     RELEASE=`echo $TOOLCHAIN_GIT_DATE | sed -e 's!-!!g'`
     34 else
     35     RELEASE=`date +%Y%m%d`
     36 fi
     37 register_var_option "--release=<name>" RELEASE "Specify release name"
     38 
     39 GIT_DATE=$TOOLCHAIN_GIT_DATE
     40 register_var_option "--git-date=<date>" GIT_DATE "Only sources that existed until specified <date>"
     41 
     42 GITCMD=git
     43 register_var_option "--git=<executable>" GITCMD "Use this version of the git tool"
     44 
     45 OPTION_GIT_BASE=https://android.googlesource.com/toolchain
     46 register_var_option "--git-base=<git-uri>" OPTION_GIT_BASE "Use specific git repository base"
     47 
     48 OPTION_GIT_REFERENCE=
     49 register_var_option "--git-reference=<path>" OPTION_GIT_REFERENCE "Use local git reference"
     50 
     51 OPTION_PACKAGE=no
     52 register_var_option "--package" OPTION_PACKAGE "Create source package in /tmp/ndk-$USER"
     53 
     54 OPTION_NO_PATCHES=no
     55 register_var_option "--no-patches" OPTION_NO_PATCHES "Do not patch sources"
     56 
     57 PROGRAM_PARAMETERS="<src-dir>"
     58 PROGRAM_DESCRIPTION=\
     59 "Download the NDK toolchain sources from android.googlesource.com into <src-dir>.
     60 You will need to run this script before being able to rebuilt the NDK toolchain
     61 binaries from scratch with build/tools/build-gcc.sh."
     62 
     63 if [ -n "$TOOLCHAIN_GIT_DATE" ] ; then
     64   PROGRAM_DESCRIPTION="$PROGRAM_DESCRIPTION\
     65 
     66 
     67 By default, this script will download sources from android.googlesource.com that
     68 correspond to the date of $TOOLCHAIN_GIT_DATE. If you want to use the tip of
     69 tree, use '--git-date=now' instead.
     70 
     71 If you don't want to use the official servers, use --git-base=<path> to
     72 download the sources from another set of git repostories.
     73 
     74 You can also speed-up the download if you maintain a local copy of the
     75 toolchain repositories on your machine. Use --git-reference=<path> to
     76 specify the base path that contains all copies, and its subdirectories will
     77 be used as git clone shared references.
     78 "
     79 
     80 fi
     81 
     82 extract_parameters "$@"
     83 
     84 # Check that 'git' works
     85 $GITCMD --version > /dev/null 2>&1
     86 if [ $? != 0 ] ; then
     87     echo "The git tool doesn't seem to work. Please check $GITCMD"
     88     exit 1
     89 fi
     90 log "Git seems to work ok."
     91 
     92 SRC_DIR="$PARAMETERS"
     93 if [ -z "$SRC_DIR" -a "$OPTION_PACKAGE" = no ] ; then
     94     echo "ERROR: You need to provide a <src-dir> parameter or use the --package option!"
     95     exit 1
     96 fi
     97 
     98 if [ -n "$SRC_DIR" ] ; then
     99     mkdir -p $SRC_DIR
    100     SRC_DIR=`cd $SRC_DIR && pwd`
    101     log "Using target source directory: $SRC_DIR"
    102 fi
    103 
    104 # Create temp directory where everything will be copied first
    105 #
    106 PKGNAME=android-ndk-toolchain-$RELEASE
    107 TMPDIR=/tmp/ndk-$USER/$PKGNAME
    108 log "Creating temporary directory $TMPDIR"
    109 rm -rf $TMPDIR && mkdir $TMPDIR
    110 fail_panic "Could not create temporary directory: $TMPDIR"
    111 
    112 # prefix used for all clone operations
    113 GITPREFIX="$OPTION_GIT_BASE"
    114 dump "Using git clone prefix: $GITPREFIX"
    115 
    116 GITREFERENCE=
    117 if [ -n "$OPTION_GIT_REFERENCE" ] ; then
    118     GITREFERENCE=$OPTION_GIT_REFERENCE
    119     if [ ! -d "$GITREFERENCE" -o ! -d "$GITREFERENCE/build" ]; then
    120         echo "ERROR: Invalid reference repository directory path: $GITREFERENCE"
    121         exit 1
    122     fi
    123     dump "Using git clone reference: $GITREFERENCE"
    124 fi
    125 
    126 toolchain_clone ()
    127 {
    128     local GITFLAGS
    129     GITFLAGS=
    130     if [ "$GITREFERENCE" ]; then
    131         GITFLAGS="$GITFLAGS --shared --reference $GITREFERENCE/$1"
    132     fi
    133     dump "downloading sources for toolchain/$1"
    134     if [ -d "$GITPREFIX/$1" ]; then
    135         log "cloning $GITPREFIX/$1"
    136         run git clone $GITFLAGS $GITPREFIX/$1 $1
    137     else
    138         log "cloning $GITPREFIX/$1.git"
    139         run git clone $GITFLAGS $GITPREFIX/$1.git $1
    140     fi
    141     fail_panic "Could not clone $GITPREFIX/$1.git ?"
    142     log "checking out $BRANCH branch of $1.git"
    143     cd $1
    144     if [ "$BRANCH" != "master" ] ; then
    145         run git checkout -b $BRANCH origin/$BRANCH
    146         fail_panic "Could not checkout $1 ?"
    147     fi
    148     # If --git-date is used, or we have a default
    149     if [ -n "$GIT_DATE" ] ; then
    150         REVISION=`git rev-list -n 1 --until="$GIT_DATE" HEAD`
    151         dump "Using sources for date '$GIT_DATE': toolchain/$1 revision $REVISION"
    152         run git checkout $REVISION
    153         fail_panic "Could not checkout $1 ?"
    154     fi
    155     (printf "%-32s " "toolchain/$1.git"; git log -1 --format=oneline) >> $SOURCES_LIST
    156     # get rid of .git directory, we won't need it.
    157     cd ..
    158     log "getting rid of .git directory for $1."
    159     run rm -rf $1/.git
    160 }
    161 
    162 cd $TMPDIR
    163 
    164 SOURCES_LIST=$(pwd)/SOURCES
    165 rm -f $SOURCES_LIST && touch $SOURCES_LIST
    166 
    167 toolchain_clone binutils
    168 toolchain_clone build
    169 toolchain_clone gcc
    170 toolchain_clone gdb
    171 toolchain_clone gmp
    172 toolchain_clone gold  # not sure about this one !
    173 toolchain_clone mpfr
    174 
    175 # Patch the toolchain sources
    176 if [ "$OPTION_NO_PATCHES" != "yes" ]; then
    177     PATCHES_DIR="$PROGDIR/toolchain-patches"
    178     if [ -d "$PATCHES_DIR" ] ; then
    179         dump "Patching toolchain sources"
    180         run $PROGDIR/patch-sources.sh $FLAGS $TMPDIR $PATCHES_DIR
    181         if [ $? != 0 ] ; then
    182             dump "ERROR: Could not patch sources."
    183             exit 1
    184         fi
    185     fi
    186 fi
    187 
    188 
    189 # We only keep one version of gcc and binutils
    190 
    191 # we clearly don't need this
    192 log "getting rid of obsolete sources: gcc-4.2.1 gcc-4.3.1 gcc-4.4.0 gdb-6.8 binutils-2.17"
    193 rm -rf $TMPDIR/gcc/gcc-4.2.1
    194 rm -rf $TMPDIR/gcc/gcc-4.3.1
    195 rm -rf $TMPDIR/gcc/gcc-4.4.0
    196 rm -rf $TMPDIR/gcc/gdb-6.8
    197 rm -rf $TMPDIR/binutils/binutils-2.17
    198 
    199 # Check out binutils 2.21 from master
    200 if [ ! -d "$TMPDIR/binutils/binutils-2.21" ] ; then
    201     dump "Get binutils 2.21 from https://android.googlesource.com/toolchain/binutils.git"
    202     rm -rf new
    203     mkdir new
    204     cd new
    205     run git clone https://android.googlesource.com/toolchain/binutils.git binutils
    206     cd binutils
    207     # Find out the version at 2012/3/29
    208     MYDATE=2012-03-29
    209     MYREVISION=`git rev-list -n 1 --until="$MYDATE" HEAD`
    210     dump "Using sources for date '$MYDATE': revision $MYREVISION"
    211     run git checkout $MYREVISION
    212     fail_panic "Could not checkout $MYREVISION ?"
    213     cd ..
    214     mv binutils/binutils-2.21 $TMPDIR/binutils
    215     cd ..
    216     rm -rf new
    217 fi
    218 
    219 # remove all info files from the toolchain sources
    220 # they create countless little problems during the build
    221 # if you don't have exactly the configuration expected by
    222 # the scripts.
    223 #
    224 find $TMPDIR -type f -a -name "*.info" ! -name sysroff.info -print0 | xargs -0 rm -f
    225 
    226 if [ $OPTION_PACKAGE = "yes" ] ; then
    227     # create the package
    228     PACKAGE=/tmp/ndk-$USER/$PKGNAME.tar.bz2
    229     dump "Creating package archive $PACKAGE"
    230     pack_archive "$PACKAGE" "$TMPDIR" "."
    231     fail_panic "Could not package toolchain source archive ?. See $TMPLOG"
    232     dump "Toolchain sources downloaded and packaged succesfully at $PACKAGE"
    233 else
    234     # copy sources to <src-dir>
    235     SRC_DIR=`cd $SRC_DIR && pwd`
    236     rm -rf $SRC_DIR && mkdir -p $SRC_DIR
    237     fail_panic "Could not create target source directory: $SRC_DIR"
    238     copy_directory "$TMPDIR" "$SRC_DIR"
    239     cp $SOURCES_LIST $SRC_DIR/SOURCES
    240     fail_panic "Could not copy downloaded sources to: $SRC_DIR"
    241     dump "Toolchain sources downloaded and copied to $SRC_DIR"
    242 fi
    243 
    244 dump "Cleaning up..."
    245 rm -rf $TMPDIR
    246 rm -f $TMPLOG
    247 dump "Done."
    248