• 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.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
24PROGDIR=`dirname $0`
25PROGDIR=`cd $PROGDIR && pwd`
26
27# the default branch to use
28BRANCH=master
29register_var_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_BASE=https://android.googlesource.com/toolchain
46register_var_option "--git-base=<git-uri>" OPTION_GIT_BASE "Use specific git repository base"
47
48OPTION_GIT_REFERENCE=
49register_var_option "--git-reference=<path>" OPTION_GIT_REFERENCE "Use local git reference"
50
51OPTION_PACKAGE=no
52register_var_option "--package" OPTION_PACKAGE "Create source package in /tmp/ndk-$USER"
53
54OPTION_NO_PATCHES=no
55register_var_option "--no-patches" OPTION_NO_PATCHES "Do not patch sources"
56
57LLVM_VERSION_LIST=$DEFAULT_LLVM_VERSION_LIST
58register_var_option "--llvm-ver-list=<vers>" LLVM_VERSION_LIST "List of LLVM release versions"
59
60LLVM_URL=$DEFAULT_LLVM_URL
61register_var_option "--llvm-url=<url>" LLVM_URL "URL to download LLVM tar archive"
62
63PROGRAM_PARAMETERS="<src-dir>"
64PROGRAM_DESCRIPTION=\
65"Download the NDK toolchain sources from android.googlesource.com into <src-dir>.
66You will need to run this script before being able to rebuilt the NDK toolchain
67binaries from scratch with build/tools/build-gcc.sh."
68
69if [ -n "$TOOLCHAIN_GIT_DATE" ] ; then
70  PROGRAM_DESCRIPTION="$PROGRAM_DESCRIPTION\
71
72
73By default, this script will download sources from android.googlesource.com that
74correspond to the date of $TOOLCHAIN_GIT_DATE. If you want to use the tip of
75tree, use '--git-date=now' instead.
76
77If you don't want to use the official servers, use --git-base=<path> to
78download the sources from another set of git repostories.
79
80You can also speed-up the download if you maintain a local copy of the
81toolchain repositories on your machine. Use --git-reference=<path> to
82specify the base path that contains all copies, and its subdirectories will
83be used as git clone shared references.
84"
85
86fi
87
88extract_parameters "$@"
89
90# Check that 'git' works
91$GITCMD --version > /dev/null 2>&1
92if [ $? != 0 ] ; then
93    echo "The git tool doesn't seem to work. Please check $GITCMD"
94    exit 1
95fi
96log "Git seems to work ok."
97
98SRC_DIR="$PARAMETERS"
99if [ -z "$SRC_DIR" -a "$OPTION_PACKAGE" = no ] ; then
100    echo "ERROR: You need to provide a <src-dir> parameter or use the --package option!"
101    exit 1
102fi
103
104if [ -n "$SRC_DIR" ] ; then
105    mkdir -p $SRC_DIR
106    SRC_DIR=`cd $SRC_DIR && pwd`
107    log "Using target source directory: $SRC_DIR"
108fi
109
110# Normalize the parameters
111LLVM_VERSION_LIST=$(commas_to_spaces $LLVM_VERSION_LIST)
112LLVM_URL=${LLVM_URL%"/"}
113
114# Create temp directory where everything will be copied first
115#
116PKGNAME=android-ndk-toolchain-$RELEASE
117TMPDIR=/tmp/ndk-$USER/$PKGNAME
118log "Creating temporary directory $TMPDIR"
119rm -rf $TMPDIR && mkdir $TMPDIR
120fail_panic "Could not create temporary directory: $TMPDIR"
121
122# prefix used for all clone operations
123GITPREFIX="$OPTION_GIT_BASE"
124dump "Using git clone prefix: $GITPREFIX"
125
126GITREFERENCE=
127if [ -n "$OPTION_GIT_REFERENCE" ] ; then
128    GITREFERENCE=$OPTION_GIT_REFERENCE
129    if [ ! -d "$GITREFERENCE" -o ! -d "$GITREFERENCE/build" ]; then
130        echo "ERROR: Invalid reference repository directory path: $GITREFERENCE"
131        exit 1
132    fi
133    dump "Using git clone reference: $GITREFERENCE"
134fi
135
136# Clone a given toolchain git repository
137# $1: repository name, relative to $GITPREFIX (e.g. 'gcc')
138#
139toolchain_clone ()
140{
141    local GITFLAGS
142    GITFLAGS="--no-checkout"
143    if [ "$GITREFERENCE" ]; then
144        GITFLAGS=$GITFLAGS" --shared --reference $GITREFERENCE/$1"
145    fi
146    dump "Cloning git repository for toolchain/$1"
147    if [ -d "$GITPREFIX/$1" ]; then
148        run ln -s "$GITPREFIX/$1" $CLONE_DIR/$1.git
149    else
150        log "cloning $GITPREFIX/$1.git"
151        (cd $CLONE_DIR && run git clone $GITFLAGS $GITPREFIX/$1.git)
152    fi
153    fail_panic "Could not clone $GITPREFIX/$1.git ?"
154}
155
156# Checkout sources from a git clone created with toolchain_clone
157# $1: repository/clone name (e.g. 'gcc')
158# $2: sub-path to extract, relative to clone top-level (e.g. 'gcc-4.4.3')
159#
160toolchain_checkout ()
161{
162    local NAME=$1
163    shift
164    local GITOPTS="--git-dir=$CLONE_DIR/$NAME/.git"
165    log "Checking out $BRANCH branch of $NAME.git: $@"
166    local REVISION=origin/$BRANCH
167    if [ -n "$GIT_DATE" ] ; then
168        REVISION=`git $GITOPTS rev-list -n 1 --until="$GIT_DATE" $REVISION`
169    fi
170    (mkdir -p $TMPDIR/$NAME && cd $TMPDIR/$NAME && run git $GITOPTS checkout $REVISION "$@")
171    fail_panic "Could not checkout $NAME / $@ ?"
172    (printf "%-32s " "toolchain/$NAME.git"; git $GITOPTS log -1 --format=oneline $REVISION) >> $SOURCES_LIST
173}
174
175# Download and extract LLVM/Clang source from $LLVM_URL
176# $1: LLVM/Clang release version
177#
178llvm_checkout () {
179    local VER=$1
180
181    # Determine the tar archive name and directory name by release version
182    if [ $VER = "2.6" ]; then
183        local LLVM_NAME=llvm-$VER
184        local LLVM_TAR=llvm-$VER.tar.gz
185        local CLANG_NAME=clang-$VER
186        local CLANG_TAR=clang-$VER.tar.gz
187    elif [ $VER = "2.7" -o $VER = "2.8" -o $VER = "2.9" ]; then
188        local LLVM_NAME=llvm-$VER
189        local LLVM_TAR=llvm-$VER.tgz
190        local CLANG_NAME=clang-$VER
191        local CLANG_TAR=clang-$VER.tgz
192    elif [ $VER = "3.0" ]; then
193        local LLVM_NAME=llvm-$VER.src
194        local LLVM_TAR=llvm-$VER.tar.gz
195        local CLANG_NAME=clang-$VER.src
196        local CLANG_TAR=clang-$VER.tar.gz
197    else
198        # Use the latest scheme by default (LLVM 3.1)
199        local LLVM_NAME=llvm-$VER.src
200        local LLVM_TAR=llvm-$VER.src.tar.gz
201        local CLANG_NAME=clang-$VER.src
202        local CLANG_TAR=clang-$VER.src.tar.gz
203    fi
204
205    local URL_PREFIX=$LLVM_URL/$VER
206    local OUT_DIR=llvm-$VER
207
208    # Create "llvm" subdirectory under $SRC_DIR
209    LLVM_DIR=$TMPDIR/llvm
210    mkdir -p $LLVM_DIR
211    fail_panic "Could not create $LLVM_DIR as directory"
212
213    # Cleanup existing tar archives or output directory
214    (cd $LLVM_DIR && rm -rf $LLVM_TAR $CLANG_TAR $OUT_DIR || true)
215
216    # Download the source code and extract them
217    dump "Downloading $URL_PREFIX/$LLVM_TAR"
218    (cd $LLVM_DIR && run curl -S -O $URL_PREFIX/$LLVM_TAR && tar xzf $LLVM_TAR)
219    fail_panic "Could not download and extract llvm source code"
220
221    dump "Downloading $URL_PREFIX/$CLANG_TAR"
222    (cd $LLVM_DIR && \
223        run curl -S -O $URL_PREFIX/$CLANG_TAR && tar xzf $CLANG_TAR)
224    fail_panic "Could not download and extract clang source code"
225
226    # Rename the extracted directory
227    if [ $LLVM_NAME != $OUT_DIR ]; then
228        (cd $LLVM_DIR && mv $LLVM_NAME $OUT_DIR)
229        fail_panic "Could not rename $LLVM_NAME to $OUT_DIR"
230    fi
231
232    (cd $LLVM_DIR && mv $CLANG_NAME $OUT_DIR/tools/clang)
233    fail_panic "Could not rename $CLANG_NAME to $OUT_DIR/tools/clang"
234
235    # Cleanup the tar archive
236    (cd $LLVM_DIR && rm $LLVM_TAR $CLANG_TAR || true)
237}
238
239cd $TMPDIR
240
241CLONE_DIR=$TMPDIR/git
242mkdir -p $CLONE_DIR
243
244SOURCES_LIST=$(pwd)/SOURCES
245rm -f $SOURCES_LIST && touch $SOURCES_LIST
246
247toolchain_clone build
248
249toolchain_clone gmp
250toolchain_clone mpfr
251toolchain_clone mpc
252toolchain_clone binutils
253toolchain_clone gcc
254toolchain_clone gdb
255toolchain_clone expat
256
257
258toolchain_checkout build .
259toolchain_checkout gmp  .
260toolchain_checkout mpfr .
261toolchain_checkout mpc  .
262toolchain_checkout expat .
263toolchain_checkout binutils binutils-2.19 binutils-2.21
264toolchain_checkout gcc gcc-4.4.3 gcc-4.6
265toolchain_checkout gdb gdb-6.6 gdb-7.1.x gdb-7.3.x
266
267for LLVM_VERSION in $LLVM_VERSION_LIST; do
268    llvm_checkout $LLVM_VERSION
269done
270
271PYVERSION=2.7.3
272PYVERSION_FOLDER=$(echo ${PYVERSION} | sed 's/\([0-9\.]*\).*/\1/')
273dump "Downloading http://www.python.org/ftp/python/${PYVERSION_FOLDER}/Python-${PYVERSION}.tar.bz2"
274(mkdir -p $TMPDIR/python && cd $TMPDIR/python && run curl -S -O http://www.python.org/ftp/python/${PYVERSION_FOLDER}/Python-${PYVERSION}.tar.bz2 && tar -xjf Python-${PYVERSION}.tar.bz2)
275
276# Patch the toolchain sources
277if [ "$OPTION_NO_PATCHES" != "yes" ]; then
278    PATCHES_DIR="$PROGDIR/toolchain-patches"
279    if [ -d "$PATCHES_DIR" ] ; then
280        dump "Patching toolchain sources"
281        run $PROGDIR/patch-sources.sh $FLAGS $TMPDIR $PATCHES_DIR
282        if [ $? != 0 ] ; then
283            dump "ERROR: Could not patch sources."
284            exit 1
285        fi
286    fi
287fi
288
289# remove all info files from the toolchain sources
290# they create countless little problems during the build
291# if you don't have exactly the configuration expected by
292# the scripts.
293#
294find $TMPDIR -type f -a -name "*.info" ! -name sysroff.info -print0 | xargs -0 rm -f
295
296if [ $OPTION_PACKAGE = "yes" ] ; then
297    # create the package
298    PACKAGE=/tmp/ndk-$USER/$PKGNAME.tar.bz2
299    dump "Creating package archive $PACKAGE"
300    pack_archive "$PACKAGE" "$TMPDIR" "."
301    fail_panic "Could not package toolchain source archive ?. See $TMPLOG"
302    dump "Toolchain sources downloaded and packaged succesfully at $PACKAGE"
303else
304    # copy sources to <src-dir>
305    SRC_DIR=`cd $SRC_DIR && pwd`
306    rm -rf $SRC_DIR && mkdir -p $SRC_DIR
307    fail_panic "Could not create target source directory: $SRC_DIR"
308    copy_directory "$TMPDIR" "$SRC_DIR"
309    cp $SOURCES_LIST $SRC_DIR/SOURCES
310    fail_panic "Could not copy downloaded sources to: $SRC_DIR"
311    dump "Toolchain sources downloaded and copied to $SRC_DIR"
312fi
313
314dump "Cleaning up..."
315rm -rf $TMPDIR
316rm -f $TMPLOG
317dump "Done."
318