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