1#!/bin/sh
2#
3# Copyright (C) 2013 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# Rebuild the host GCC toolchain binaries from sources.
18#
19# NOTE: this script does not rebuild gdb, see build-host-gdb.sh for this.
20#
21
22# include common function and variable definitions
23. `dirname $0`/prebuilt-common.sh
24
25PROGRAM_PARAMETERS=""
26PROGRAM_DESCRIPTION="\
27This program is used to deploy mclinker (ld.mcld) to GCC directories.
28Although ld.mcld depends on lots of LLVM modules and is built in
29build-llvm.sh to reduce long LLVM compilation time, it can be used as
30a drop-in replacement for ld.bfd and ld.gold in GCC.
31
32Running after completion of both build-llvm.sh and build-[host-]gcc.sh,
33this script copy toolchains/llvm-$DEFAULT_LLVM_VERSION/prebuilt/$SYSTEM/bin/ld.mcld[.exe]
34to be sibling of ld in all GCC and LLVM directories with same HOST_OS and bitness,
35ie. {linux, darwin, windows} x {64, 32}
36
37If --systems isn't specified, this script discovers all ld.mcld[.exe] in
38toolchains/llvm-$DEFAULT_LLVM_VERSION
39
40Note that one copy of ld.mcld serves all GCC {4.9, 4.8} x {arm, x86, mips} and
41LLVM {3.4, 3.5}.
42
43GCC passes -m flag for ld.mcld to figure out the right target.
44"
45NDK_DIR=
46register_var_option "--ndk-dir=<path>" NDK_DIR "NDK installation directory"
47
48PACKAGE_DIR=
49register_var_option "--package-dir=<path>" PACKAGE_DIR "Create archive tarball in specific directory"
50
51SYSTEMS=
52register_var_option "--systems=<list>" SYSTEMS "List of host systems to deply ld.mcld for"
53
54extract_parameters "$@"
55
56if [ -z "$NDK_DIR" ] ; then
57    NDK_DIR=$ANDROID_NDK_ROOT
58    log "Auto-config: --ndk-dir=$NDK_DIR"
59else
60    if [ ! -d "$NDK_DIR" ] ; then
61        echo "ERROR: NDK directory does not exists: $NDK_DIR"
62        exit 1
63    fi
64fi
65
66if [ "$PACKAGE_DIR" ]; then
67    mkdir -p "$PACKAGE_DIR"
68    fail_panic "Could not create package directory: $PACKAGE_DIR"
69fi
70
71cd $NDK_DIR
72
73if [ -z "$SYSTEMS" ]; then
74    # find all ld.mcld
75    ALL_MCLDS=`find toolchains/llvm-$DEFAULT_LLVM_VERSION -name "ld.mcld*"`
76
77    for MCLD in $ALL_MCLDS; do
78        # compute SYSTEM of this ld.mcld
79        SYSTEM=${MCLD%%/bin/*}
80        SYSTEM=${SYSTEM##*prebuilt/}
81        SYSTEMS=$SYSTEMS" $SYSTEM"
82    done
83fi
84
85for SYSTEM in $SYSTEMS; do
86    HOST_EXE=
87    if [ "$SYSTEM" != "${SYSTEM%%windows*}" ] ; then
88        HOST_EXE=.exe
89    fi
90
91    MCLD=toolchains/llvm-$DEFAULT_LLVM_VERSION/prebuilt/$SYSTEM/bin/ld.mcld$HOST_EXE
92    test -f "$MCLD" || fail_panic "Could not find $MCLD"
93
94    ALL_LD_MCLDS=
95
96    # find all GNU ld with the same SYSTEM
97    ALL_LDS=`find toolchains \( -name "*-ld" -o -name "ld" -o -name "*-ld.exe" -o -name "ld.exe" \) | egrep "/arm|/x86|/mips|/aarch64" | grep $SYSTEM/`
98    for LD in $ALL_LDS; do
99        LD_NOEXE=${LD%%.exe}
100        LD_MCLD=${LD_NOEXE}.mcld$HOST_EXE
101        run rm -f "$LD_MCLD"
102        if [ "$LD_NOEXE" != "${LD_NOEXE%%/ld}" ] ; then
103          # ld in $ABI/bin/ld
104            run ln -s "../../../../../../$MCLD" "$LD_MCLD"
105        else
106          # ld in bin/$ABI-ld
107            run ln -s "../../../../../$MCLD" "$LD_MCLD"
108        fi
109        ALL_LD_MCLDS=$ALL_LD_MCLDS" $LD_MCLD"
110    done
111
112    # find all llvm-* which isn't llvm-$DEFAULT_LLVM_VERSION
113    for LLVM in $DEFAULT_LLVM_VERSION_LIST; do
114        if [ "$LLVM" != "$DEFAULT_LLVM_VERSION" ]; then
115            LD_MCLD=toolchains/llvm-$LLVM/prebuilt/$SYSTEM/bin/ld.mcld$HOST_EXE
116            run rm -f "$LD_MCLD"
117            run ln -s "../../../../../$MCLD" "$LD_MCLD"
118            ALL_LD_MCLDS=$ALL_LD_MCLDS" $LD_MCLD"
119	fi
120    done
121
122    # package
123    if [ "$PACKAGE_DIR" ]; then
124        ARCHIVE="ld.mcld-$SYSTEM.tar.bz2"
125        #echo $ARCHIVE
126        echo  "Packaging $ARCHIVE"
127        pack_archive "$PACKAGE_DIR/$ARCHIVE" "$NDK_DIR" $ALL_LD_MCLDS
128    fi
129done
130
131dump "Done."
132