source: mainline/tools/toolchain.sh@ 3012ae7

Last change on this file since 3012ae7 was 3012ae7, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

-helenos target

  • Property mode set to 100755
File size: 12.7 KB
Line 
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
31BINUTILS_GDB_GIT="https://github.com/HelenOS/binutils-gdb.git"
32
33BINUTILS_BRANCH="binutils-2_30-helenos"
34BINUTILS_VERSION="2.30"
35
36GDB_BRANCH="gdb-8_1-helenos"
37GDB_VERSION="8.1"
38
39GCC_GIT="https://github.com/HelenOS/gcc.git"
40GCC_BRANCH="8_2_0-helenos"
41GCC_VERSION="8.2.0"
42
43BASEDIR="`pwd`"
44SRCDIR="$(readlink -f $(dirname "$0"))"
45
46REAL_INSTALL=true
47USE_HELENOS_TARGET=true
48
49check_error() {
50 if [ "$1" -ne "0" ] ; then
51 echo
52 echo "Script failed: $2"
53
54 exit 1
55 fi
56}
57
58show_usage() {
59 echo "Cross-compiler toolchain build script"
60 echo
61 echo "Syntax:"
62 echo " $0 [--no-install] [--non-helenos-target] <platform>"
63 echo
64 echo "Possible target platforms are:"
65 echo " amd64 AMD64 (x86-64, x64)"
66 echo " arm32 ARM 32b"
67 echo " ia32 IA-32 (x86, i386)"
68 echo " ia64 IA-64 (Itanium)"
69 echo " mips32 MIPS little-endian 32b"
70 echo " mips32eb MIPS big-endian 32b"
71 echo " mips64 MIPS little-endian 64b"
72 echo " ppc32 PowerPC 32b"
73 echo " ppc64 PowerPC 64v"
74 echo " riscv64 RISC-V 64b"
75 echo " sparc64 SPARC V9"
76 echo " all build all targets"
77 echo " essential build only targets currently needed for HelenOS development"
78 echo " parallel same as 'all', but all in parallel"
79 echo " 2-way same as 'all', but 2-way parallel"
80 echo
81 echo "The toolchain is installed into directory specified by the"
82 echo "CROSS_PREFIX environment variable. If the variable is not"
83 echo "defined, /usr/local/cross/ is used as default."
84 echo
85 echo "If --no-install is present, the toolchain still uses the"
86 echo "CROSS_PREFIX as the target directory but the installation"
87 echo "copies the files into PKG/ subdirectory without affecting"
88 echo "the actual root file system. That is only useful if you do"
89 echo "not want to run the script under the super user."
90 echo
91 echo "The --non-helenos-target will build non-HelenOS-specific toolchain"
92 echo "(i.e. it will use *-linux-* triplet instead of *-helenos)."
93 echo "Using this toolchain for building HelenOS is not supported."
94 echo
95
96 exit 3
97}
98
99change_title() {
100 echo -en "\e]0;$1\a"
101}
102
103show_countdown() {
104 TM="$1"
105
106 if [ "${TM}" -eq 0 ] ; then
107 echo
108 return 0
109 fi
110
111 echo -n "${TM} "
112 change_title "${TM}"
113 sleep 1
114
115 TM="`expr "${TM}" - 1`"
116 show_countdown "${TM}"
117}
118
119show_dependencies() {
120 echo "IMPORTANT NOTICE:"
121 echo
122 echo "For a successful compilation and use of the cross-compiler"
123 echo "toolchain you need at least the following dependencies."
124 echo
125 echo "Please make sure that the dependencies are present in your"
126 echo "system. Otherwise the compilation process might fail after"
127 echo "a few seconds or minutes."
128 echo
129 echo " - SED, AWK, Flex, Bison, gzip, bzip2, Bourne Shell"
130 echo " - gettext, zlib, Texinfo, libelf, libgomp"
131 echo " - GNU Make, Coreutils, Sharutils, tar"
132 echo " - native C and C++ compiler, assembler and linker"
133 echo " - native C and C++ standard library with headers"
134 echo
135}
136
137cleanup_dir() {
138 DIR="$1"
139
140 if [ -d "${DIR}" ] ; then
141 change_title "Removing ${DIR}"
142 echo " >>> Removing ${DIR}"
143 rm -fr "${DIR}"
144 fi
145}
146
147create_dir() {
148 DIR="$1"
149 DESC="$2"
150
151 change_title "Creating ${DESC}"
152 echo ">>> Creating ${DESC}"
153
154 mkdir -p "${DIR}"
155 test -d "${DIR}"
156 check_error $? "Unable to create ${DIR}."
157}
158
159check_dirs() {
160 OUTSIDE="$1"
161 BASE="$2"
162 ORIGINAL="`pwd`"
163
164 mkdir -p "${OUTSIDE}"
165
166 cd "${OUTSIDE}"
167 check_error $? "Unable to change directory to ${OUTSIDE}."
168 ABS_OUTSIDE="`pwd`"
169
170 cd "${BASE}"
171 check_error $? "Unable to change directory to ${BASE}."
172 ABS_BASE="`pwd`"
173
174 cd "${ORIGINAL}"
175 check_error $? "Unable to change directory to ${ORIGINAL}."
176
177 BASE_LEN="${#ABS_BASE}"
178 OUTSIDE_TRIM="${ABS_OUTSIDE:0:${BASE_LEN}}"
179
180 if [ "${OUTSIDE_TRIM}" == "${ABS_BASE}" ] ; then
181 echo
182 echo "CROSS_PREFIX cannot reside within the working directory."
183
184 exit 5
185 fi
186}
187
188prepare() {
189 show_dependencies
190 show_countdown 10
191
192 mkdir -p "${BASEDIR}/downloads"
193 cd "${BASEDIR}/downloads"
194 check_error $? "Change directory failed."
195
196 echo ">>> Downloading sources"
197 git clone --depth 1 -b "$BINUTILS_BRANCH" "$BINUTILS_GDB_GIT" "binutils-$BINUTILS_VERSION"
198 git clone --depth 1 -b "$GDB_BRANCH" "$BINUTILS_GDB_GIT" "gdb-$GDB_VERSION"
199 git clone --depth 1 -b "$GCC_BRANCH" "$GCC_GIT" "gcc-$GCC_VERSION"
200
201 # If the directory already existed, pull upstream changes.
202 git -C "binutils-$BINUTILS_VERSION" pull
203 git -C "gdb-$GDB_VERSION" pull
204 git -C "gcc-$GCC_VERSION" pull
205
206 echo ">>> Downloading GCC prerequisites"
207 cd "gcc-${GCC_VERSION}"
208 ./contrib/download_prerequisites
209 cd ..
210}
211
212set_target_from_platform() {
213 case "$1" in
214 "arm32")
215 GNU_ARCH="arm"
216 ;;
217 "ia32")
218 GNU_ARCH="i686"
219 ;;
220 "mips32")
221 GNU_ARCH="mipsel"
222 ;;
223 "mips32eb")
224 GNU_ARCH="mips"
225 ;;
226 "mips64")
227 GNU_ARCH="mips64el"
228 ;;
229 "ppc32")
230 GNU_ARCH="ppc"
231 ;;
232 *)
233 GNU_ARCH="$1"
234 ;;
235 esac
236
237 HELENOS_TARGET="${GNU_ARCH}-helenos"
238
239 case "$1" in
240 "arm32")
241 LINUX_TARGET="${GNU_ARCH}-linux-gnueabi"
242 ;;
243 *)
244 LINUX_TARGET="${GNU_ARCH}-linux-gnu"
245 ;;
246 esac
247}
248
249build_target() {
250 PLATFORM="$1"
251
252 # This sets the *_TARGET variables
253 set_target_from_platform "$PLATFORM"
254 if $USE_HELENOS_TARGET ; then
255 TARGET="$HELENOS_TARGET"
256 else
257 TARGET="$LINUX_TARGET"
258 fi
259
260 WORKDIR="${BASEDIR}/${TARGET}"
261 INSTALL_DIR="${WORKDIR}/PKG"
262 BINUTILSDIR="${WORKDIR}/binutils-${BINUTILS_VERSION}"
263 GCCDIR="${WORKDIR}/gcc-${GCC_VERSION}"
264 GDBDIR="${WORKDIR}/gdb-${GDB_VERSION}"
265
266 if [ -z "${CROSS_PREFIX}" ] ; then
267 CROSS_PREFIX="/usr/local/cross"
268 fi
269
270 if [ -z "$JOBS" ] ; then
271 JOBS=`nproc`
272 fi
273
274 PREFIX="${CROSS_PREFIX}"
275
276 echo ">>> Removing previous content"
277 cleanup_dir "${WORKDIR}"
278 mkdir -p "${WORKDIR}"
279 check_dirs "${PREFIX}" "${WORKDIR}"
280
281 if $USE_HELENOS_TARGET ; then
282 echo ">>> Creating build sysroot"
283 mkdir -p "${WORKDIR}/sysroot/include"
284 mkdir "${WORKDIR}/sysroot/lib"
285 cp -r -L -t "${WORKDIR}/sysroot/include" \
286 ${SRCDIR}/../abi/include/* \
287 ${SRCDIR}/../uspace/lib/c/arch/${PLATFORM}/include/* \
288 ${SRCDIR}/../uspace/lib/c/include/*
289 check_error $? "Failed to create build sysroot."
290 fi
291
292 echo ">>> Processing binutils (${PLATFORM})"
293 mkdir -p "${BINUTILSDIR}"
294 cd "${BINUTILSDIR}"
295 check_error $? "Change directory failed."
296
297 change_title "binutils: configure (${PLATFORM})"
298 CFLAGS=-Wno-error "${BASEDIR}/downloads/binutils-${BINUTILS_VERSION}/configure" \
299 "--target=${TARGET}" \
300 "--prefix=${PREFIX}" \
301 "--program-prefix=${TARGET}-" \
302 --disable-nls \
303 --disable-werror \
304 --enable-gold \
305 --enable-deterministic-archives \
306 --disable-gdb \
307 --with-sysroot
308 check_error $? "Error configuring binutils."
309
310 change_title "binutils: make (${PLATFORM})"
311 make all -j$JOBS
312 check_error $? "Error compiling binutils."
313
314 change_title "binutils: install (${PLATFORM})"
315 make install "DESTDIR=${INSTALL_DIR}"
316 check_error $? "Error installing binutils."
317
318
319 echo ">>> Processing GCC (${PLATFORM})"
320 mkdir -p "${GCCDIR}"
321 cd "${GCCDIR}"
322 check_error $? "Change directory failed."
323
324 if $USE_HELENOS_TARGET ; then
325 SYSROOT=--with-sysroot --with-build-sysroot="${WORKDIR}/sysroot"
326 else
327 SYSROOT=--without-headers
328 fi
329
330 change_title "GCC: configure (${PLATFORM})"
331 PATH="$PATH:${INSTALL_DIR}/${PREFIX}/bin" "${BASEDIR}/downloads/gcc-${GCC_VERSION}/configure" \
332 "--target=${TARGET}" \
333 "--prefix=${PREFIX}" \
334 "--program-prefix=${TARGET}-" \
335 --with-gnu-as \
336 --with-gnu-ld \
337 --disable-nls \
338 --enable-languages=c,c++,go \
339 --enable-lto \
340 --disable-shared \
341 --disable-werror \
342 $SYSROOT
343 check_error $? "Error configuring GCC."
344
345 change_title "GCC: make (${PLATFORM})"
346 PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-gcc -j$JOBS
347 check_error $? "Error compiling GCC."
348
349 if $USE_HELENOS_TARGET ; then
350 PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libgcc -j$JOBS
351 check_error $? "Error compiling libgcc."
352 # TODO: needs some extra care
353 #PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libatomic -j$JOBS
354 #check_error $? "Error compiling libatomic."
355 #PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libstdc++-v3 -j$JOBS
356 #check_error $? "Error compiling libstdc++."
357 fi
358
359 change_title "GCC: install (${PLATFORM})"
360 PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-gcc "DESTDIR=${INSTALL_DIR}"
361 if $USE_HELENOS_TARGET ; then
362 PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libgcc "DESTDIR=${INSTALL_DIR}"
363 #PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libatomic "DESTDIR=${INSTALL_DIR}"
364 #PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libstdc++-v3 "DESTDIR=${INSTALL_DIR}"
365 fi
366 check_error $? "Error installing GCC."
367
368
369 echo ">>> Processing GDB (${PLATFORM})"
370 mkdir -p "${GDBDIR}"
371 cd "${GDBDIR}"
372 check_error $? "Change directory failed."
373
374 change_title "GDB: configure (${PLATFORM})"
375 PATH="$PATH:${INSTALL_DIR}/${PREFIX}/bin" "${BASEDIR}/downloads/gdb-${GDB_VERSION}/configure" \
376 "--target=${TARGET}" \
377 "--prefix=${PREFIX}" \
378 "--program-prefix=${TARGET}-" \
379 --enable-werror=no
380 check_error $? "Error configuring GDB."
381
382 change_title "GDB: make (${PLATFORM})"
383 PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-gdb -j$JOBS
384 check_error $? "Error compiling GDB."
385
386 change_title "GDB: make (${PLATFORM})"
387 PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-gdb "DESTDIR=${INSTALL_DIR}"
388 check_error $? "Error installing GDB."
389
390 # Symlink clang and lld to the install path.
391 CLANG="`which clang 2> /dev/null || echo "/usr/bin/clang"`"
392 LLD="`which ld.lld 2> /dev/null || echo "/usr/bin/ld.lld"`"
393
394 ln -s $CLANG "${INSTALL_DIR}/${PREFIX}/bin/${TARGET}-clang"
395 ln -s $LLD "${INSTALL_DIR}/${PREFIX}/bin/${TARGET}-ld.lld"
396
397 if $REAL_INSTALL ; then
398 echo ">>> Moving to the destination directory."
399 echo cp -r -t "${PREFIX}" "${INSTALL_DIR}/${PREFIX}/"*
400 cp -r -t "${PREFIX}" "${INSTALL_DIR}/${PREFIX}/"*
401 fi
402
403 cd "${BASEDIR}"
404 check_error $? "Change directory failed."
405
406 echo ">>> Cleaning up"
407 cleanup_dir "${WORKDIR}"
408
409 echo
410 echo ">>> Cross-compiler for ${TARGET} installed."
411}
412
413while [ "$#" -gt 1 ] ; do
414 case "$1" in
415 --no-install)
416 REAL_INSTALL=false
417 shift
418 ;;
419 --non-helenos-target)
420 USE_HELENOS_TARGET=false
421 shift
422 ;;
423 *)
424 show_usage
425 ;;
426 esac
427done
428
429if [ "$#" -lt "1" ] ; then
430 show_usage
431fi
432
433case "$1" in
434 amd64|arm32|ia32|ia64|mips32|mips32eb|mips64|ppc32|ppc64|riscv64|sparc64)
435 prepare
436 build_target "$1"
437 ;;
438 "all")
439 prepare
440 build_target "amd64"
441 build_target "arm32"
442 build_target "ia32"
443 build_target "ia64"
444 build_target "mips32"
445 build_target "mips32eb"
446 build_target "mips64"
447 build_target "ppc32"
448 build_target "ppc64"
449 build_target "riscv64"
450 build_target "sparc64"
451 ;;
452 "essential")
453 prepare
454 build_target "amd64"
455 build_target "arm32"
456 build_target "ia32"
457 build_target "ia64"
458 build_target "mips32"
459 build_target "mips32eb"
460 build_target "ppc32"
461 build_target "sparc64"
462 ;;
463 "parallel")
464 prepare
465 build_target "amd64" &
466 build_target "arm32" &
467 build_target "ia32" &
468 build_target "ia64" &
469 build_target "mips32" &
470 build_target "mips32eb" &
471 build_target "mips64" &
472 build_target "ppc32" &
473 build_target "ppc64" &
474 build_target "riscv64" &
475 build_target "sparc64" &
476 wait
477 ;;
478 "2-way")
479 prepare
480 build_target "amd64" &
481 build_target "arm32" &
482 wait
483
484 build_target "ia32" &
485 build_target "ia64" &
486 wait
487
488 build_target "mips32" &
489 build_target "mips32eb" &
490 wait
491
492 build_target "mips64" &
493 build_target "ppc32" &
494 wait
495
496 build_target "riscv64" &
497 build_target "ppc64" &
498 wait
499
500 build_target "sparc64" &
501 wait
502 ;;
503 *)
504 show_usage
505 ;;
506esac
Note: See TracBrowser for help on using the repository browser.