06-gcc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # Build script for gcc.
  2. #
  3. # Copyright (c) 2016-2023 Matias Fonzo, <selk@dragora.org>.
  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. version=11-20230310
  17. # Prerequisites
  18. gmp_version=6.2.1
  19. mpfr_version=4.2.0
  20. mpc_version=1.3.1
  21. isl_version=0.24
  22. cd -- "$TMPDIR"
  23. rm -rf gcc-${version} gmp-${gmp_version} \
  24. mpfr-${mpfr_version} mpc-${mpc_version} isl-${isl_version}
  25. unpack "${worktree}/sources/gcc-${version}.tar.xz" \
  26. "${worktree}/sources/gmp-${gmp_version}.tar.lz" \
  27. "${worktree}/sources/mpfr-${mpfr_version}.tar.bz2" \
  28. "${worktree}/sources/mpc-${mpc_version}.tar.gz" \
  29. "${worktree}/sources/isl-${isl_version}.tar.bz2"
  30. # Build instructions
  31. cd gcc-${version}
  32. # Apply specific patches for the support in musl.
  33. # https://port70.net/~nsz/musl/gcc-11.1.0/
  34. patch -Np1 -i "${worktree}/patches/gcc/11/0002-posix_memalign.patch"
  35. patch -Np1 -i "${worktree}/patches/gcc/11/0003-j2.patch"
  36. patch -Np1 -i "${worktree}/patches/gcc/11/0004-static-pie.patch"
  37. patch -Np1 -i "${worktree}/patches/gcc/11/0005-m68k-sqrt.patch"
  38. patch -Np1 -i "${worktree}/patches/gcc/11/extra-musl_libssp.patch"
  39. # Apply patches from "Alpine Linux" in order to improve the security (Thanks!)
  40. echo "patch1:"
  41. patch -Np1 -i "${worktree}/patches/gcc/0004-Turn-on-D_FORTIFY_SOURCE-2-by-default-for-C-C-ObjC-O.patch"
  42. echo "patch2:"
  43. patch -Np1 -i "${worktree}/patches/gcc/0005-On-linux-targets-pass-as-needed-by-default-to-the-li.patch"
  44. echo "patch3:"
  45. patch -Np1 -i "${worktree}/patches/gcc/0006-Enable-Wformat-and-Wformat-security-by-default.patch"
  46. echo "patch4:"
  47. patch -Np1 -i "${worktree}/patches/gcc/0007-Enable-Wtrampolines-by-default.patch"
  48. echo "patch5:"
  49. patch -Np1 -i "${worktree}/patches/gcc/0008-Disable-ssp-on-nostdlib-nodefaultlibs-and-ffreestand.patch"
  50. echo "patch6:"
  51. patch -Np1 -i "${worktree}/patches/gcc/0014-nopie.patch"
  52. echo "patch7:"
  53. patch -Np1 -i "${worktree}/patches/gcc/0021-add-fortify-headers-paths.patch"
  54. # Hard code default ld path to look at /tools
  55. find . -type f -name '*.h' -exec grep -l "/lib/ld-" {} + | while read -r file
  56. do
  57. test -f "$file" || continue;
  58. cp -f "$file" "${file}.orig"
  59. sed -e 's@/lib\(64\)\{0,1\}\(32\)\{0,1\}/ld@/tools&@g' \
  60. -e 's@/usr@/tools@g' "${file}.orig" > "$file"
  61. echo '
  62. #undef STANDARD_STARTFILE_PREFIX_1
  63. #undef STANDARD_STARTFILE_PREFIX_2
  64. #define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
  65. #define STANDARD_STARTFILE_PREFIX_2 ""' >> "$file"
  66. done
  67. # Make symlinks for requisites
  68. ln -s ../gmp-${gmp_version} gmp
  69. ln -s ../mpfr-${mpfr_version} mpfr
  70. ln -s ../mpc-${mpc_version} mpc
  71. ln -s ../isl-${isl_version} isl
  72. # Apply patches for MPFR
  73. (
  74. cd mpfr || exit 1
  75. for file in "${worktree}"/patches/mpfr/*
  76. do
  77. if test -f "$file"
  78. then
  79. rm -f PATCHES
  80. patch -p1 < "$file"
  81. fi
  82. done
  83. )
  84. # Update detection for hosts based on musl
  85. cp -f "${worktree}/archive/common/config.guess" gmp/configfsf.guess
  86. cp -f "${worktree}/archive/common/config.sub" gmp/config.sub
  87. cp -f "${worktree}/archive/common/config.guess" isl/config.guess
  88. cp -f "${worktree}/archive/common/config.sub" isl/config.sub
  89. # Build in a separate directory
  90. rm -rf ../gcc-build
  91. mkdir ../gcc-build
  92. cd ../gcc-build
  93. # Import and export toolchain variables
  94. . "${worktree}/stages/env.d/cross-staticenv"
  95. ../gcc-${version}/configure \
  96. AR="$AR" AS="$AS" LD="$LD" RANLIB="$RANLIB" READELF="$READELF" STRIP="$STRIP" \
  97. CC="$BTCC" CXX="$BTCXX" \
  98. CFLAGS="$BTCFLAGS" CXXFLAGS="$BTCXXFLAGS" LDFLAGS="$BTLDFLAGS" \
  99. --prefix=/tools \
  100. --libdir=/tools/lib${libSuffix} \
  101. --build=$host \
  102. --host=$target \
  103. --target=$target \
  104. --enable-languages=c,c++ \
  105. --enable-clocale=generic \
  106. --enable-cet=auto \
  107. --enable-initfini-array \
  108. --enable-tls \
  109. --enable-libstdcxx-time \
  110. --enable-fully-dynamic-string \
  111. --enable-default-ssp \
  112. --enable-default-pie \
  113. --enable-libssp \
  114. --disable-symvers \
  115. --disable-bootstrap \
  116. --disable-libstdcxx-pch \
  117. --disable-gnu-indirect-function \
  118. --disable-libsanitizer \
  119. --disable-nls \
  120. --disable-install-libiberty \
  121. --with-linker-hash-style=gnu \
  122. --with-local-prefix=/tools \
  123. --with-native-system-header-dir=/tools/include \
  124. $multilib_options \
  125. $gcc_options
  126. make -j${jobs} all \
  127. MAKEINFO="true" \
  128. AS_FOR_TARGET="$AS" \
  129. LD_FOR_TARGET="$LD"
  130. make -j${jobs} MAKEINFO="true" install
  131. # Provide minimal libssp_nonshared.a
  132. $BTCC -c "${worktree}/archive/gcc/__stack_chk_fail_local.c" \
  133. -o __stack_chk_fail_local.o
  134. $AR rc libssp_nonshared.a __stack_chk_fail_local.o
  135. $RANLIB libssp_nonshared.a
  136. cp -p libssp_nonshared.a /tools/lib${libSuffix}
  137. # Unset some imported variables from file
  138. unset AR AS LD RANLIB READELF STRIP
  139. cd -- "$TMPDIR"
  140. cleanup()
  141. {
  142. rm -rf gcc-${version} gcc-build \
  143. gmp-${gmp_version} mpfr-${mpfr_version} \
  144. mpc-${mpc_version} isl-${isl_version}
  145. }