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 | GMP_MAIN=<<EOF
|
---|
32 | #define GCC_GMP_VERSION_NUM(a, b, c) \
|
---|
33 | (((a) << 16L) | ((b) << 8) | (c))
|
---|
34 |
|
---|
35 | #define GCC_GMP_VERSION \
|
---|
36 | GCC_GMP_VERSION_NUM(__GNU_MP_VERSION, __GNU_MP_VERSION_MINOR, __GNU_MP_VERSION_PATCHLEVEL)
|
---|
37 |
|
---|
38 | #if GCC_GMP_VERSION < GCC_GMP_VERSION_NUM(4, 3, 2)
|
---|
39 | choke me
|
---|
40 | #endif
|
---|
41 | EOF
|
---|
42 |
|
---|
43 | MPFR_MAIN=<<EOF
|
---|
44 | #if MPFR_VERSION < MPFR_VERSION_NUM(2, 4, 2)
|
---|
45 | choke me
|
---|
46 | #endif
|
---|
47 | EOF
|
---|
48 |
|
---|
49 | MPC_MAIN=<<EOF
|
---|
50 | #if MPC_VERSION < MPC_VERSION_NUM(0, 8, 1)
|
---|
51 | choke me
|
---|
52 | #endif
|
---|
53 | EOF
|
---|
54 |
|
---|
55 | ISL_MAIN=<<EOF
|
---|
56 | isl_ctx_get_max_operations (isl_ctx_alloc ());
|
---|
57 | EOF
|
---|
58 |
|
---|
59 | BINUTILS_VERSION="2.25.1"
|
---|
60 | BINUTILS_RELEASE=""
|
---|
61 | GCC_VERSION="5.3.0"
|
---|
62 |
|
---|
63 | BASEDIR="`pwd`"
|
---|
64 | SRCDIR="$(readlink -f $(dirname "$0"))"
|
---|
65 | BINUTILS="riscv-binutils-${BINUTILS_VERSION}${BINUTILS_RELEASE}.tar.bz2"
|
---|
66 | GCC="riscv-gcc-${GCC_VERSION}.tar.bz2"
|
---|
67 |
|
---|
68 | REAL_INSTALL=true
|
---|
69 | USE_HELENOS_TARGET=false
|
---|
70 | INSTALL_DIR="${BASEDIR}/PKG"
|
---|
71 |
|
---|
72 | #
|
---|
73 | # Check if the library described in the argument
|
---|
74 | # exists and has acceptable version.
|
---|
75 | #
|
---|
76 | check_dependency() {
|
---|
77 | DEPENDENCY="$1"
|
---|
78 | HEADER="$2"
|
---|
79 | BODY="$3"
|
---|
80 |
|
---|
81 | FNAME="/tmp/conftest-$$"
|
---|
82 |
|
---|
83 | echo "#include ${HEADER}" > "${FNAME}.c"
|
---|
84 | echo >> "${FNAME}.c"
|
---|
85 | echo "int main()" >> "${FNAME}.c"
|
---|
86 | echo "{" >> "${FNAME}.c"
|
---|
87 | echo "${BODY}" >> "${FNAME}.c"
|
---|
88 | echo " return 0;" >> "${FNAME}.c"
|
---|
89 | echo "}" >> "${FNAME}.c"
|
---|
90 |
|
---|
91 | cc -c -o "${FNAME}.o" "${FNAME}.c" 2> "${FNAME}.log"
|
---|
92 | RC="$?"
|
---|
93 |
|
---|
94 | if [ "$RC" -ne "0" ] ; then
|
---|
95 | echo " ${DEPENDENCY} not found, too old or compiler error."
|
---|
96 | echo " Please recheck manually the source file \"${FNAME}.c\"."
|
---|
97 | echo " The compilation of the toolchain is probably going to fail,"
|
---|
98 | echo " you have been warned."
|
---|
99 | echo
|
---|
100 | echo " ===== Compiler output ====="
|
---|
101 | cat "${FNAME}.log"
|
---|
102 | echo " ==========================="
|
---|
103 | echo
|
---|
104 | else
|
---|
105 | echo " ${DEPENDENCY} found"
|
---|
106 | rm -f "${FNAME}.log" "${FNAME}.o" "${FNAME}.c"
|
---|
107 | fi
|
---|
108 | }
|
---|
109 |
|
---|
110 | check_dependecies() {
|
---|
111 | echo ">>> Basic dependency check"
|
---|
112 | check_dependency "GMP" "<gmp.h>" "${GMP_MAIN}"
|
---|
113 | check_dependency "MPFR" "<mpfr.h>" "${MPFR_MAIN}"
|
---|
114 | check_dependency "MPC" "<mpc.h>" "${MPC_MAIN}"
|
---|
115 | check_dependency "isl" "<isl/ctx.h>" "${ISL_MAIN}"
|
---|
116 | echo
|
---|
117 | }
|
---|
118 |
|
---|
119 | check_error() {
|
---|
120 | if [ "$1" -ne "0" ]; then
|
---|
121 | echo
|
---|
122 | echo "Script failed: $2"
|
---|
123 |
|
---|
124 | exit 1
|
---|
125 | fi
|
---|
126 | }
|
---|
127 |
|
---|
128 | check_md5() {
|
---|
129 | FILE="$1"
|
---|
130 | SUM="$2"
|
---|
131 |
|
---|
132 | COMPUTED="`md5sum "${FILE}" | cut -d' ' -f1`"
|
---|
133 | if [ "${SUM}" != "${COMPUTED}" ] ; then
|
---|
134 | echo
|
---|
135 | echo "Checksum of ${FILE} does not match."
|
---|
136 |
|
---|
137 | exit 2
|
---|
138 | fi
|
---|
139 | }
|
---|
140 |
|
---|
141 | change_title() {
|
---|
142 | echo -en "\e]0;$1\a"
|
---|
143 | }
|
---|
144 |
|
---|
145 | show_countdown() {
|
---|
146 | TM="$1"
|
---|
147 |
|
---|
148 | if [ "${TM}" -eq 0 ] ; then
|
---|
149 | echo
|
---|
150 | return 0
|
---|
151 | fi
|
---|
152 |
|
---|
153 | echo -n "${TM} "
|
---|
154 | change_title "${TM}"
|
---|
155 | sleep 1
|
---|
156 |
|
---|
157 | TM="`expr "${TM}" - 1`"
|
---|
158 | show_countdown "${TM}"
|
---|
159 | }
|
---|
160 |
|
---|
161 | show_dependencies() {
|
---|
162 | echo "IMPORTANT NOTICE:"
|
---|
163 | echo
|
---|
164 | echo "For a successful compilation and use of the cross-compiler"
|
---|
165 | echo "toolchain you need at least the following dependencies."
|
---|
166 | echo
|
---|
167 | echo "Please make sure that the dependencies are present in your"
|
---|
168 | echo "system. Otherwise the compilation process might fail after"
|
---|
169 | echo "a few seconds or minutes."
|
---|
170 | echo
|
---|
171 | echo " - SED, AWK, Flex, Bison, gzip, bzip2, Bourne Shell"
|
---|
172 | echo " - gettext, zlib, Texinfo, libelf, libgomp"
|
---|
173 | echo " - GNU Make, Coreutils, Sharutils, tar"
|
---|
174 | echo " - GNU Multiple Precision Library (GMP)"
|
---|
175 | echo " - MPFR"
|
---|
176 | echo " - MPC"
|
---|
177 | echo " - integer point manipulation library (isl)"
|
---|
178 | echo " - native C and C++ compiler, assembler and linker"
|
---|
179 | echo " - native C and C++ standard library with headers"
|
---|
180 | echo
|
---|
181 | }
|
---|
182 |
|
---|
183 | download_fetch() {
|
---|
184 | SOURCE="$1"
|
---|
185 | FILE="$2"
|
---|
186 | CHECKSUM="$3"
|
---|
187 |
|
---|
188 | if [ ! -f "${FILE}" ]; then
|
---|
189 | change_title "Downloading ${FILE}"
|
---|
190 | wget -c "${SOURCE}${FILE}"
|
---|
191 | check_error $? "Error downloading ${FILE}."
|
---|
192 | fi
|
---|
193 |
|
---|
194 | check_md5 "${FILE}" "${CHECKSUM}"
|
---|
195 | }
|
---|
196 |
|
---|
197 | source_check() {
|
---|
198 | FILE="$1"
|
---|
199 |
|
---|
200 | if [ ! -f "${FILE}" ]; then
|
---|
201 | echo
|
---|
202 | echo "File ${FILE} not found."
|
---|
203 |
|
---|
204 | exit 4
|
---|
205 | fi
|
---|
206 | }
|
---|
207 |
|
---|
208 | cleanup_dir() {
|
---|
209 | DIR="$1"
|
---|
210 |
|
---|
211 | if [ -d "${DIR}" ]; then
|
---|
212 | change_title "Removing ${DIR}"
|
---|
213 | echo " >>> Removing ${DIR}"
|
---|
214 | rm -fr "${DIR}"
|
---|
215 | fi
|
---|
216 | }
|
---|
217 |
|
---|
218 | create_dir() {
|
---|
219 | DIR="$1"
|
---|
220 | DESC="$2"
|
---|
221 |
|
---|
222 | change_title "Creating ${DESC}"
|
---|
223 | echo ">>> Creating ${DESC}"
|
---|
224 |
|
---|
225 | mkdir -p "${DIR}"
|
---|
226 | test -d "${DIR}"
|
---|
227 | check_error $? "Unable to create ${DIR}."
|
---|
228 | }
|
---|
229 |
|
---|
230 | check_dirs() {
|
---|
231 | OUTSIDE="$1"
|
---|
232 | BASE="$2"
|
---|
233 | ORIGINAL="`pwd`"
|
---|
234 |
|
---|
235 | cd "${OUTSIDE}"
|
---|
236 | check_error $? "Unable to change directory to ${OUTSIDE}."
|
---|
237 | ABS_OUTSIDE="`pwd`"
|
---|
238 |
|
---|
239 | cd "${BASE}"
|
---|
240 | check_error $? "Unable to change directory to ${BASE}."
|
---|
241 | ABS_BASE="`pwd`"
|
---|
242 |
|
---|
243 | cd "${ORIGINAL}"
|
---|
244 | check_error $? "Unable to change directory to ${ORIGINAL}."
|
---|
245 |
|
---|
246 | BASE_LEN="${#ABS_BASE}"
|
---|
247 | OUTSIDE_TRIM="${ABS_OUTSIDE:0:${BASE_LEN}}"
|
---|
248 |
|
---|
249 | if [ "${OUTSIDE_TRIM}" == "${ABS_BASE}" ] ; then
|
---|
250 | echo
|
---|
251 | echo "CROSS_PREFIX cannot reside within the working directory."
|
---|
252 |
|
---|
253 | exit 5
|
---|
254 | fi
|
---|
255 | }
|
---|
256 |
|
---|
257 | unpack_tarball() {
|
---|
258 | FILE="$1"
|
---|
259 | DESC="$2"
|
---|
260 |
|
---|
261 | change_title "Unpacking ${DESC}"
|
---|
262 | echo " >>> Unpacking ${DESC}"
|
---|
263 |
|
---|
264 | case "${FILE}" in
|
---|
265 | *.gz)
|
---|
266 | tar -xzf "${FILE}"
|
---|
267 | ;;
|
---|
268 | *.xz)
|
---|
269 | tar -xJf "${FILE}"
|
---|
270 | ;;
|
---|
271 | *.bz2)
|
---|
272 | tar -xjf "${FILE}"
|
---|
273 | ;;
|
---|
274 | *)
|
---|
275 | check_error 1 "Don't know how to unpack ${DESC}."
|
---|
276 | ;;
|
---|
277 | esac
|
---|
278 | check_error $? "Error unpacking ${DESC}."
|
---|
279 | }
|
---|
280 |
|
---|
281 | prepare() {
|
---|
282 | show_dependencies
|
---|
283 | check_dependecies
|
---|
284 | show_countdown 10
|
---|
285 |
|
---|
286 | BINUTILS_SOURCE="http://gist.decky.cz/"
|
---|
287 | GCC_SOURCE="http://gist.decky.cz/"
|
---|
288 |
|
---|
289 | download_fetch "${BINUTILS_SOURCE}" "${BINUTILS}" "62a8b269139e9625a23dc3174809edfa"
|
---|
290 | download_fetch "${GCC_SOURCE}" "${GCC}" "aaf5fa585a3328509c23ef6fbfba78fb"
|
---|
291 | }
|
---|
292 |
|
---|
293 | set_target_from_platform() {
|
---|
294 | case "$1" in
|
---|
295 | "riscv64")
|
---|
296 | LINUX_TARGET="riscv64-unknown-linux-gnu"
|
---|
297 | HELENOS_TARGET="riscv64-helenos"
|
---|
298 | ;;
|
---|
299 | *)
|
---|
300 | check_error 1 "No target known for $1."
|
---|
301 | ;;
|
---|
302 | esac
|
---|
303 | }
|
---|
304 |
|
---|
305 | build_target() {
|
---|
306 | PLATFORM="$1"
|
---|
307 | # This sets the *_TARGET variables
|
---|
308 | set_target_from_platform "$PLATFORM"
|
---|
309 | if $USE_HELENOS_TARGET; then
|
---|
310 | TARGET="$HELENOS_TARGET"
|
---|
311 | else
|
---|
312 | TARGET="$LINUX_TARGET"
|
---|
313 | fi
|
---|
314 |
|
---|
315 | WORKDIR="${BASEDIR}/${PLATFORM}"
|
---|
316 | BINUTILSDIR="${WORKDIR}/binutils-${BINUTILS_VERSION}"
|
---|
317 | GCCDIR="${WORKDIR}/gcc-${GCC_VERSION}"
|
---|
318 | OBJDIR="${WORKDIR}/gcc-obj"
|
---|
319 |
|
---|
320 | if [ -z "${CROSS_PREFIX}" ] ; then
|
---|
321 | CROSS_PREFIX="/usr/local/cross"
|
---|
322 | fi
|
---|
323 | if [ -z "${CROSS_HELENOS_PREFIX}" ] ; then
|
---|
324 | CROSS_HELENOS_PREFIX="/usr/local/cross-helenos"
|
---|
325 | fi
|
---|
326 |
|
---|
327 | if $USE_HELENOS_TARGET; then
|
---|
328 | PREFIX="${CROSS_HELENOS_PREFIX}/${PLATFORM}"
|
---|
329 | else
|
---|
330 | PREFIX="${CROSS_PREFIX}/${PLATFORM}"
|
---|
331 | fi
|
---|
332 |
|
---|
333 | echo ">>> Downloading tarballs"
|
---|
334 | source_check "${BASEDIR}/${BINUTILS}"
|
---|
335 | source_check "${BASEDIR}/${GCC}"
|
---|
336 |
|
---|
337 | echo ">>> Removing previous content"
|
---|
338 | $REAL_INSTALL && cleanup_dir "${PREFIX}"
|
---|
339 | cleanup_dir "${WORKDIR}"
|
---|
340 |
|
---|
341 | $REAL_INSTALL && create_dir "${PREFIX}" "destination directory"
|
---|
342 | create_dir "${OBJDIR}" "GCC object directory"
|
---|
343 |
|
---|
344 | check_dirs "${PREFIX}" "${WORKDIR}"
|
---|
345 |
|
---|
346 | echo ">>> Unpacking tarballs"
|
---|
347 | cd "${WORKDIR}"
|
---|
348 | check_error $? "Change directory failed."
|
---|
349 |
|
---|
350 | unpack_tarball "${BASEDIR}/${BINUTILS}" "binutils"
|
---|
351 | unpack_tarball "${BASEDIR}/${GCC}" "GCC"
|
---|
352 |
|
---|
353 | echo ">>> Processing binutils (${PLATFORM})"
|
---|
354 | cd "${BINUTILSDIR}"
|
---|
355 | check_error $? "Change directory failed."
|
---|
356 |
|
---|
357 | change_title "binutils: configure (${PLATFORM})"
|
---|
358 | CFLAGS=-Wno-error ./configure \
|
---|
359 | "--target=${TARGET}" \
|
---|
360 | "--prefix=${PREFIX}" "--program-prefix=${TARGET}-" \
|
---|
361 | --disable-nls --disable-werror
|
---|
362 | check_error $? "Error configuring binutils."
|
---|
363 |
|
---|
364 | change_title "binutils: make (${PLATFORM})"
|
---|
365 | make all
|
---|
366 | check_error $? "Error compiling binutils."
|
---|
367 |
|
---|
368 | change_title "binutils: install (${PLATFORM})"
|
---|
369 | if $REAL_INSTALL; then
|
---|
370 | make install
|
---|
371 | else
|
---|
372 | make install "DESTDIR=${INSTALL_DIR}"
|
---|
373 | fi
|
---|
374 | check_error $? "Error installing binutils."
|
---|
375 |
|
---|
376 |
|
---|
377 | echo ">>> Processing GCC (${PLATFORM})"
|
---|
378 | cd "${OBJDIR}"
|
---|
379 | check_error $? "Change directory failed."
|
---|
380 |
|
---|
381 | change_title "GCC: configure (${PLATFORM})"
|
---|
382 | PATH="$PATH:${INSTALL_DIR}/${PREFIX}/bin" "${GCCDIR}/configure" \
|
---|
383 | "--target=${TARGET}" \
|
---|
384 | "--prefix=${PREFIX}" "--program-prefix=${TARGET}-" \
|
---|
385 | --with-gnu-as --with-gnu-ld --disable-nls --disable-threads \
|
---|
386 | --enable-languages=c,objc,c++,obj-c++ \
|
---|
387 | --disable-multilib --disable-libgcj --without-headers \
|
---|
388 | --disable-shared --enable-lto --disable-werror
|
---|
389 | check_error $? "Error configuring GCC."
|
---|
390 |
|
---|
391 | change_title "GCC: make (${PLATFORM})"
|
---|
392 | PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-gcc
|
---|
393 | check_error $? "Error compiling GCC."
|
---|
394 |
|
---|
395 | change_title "GCC: install (${PLATFORM})"
|
---|
396 | if $REAL_INSTALL; then
|
---|
397 | PATH="${PATH}:${PREFIX}/bin" make install-gcc
|
---|
398 | else
|
---|
399 | PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-gcc "DESTDIR=${INSTALL_DIR}"
|
---|
400 | fi
|
---|
401 | check_error $? "Error installing GCC."
|
---|
402 |
|
---|
403 |
|
---|
404 | cd "${BASEDIR}"
|
---|
405 | check_error $? "Change directory failed."
|
---|
406 |
|
---|
407 | echo ">>> Cleaning up"
|
---|
408 | cleanup_dir "${WORKDIR}"
|
---|
409 |
|
---|
410 | echo
|
---|
411 | echo ">>> Cross-compiler for ${TARGET} installed."
|
---|
412 | }
|
---|
413 |
|
---|
414 | while [ "$#" -gt 1 ]; do
|
---|
415 | case "$1" in
|
---|
416 | --no-install)
|
---|
417 | REAL_INSTALL=false
|
---|
418 | shift
|
---|
419 | ;;
|
---|
420 | --helenos-target)
|
---|
421 | USE_HELENOS_TARGET=true
|
---|
422 | shift
|
---|
423 | ;;
|
---|
424 | *)
|
---|
425 | show_usage
|
---|
426 | ;;
|
---|
427 | esac
|
---|
428 | done
|
---|
429 |
|
---|
430 | prepare
|
---|
431 | build_target riscv64
|
---|