1 | #!/bin/bash
|
---|
2 |
|
---|
3 | #
|
---|
4 | # Copyright (c) 2009 Martin Decky
|
---|
5 | # All rights reserved.
|
---|
6 | #
|
---|
7 | # Redistribution and use in source and binary forms, with or without
|
---|
8 | # modification, are permitted provided that the following conditions
|
---|
9 | # are met:
|
---|
10 | #
|
---|
11 | # - Redistributions of source code must retain the above copyright
|
---|
12 | # notice, this list of conditions and the following disclaimer.
|
---|
13 | # - Redistributions in binary form must reproduce the above copyright
|
---|
14 | # notice, this list of conditions and the following disclaimer in the
|
---|
15 | # documentation and/or other materials provided with the distribution.
|
---|
16 | # - The name of the author may not be used to endorse or promote products
|
---|
17 | # derived from this software without specific prior written permission.
|
---|
18 | #
|
---|
19 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
20 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
21 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
22 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
23 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
24 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
25 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
26 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
28 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
29 | #
|
---|
30 |
|
---|
31 | check_error() {
|
---|
32 | if [ "$1" -ne "0" ]; then
|
---|
33 | echo
|
---|
34 | echo "Script failed: $2"
|
---|
35 | exit 1
|
---|
36 | fi
|
---|
37 | }
|
---|
38 |
|
---|
39 | check_md5() {
|
---|
40 | FILE="$1"
|
---|
41 | SUM="$2"
|
---|
42 |
|
---|
43 | COMPUTED="`md5sum "${FILE}" | cut -d' ' -f1`"
|
---|
44 | if [ "${SUM}" != "${COMPUTED}" ] ; then
|
---|
45 | echo
|
---|
46 | echo "Checksum of ${FILE} does not match."
|
---|
47 | exit 2
|
---|
48 | fi
|
---|
49 | }
|
---|
50 |
|
---|
51 | show_usage() {
|
---|
52 | echo "Cross-compiler toolchain build script"
|
---|
53 | echo
|
---|
54 | echo "Syntax:"
|
---|
55 | echo " $0 <platform>"
|
---|
56 | echo
|
---|
57 | echo "Possible target platforms are:"
|
---|
58 | echo " amd64 AMD64 (x86-64, x64)"
|
---|
59 | echo " arm32 ARM"
|
---|
60 | echo " ia32 IA-32 (x86, i386)"
|
---|
61 | echo " ia64 IA-64 (Itanium)"
|
---|
62 | echo " mips32 MIPS little-endian"
|
---|
63 | echo " mips32eb MIPS big-endian"
|
---|
64 | echo " ppc32 32-bit PowerPC"
|
---|
65 | echo " ppc64 64-bit PowerPC"
|
---|
66 | echo " sparc64 SPARC V9"
|
---|
67 | echo " all build all targets"
|
---|
68 | echo
|
---|
69 |
|
---|
70 | exit 3
|
---|
71 | }
|
---|
72 |
|
---|
73 | download_check() {
|
---|
74 | SOURCE="$1"
|
---|
75 | FILE="$2"
|
---|
76 | CHECKSUM="$3"
|
---|
77 |
|
---|
78 | if [ ! -f "${FILE}" ]; then
|
---|
79 | wget -c "${SOURCE}${FILE}"
|
---|
80 | check_error $? "Error downloading ${FILE}."
|
---|
81 | fi
|
---|
82 |
|
---|
83 | check_md5 "${FILE}" "${CHECKSUM}"
|
---|
84 | }
|
---|
85 |
|
---|
86 | cleanup_dir() {
|
---|
87 | DIR="$1"
|
---|
88 |
|
---|
89 | if [ -d "${DIR}" ]; then
|
---|
90 | echo " >>> Removing ${DIR}"
|
---|
91 | rm -fr "${DIR}"
|
---|
92 | fi
|
---|
93 | }
|
---|
94 |
|
---|
95 | create_dir() {
|
---|
96 | DIR="$1"
|
---|
97 | DESC="$2"
|
---|
98 |
|
---|
99 | echo ">>> Creating ${DESC}"
|
---|
100 |
|
---|
101 | mkdir -p "${DIR}"
|
---|
102 | test -d "${DIR}"
|
---|
103 | check_error $? "Unable to create ${DIR}."
|
---|
104 | }
|
---|
105 |
|
---|
106 | unpack_tarball() {
|
---|
107 | FILE="$1"
|
---|
108 | DESC="$2"
|
---|
109 |
|
---|
110 | echo " >>> ${DESC}"
|
---|
111 |
|
---|
112 | tar -xjf "${FILE}"
|
---|
113 | check_error $? "Error unpacking ${DESC}."
|
---|
114 | }
|
---|
115 |
|
---|
116 | patch_binutils() {
|
---|
117 | PLATFORM="$1"
|
---|
118 |
|
---|
119 | if [ "${PLATFORM}" == "arm32" ] ; then
|
---|
120 | patch -p1 <<EOF
|
---|
121 | diff -Naur binutils-2.20.orig/gas/config/tc-arm.c binutils-2.20/gas/config/tc-arm.c
|
---|
122 | --- binutils-2.20.orig/gas/config/tc-arm.c 2009-08-30 00:10:59.000000000 +0200
|
---|
123 | +++ binutils-2.20/gas/config/tc-arm.c 2009-11-02 14:25:11.000000000 +0100
|
---|
124 | @@ -2485,8 +2485,9 @@
|
---|
125 | know (frag->tc_frag_data.first_map == NULL);
|
---|
126 | frag->tc_frag_data.first_map = symbolP;
|
---|
127 | }
|
---|
128 | - if (frag->tc_frag_data.last_map != NULL)
|
---|
129 | + if (frag->tc_frag_data.last_map != NULL) {
|
---|
130 | know (S_GET_VALUE (frag->tc_frag_data.last_map) < S_GET_VALUE (symbolP));
|
---|
131 | + }
|
---|
132 | frag->tc_frag_data.last_map = symbolP;
|
---|
133 | }
|
---|
134 | EOF
|
---|
135 | check_error $? "Error patching binutils"
|
---|
136 | fi
|
---|
137 | }
|
---|
138 |
|
---|
139 | build_target() {
|
---|
140 | PLATFORM="$1"
|
---|
141 | TARGET="$2"
|
---|
142 |
|
---|
143 | BINUTILS_VERSION="2.20"
|
---|
144 | GCC_VERSION="4.5.0"
|
---|
145 |
|
---|
146 | BINUTILS="binutils-${BINUTILS_VERSION}.tar.bz2"
|
---|
147 | GCC_CORE="gcc-core-${GCC_VERSION}.tar.bz2"
|
---|
148 | GCC_OBJC="gcc-objc-${GCC_VERSION}.tar.bz2"
|
---|
149 | GCC_CPP="gcc-g++-${GCC_VERSION}.tar.bz2"
|
---|
150 |
|
---|
151 | BINUTILS_SOURCE="ftp://ftp.gnu.org/gnu/binutils/"
|
---|
152 | GCC_SOURCE="ftp://ftp.gnu.org/gnu/gcc/gcc-${GCC_VERSION}/"
|
---|
153 |
|
---|
154 | WORKDIR="`pwd`"
|
---|
155 | BINUTILSDIR="${WORKDIR}/binutils-${BINUTILS_VERSION}"
|
---|
156 | GCCDIR="${WORKDIR}/gcc-${GCC_VERSION}"
|
---|
157 | OBJDIR="${WORKDIR}/gcc-obj"
|
---|
158 |
|
---|
159 | if [ -z "${CROSS_PREFIX}" ] ; then
|
---|
160 | CROSS_PREFIX="/usr/local"
|
---|
161 | fi
|
---|
162 |
|
---|
163 | PREFIX="${CROSS_PREFIX}/${PLATFORM}"
|
---|
164 |
|
---|
165 | echo ">>> Downloading tarballs"
|
---|
166 | download_check "${BINUTILS_SOURCE}" "${BINUTILS}" "ee2d3e996e9a2d669808713360fa96f8"
|
---|
167 | download_check "${GCC_SOURCE}" "${GCC_CORE}" "58eda33c3184303628f91c42a7ab15b5"
|
---|
168 | download_check "${GCC_SOURCE}" "${GCC_OBJC}" "8d8c01b6631b020cc6c167860fde2398"
|
---|
169 | download_check "${GCC_SOURCE}" "${GCC_CPP}" "5ab93605af40def4844eda09ca769c2d"
|
---|
170 |
|
---|
171 | echo ">>> Removing previous content"
|
---|
172 | cleanup_dir "${PREFIX}"
|
---|
173 | cleanup_dir "${OBJDIR}"
|
---|
174 | cleanup_dir "${BINUTILSDIR}"
|
---|
175 | cleanup_dir "${GCCDIR}"
|
---|
176 |
|
---|
177 | create_dir "${PREFIX}" "destination directory"
|
---|
178 | create_dir "${OBJDIR}" "GCC object directory"
|
---|
179 |
|
---|
180 | echo ">>> Unpacking tarballs"
|
---|
181 | unpack_tarball "${BINUTILS}" "binutils"
|
---|
182 | unpack_tarball "${GCC_CORE}" "GCC Core"
|
---|
183 | unpack_tarball "${GCC_OBJC}" "Objective C"
|
---|
184 | unpack_tarball "${GCC_CPP}" "C++"
|
---|
185 |
|
---|
186 | echo ">>> Compiling and installing binutils"
|
---|
187 | cd "${BINUTILSDIR}"
|
---|
188 | check_error $? "Change directory failed."
|
---|
189 | patch_binutils "${PLATFORM}"
|
---|
190 | ./configure "--target=${TARGET}" "--prefix=${PREFIX}" "--program-prefix=${TARGET}-" "--disable-nls"
|
---|
191 | check_error $? "Error configuring binutils."
|
---|
192 | make all install
|
---|
193 | check_error $? "Error compiling/installing binutils."
|
---|
194 |
|
---|
195 | echo ">>> Compiling and installing GCC"
|
---|
196 | cd "${OBJDIR}"
|
---|
197 | check_error $? "Change directory failed."
|
---|
198 | "${GCCDIR}/configure" "--target=${TARGET}" "--prefix=${PREFIX}" "--program-prefix=${TARGET}-" --with-gnu-as --with-gnu-ld --disable-nls --disable-threads --enable-languages=c,objc,c++,obj-c++ --disable-multilib --disable-libgcj --without-headers --disable-shared --enable-lto
|
---|
199 | check_error $? "Error configuring GCC."
|
---|
200 | PATH="${PATH}:${PREFIX}/bin" make all-gcc install-gcc
|
---|
201 | check_error $? "Error compiling/installing GCC."
|
---|
202 |
|
---|
203 | cd "${WORKDIR}"
|
---|
204 | check_error $? "Change directory failed."
|
---|
205 |
|
---|
206 | echo ">>> Cleaning up"
|
---|
207 | cleanup_dir "${OBJDIR}"
|
---|
208 | cleanup_dir "${BINUTILSDIR}"
|
---|
209 | cleanup_dir "${GCCDIR}"
|
---|
210 |
|
---|
211 | echo
|
---|
212 | echo ">>> Cross-compiler for ${TARGET} installed."
|
---|
213 | }
|
---|
214 |
|
---|
215 | if [ "$#" -lt "1" ]; then
|
---|
216 | show_usage
|
---|
217 | fi
|
---|
218 |
|
---|
219 | case "$1" in
|
---|
220 | "amd64")
|
---|
221 | build_target "amd64" "amd64-linux-gnu"
|
---|
222 | ;;
|
---|
223 | "arm32")
|
---|
224 | build_target "arm32" "arm-linux-gnu"
|
---|
225 | ;;
|
---|
226 | "ia32")
|
---|
227 | build_target "ia32" "i686-pc-linux-gnu"
|
---|
228 | ;;
|
---|
229 | "ia64")
|
---|
230 | build_target "ia64" "ia64-pc-linux-gnu"
|
---|
231 | ;;
|
---|
232 | "ia64")
|
---|
233 | build_target "ia64" "ia64-pc-linux-gnu"
|
---|
234 | ;;
|
---|
235 | "mips32")
|
---|
236 | build_target "mips32" "mipsel-linux-gnu"
|
---|
237 | ;;
|
---|
238 | "mips32eb")
|
---|
239 | build_target "mips32eb" "mips-linux-gnu"
|
---|
240 | ;;
|
---|
241 | "ppc32")
|
---|
242 | build_target "ppc32" "ppc-linux-gnu"
|
---|
243 | ;;
|
---|
244 | "ppc64")
|
---|
245 | build_target "ppc64" "ppc64-linux-gnu"
|
---|
246 | ;;
|
---|
247 | "sparc64")
|
---|
248 | build_target "sparc64" "sparc64-linux-gnu"
|
---|
249 | ;;
|
---|
250 | "all")
|
---|
251 | build_target "amd64" "amd64-linux-gnu"
|
---|
252 | build_target "arm32" "arm-linux-gnu"
|
---|
253 | build_target "ia32" "i686-pc-linux-gnu"
|
---|
254 | build_target "ia64" "ia64-pc-linux-gnu"
|
---|
255 | build_target "ia64" "ia64-pc-linux-gnu"
|
---|
256 | build_target "mips32" "mipsel-linux-gnu"
|
---|
257 | build_target "mips32eb" "mips-linux-gnu"
|
---|
258 | build_target "ppc32" "ppc-linux-gnu"
|
---|
259 | build_target "ppc64" "ppc64-linux-gnu"
|
---|
260 | build_target "sparc64" "sparc64-linux-gnu"
|
---|
261 | ;;
|
---|
262 | *)
|
---|
263 | show_usage
|
---|
264 | ;;
|
---|
265 | esac
|
---|