| 1 | #!/bin/bash -x |
| 2 | # |
| 3 | # SPDX-FileCopyrightText: 2024 Kienan Stewart <kstewart@efficios.com |
| 4 | # SPDX-License-Identifier: GPL-2.0-or-later |
| 5 | # |
| 6 | |
| 7 | SRC_DIR="${SRC_DIR:-/src/gcc-releases-gcc-4.8.5}" |
| 8 | PATCH_DIR="${PATCH_DIR:-/src/patches}" |
| 9 | BIN_SUFFIX="${BIN_SUFFIX:-4.8}" |
| 10 | IFS=' ' read -r -a TARGETS <<< "${TARGETS:-aarch64-linux-gnu}" |
| 11 | HOST="${HOST:-x86_64-pc-linux-gnu}" |
| 12 | CONFIGURE_ARGS=(${CONFIGURE_ARGS:-}) |
| 13 | MAKE_ARGS=(${MAKE_ARGS:-}) |
| 14 | MAKE_INSTALL_ARGS=(${MAKE_INSTALL_ARGS:-}) |
| 15 | DEBUG="${DEBUG:-}" |
| 16 | CSTD="${CSTD:-gnu99}" |
| 17 | CXXSTD="${CXXSTD:-gnu++98}" |
| 18 | |
| 19 | BUILD_DIR="$(pwd)" |
| 20 | |
| 21 | cd "${SRC_DIR}" || exit 1 |
| 22 | while read -r line ; do |
| 23 | EXT=$(echo "$line" | rev | cut -d. -f1 | rev) |
| 24 | PATCH_LEVEL=1 |
| 25 | if [[ "${EXT}" =~ [0-9]+ ]] ; then |
| 26 | PATCH_LEVEL="${EXT}" |
| 27 | fi |
| 28 | patch -p"${PATCH_LEVEL}" < "${line}" |
| 29 | done < <(find "${PATCH_DIR}" -type f | sort) |
| 30 | |
| 31 | NPROC="${NPROC:=$(nproc)}" |
| 32 | CFLAGS=(-std="${CSTD}" -w) |
| 33 | CXXFLAGS=(-std="${CXXSTD}" -w) |
| 34 | |
| 35 | |
| 36 | cd "${SRC_DIR}" || exit 1 |
| 37 | mkdir -p "/output/usr/local/gcc${BIN_SUFFIX}" |
| 38 | ./contrib/download_prerequisites |
| 39 | PREREQS=(gmp isl mpfr mpc) |
| 40 | for PREREQ in "${PREREQS[@]}" ; do |
| 41 | cd "${SRC_DIR}/${PREREQ}" || continue |
| 42 | ARGS=( |
| 43 | --prefix="/usr/local/gcc${BIN_SUFFIX}" |
| 44 | ) |
| 45 | case "${PREREQ}" in |
| 46 | "isl") |
| 47 | ARG+=( |
| 48 | --with-gmp-prefix="/usr/local/gcc${BIN_SUFFIX}" |
| 49 | ) |
| 50 | ;; |
| 51 | "mpc") |
| 52 | ARGS+=( |
| 53 | --with-gmp="/usr/local/gcc${BIN_SUFFIX}" |
| 54 | --with-mpfr="/usr/local/gcc${BIN_SUFFIX}" |
| 55 | ) |
| 56 | ;; |
| 57 | "mpfr") |
| 58 | ARGS+=(--with-gmp="/usr/local/gcc${BIN_SUFFIX}") |
| 59 | ;; |
| 60 | esac |
| 61 | ./configure "${ARGS[@]}" |
| 62 | make -j"${NPROC}" |
| 63 | make install |
| 64 | cd .. |
| 65 | rm -rf "${PREREQ}"* |
| 66 | done |
| 67 | cd "${BUILD_DIR}" || exit 1 |
| 68 | |
| 69 | for TARGET in "${TARGETS[@]}" ; do |
| 70 | echo "*** Building for target: ${TARGET} ***" |
| 71 | rm -rf "${BUILD_DIR}" |
| 72 | mkdir -p "${BUILD_DIR}" |
| 73 | cd "${BUILD_DIR}" || continue |
| 74 | |
| 75 | TARGET_ARGS=() |
| 76 | PROGRAM_PREFIX="${TARGET}-" |
| 77 | case "${TARGET}" in |
| 78 | aarch64-linux-gnu) |
| 79 | TARGET_ARGS+=( |
| 80 | --enable-fix-cortex-a64-84319 |
| 81 | ) |
| 82 | ;; |
| 83 | arm-linux-gnueabihf) |
| 84 | TARGET_ARGS+=( |
| 85 | --with-arch=armv7-a |
| 86 | --with-float=hard |
| 87 | --with-fpu=vfpv3-d16 |
| 88 | --with-mode=thumb |
| 89 | ) |
| 90 | ;; |
| 91 | i686-linux-gnu) |
| 92 | TARGET_ARGS+=( |
| 93 | --with-arch-i686 |
| 94 | --with-tune=generic |
| 95 | ) |
| 96 | ;; |
| 97 | powerpc64le-linux-gnu) |
| 98 | # Disable multilib so that ppc64el kernel can be built, since |
| 99 | # legacy Makefiles compile vdso in 32bits unconditionally. |
| 100 | # @see https://bugzilla.redhat.com/show_bug.cgi?id=1237363 |
| 101 | # @see https://bugzilla.redhat.com/show_bug.cgi?id=1205236 |
| 102 | # @see https://bugs.launchpad.net/ubuntu/trusty/+source/linux/+bug/1433809/ |
| 103 | TARGET_ARGS+=( |
| 104 | --disable-multilib |
| 105 | --enable-targets=powerpcle-linux |
| 106 | --with-cpu=power8 |
| 107 | --with-long-double-128 |
| 108 | ) |
| 109 | ;; |
| 110 | powerpc-linux-gnu) |
| 111 | TARGET_ARGS+=( |
| 112 | '--disable-softfloat' |
| 113 | '--enable-secureplt' |
| 114 | '--enable-targets=powerpc-linux,powerpc64-linux' |
| 115 | '--with-cpu=default32' |
| 116 | '--with-long-double-128' |
| 117 | ) |
| 118 | ;; |
| 119 | riscv64-linux-gnu) |
| 120 | echo "Not supported in gcc-4.8" |
| 121 | continue |
| 122 | ;; |
| 123 | s390x-linux-gnu) |
| 124 | TARGET_ARGS+=( |
| 125 | --with-arch=zEC12 |
| 126 | --with-long-double-128 |
| 127 | ) |
| 128 | ;; |
| 129 | "${HOST}") |
| 130 | TARGET="${HOST}" |
| 131 | PROGRAM_PREFIX='' |
| 132 | ;; |
| 133 | *) |
| 134 | echo "Unrecognized target: ${TARGET}" |
| 135 | continue |
| 136 | ;; |
| 137 | esac |
| 138 | mkdir -p "/output/usr/local/gcc${BIN_SUFFIX}/lib/gcc-cross/${TARGET}" /output/usr/local |
| 139 | |
| 140 | START=$(date +%s) |
| 141 | "${SRC_DIR}/configure" --build="${HOST}" --host="${HOST}" --enable-languages=c,c++ \ |
| 142 | --program-prefix="${PROGRAM_PREFIX}" --target="${TARGET}" --program-suffix="-${BIN_SUFFIX}" \ |
| 143 | --prefix="/usr/local/gcc${BIN_SUFFIX}" --with-system-zlib \ |
| 144 | --libexecdir="/usr/local/gcc${BIN_SUFFIX}/lib/" \ |
| 145 | --libdir="/usr/local/gcc${BIN_SUFFIX}/lib/" \ |
| 146 | --includedir="/usr/local/gcc${BIN_SUFFIX}/${TARGET}/include" \ |
| 147 | --disable-bootstrap --disable-nls --disable-shared --enable-host-shared \ |
| 148 | --enable-threads=posix --enable-default-pie --with-sysroot=/ \ |
| 149 | --without-target-system-zlib --enable-multiarch \ |
| 150 | --with-isl="/usr/local/gcc${BIN_SUFFIX}" \ |
| 151 | --with-gmp="/usr/local/gcc${BIN_SUFFIX}" \ |
| 152 | --with-mpfr="/usr/local/gcc${BIN_SUFFIX}" \ |
| 153 | --with-mpc="/usr/local/gcc${BIN_SUFFIX}" \ |
| 154 | "${TARGET_ARGS[@]}" "${CONFIGURE_ARGS[@]}" \ |
| 155 | CFLAGS="${CFLAGS[*]}" CXXFLAGS="${CXXFLAGS[*]}" |
| 156 | |
| 157 | # This avoids building libgcc and binutils |
| 158 | # Copy include files from gcc-12 cross prior to build |
| 159 | mkdir -p "/usr/local/gcc${BIN_SUFFIX}/${TARGET}/lib" |
| 160 | mkdir -p "/usr/local/gcc${BIN_SUFFIX}/bin/" |
| 161 | cp -r "/usr/lib/gcc-cross/${TARGET}/12/"* "/usr/local/gcc${BIN_SUFFIX}/${TARGET}/" |
| 162 | cp -r "/usr/lib/gcc/x86_64-linux-gnu/12/"* "/usr/local/gcc${BIN_SUFFIX}/${TARGET}/lib/" |
| 163 | cp -r "/usr/${TARGET}/"* "/usr/local/gcc${BIN_SUFFIX}/${TARGET}/" |
| 164 | # And the binutils binaries |
| 165 | cp "/usr/bin/${TARGET}-"* "/usr/local/gcc${BIN_SUFFIX}/bin/" |
| 166 | |
| 167 | make -j"${NPROC}" "${MAKE_ARGS[@]}" CFLAGS="${CFLAGS[*]}" CXXFLAGS="${CXXFLAGS[*]}" \ |
| 168 | LD_LIBRARY_PATH="/usr/local/gcc${BIN_SUFFIX}/lib:${LD_LIBRARY_PATH}" |
| 169 | # Do not use -jN with make install, it often breaks. |
| 170 | make install "${MAKE_INSTALL_ARGS[@]}" \ |
| 171 | LD_LIBRARY_PATH="/usr/local/gcc${BIN_SUFFIX}/lib:${LD_LIBRARY_PATH}" |
| 172 | |
| 173 | if [ -n "${DEBUG}" ] ; then |
| 174 | echo $(($(date +%s) - START)) > "/output/${TARGET}.time" |
| 175 | fi |
| 176 | |
| 177 | cp config.log "/output/config-${TARGET}.log" |
| 178 | done |
| 179 | |
| 180 | cp -r "/usr/local/gcc${BIN_SUFFIX}" "/output/usr/local/" |
| 181 | |
| 182 | # To test |
| 183 | # 1. Copy the output tarball from the Makefile to a Debian bookworm instance |
| 184 | # 2. Unpack, eg. `tar -xzf gcc55.tar.gz -C /` |
| 185 | # 3. Build a small test program, eg. |
| 186 | # LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/gcc5.5/bin \ |
| 187 | # PATH=$PATH:/usr/local/gcc5.5/bin \ |
| 188 | # aarch64-linux-gnu-gcc-5.5 -o hello hello.c |
| 189 | # 4. Copy the built binary to a system with that native architecture and run it |
| 190 | # 5. Upload to object storage, eg. |
| 191 | # s3cmd put gcc55.tar.gz s3://jenkins/gcc-5.5-x86_64-linux-gnu-cross.tgz |
| 192 | # |