toolchain.eclass: drop support for deprecated mingw-runtime
[gentoo.git] / eclass / toolchain.eclass
1 # Copyright 1999-2019 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # Maintainer: Toolchain Ninjas <toolchain@gentoo.org>
5 # @SUPPORTED_EAPIS: 5 6 7
6
7 DESCRIPTION="The GNU Compiler Collection"
8 HOMEPAGE="https://gcc.gnu.org/"
9 RESTRICT="strip" # cross-compilers need controlled stripping
10
11 inherit eutils fixheadtails flag-o-matic gnuconfig libtool multilib pax-utils toolchain-funcs prefix
12
13 if [[ ${PV} == *_pre9999* ]] ; then
14         EGIT_REPO_URI="git://gcc.gnu.org/git/gcc.git"
15         # naming style:
16         # gcc-4.7.1_pre9999 -> gcc-4_7-branch
17         #  Note that the micro version is required or lots of stuff will break.
18         #  To checkout master set gcc_LIVE_BRANCH="master" in the ebuild before
19         #  inheriting this eclass.
20         EGIT_BRANCH="${PN}-${PV%.?_pre9999}-branch"
21         EGIT_BRANCH=${EGIT_BRANCH//./_}
22         inherit git-2
23 fi
24
25 FEATURES=${FEATURES/multilib-strict/}
26
27 case ${EAPI:-0} in
28         0|1|2|3|4*) die "Need to upgrade to at least EAPI=5" ;;
29         5*|6) inherit eapi7-ver ;;
30         7) ;;
31         *) die "I don't speak EAPI ${EAPI}." ;;
32 esac
33 EXPORT_FUNCTIONS pkg_pretend pkg_setup src_unpack src_prepare src_configure \
34         src_compile src_test src_install pkg_postinst pkg_postrm
35
36 #---->> globals <<----
37
38 export CTARGET=${CTARGET:-${CHOST}}
39 if [[ ${CTARGET} = ${CHOST} ]] ; then
40         if [[ ${CATEGORY} == cross-* ]] ; then
41                 export CTARGET=${CATEGORY#cross-}
42         fi
43 fi
44 : ${TARGET_ABI:=${ABI}}
45 : ${TARGET_MULTILIB_ABIS:=${MULTILIB_ABIS}}
46 : ${TARGET_DEFAULT_ABI:=${DEFAULT_ABI}}
47
48 is_crosscompile() {
49         [[ ${CHOST} != ${CTARGET} ]]
50 }
51
52 # General purpose version check.  Without a second arg matches up to minor version (x.x.x)
53 tc_version_is_at_least() {
54         ver_test "${2:-${GCC_RELEASE_VER}}" -ge "$1"
55 }
56
57 # General purpose version range check
58 # Note that it matches up to but NOT including the second version
59 tc_version_is_between() {
60         tc_version_is_at_least "${1}" && ! tc_version_is_at_least "${2}"
61 }
62
63 GCC_PV=${TOOLCHAIN_GCC_PV:-${PV}}
64 GCC_PVR=${GCC_PV}
65 [[ ${PR} != "r0" ]] && GCC_PVR=${GCC_PVR}-${PR}
66 GCC_RELEASE_VER=$(ver_cut 1-3 ${GCC_PV})
67 GCC_BRANCH_VER=$(ver_cut 1-2 ${GCC_PV})
68 GCCMAJOR=$(ver_cut 1 ${GCC_PV})
69 GCCMINOR=$(ver_cut 2 ${GCC_PV})
70 GCCMICRO=$(ver_cut 3 ${GCC_PV})
71
72 # According to gcc/c-cppbuiltin.c, GCC_CONFIG_VER MUST match this regex.
73 # ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)?
74 GCC_CONFIG_VER=${GCC_CONFIG_VER:-$(ver_rs 3 '-' ${GCC_PV})}
75
76 # Pre-release support
77 if [[ ${GCC_PV} == *_pre* ]] ; then
78         PRERELEASE=${GCC_PV/_pre/-}
79 elif [[ ${GCC_PV} == *_alpha* ]] ; then
80         SNAPSHOT=${GCC_BRANCH_VER}-${GCC_PV##*_alpha}
81 elif [[ ${GCC_PV} == *_beta* ]] ; then
82         SNAPSHOT=${GCC_BRANCH_VER}-${GCC_PV##*_beta}
83 elif [[ ${GCC_PV} == *_rc* ]] ; then
84         SNAPSHOT=${GCC_PV%_rc*}-RC-${GCC_PV##*_rc}
85 fi
86
87 if [[ ${SNAPSHOT} == [56789].0-* ]] ; then
88         # The gcc-5+ releases have dropped the .0 for some reason.
89         SNAPSHOT=${SNAPSHOT/.0}
90 fi
91
92 PREFIX=${TOOLCHAIN_PREFIX:-${EPREFIX}/usr}
93
94 if tc_version_is_at_least 3.4.0 ; then
95         LIBPATH=${TOOLCHAIN_LIBPATH:-${PREFIX}/lib/gcc/${CTARGET}/${GCC_CONFIG_VER}}
96 else
97         LIBPATH=${TOOLCHAIN_LIBPATH:-${PREFIX}/lib/gcc-lib/${CTARGET}/${GCC_CONFIG_VER}}
98 fi
99 INCLUDEPATH=${TOOLCHAIN_INCLUDEPATH:-${LIBPATH}/include}
100
101 if is_crosscompile ; then
102         BINPATH=${TOOLCHAIN_BINPATH:-${PREFIX}/${CHOST}/${CTARGET}/gcc-bin/${GCC_CONFIG_VER}}
103         HOSTLIBPATH=${PREFIX}/${CHOST}/${CTARGET}/lib/${GCC_CONFIG_VER}
104 else
105         BINPATH=${TOOLCHAIN_BINPATH:-${PREFIX}/${CTARGET}/gcc-bin/${GCC_CONFIG_VER}}
106 fi
107
108 DATAPATH=${TOOLCHAIN_DATAPATH:-${PREFIX}/share/gcc-data/${CTARGET}/${GCC_CONFIG_VER}}
109
110 # Dont install in /usr/include/g++-v3/, but in gcc internal directory.
111 # We will handle /usr/include/g++-v3/ with gcc-config ...
112 STDCXX_INCDIR=${TOOLCHAIN_STDCXX_INCDIR:-${LIBPATH}/include/g++-v${GCC_BRANCH_VER/\.*/}}
113
114 #---->> LICENSE+SLOT+IUSE logic <<----
115
116 if tc_version_is_at_least 4.6 ; then
117         LICENSE="GPL-3+ LGPL-3+ || ( GPL-3+ libgcc libstdc++ gcc-runtime-library-exception-3.1 ) FDL-1.3+"
118 elif tc_version_is_at_least 4.4 ; then
119         LICENSE="GPL-3+ LGPL-3+ || ( GPL-3+ libgcc libstdc++ gcc-runtime-library-exception-3.1 ) FDL-1.2+"
120 elif tc_version_is_at_least 4.3 ; then
121         LICENSE="GPL-3+ LGPL-3+ || ( GPL-3+ libgcc libstdc++ ) FDL-1.2+"
122 elif tc_version_is_at_least 4.2 ; then
123         LICENSE="GPL-3+ LGPL-2.1+ || ( GPL-3+ libgcc libstdc++ ) FDL-1.2+"
124 elif tc_version_is_at_least 3.3 ; then
125         LICENSE="GPL-2+ LGPL-2.1+ FDL-1.2+"
126 else
127         LICENSE="GPL-2+ LGPL-2.1+ FDL-1.1+"
128 fi
129
130 if tc_version_is_at_least 8.3; then
131         GCC_EBUILD_TEST_FLAG='test'
132 else
133         # Don't force USE regression-test->test change on every
134         # gcc ebuild just yet. Let's do the change when >=gcc-8.3
135         # is commonly used as a main compiler.
136         GCC_EBUILD_TEST_FLAG='regression-test'
137 fi
138 IUSE="${GCC_EBUILD_TEST_FLAG} vanilla +nls"
139
140 TC_FEATURES=()
141
142 tc_has_feature() {
143         has "$1" "${TC_FEATURES[@]}"
144 }
145
146 if [[ ${PN} != "kgcc64" && ${PN} != gcc-* ]] ; then
147         IUSE+=" altivec debug +cxx +fortran +nptl" TC_FEATURES+=(fortran nptl)
148         [[ -n ${PIE_VER} ]] && IUSE+=" nopie"
149         [[ -n ${HTB_VER} ]] && IUSE+=" boundschecking"
150         [[ -n ${D_VER}   ]] && IUSE+=" d"
151         [[ -n ${SPECS_VER} ]] && IUSE+=" nossp"
152         tc_version_is_at_least 3 && IUSE+=" doc hardened multilib objc"
153         tc_version_is_between 3 7 && IUSE+=" awt gcj" TC_FEATURES+=(gcj)
154         tc_version_is_at_least 3.3 && IUSE+=" pgo"
155         tc_version_is_at_least 4.0 &&
156                 IUSE+=" objc-gc" TC_FEATURES+=(objc-gc)
157         tc_version_is_between 4.0 4.9 && IUSE+=" mudflap"
158         tc_version_is_at_least 4.1 && IUSE+=" libssp objc++"
159         tc_version_is_at_least 4.2 && IUSE+=" +openmp"
160         tc_version_is_at_least 4.3 && IUSE+=" fixed-point"
161         tc_version_is_at_least 4.7 && IUSE+=" go"
162         # Note: while <=gcc-4.7 also supported graphite, it required forked ppl
163         # versions which we dropped.  Since graphite was also experimental in
164         # the older versions, we don't want to bother supporting it.  #448024
165         tc_version_is_at_least 4.8 &&
166                 IUSE+=" graphite +sanitize" TC_FEATURES+=(graphite)
167         tc_version_is_between 4.9 8 && IUSE+=" cilk"
168         tc_version_is_at_least 4.9 && IUSE+=" +vtv"
169         tc_version_is_at_least 5.0 && IUSE+=" jit"
170         tc_version_is_between 5.0 9 && IUSE+=" mpx"
171         tc_version_is_at_least 6.0 && IUSE+=" +pie +ssp +pch"
172         # systemtap is a gentoo-specific switch: bug #654748
173         tc_version_is_at_least 8.0 &&
174                 IUSE+=" systemtap" TC_FEATURES+=(systemtap)
175         tc_version_is_at_least 9.0 && IUSE+=" d"
176         tc_version_is_at_least 9.1 && IUSE+=" lto"
177 fi
178
179 SLOT="${GCC_CONFIG_VER}"
180
181 #---->> DEPEND <<----
182
183 RDEPEND="sys-libs/zlib
184         nls? ( virtual/libintl )"
185
186 tc_version_is_at_least 3 && RDEPEND+=" virtual/libiconv"
187
188 if tc_version_is_at_least 4 ; then
189         GMP_MPFR_DEPS=">=dev-libs/gmp-4.3.2:0= >=dev-libs/mpfr-2.4.2:0="
190         if tc_version_is_at_least 4.3 ; then
191                 RDEPEND+=" ${GMP_MPFR_DEPS}"
192         elif tc_has_feature fortran ; then
193                 RDEPEND+=" fortran? ( ${GMP_MPFR_DEPS} )"
194         fi
195 fi
196
197 tc_version_is_at_least 4.5 && RDEPEND+=" >=dev-libs/mpc-0.8.1:0="
198
199 if tc_has_feature objc-gc ; then
200         if tc_version_is_at_least 7 ; then
201                 RDEPEND+=" objc-gc? ( >=dev-libs/boehm-gc-7.4.2 )"
202         fi
203 fi
204
205 if tc_has_feature graphite ; then
206         if tc_version_is_at_least 5.0 ; then
207                 RDEPEND+=" graphite? ( >=dev-libs/isl-0.14:0= )"
208         elif tc_version_is_at_least 4.8 ; then
209                 RDEPEND+="
210                         graphite? (
211                                 >=dev-libs/cloog-0.18.0:0=
212                                 >=dev-libs/isl-0.11.1:0=
213                         )"
214         fi
215 fi
216
217 DEPEND="${RDEPEND}
218         >=sys-devel/bison-1.875
219         >=sys-devel/flex-2.5.4
220         nls? ( sys-devel/gettext )
221         ${GCC_EBUILD_TEST_FLAG}? (
222                 >=dev-util/dejagnu-1.4.4
223                 >=sys-devel/autogen-5.5.4
224         )"
225
226 if tc_has_feature gcj ; then
227         GCJ_DEPS=">=media-libs/libart_lgpl-2.1"
228         GCJ_GTK_DEPS="
229                 x11-base/xorg-proto
230                 x11-libs/libXt
231                 x11-libs/libX11
232                 x11-libs/libXtst
233                 =x11-libs/gtk+-2*
234                 virtual/pkgconfig
235         "
236         tc_version_is_at_least 3.4 && GCJ_GTK_DEPS+=" x11-libs/pango"
237         tc_version_is_at_least 4.2 && GCJ_DEPS+=" app-arch/zip app-arch/unzip"
238         DEPEND+=" gcj? ( awt? ( ${GCJ_GTK_DEPS} ) ${GCJ_DEPS} )"
239 fi
240
241 if tc_has_feature systemtap ; then
242         # gcc needs sys/sdt.h headers on target
243         DEPEND+=" systemtap? ( dev-util/systemtap )"
244 fi
245
246 PDEPEND=">=sys-devel/gcc-config-1.7"
247
248 #---->> S + SRC_URI essentials <<----
249
250 # Set the source directory depending on whether we're using
251 # a prerelease, snapshot, or release tarball.
252 S=$(
253         if [[ -n ${PRERELEASE} ]] ; then
254                 echo ${WORKDIR}/gcc-${PRERELEASE}
255         elif [[ -n ${SNAPSHOT} ]] ; then
256                 echo ${WORKDIR}/gcc-${SNAPSHOT}
257         else
258                 echo ${WORKDIR}/gcc-${GCC_RELEASE_VER}
259         fi
260 )
261
262 gentoo_urls() {
263         local devspace="HTTP~vapier/dist/URI HTTP~rhill/dist/URI
264         HTTP~zorry/patches/gcc/URI HTTP~blueness/dist/URI
265         HTTP~tamiko/distfiles/URI HTTP~slyfox/distfiles/URI"
266         devspace=${devspace//HTTP/https:\/\/dev.gentoo.org\/}
267         echo mirror://gentoo/$1 ${devspace//URI/$1}
268 }
269
270 # This function handles the basics of setting the SRC_URI for a gcc ebuild.
271 # To use, set SRC_URI with:
272 #
273 #       SRC_URI="$(get_gcc_src_uri)"
274 #
275 # Other than the variables normally set by portage, this function's behavior
276 # can be altered by setting the following:
277 #
278 #       SNAPSHOT
279 #                       If set, this variable signals that we should be using a snapshot of
280 #                       gcc. It is expected to be in the format "YYYY-MM-DD". Note that if
281 #                       the ebuild has a _pre suffix, this variable is ignored and the
282 #                       prerelease tarball is used instead.
283 #
284 #       PATCH_VER
285 #       PATCH_GCC_VER
286 #                       This should be set to the version of the gentoo patch tarball.
287 #                       The resulting filename of this tarball will be:
288 #                       gcc-${PATCH_GCC_VER:-${GCC_RELEASE_VER}}-patches-${PATCH_VER}.tar.bz2
289 #
290 #       PIE_VER
291 #       PIE_GCC_VER
292 #                       These variables control patching in various updates for the logic
293 #                       controlling Position Independant Executables. PIE_VER is expected
294 #                       to be the version of this patch, and PIE_GCC_VER the gcc version of
295 #                       the patch:
296 #                       An example:
297 #                                       PIE_VER="8.7.6.5"
298 #                                       PIE_GCC_VER="3.4.0"
299 #                       The resulting filename of this tarball will be:
300 #                       gcc-${PIE_GCC_VER:-${GCC_RELEASE_VER}}-piepatches-v${PIE_VER}.tar.bz2
301 #
302 #       SPECS_VER
303 #       SPECS_GCC_VER
304 #                       This is for the minispecs files included in the hardened gcc-4.x
305 #                       The specs files for hardenedno*, vanilla and for building the "specs" file.
306 #                       SPECS_VER is expected to be the version of this patch, SPECS_GCC_VER
307 #                       the gcc version of the patch.
308 #                       An example:
309 #                                       SPECS_VER="8.7.6.5"
310 #                                       SPECS_GCC_VER="3.4.0"
311 #                       The resulting filename of this tarball will be:
312 #                       gcc-${SPECS_GCC_VER:-${GCC_RELEASE_VER}}-specs-${SPECS_VER}.tar.bz2
313 #
314 #       HTB_VER
315 #       HTB_GCC_VER
316 #                       These variables control whether or not an ebuild supports Herman
317 #                       ten Brugge's bounds-checking patches. If you want to use a patch
318 #                       for an older gcc version with a new gcc, make sure you set
319 #                       HTB_GCC_VER to that version of gcc.
320 #
321 #       CYGWINPORTS_GITREV
322 #                       If set, this variable signals that we should apply additional patches
323 #                       maintained by upstream Cygwin developers at github/cygwinports/gcc,
324 #                       using the specified git commit id there.  The list of patches to
325 #                       apply is extracted from gcc.cygport, maintained there as well.
326 #                       This is done for compilers running on Cygwin, not for cross compilers
327 #                       with a Cygwin target.
328 get_gcc_src_uri() {
329         export PATCH_GCC_VER=${PATCH_GCC_VER:-${GCC_RELEASE_VER}}
330         export UCLIBC_GCC_VER=${UCLIBC_GCC_VER:-${PATCH_GCC_VER}}
331         export PIE_GCC_VER=${PIE_GCC_VER:-${GCC_RELEASE_VER}}
332         export HTB_GCC_VER=${HTB_GCC_VER:-${GCC_RELEASE_VER}}
333         export SPECS_GCC_VER=${SPECS_GCC_VER:-${GCC_RELEASE_VER}}
334
335         # Set where to download gcc itself depending on whether we're using a
336         # prerelease, snapshot, or release tarball.
337         if [[ ${PV} == *9999* ]] ; then
338                 # Nothing to do w/git snapshots.
339                 :
340         elif [[ -n ${PRERELEASE} ]] ; then
341                 GCC_SRC_URI="ftp://gcc.gnu.org/pub/gcc/prerelease-${PRERELEASE}/gcc-${PRERELEASE}.tar.bz2"
342         elif [[ -n ${SNAPSHOT} ]] ; then
343                 if tc_version_is_between 5.5 6 || tc_version_is_between 6.4 7 || tc_version_is_at_least 7.2 ; then
344                         GCC_SRC_URI="ftp://gcc.gnu.org/pub/gcc/snapshots/${SNAPSHOT}/gcc-${SNAPSHOT}.tar.xz"
345                 else
346                         GCC_SRC_URI="ftp://gcc.gnu.org/pub/gcc/snapshots/${SNAPSHOT}/gcc-${SNAPSHOT}.tar.bz2"
347                 fi
348         else
349                 if tc_version_is_between 5.5 6 || tc_version_is_between 6.4 7 || tc_version_is_at_least 7.2 ; then
350                         GCC_SRC_URI="mirror://gnu/gcc/gcc-${GCC_PV}/gcc-${GCC_RELEASE_VER}.tar.xz"
351                 else
352                         GCC_SRC_URI="mirror://gnu/gcc/gcc-${GCC_PV}/gcc-${GCC_RELEASE_VER}.tar.bz2"
353                 fi
354         fi
355
356         [[ -n ${UCLIBC_VER} ]] && \
357                 GCC_SRC_URI+=" $(gentoo_urls gcc-${UCLIBC_GCC_VER}-uclibc-patches-${UCLIBC_VER}.tar.bz2)"
358         [[ -n ${PATCH_VER} ]] && \
359                 GCC_SRC_URI+=" $(gentoo_urls gcc-${PATCH_GCC_VER}-patches-${PATCH_VER}.tar.bz2)"
360
361         [[ -n ${PIE_VER} ]] && \
362                 PIE_CORE=${PIE_CORE:-gcc-${PIE_GCC_VER}-piepatches-v${PIE_VER}.tar.bz2} && \
363                 GCC_SRC_URI+=" $(gentoo_urls ${PIE_CORE})"
364
365         # gcc minispec for the hardened gcc 4 compiler
366         [[ -n ${SPECS_VER} ]] && \
367                 GCC_SRC_URI+=" $(gentoo_urls gcc-${SPECS_GCC_VER}-specs-${SPECS_VER}.tar.bz2)"
368
369         # gcc bounds checking patch
370         if [[ -n ${HTB_VER} ]] ; then
371                 local HTBFILE="bounds-checking-gcc-${HTB_GCC_VER}-${HTB_VER}.patch.bz2"
372                 GCC_SRC_URI+="
373                         boundschecking? (
374                                 mirror://sourceforge/boundschecking/${HTBFILE}
375                                 $(gentoo_urls ${HTBFILE})
376                         )"
377         fi
378
379         [[ -n ${D_VER} ]] && \
380                 GCC_SRC_URI+=" d? ( mirror://sourceforge/dgcc/gdc-${D_VER}-src.tar.bz2 )"
381
382         if tc_has_feature gcj ; then
383                 if tc_version_is_at_least 4.5 ; then
384                         GCC_SRC_URI+=" gcj? ( ftp://sourceware.org/pub/java/ecj-4.5.jar )"
385                 elif tc_version_is_at_least 4.3 ; then
386                         GCC_SRC_URI+=" gcj? ( ftp://sourceware.org/pub/java/ecj-4.3.jar )"
387                 fi
388         fi
389
390         # Cygwin patches from https://github.com/cygwinports/gcc
391         [[ -n ${CYGWINPORTS_GITREV} ]] && \
392                 GCC_SRC_URI+=" elibc_Cygwin? ( https://github.com/cygwinports/gcc/archive/${CYGWINPORTS_GITREV}.tar.gz
393                         -> gcc-cygwinports-${CYGWINPORTS_GITREV}.tar.gz )"
394
395         echo "${GCC_SRC_URI}"
396 }
397
398 SRC_URI=$(get_gcc_src_uri)
399
400 #---->> pkg_pretend <<----
401
402 toolchain_pkg_pretend() {
403         if [[ -n ${PRERELEASE}${SNAPSHOT} || ${PV} == *9999* ]] &&
404            [[ -z ${I_PROMISE_TO_SUPPLY_PATCHES_WITH_BUGS} ]] ; then
405                 die "Please \`export I_PROMISE_TO_SUPPLY_PATCHES_WITH_BUGS=1\` or define it" \
406                         "in your make.conf if you want to use this version."
407         fi
408
409         if ! use_if_iuse cxx ; then
410                 use_if_iuse go && ewarn 'Go requires a C++ compiler, disabled due to USE="-cxx"'
411                 use_if_iuse objc++ && ewarn 'Obj-C++ requires a C++ compiler, disabled due to USE="-cxx"'
412                 use_if_iuse gcj && ewarn 'GCJ requires a C++ compiler, disabled due to USE="-cxx"'
413         fi
414
415         want_minispecs
416 }
417
418 #---->> pkg_setup <<----
419
420 toolchain_pkg_setup() {
421         # we dont want to use the installed compiler's specs to build gcc
422         unset GCC_SPECS
423         unset LANGUAGES #265283
424 }
425
426 #---->> src_unpack <<----
427
428 toolchain_src_unpack() {
429         if [[ ${PV} == *9999* ]]; then
430                 git-2_src_unpack
431         else
432                 gcc_quick_unpack
433         fi
434 }
435
436 gcc_quick_unpack() {
437         pushd "${WORKDIR}" > /dev/null
438         export PATCH_GCC_VER=${PATCH_GCC_VER:-${GCC_RELEASE_VER}}
439         export UCLIBC_GCC_VER=${UCLIBC_GCC_VER:-${PATCH_GCC_VER}}
440         export PIE_GCC_VER=${PIE_GCC_VER:-${GCC_RELEASE_VER}}
441         export HTB_GCC_VER=${HTB_GCC_VER:-${GCC_RELEASE_VER}}
442         export SPECS_GCC_VER=${SPECS_GCC_VER:-${GCC_RELEASE_VER}}
443
444         # Injection point for more tarballs. dev-lang/gnat-gpl uses
445         # 'GCC_A_FAKEIT' to specify it's own source and binary tarballs.
446         if [[ -n ${GCC_A_FAKEIT} ]] ; then
447                 unpack ${GCC_A_FAKEIT}
448         elif [[ -n ${PRERELEASE} ]] ; then
449                 unpack gcc-${PRERELEASE}.tar.bz2
450         elif [[ -n ${SNAPSHOT} ]] ; then
451                 if tc_version_is_between 5.5 6 || tc_version_is_between 6.4 7 || tc_version_is_at_least 7.2 ; then
452                         unpack gcc-${SNAPSHOT}.tar.xz
453                 else
454                         unpack gcc-${SNAPSHOT}.tar.bz2
455                 fi
456         elif [[ ${PV} != *9999* ]] ; then
457                 if tc_version_is_between 5.5 6 || tc_version_is_between 6.4 7 || tc_version_is_at_least 7.2 ; then
458                         unpack gcc-${GCC_RELEASE_VER}.tar.xz
459                 else
460                         unpack gcc-${GCC_RELEASE_VER}.tar.bz2
461                 fi
462         fi
463
464         if [[ -n ${D_VER} ]] && use d ; then
465                 pushd "${S}"/gcc > /dev/null
466                 unpack gdc-${D_VER}-src.tar.bz2
467                 cd ..
468                 ebegin "Adding support for the D language"
469                 ./gcc/d/setup-gcc.sh >& "${T}"/dgcc.log
470                 if ! eend $? ; then
471                         eerror "The D GCC package failed to apply"
472                         eerror "Please include this log file when posting a bug report:"
473                         eerror "  ${T}/dgcc.log"
474                         die "failed to include the D language"
475                 fi
476                 popd > /dev/null
477         fi
478
479         [[ -n ${PATCH_VER} ]] && \
480                 unpack gcc-${PATCH_GCC_VER}-patches-${PATCH_VER}.tar.bz2
481
482         [[ -n ${UCLIBC_VER} ]] && \
483                 unpack gcc-${UCLIBC_GCC_VER}-uclibc-patches-${UCLIBC_VER}.tar.bz2
484
485         if want_pie ; then
486                 if [[ -n ${PIE_CORE} ]] ; then
487                         unpack ${PIE_CORE}
488                 else
489                         unpack gcc-${PIE_GCC_VER}-piepatches-v${PIE_VER}.tar.bz2
490                 fi
491                 [[ -n ${SPECS_VER} ]] && \
492                         unpack gcc-${SPECS_GCC_VER}-specs-${SPECS_VER}.tar.bz2
493         fi
494
495         use_if_iuse boundschecking && unpack "bounds-checking-gcc-${HTB_GCC_VER}-${HTB_VER}.patch.bz2"
496
497         [[ -n ${CYGWINPORTS_GITREV} ]] && use elibc_Cygwin && unpack "gcc-cygwinports-${CYGWINPORTS_GITREV}.tar.gz"
498
499         popd > /dev/null
500 }
501
502 #---->> src_prepare <<----
503
504 # 'epatch' is not available in EAPI=7. Abstract away patchset application
505 # until we eventually get all gcc ebuilds on EAPI=7 or later.
506 tc_apply_patches() {
507         [[ ${#@} -lt 2 ]] && die "usage: tc_apply_patches <message> <patches...>"
508
509         einfo "$1"; shift
510
511         case ${EAPI:-0} in
512                 # Note: even for EAPI=6 we used 'epatch' semantics. To avoid
513                 # breaking existing ebuilds use 'eapply' only in EAPI=7 or later.
514                 5*|6) epatch "$@" ;;
515                 7) eapply "$@" ;;
516                 *) die "Update apply_patches() for ${EAPI}." ;;
517         esac
518 }
519
520 toolchain_src_prepare() {
521         export BRANDING_GCC_PKGVERSION="Gentoo ${GCC_PVR}"
522         cd "${S}"
523
524         do_gcc_gentoo_patches
525         do_gcc_HTB_patches
526         do_gcc_PIE_patches
527         do_gcc_CYGWINPORTS_patches
528
529         case ${EAPI:-0} in
530                 5*) epatch_user;;
531                 6|7) eapply_user ;;
532                 *) die "Update toolchain_src_prepare() for ${EAPI}." ;;
533         esac
534
535         if ( tc_version_is_at_least 4.8.2 || use_if_iuse hardened ) && ! use vanilla ; then
536                 make_gcc_hard
537         fi
538
539         # install the libstdc++ python into the right location
540         # http://gcc.gnu.org/PR51368
541         if tc_version_is_between 4.5 4.7 ; then
542                 sed -i \
543                         '/^pythondir =/s:=.*:= $(datadir)/python:' \
544                         "${S}"/libstdc++-v3/python/Makefile.in || die
545         fi
546
547         # make sure the pkg config files install into multilib dirs.
548         # since we configure with just one --libdir, we can't use that
549         # (as gcc itself takes care of building multilibs).  #435728
550         find "${S}" -name Makefile.in \
551                 -exec sed -i '/^pkgconfigdir/s:=.*:=$(toolexeclibdir)/pkgconfig:' {} +
552
553         # No idea when this first started being fixed, but let's go with 4.3.x for now
554         if ! tc_version_is_at_least 4.3 ; then
555                 fix_files=""
556                 for x in contrib/test_summary libstdc++-v3/scripts/check_survey.in ; do
557                         [[ -e ${x} ]] && fix_files="${fix_files} ${x}"
558                 done
559                 ht_fix_file ${fix_files} */configure *.sh */Makefile.in
560         fi
561
562         setup_multilib_osdirnames
563         gcc_version_patch
564
565         if tc_version_is_at_least 4.1 ; then
566                 if [[ -n ${SNAPSHOT} || -n ${PRERELEASE} ]] ; then
567                         # BASE-VER must be a three-digit version number
568                         # followed by an optional -pre string
569                         #   eg. 4.5.1, 4.6.2-pre20120213, 4.7.0-pre9999
570                         # If BASE-VER differs from ${PV/_/-} then libraries get installed in
571                         # the wrong directory.
572                         echo ${PV/_/-} > "${S}"/gcc/BASE-VER
573                 fi
574         fi
575
576         # >= gcc-4.3 doesn't bundle ecj.jar, so copy it
577         if tc_version_is_at_least 4.3 && use_if_iuse gcj ; then
578                 if tc_version_is_at_least 4.5 ; then
579                         einfo "Copying ecj-4.5.jar"
580                         cp -pPR "${DISTDIR}/ecj-4.5.jar" "${S}/ecj.jar" || die
581                 else
582                         einfo "Copying ecj-4.3.jar"
583                         cp -pPR "${DISTDIR}/ecj-4.3.jar" "${S}/ecj.jar" || die
584                 fi
585         fi
586
587         # disable --as-needed from being compiled into gcc specs
588         # natively when using a gcc version < 3.4.4
589         # http://gcc.gnu.org/PR14992
590         if ! tc_version_is_at_least 3.4.4 ; then
591                 sed -i -e s/HAVE_LD_AS_NEEDED/USE_LD_AS_NEEDED/g "${S}"/gcc/config.in
592         fi
593
594         # In gcc 3.3.x and 3.4.x, rename the java bins to gcc-specific names
595         # in line with gcc-4.
596         if tc_version_is_between 3.3 4.0 ; then
597                 do_gcc_rename_java_bins
598         fi
599
600         # Prevent libffi from being installed
601         if tc_version_is_between 3.0 4.8 ; then
602                 sed -i -e 's/\(install.*:\) install-.*recursive/\1/' "${S}"/libffi/Makefile.in || die
603                 sed -i -e 's/\(install-data-am:\).*/\1/' "${S}"/libffi/include/Makefile.in || die
604         fi
605
606         # Fixup libtool to correctly generate .la files with portage
607         elibtoolize --portage --shallow --no-uclibc
608
609         gnuconfig_update
610
611         # update configure files
612         local f
613         einfo "Fixing misc issues in configure files"
614         for f in $(grep -l 'autoconf version 2.13' $(find "${S}" -name configure)) ; do
615                 ebegin "  Updating ${f/${S}\/} [LANG]"
616                 patch "${f}" "${FILESDIR}"/gcc-configure-LANG.patch >& "${T}"/configure-patch.log \
617                         || eerror "Please file a bug about this"
618                 eend $?
619         done
620         sed -i 's|A-Za-z0-9|[:alnum:]|g' "${S}"/gcc/*.awk #215828
621
622         # Prevent new texinfo from breaking old versions (see #198182, #464008)
623         if tc_version_is_at_least 4.1; then
624                 tc_apply_patches "Remove texinfo (bug #198182, bug ##464008)" "${FILESDIR}"/gcc-configure-texinfo.patch
625         fi
626
627         # >=gcc-4
628         if [[ -x contrib/gcc_update ]] ; then
629                 einfo "Touching generated files"
630                 ./contrib/gcc_update --touch | \
631                         while read f ; do
632                                 einfo "  ${f%%...}"
633                         done
634         fi
635 }
636
637 do_gcc_gentoo_patches() {
638         if ! use vanilla ; then
639                 if [[ -n ${PATCH_VER} ]] ; then
640                         tc_apply_patches "Applying Gentoo patches ..." "${WORKDIR}"/patch/*.patch
641                         BRANDING_GCC_PKGVERSION="${BRANDING_GCC_PKGVERSION} p${PATCH_VER}"
642                 fi
643                 if [[ -n ${UCLIBC_VER} ]] ; then
644                         tc_apply_patches "Applying uClibc patches ..." "${WORKDIR}"/uclibc/*.patch
645                 fi
646         fi
647 }
648
649 do_gcc_HTB_patches() {
650         use_if_iuse boundschecking || return 0
651
652         # modify the bounds checking patch with a regression patch
653         tc_apply_patches "Bounds checking patch" "${WORKDIR}/bounds-checking-gcc-${HTB_GCC_VER}-${HTB_VER}.patch"
654         BRANDING_GCC_PKGVERSION="${BRANDING_GCC_PKGVERSION}, HTB-${HTB_GCC_VER}-${HTB_VER}"
655 }
656
657 do_gcc_PIE_patches() {
658         want_pie || return 0
659         use vanilla && return 0
660
661         tc_apply_patches "Applying pie patches ..." "${WORKDIR}"/piepatch/*.patch
662
663         BRANDING_GCC_PKGVERSION="${BRANDING_GCC_PKGVERSION}, pie-${PIE_VER}"
664 }
665
666 do_gcc_CYGWINPORTS_patches() {
667         [[ -n ${CYGWINPORTS_GITREV} ]] || return 0
668         use elibc_Cygwin || return 0
669
670         local p d="${WORKDIR}/gcc-${CYGWINPORTS_GITREV}"
671         # readarray -t is available since bash-4.4 only, #690686
672         local patches=( $(sed -e '1,/PATCH_URI="/d;/"/,$d' < "${d}"/gcc.cygport) )
673         tc_apply_patches "Applying cygwin port patches ..." ${patches[*]}
674 }
675
676 # configure to build with the hardened GCC specs as the default
677 make_gcc_hard() {
678         local gcc_hard_flags=""
679
680         # If we use gcc-6 or newer with pie enable to compile older gcc we need to pass -no-pie
681         # to stage1; bug #618908
682         if ! tc_version_is_at_least 6.0 && [[ $(gcc-major-version) -ge 6 ]] ; then
683                 einfo "Disabling PIE in stage1 (only) ..."
684                 sed -i -e "/^STAGE1_LDFLAGS/ s/$/ -no-pie/" "${S}"/Makefile.in || die
685         fi
686
687         # Gcc >= 6.X we can use configurations options to turn pie/ssp on as default
688         if tc_version_is_at_least 6.0 ; then
689                 if use_if_iuse pie ; then
690                         einfo "Updating gcc to use automatic PIE building ..."
691                 fi
692                 if use_if_iuse ssp ; then
693                         einfo "Updating gcc to use automatic SSP building ..."
694                 fi
695                 if use_if_iuse hardened ; then
696                         # Will add some hardened options as default, like:
697                         # -fstack-clash-protection
698                         # -z now
699                         # see *_all_extra-options.patch gcc patches.
700                         gcc_hard_flags+=" -DEXTRA_OPTIONS"
701                         # rebrand to make bug reports easier
702                         BRANDING_GCC_PKGVERSION=${BRANDING_GCC_PKGVERSION/Gentoo/Gentoo Hardened}
703                 fi
704         else
705                 if use_if_iuse hardened ; then
706                         # rebrand to make bug reports easier
707                         BRANDING_GCC_PKGVERSION=${BRANDING_GCC_PKGVERSION/Gentoo/Gentoo Hardened}
708                         if hardened_gcc_works ; then
709                                 einfo "Updating gcc to use automatic PIE + SSP building ..."
710                                 gcc_hard_flags+=" -DEFAULT_PIE_SSP"
711                         elif hardened_gcc_works pie ; then
712                                 einfo "Updating gcc to use automatic PIE building ..."
713                                 ewarn "SSP has not been enabled by default"
714                                 gcc_hard_flags+=" -DEFAULT_PIE"
715                         elif hardened_gcc_works ssp ; then
716                                 einfo "Updating gcc to use automatic SSP building ..."
717                                 ewarn "PIE has not been enabled by default"
718                                 gcc_hard_flags+=" -DEFAULT_SSP"
719                         else
720                                 # do nothing if hardened isn't supported, but don't die either
721                                 ewarn "hardened is not supported for this arch in this gcc version"
722                                 return 0
723                         fi
724                 else
725                         if hardened_gcc_works ssp ; then
726                                 einfo "Updating gcc to use automatic SSP building ..."
727                                 gcc_hard_flags+=" -DEFAULT_SSP"
728                         fi
729                 fi
730         fi
731
732         # we want to be able to control the pie patch logic via something other
733         # than ALL_CFLAGS...
734         sed -e '/^ALL_CFLAGS/iHARD_CFLAGS = ' \
735                 -e 's|^ALL_CFLAGS = |ALL_CFLAGS = $(HARD_CFLAGS) |' \
736                 -i "${S}"/gcc/Makefile.in
737         # Need to add HARD_CFLAGS to ALL_CXXFLAGS on >= 4.7
738         if tc_version_is_at_least 4.7 ; then
739                 sed -e '/^ALL_CXXFLAGS/iHARD_CFLAGS = ' \
740                         -e 's|^ALL_CXXFLAGS = |ALL_CXXFLAGS = $(HARD_CFLAGS) |' \
741                         -i "${S}"/gcc/Makefile.in
742         fi
743
744         sed -i \
745                 -e "/^HARD_CFLAGS = /s|=|= ${gcc_hard_flags} |" \
746                 "${S}"/gcc/Makefile.in || die
747
748 }
749
750 # This is a historical wart.  The original Gentoo/amd64 port used:
751 #    lib32 - 32bit binaries (x86)
752 #    lib64 - 64bit binaries (x86_64)
753 #    lib   - "native" binaries (a symlink to lib64)
754 # Most other distros use the logic (including mainline gcc):
755 #    lib   - 32bit binaries (x86)
756 #    lib64 - 64bit binaries (x86_64)
757 # Over time, Gentoo is migrating to the latter form.
758 #
759 # Unfortunately, due to distros picking the lib32 behavior, newer gcc
760 # versions will dynamically detect whether to use lib or lib32 for its
761 # 32bit multilib.  So, to keep the automagic from getting things wrong
762 # while people are transitioning from the old style to the new style,
763 # we always set the MULTILIB_OSDIRNAMES var for relevant targets.
764 setup_multilib_osdirnames() {
765         is_multilib || return 0
766
767         local config
768         local libdirs="../lib64 ../lib32"
769
770         # this only makes sense for some Linux targets
771         case ${CTARGET} in
772         x86_64*-linux*)    config="i386" ;;
773         powerpc64*-linux*) config="rs6000" ;;
774         sparc64*-linux*)   config="sparc" ;;
775         s390x*-linux*)     config="s390" ;;
776         *)                     return 0 ;;
777         esac
778         config+="/t-linux64"
779
780         local sed_args=()
781         if tc_version_is_at_least 4.6 ; then
782                 sed_args+=( -e 's:$[(]call if_multiarch[^)]*[)]::g' )
783         fi
784         if [[ ${SYMLINK_LIB} == "yes" ]] ; then
785                 einfo "updating multilib directories to be: ${libdirs}"
786                 if tc_version_is_at_least 4.6.4 || tc_version_is_at_least 4.7 ; then
787                         sed_args+=( -e '/^MULTILIB_OSDIRNAMES.*lib32/s:[$][(]if.*):../lib32:' )
788                 else
789                         sed_args+=( -e "/^MULTILIB_OSDIRNAMES/s:=.*:= ${libdirs}:" )
790                 fi
791         else
792                 einfo "using upstream multilib; disabling lib32 autodetection"
793                 sed_args+=( -r -e 's:[$][(]if.*,(.*)[)]:\1:' )
794         fi
795         sed -i "${sed_args[@]}" "${S}"/gcc/config/${config} || die
796 }
797
798 gcc_version_patch() {
799         # gcc-4.3+ has configure flags (whoo!)
800         tc_version_is_at_least 4.3 && return 0
801
802         local version_string=${GCC_CONFIG_VER}
803
804         einfo "patching gcc version: ${version_string} (${BRANDING_GCC_PKGVERSION})"
805
806         local gcc_sed=( -e 's:gcc\.gnu\.org/bugs\.html:bugs\.gentoo\.org/:' )
807         if grep -qs VERSUFFIX "${S}"/gcc/version.c ; then
808                 gcc_sed+=( -e "/VERSUFFIX \"\"/s:\"\":\" (${BRANDING_GCC_PKGVERSION})\":" )
809         else
810                 version_string="${version_string} (${BRANDING_GCC_PKGVERSION})"
811                 gcc_sed+=( -e "/const char version_string\[\] = /s:= \".*\":= \"${version_string}\":" )
812         fi
813         sed -i "${gcc_sed[@]}" "${S}"/gcc/version.c || die
814 }
815
816 do_gcc_rename_java_bins() {
817         # bug #139918 - conflict between gcc and java-config-2 for ownership of
818         # /usr/bin/rmi{c,registry}.      Done with mv & sed rather than a patch
819         # because patches would be large (thanks to the rename of man files),
820         # and it's clear from the sed invocations that all that changes is the
821         # rmi{c,registry} names to grmi{c,registry} names.
822         # Kevin F. Quinn 2006-07-12
823         einfo "Renaming jdk executables rmic and rmiregistry to grmic and grmiregistry."
824         # 1) Move the man files if present (missing prior to gcc-3.4)
825         for manfile in rmic rmiregistry ; do
826                 [[ -f ${S}/gcc/doc/${manfile}.1 ]] || continue
827                 mv "${S}"/gcc/doc/${manfile}.1 "${S}"/gcc/doc/g${manfile}.1
828         done
829         # 2) Fixup references in the docs if present (mission prior to gcc-3.4)
830         for jfile in gcc/doc/gcj.info gcc/doc/grmic.1 gcc/doc/grmiregistry.1 gcc/java/gcj.texi ; do
831                 [[ -f ${S}/${jfile} ]] || continue
832                 sed -i -e 's:rmiregistry:grmiregistry:g' "${S}"/${jfile} ||
833                         die "Failed to fixup file ${jfile} for rename to grmiregistry"
834                 sed -i -e 's:rmic:grmic:g' "${S}"/${jfile} ||
835                         die "Failed to fixup file ${jfile} for rename to grmic"
836         done
837         # 3) Fixup Makefiles to build the changed executable names
838         #        These are present in all 3.x versions, and are the important bit
839         #        to get gcc to build with the new names.
840         for jfile in libjava/Makefile.am libjava/Makefile.in gcc/java/Make-lang.in ; do
841                 sed -i -e 's:rmiregistry:grmiregistry:g' "${S}"/${jfile} ||
842                         die "Failed to fixup file ${jfile} for rename to grmiregistry"
843                 # Careful with rmic on these files; it's also the name of a directory
844                 # which should be left unchanged.  Replace occurrences of 'rmic$',
845                 # 'rmic_' and 'rmic '.
846                 sed -i -e 's:rmic\([$_ ]\):grmic\1:g' "${S}"/${jfile} ||
847                         die "Failed to fixup file ${jfile} for rename to grmic"
848         done
849 }
850
851 #---->> src_configure <<----
852
853 toolchain_src_configure() {
854         downgrade_arch_flags
855         gcc_do_filter_flags
856
857         einfo "CFLAGS=\"${CFLAGS}\""
858         einfo "CXXFLAGS=\"${CXXFLAGS}\""
859         einfo "LDFLAGS=\"${LDFLAGS}\""
860
861         # Force internal zip based jar script to avoid random
862         # issues with 3rd party jar implementations.  #384291
863         export JAR=no
864
865         # For hardened gcc 4.3 piepatchset to build the hardened specs
866         # file (build.specs) to use when building gcc.
867         if ! tc_version_is_at_least 4.4 && want_minispecs ; then
868                 setup_minispecs_gcc_build_specs
869         fi
870
871         local confgcc=( --host=${CHOST} )
872
873         if is_crosscompile || tc-is-cross-compiler ; then
874                 # Straight from the GCC install doc:
875                 # "GCC has code to correctly determine the correct value for target
876                 # for nearly all native systems. Therefore, we highly recommend you
877                 # not provide a configure target when configuring a native compiler."
878                 confgcc+=( --target=${CTARGET} )
879         fi
880         [[ -n ${CBUILD} ]] && confgcc+=( --build=${CBUILD} )
881
882         confgcc+=(
883                 --prefix="${PREFIX}"
884                 --bindir="${BINPATH}"
885                 --includedir="${INCLUDEPATH}"
886                 --datadir="${DATAPATH}"
887                 --mandir="${DATAPATH}/man"
888                 --infodir="${DATAPATH}/info"
889                 --with-gxx-include-dir="${STDCXX_INCDIR}"
890         )
891
892         # Stick the python scripts in their own slotted directory (bug #279252)
893         #
894         #  --with-python-dir=DIR
895         #  Specifies where to install the Python modules used for aot-compile. DIR
896         #  should not include the prefix used in installation. For example, if the
897         #  Python modules are to be installed in /usr/lib/python2.5/site-packages,
898         #  then --with-python-dir=/lib/python2.5/site-packages should be passed.
899         #
900         # This should translate into "/share/gcc-data/${CTARGET}/${GCC_CONFIG_VER}/python"
901         if tc_version_is_at_least 4.4 ; then
902                 confgcc+=( --with-python-dir=${DATAPATH/$PREFIX/}/python )
903         fi
904
905         ### language options
906
907         local GCC_LANG="c"
908         is_cxx && GCC_LANG+=",c++"
909         is_d   && GCC_LANG+=",d"
910         is_gcj && GCC_LANG+=",java"
911         is_go  && GCC_LANG+=",go"
912         is_jit && GCC_LANG+=",jit"
913         if is_objc || is_objcxx ; then
914                 GCC_LANG+=",objc"
915                 if tc_version_is_at_least 4 ; then
916                         use objc-gc && confgcc+=( --enable-objc-gc )
917                 fi
918                 is_objcxx && GCC_LANG+=",obj-c++"
919         fi
920
921         # fortran support just got sillier! the lang value can be f77 for
922         # fortran77, f95 for fortran95, or just plain old fortran for the
923         # currently supported standard depending on gcc version.
924         is_fortran && GCC_LANG+=",fortran"
925         is_f77 && GCC_LANG+=",f77"
926         is_f95 && GCC_LANG+=",f95"
927
928         # We do NOT want 'ADA support' in here!
929         # is_ada && GCC_LANG+=",ada"
930
931         confgcc+=( --enable-languages=${GCC_LANG} )
932
933         ### general options
934
935         confgcc+=(
936                 --enable-obsolete
937                 --enable-secureplt
938                 --disable-werror
939                 --with-system-zlib
940         )
941
942         if use nls ; then
943                 confgcc+=( --enable-nls --without-included-gettext )
944         else
945                 confgcc+=( --disable-nls )
946         fi
947
948         tc_version_is_at_least 3.4 || confgcc+=( --disable-libunwind-exceptions )
949
950         # Use the default ("release") checking because upstream usually neglects
951         # to test "disabled" so it has a history of breaking. bug #317217
952         if tc_version_is_at_least 3.4 && in_iuse debug ; then
953                 # The "release" keyword is new to 4.0. bug #551636
954                 local off=$(tc_version_is_at_least 4.0 && echo release || echo no)
955                 confgcc+=( --enable-checking="${GCC_CHECKS_LIST:-$(usex debug yes ${off})}" )
956         fi
957
958         # Branding
959         tc_version_is_at_least 4.3 && confgcc+=(
960                 --with-bugurl=https://bugs.gentoo.org/
961                 --with-pkgversion="${BRANDING_GCC_PKGVERSION}"
962         )
963
964         # If we want hardened support with the newer piepatchset for >=gcc 4.4
965         if tc_version_is_at_least 4.4 && want_minispecs && in_iuse hardened ; then
966                 confgcc+=( $(use_enable hardened esp) )
967         fi
968
969         # allow gcc to search for clock funcs in the main C lib.
970         # if it can't find them, then tough cookies -- we aren't
971         # going to link in -lrt to all C++ apps. bug #411681
972         if tc_version_is_at_least 4.4 && is_cxx ; then
973                 confgcc+=( --enable-libstdcxx-time )
974         fi
975
976         # Build compiler itself using LTO
977         if tc_version_is_at_least 9.1 && use_if_iuse lto ; then
978                 confgcc+=( --with-build-config=bootstrap-lto )
979         fi
980
981         # Support to disable pch when building libstdcxx
982         if tc_version_is_at_least 6.0 && ! use_if_iuse pch ; then
983                 confgcc+=( --disable-libstdcxx-pch )
984         fi
985
986         # The jit language requires this.
987         is_jit && confgcc+=( --enable-host-shared )
988
989         # build-id was disabled for file collisions: bug #526144
990         #
991         # # Turn on the -Wl,--build-id flag by default for ELF targets. bug #525942
992         # # This helps with locating debug files.
993         # case ${CTARGET} in
994         # *-linux-*|*-elf|*-eabi)
995         #       tc_version_is_at_least 4.5 && confgcc+=(
996         #               --enable-linker-build-id
997         #       )
998         #       ;;
999         # esac
1000
1001         # newer gcc versions like to bootstrap themselves with C++,
1002         # so we need to manually disable it ourselves
1003         if tc_version_is_between 4.7 4.8 && ! is_cxx ; then
1004                 confgcc+=( --disable-build-with-cxx --disable-build-poststage1-with-cxx )
1005         fi
1006
1007         ### Cross-compiler options
1008         if is_crosscompile ; then
1009                 # Enable build warnings by default with cross-compilers when system
1010                 # paths are included (e.g. via -I flags).
1011                 confgcc+=( --enable-poison-system-directories )
1012
1013                 # When building a stage1 cross-compiler (just C compiler), we have to
1014                 # disable a bunch of features or gcc goes boom
1015                 local needed_libc=""
1016                 case ${CTARGET} in
1017                 *-linux)                 needed_libc=error-unknown-libc;;
1018                 *-dietlibc)              needed_libc=dietlibc;;
1019                 *-elf|*-eabi)
1020                         needed_libc=newlib
1021                         # Bare-metal targets don't have access to clock_gettime()
1022                         # arm-none-eabi example: bug #589672
1023                         # But we explicitly do --enable-libstdcxx-time above.
1024                         # Undoing it here.
1025                         confgcc+=( --disable-libstdcxx-time )
1026                         ;;
1027                 *-freebsd*)              needed_libc=freebsd-lib;;
1028                 *-gnu*)                  needed_libc=glibc;;
1029                 *-klibc)                 needed_libc=klibc;;
1030                 *-musl*)                 needed_libc=musl;;
1031                 *-uclibc*)
1032                         # Enable shared library support only on targets
1033                         # that support it: bug #291870
1034                         if ! echo '#include <features.h>' | \
1035                            $(tc-getCPP ${CTARGET}) -E -dD - 2>/dev/null | \
1036                            grep -q __HAVE_SHARED__
1037                         then
1038                                 confgcc+=( --disable-shared )
1039                         fi
1040                         needed_libc=uclibc-ng
1041                         ;;
1042                 *-cygwin)                needed_libc=cygwin;;
1043                 x86_64-*-mingw*|\
1044                 *-w64-mingw*)    needed_libc=mingw64-runtime;;
1045                 avr)                     confgcc+=( --enable-shared --disable-threads );;
1046                 esac
1047                 if [[ -n ${needed_libc} ]] ; then
1048                         local confgcc_no_libc=( --disable-shared )
1049                         tc_version_is_at_least 4.8 && confgcc_no_libc+=( --disable-libatomic )
1050                         if ! has_version ${CATEGORY}/${needed_libc} ; then
1051                                 confgcc+=(
1052                                         "${confgcc_no_libc[@]}"
1053                                         --disable-threads
1054                                         --without-headers
1055                                 )
1056                         elif has_version "${CATEGORY}/${needed_libc}[headers-only(-)]" ; then
1057                                 confgcc+=(
1058                                         "${confgcc_no_libc[@]}"
1059                                         --with-sysroot="${PREFIX}"/${CTARGET}
1060                                 )
1061                         else
1062                                 confgcc+=( --with-sysroot="${PREFIX}"/${CTARGET} )
1063                         fi
1064                 fi
1065
1066                 tc_version_is_at_least 4.2 && confgcc+=( --disable-bootstrap )
1067         else
1068                 if tc-is-static-only ; then
1069                         confgcc+=( --disable-shared )
1070                 else
1071                         confgcc+=( --enable-shared )
1072                 fi
1073                 case ${CHOST} in
1074                 mingw*|*-mingw*)
1075                         confgcc+=( --enable-threads=win32 ) ;;
1076                 *)
1077                         confgcc+=( --enable-threads=posix ) ;;
1078                 esac
1079         fi
1080
1081         # __cxa_atexit is "essential for fully standards-compliant handling of
1082         # destructors", but apparently requires glibc.
1083         case ${CTARGET} in
1084         *-uclibc*)
1085                 if tc_has_feature nptl ; then
1086                         confgcc+=(
1087                                 --disable-__cxa_atexit
1088                                 $(use_enable nptl tls)
1089                         )
1090                 fi
1091                 tc_version_is_between 3.3 3.4 && confgcc+=( --enable-sjlj-exceptions )
1092                 if tc_version_is_between 3.4 4.3 ; then
1093                         confgcc+=( --enable-clocale=uclibc )
1094                 fi
1095                 ;;
1096         *-elf|*-eabi)
1097                 confgcc+=( --with-newlib )
1098                 ;;
1099         *-musl*)
1100                 confgcc+=( --enable-__cxa_atexit )
1101                 ;;
1102         *-gnu*)
1103                 confgcc+=(
1104                         --enable-__cxa_atexit
1105                         --enable-clocale=gnu
1106                 )
1107                 ;;
1108         *-freebsd*)
1109                 confgcc+=( --enable-__cxa_atexit )
1110                 ;;
1111         *-solaris*)
1112                 confgcc+=( --enable-__cxa_atexit )
1113                 ;;
1114         esac
1115
1116         ### arch options
1117
1118         gcc-multilib-configure
1119
1120         # ppc altivec support
1121         in_iuse altivec && confgcc+=( $(use_enable altivec) )
1122
1123         # gcc has fixed-point arithmetic support in 4.3 for mips targets that can
1124         # significantly increase compile time by several hours.  This will allow
1125         # users to control this feature in the event they need the support.
1126         tc_version_is_at_least 4.3 && in_iuse fixed-point && confgcc+=( $(use_enable fixed-point) )
1127
1128         case $(tc-is-softfloat) in
1129         yes)    confgcc+=( --with-float=soft ) ;;
1130         softfp) confgcc+=( --with-float=softfp ) ;;
1131         *)
1132                 # If they've explicitly opt-ed in, do hardfloat,
1133                 # otherwise let the gcc default kick in.
1134                 case ${CTARGET//_/-} in
1135                 *-hardfloat-*|*eabihf) confgcc+=( --with-float=hard ) ;;
1136                 esac
1137         esac
1138
1139         local with_abi_map=()
1140         case $(tc-arch) in
1141         arm)    #264534 #414395
1142                 local a arm_arch=${CTARGET%%-*}
1143                 # Remove trailing endian variations first: eb el be bl b l
1144                 for a in e{b,l} {b,l}e b l ; do
1145                         if [[ ${arm_arch} == *${a} ]] ; then
1146                                 arm_arch=${arm_arch%${a}}
1147                                 break
1148                         fi
1149                 done
1150                 # Convert armv7{a,r,m} to armv7-{a,r,m}
1151                 [[ ${arm_arch} == armv7? ]] && arm_arch=${arm_arch/7/7-}
1152                 # See if this is a valid --with-arch flag
1153                 if (srcdir=${S}/gcc target=${CTARGET} with_arch=${arm_arch};
1154                     . "${srcdir}"/config.gcc) &>/dev/null
1155                 then
1156                         confgcc+=( --with-arch=${arm_arch} )
1157                 fi
1158
1159                 # Make default mode thumb for microcontroller classes #418209
1160                 [[ ${arm_arch} == *-m ]] && confgcc+=( --with-mode=thumb )
1161
1162                 # Enable hardvfp
1163                 if [[ $(tc-is-softfloat) == "no" ]] && \
1164                    [[ ${CTARGET} == armv[67]* ]] && \
1165                    tc_version_is_at_least 4.5
1166                 then
1167                         # Follow the new arm hardfp distro standard by default
1168                         confgcc+=( --with-float=hard )
1169                         case ${CTARGET} in
1170                         armv6*) confgcc+=( --with-fpu=vfp ) ;;
1171                         armv7*) confgcc+=( --with-fpu=vfpv3-d16 ) ;;
1172                         esac
1173                 fi
1174                 ;;
1175         mips)
1176                 # Add --with-abi flags to set default ABI
1177                 confgcc+=( --with-abi=$(gcc-abi-map ${TARGET_DEFAULT_ABI}) )
1178                 ;;
1179         amd64)
1180                 # drop the older/ABI checks once this get's merged into some
1181                 # version of gcc upstream
1182                 if tc_version_is_at_least 4.8 && has x32 $(get_all_abis TARGET) ; then
1183                         confgcc+=( --with-abi=$(gcc-abi-map ${TARGET_DEFAULT_ABI}) )
1184                 fi
1185                 ;;
1186         x86)
1187                 # Default arch for x86 is normally i386, lets give it a bump
1188                 # since glibc will do so based on CTARGET anyways
1189                 confgcc+=( --with-arch=${CTARGET%%-*} )
1190                 ;;
1191         hppa)
1192                 # Enable sjlj exceptions for backward compatibility on hppa
1193                 [[ ${GCCMAJOR} == "3" ]] && confgcc+=( --enable-sjlj-exceptions )
1194                 ;;
1195         ppc)
1196                 # Set up defaults based on current CFLAGS
1197                 is-flagq -mfloat-gprs=double && confgcc+=( --enable-e500-double )
1198                 [[ ${CTARGET//_/-} == *-e500v2-* ]] && confgcc+=( --enable-e500-double )
1199                 ;;
1200         riscv)
1201                 # Add --with-abi flags to set default ABI
1202                 confgcc+=( --with-abi=$(gcc-abi-map ${TARGET_DEFAULT_ABI}) )
1203                 ;;
1204         esac
1205
1206         # if the target can do biarch (-m32/-m64), enable it.  overhead should
1207         # be small, and should simplify building of 64bit kernels in a 32bit
1208         # userland by not needing sys-devel/kgcc64.  #349405
1209         case $(tc-arch) in
1210         ppc|ppc64) tc_version_is_at_least 3.4 && confgcc+=( --enable-targets=all ) ;;
1211         sparc)     tc_version_is_at_least 4.4 && confgcc+=( --enable-targets=all ) ;;
1212         amd64|x86) tc_version_is_at_least 4.3 && confgcc+=( --enable-targets=all ) ;;
1213         esac
1214
1215         # On Darwin we need libdir to be set in order to get correct install names
1216         # for things like libobjc-gnu, libgcj and libfortran.  If we enable it on
1217         # non-Darwin we screw up the behaviour this eclass relies on.  We in
1218         # particular need this over --libdir for bug #255315.
1219         [[ ${CTARGET} == *-darwin* ]] && \
1220                 confgcc+=( --enable-version-specific-runtime-libs )
1221
1222         ### library options
1223
1224         if tc_version_is_between 3.0 7.0 ; then
1225                 if is_gcj ; then
1226                         confgcc+=( --disable-gjdoc )
1227                         use awt && confgcc+=( --enable-java-awt=gtk )
1228                 else
1229                         confgcc+=( --disable-libgcj )
1230                 fi
1231         fi
1232
1233         if tc_version_is_at_least 4.2 ; then
1234                 if in_iuse openmp ; then
1235                         # Make sure target has pthreads support. #326757 #335883
1236                         # There shouldn't be a chicken & egg problem here as openmp won't
1237                         # build without a C library, and you can't build that w/out
1238                         # already having a compiler ...
1239                         if ! is_crosscompile || \
1240                            $(tc-getCPP ${CTARGET}) -E - <<<"#include <pthread.h>" >& /dev/null
1241                         then
1242                                 confgcc+=( $(use_enable openmp libgomp) )
1243                         else
1244                                 # Force disable as the configure script can be dumb #359855
1245                                 confgcc+=( --disable-libgomp )
1246                         fi
1247                 else
1248                         # For gcc variants where we don't want openmp (e.g. kgcc)
1249                         confgcc+=( --disable-libgomp )
1250                 fi
1251         fi
1252
1253         if tc_version_is_at_least 4.0 ; then
1254                 if in_iuse mudflap ; then
1255                         confgcc+=( $(use_enable mudflap libmudflap) )
1256                 else
1257                         confgcc+=( --disable-libmudflap )
1258                 fi
1259
1260                 if use_if_iuse libssp ; then
1261                         confgcc+=( --enable-libssp )
1262                 else
1263                         if hardened_gcc_is_stable ssp; then
1264                                 export gcc_cv_libc_provides_ssp=yes
1265                         fi
1266                         if use_if_iuse ssp; then
1267                                 # On some targets USE="ssp -libssp" is an invalid
1268                                 # configuration as target libc does not provide
1269                                 # stack_chk_* functions. Do not disable libssp there.
1270                                 case ${CTARGET} in
1271                                         mingw*|*-mingw*) ewarn "Not disabling libssp" ;;
1272                                         *) confgcc+=( --disable-libssp ) ;;
1273                                 esac
1274                         else
1275                                 confgcc+=( --disable-libssp )
1276                         fi
1277                 fi
1278         fi
1279
1280         if in_iuse cilk ; then
1281                 confgcc+=( $(use_enable cilk libcilkrts) )
1282         fi
1283
1284         if in_iuse mpx ; then
1285                 confgcc+=( $(use_enable mpx libmpx) )
1286         fi
1287
1288         if in_iuse systemtap ; then
1289                 confgcc+=( $(use_enable systemtap) )
1290         fi
1291
1292         if in_iuse vtv ; then
1293                 confgcc+=(
1294                         $(use_enable vtv vtable-verify)
1295                         # See Note [implicitly enabled flags]
1296                         $(usex vtv '' --disable-libvtv)
1297                 )
1298         fi
1299
1300         # newer gcc's come with libquadmath, but only fortran uses
1301         # it, so auto punt it when we don't care
1302         if tc_version_is_at_least 4.6 && ! is_fortran ; then
1303                 confgcc+=( --disable-libquadmath )
1304         fi
1305
1306         if tc_version_is_at_least 4.6 ; then
1307                 confgcc+=( --enable-lto )
1308         elif tc_version_is_at_least 4.5 ; then
1309                 confgcc+=( --disable-lto )
1310         fi
1311
1312         # graphite was added in 4.4 but we only support it in 4.8+ due to external
1313         # library issues.  #448024
1314         if tc_version_is_at_least 5.0 && in_iuse graphite ; then
1315                 confgcc+=( $(use_with graphite isl) )
1316                 use graphite && confgcc+=( --disable-isl-version-check )
1317         elif tc_version_is_at_least 4.8 && in_iuse graphite ; then
1318                 confgcc+=( $(use_with graphite cloog) )
1319                 use graphite && confgcc+=( --disable-isl-version-check )
1320         elif tc_version_is_at_least 4.4 ; then
1321                 confgcc+=( --without-{cloog,ppl} )
1322         fi
1323
1324         if tc_version_is_at_least 4.8 && in_iuse sanitize ; then
1325                 # See Note [implicitly enabled flags]
1326                 confgcc+=( $(usex sanitize '' --disable-libsanitizer) )
1327         fi
1328
1329         if tc_version_is_at_least 6.0 && in_iuse pie ; then
1330                 confgcc+=( $(use_enable pie default-pie) )
1331         fi
1332
1333         if tc_version_is_at_least 6.0 && in_iuse ssp ; then
1334                 confgcc+=(
1335                         # This defaults to -fstack-protector-strong.
1336                         $(use_enable ssp default-ssp)
1337                 )
1338         fi
1339
1340         # Disable gcc info regeneration -- it ships with generated info pages
1341         # already.  Our custom version/urls/etc... trigger it.  #464008
1342         export gcc_cv_prog_makeinfo_modern=no
1343
1344         # Do not let the X detection get in our way.  We know things can be found
1345         # via system paths, so no need to hardcode things that'll break multilib.
1346         # Older gcc versions will detect ac_x_libraries=/usr/lib64 which ends up
1347         # killing the 32bit builds which want /usr/lib.
1348         export ac_cv_have_x='have_x=yes ac_x_includes= ac_x_libraries='
1349
1350         confgcc+=( "$@" ${EXTRA_ECONF} )
1351
1352         # Nothing wrong with a good dose of verbosity
1353         echo
1354         einfo "PREFIX:          ${PREFIX}"
1355         einfo "BINPATH:         ${BINPATH}"
1356         einfo "LIBPATH:         ${LIBPATH}"
1357         einfo "DATAPATH:        ${DATAPATH}"
1358         einfo "STDCXX_INCDIR:   ${STDCXX_INCDIR}"
1359         echo
1360         einfo "Languages:       ${GCC_LANG}"
1361         echo
1362         einfo "Configuring GCC with: ${confgcc[@]//--/\n\t--}"
1363         echo
1364
1365         # Build in a separate build tree
1366         mkdir -p "${WORKDIR}"/build
1367         pushd "${WORKDIR}"/build > /dev/null
1368
1369         # and now to do the actual configuration
1370         addwrite /dev/zero
1371         echo "${S}"/configure "${confgcc[@]}"
1372         # Older gcc versions did not detect bash and re-exec itself, so force the
1373         # use of bash.  Newer ones will auto-detect, but this is not harmful.
1374         CONFIG_SHELL="${EPREFIX}/bin/bash" \
1375         bash "${S}"/configure "${confgcc[@]}" || die "failed to run configure"
1376
1377         # return to whatever directory we were in before
1378         popd > /dev/null
1379 }
1380
1381 # Replace -m flags unsupported by the version being built with the best
1382 # available equivalent
1383 downgrade_arch_flags() {
1384         local arch bver i isa myarch mytune rep ver
1385
1386         bver=${1:-${GCC_BRANCH_VER}}
1387         [[ $(gcc-version) < ${bver} ]] && return 0
1388         [[ $(tc-arch) != amd64 && $(tc-arch) != x86 ]] && return 0
1389
1390         myarch=$(get-flag march)
1391         mytune=$(get-flag mtune)
1392
1393         # If -march=native isn't supported we have to tease out the actual arch
1394         if [[ ${myarch} == native || ${mytune} == native ]] ; then
1395                 if [[ ${bver} < 4.2 ]] ; then
1396                         arch=$($(tc-getCC) -march=native -v -E -P - </dev/null 2>&1 \
1397                                 | sed -rn "/cc1.*-march/s:.*-march=([^ ']*).*:\1:p")
1398                         replace-cpu-flags native ${arch}
1399                 fi
1400         fi
1401
1402         # Handle special -mtune flags
1403         [[ ${mytune} == intel && ${bver} < 4.9 ]] && replace-cpu-flags intel generic
1404         [[ ${mytune} == generic && ${bver} < 4.2 ]] && filter-flags '-mtune=*'
1405         [[ ${mytune} == x86-64 ]] && filter-flags '-mtune=*'
1406         [[ ${bver} < 3.4 ]] && filter-flags '-mtune=*'
1407
1408         # "added" "arch" "replacement"
1409         local archlist=(
1410                 4.9 bdver4 bdver3
1411                 4.9 bonnell atom
1412                 4.9 broadwell core-avx2
1413                 4.9 haswell core-avx2
1414                 4.9 ivybridge core-avx-i
1415                 4.9 nehalem corei7
1416                 4.9 sandybridge corei7-avx
1417                 4.9 silvermont corei7
1418                 4.9 westmere corei7
1419                 4.8 bdver3 bdver2
1420                 4.8 btver2 btver1
1421                 4.7 bdver2 bdver1
1422                 4.7 core-avx2 core-avx-i
1423                 4.6 bdver1 amdfam10
1424                 4.6 btver1 amdfam10
1425                 4.6 core-avx-i core2
1426                 4.6 corei7 core2
1427                 4.6 corei7-avx core2
1428                 4.5 atom core2
1429                 4.3 amdfam10 k8
1430                 4.3 athlon64-sse3 k8
1431                 4.3 barcelona k8
1432                 4.3 core2 nocona
1433                 4.3 geode k6-2 # gcc.gnu.org/PR41989#c22
1434                 4.3 k8-sse3 k8
1435                 4.3 opteron-sse3 k8
1436                 3.4 athlon-fx x86-64
1437                 3.4 athlon64 x86-64
1438                 3.4 c3-2 c3
1439                 3.4 k8 x86-64
1440                 3.4 opteron x86-64
1441                 3.4 pentium-m pentium3
1442                 3.4 pentium3m pentium3
1443                 3.4 pentium4m pentium4
1444         )
1445
1446         for ((i = 0; i < ${#archlist[@]}; i += 3)) ; do
1447                 myarch=$(get-flag march)
1448                 mytune=$(get-flag mtune)
1449
1450                 ver=${archlist[i]}
1451                 arch=${archlist[i + 1]}
1452                 rep=${archlist[i + 2]}
1453
1454                 [[ ${myarch} != ${arch} && ${mytune} != ${arch} ]] && continue
1455
1456                 if [[ ${ver} > ${bver} ]] ; then
1457                         einfo "Replacing ${myarch} (added in gcc ${ver}) with ${rep}..."
1458                         [[ ${myarch} == ${arch} ]] && replace-cpu-flags ${myarch} ${rep}
1459                         [[ ${mytune} == ${arch} ]] && replace-cpu-flags ${mytune} ${rep}
1460                         continue
1461                 else
1462                         break
1463                 fi
1464         done
1465
1466         # we only check -mno* here since -m* get removed by strip-flags later on
1467         local isalist=(
1468                 4.9 -mno-sha
1469                 4.9 -mno-avx512pf
1470                 4.9 -mno-avx512f
1471                 4.9 -mno-avx512er
1472                 4.9 -mno-avx512cd
1473                 4.8 -mno-xsaveopt
1474                 4.8 -mno-xsave
1475                 4.8 -mno-rtm
1476                 4.8 -mno-fxsr
1477                 4.7 -mno-lzcnt
1478                 4.7 -mno-bmi2
1479                 4.7 -mno-avx2
1480                 4.6 -mno-tbm
1481                 4.6 -mno-rdrnd
1482                 4.6 -mno-fsgsbase
1483                 4.6 -mno-f16c
1484                 4.6 -mno-bmi
1485                 4.5 -mno-xop
1486                 4.5 -mno-movbe
1487                 4.5 -mno-lwp
1488                 4.5 -mno-fma4
1489                 4.4 -mno-pclmul
1490                 4.4 -mno-fma
1491                 4.4 -mno-avx
1492                 4.4 -mno-aes
1493                 4.3 -mno-ssse3
1494                 4.3 -mno-sse4a
1495                 4.3 -mno-sse4
1496                 4.3 -mno-sse4.2
1497                 4.3 -mno-sse4.1
1498                 4.3 -mno-popcnt
1499                 4.3 -mno-abm
1500         )
1501
1502         for ((i = 0; i < ${#isalist[@]}; i += 2)) ; do
1503                 ver=${isalist[i]}
1504                 isa=${isalist[i + 1]}
1505                 [[ ${ver} > ${bver} ]] && filter-flags ${isa} ${isa/-m/-mno-}
1506         done
1507 }
1508
1509 gcc_do_filter_flags() {
1510         strip-flags
1511         replace-flags -O? -O2
1512
1513         # dont want to funk ourselves
1514         filter-flags '-mabi*' -m31 -m32 -m64
1515
1516         filter-flags -frecord-gcc-switches # 490738
1517         filter-flags -mno-rtm -mno-htm # 506202
1518
1519         if tc_version_is_between 3.2 3.4 ; then
1520                 # XXX: this is so outdated it's barely useful, but it don't hurt...
1521                 replace-cpu-flags G3 750
1522                 replace-cpu-flags G4 7400
1523                 replace-cpu-flags G5 7400
1524
1525                 # XXX: should add a sed or something to query all supported flags
1526                 #      from the gcc source and trim everything else ...
1527                 filter-flags -f{no-,}unit-at-a-time -f{no-,}web -mno-tls-direct-seg-refs
1528                 filter-flags -f{no-,}stack-protector{,-all}
1529                 filter-flags -fvisibility-inlines-hidden -fvisibility=hidden
1530                 # and warning options
1531                 filter-flags -Wextra -Wstack-protector
1532         fi
1533         if ! tc_version_is_at_least 4.1 ; then
1534                 filter-flags -fdiagnostics-show-option
1535                 filter-flags -Wstack-protector
1536         fi
1537
1538         if tc_version_is_at_least 3.4 ; then
1539                 case $(tc-arch) in
1540                         amd64|x86)
1541                                 filter-flags '-mcpu=*'
1542
1543                                 tc_version_is_between 4.4 4.5 && append-flags -mno-avx # 357287
1544
1545                                 if tc_version_is_between 4.6 4.7 ; then
1546                                         # https://bugs.gentoo.org/411333
1547                                         # https://bugs.gentoo.org/466454
1548                                         replace-cpu-flags c3-2 pentium2 pentium3 pentium3m pentium-m i686
1549                                 fi
1550                                 ;;
1551                         alpha)
1552                                 # https://bugs.gentoo.org/454426
1553                                 append-ldflags -Wl,--no-relax
1554                                 ;;
1555                         sparc)
1556                                 # temporary workaround for random ICEs reproduced by multiple users
1557                                 # https://bugs.gentoo.org/457062
1558                                 tc_version_is_between 4.6 4.8 && MAKEOPTS+=" -j1"
1559                                 ;;
1560                         *-macos)
1561                                 # http://gcc.gnu.org/PR25127
1562                                 tc_version_is_between 4.0 4.2 && \
1563                                         filter-flags '-mcpu=*' '-march=*' '-mtune=*'
1564                                 ;;
1565                 esac
1566         fi
1567
1568         strip-unsupported-flags
1569
1570         # these are set here so we have something sane at configure time
1571         if is_crosscompile ; then
1572                 # Set this to something sane for both native and target
1573                 CFLAGS="-O2 -pipe"
1574                 FFLAGS=${CFLAGS}
1575                 FCFLAGS=${CFLAGS}
1576
1577                 # "hppa2.0-unknown-linux-gnu" -> hppa2_0_unknown_linux_gnu
1578                 local VAR="CFLAGS_"${CTARGET//[-.]/_}
1579                 CXXFLAGS=${!VAR-${CFLAGS}}
1580         fi
1581
1582         export GCJFLAGS=${GCJFLAGS:-${CFLAGS}}
1583 }
1584
1585 setup_minispecs_gcc_build_specs() {
1586         # Setup the "build.specs" file for gcc 4.3 to use when building.
1587         if hardened_gcc_works pie ; then
1588                 cat "${WORKDIR}"/specs/pie.specs >> "${WORKDIR}"/build.specs
1589         fi
1590         if hardened_gcc_works ssp ; then
1591                 for s in ssp sspall ; do
1592                         cat "${WORKDIR}"/specs/${s}.specs >> "${WORKDIR}"/build.specs
1593                 done
1594         fi
1595         for s in nostrict znow ; do
1596                 cat "${WORKDIR}"/specs/${s}.specs >> "${WORKDIR}"/build.specs
1597         done
1598         export GCC_SPECS="${WORKDIR}"/build.specs
1599 }
1600
1601 gcc-multilib-configure() {
1602         if ! is_multilib ; then
1603                 confgcc+=( --disable-multilib )
1604                 # Fun times: if we are building for a target that has multiple
1605                 # possible ABI formats, and the user has told us to pick one
1606                 # that isn't the default, then not specifying it via the list
1607                 # below will break that on us.
1608         else
1609                 confgcc+=( --enable-multilib )
1610         fi
1611
1612         # translate our notion of multilibs into gcc's
1613         local abi list
1614         for abi in $(get_all_abis TARGET) ; do
1615                 local l=$(gcc-abi-map ${abi})
1616                 [[ -n ${l} ]] && list+=",${l}"
1617         done
1618         if [[ -n ${list} ]] ; then
1619                 case ${CTARGET} in
1620                 x86_64*)
1621                         tc_version_is_at_least 4.8 && confgcc+=( --with-multilib-list=${list:1} )
1622                         ;;
1623                 esac
1624         fi
1625 }
1626
1627 gcc-abi-map() {
1628         # Convert the ABI name we use in Gentoo to what gcc uses
1629         local map=()
1630         case ${CTARGET} in
1631         mips*)   map=("o32 32" "n32 n32" "n64 64") ;;
1632         riscv*)  map=("lp64d lp64d" "lp64 lp64") ;;
1633         x86_64*) map=("amd64 m64" "x86 m32" "x32 mx32") ;;
1634         esac
1635
1636         local m
1637         for m in "${map[@]}" ; do
1638                 l=( ${m} )
1639                 [[ $1 == ${l[0]} ]] && echo ${l[1]} && break
1640         done
1641 }
1642
1643 #----> src_compile <----
1644
1645 toolchain_src_compile() {
1646         touch "${S}"/gcc/c-gperf.h
1647
1648         # Do not make manpages if we do not have perl ...
1649         [[ ! -x /usr/bin/perl ]] \
1650                 && find "${WORKDIR}"/build -name '*.[17]' -exec touch {} +
1651
1652         # Older gcc versions did not detect bash and re-exec itself, so force the
1653         # use of bash.  Newer ones will auto-detect, but this is not harmful.
1654         # This needs to be set for compile as well, as it's used in libtool
1655         # generation, which will break install otherwise (at least in 3.3.6): #664486
1656         CONFIG_SHELL="${EPREFIX}/bin/bash" \
1657         gcc_do_make ${GCC_MAKE_TARGET}
1658 }
1659
1660 gcc_do_make() {
1661         # This function accepts one optional argument, the make target to be used.
1662         # If omitted, gcc_do_make will try to guess whether it should use all,
1663         # or bootstrap-lean depending on CTARGET and arch.
1664         # An example of how to use this function:
1665         #
1666         #       gcc_do_make all-target-libstdc++-v3
1667
1668         [[ -n ${1} ]] && GCC_MAKE_TARGET=${1}
1669
1670         # default target
1671         if is_crosscompile || tc-is-cross-compiler ; then
1672                 # 3 stage bootstrapping doesnt quite work when you cant run the
1673                 # resulting binaries natively ^^;
1674                 GCC_MAKE_TARGET=${GCC_MAKE_TARGET-all}
1675         else
1676                 if tc_version_is_at_least 3.3 && use_if_iuse pgo; then
1677                         GCC_MAKE_TARGET=${GCC_MAKE_TARGET-profiledbootstrap}
1678                 else
1679                         GCC_MAKE_TARGET=${GCC_MAKE_TARGET-bootstrap-lean}
1680                 fi
1681         fi
1682
1683         # Older versions of GCC could not do profiledbootstrap in parallel due to
1684         # collisions with profiling info.
1685         # boundschecking also seems to introduce parallel build issues.
1686         if [[ ${GCC_MAKE_TARGET} == "profiledbootstrap" ]] || use_if_iuse boundschecking ; then
1687                 ! tc_version_is_at_least 4.6 && export MAKEOPTS="${MAKEOPTS} -j1"
1688         fi
1689
1690         if [[ ${GCC_MAKE_TARGET} == "all" ]] ; then
1691                 STAGE1_CFLAGS=${STAGE1_CFLAGS-"${CFLAGS}"}
1692         elif [[ $(gcc-version) == "3.4" && ${GCC_BRANCH_VER} == "3.4" ]] && gcc-specs-ssp ; then
1693                 # See bug #79852
1694                 STAGE1_CFLAGS=${STAGE1_CFLAGS-"-O2"}
1695         fi
1696
1697         if is_crosscompile; then
1698                 # In 3.4, BOOT_CFLAGS is never used on a crosscompile...
1699                 # but I'll leave this in anyways as someone might have had
1700                 # some reason for putting it in here... --eradicator
1701                 BOOT_CFLAGS=${BOOT_CFLAGS-"-O2"}
1702         else
1703                 # we only want to use the system's CFLAGS if not building a
1704                 # cross-compiler.
1705                 BOOT_CFLAGS=${BOOT_CFLAGS-"$(get_abi_CFLAGS ${TARGET_DEFAULT_ABI}) ${CFLAGS}"}
1706         fi
1707
1708         einfo "Compiling ${PN} (${GCC_MAKE_TARGET})..."
1709
1710         pushd "${WORKDIR}"/build >/dev/null
1711
1712         emake \
1713                 LDFLAGS="${LDFLAGS}" \
1714                 STAGE1_CFLAGS="${STAGE1_CFLAGS}" \
1715                 LIBPATH="${LIBPATH}" \
1716                 BOOT_CFLAGS="${BOOT_CFLAGS}" \
1717                 ${GCC_MAKE_TARGET} \
1718                 || die "emake failed with ${GCC_MAKE_TARGET}"
1719
1720         if ! is_crosscompile && use_if_iuse cxx && use_if_iuse doc ; then
1721                 if type -p doxygen > /dev/null ; then
1722                         if tc_version_is_at_least 4.3 ; then
1723                                 cd "${CTARGET}"/libstdc++-v3/doc
1724                                 emake doc-man-doxygen || ewarn "failed to make docs"
1725                         elif tc_version_is_at_least 3.0 ; then
1726                                 cd "${CTARGET}"/libstdc++-v3
1727                                 emake doxygen-man || ewarn "failed to make docs"
1728                         fi
1729                         # Clean bogus manpages.  #113902
1730                         find -name '*_build_*' -delete
1731                         # Blow away generated directory references.  Newer versions of gcc
1732                         # have gotten better at this, but not perfect.  This is easier than
1733                         # backporting all of the various doxygen patches.  #486754
1734                         find -name '*_.3' -exec grep -l ' Directory Reference ' {} + | \
1735                                 xargs rm -f
1736                 else
1737                         ewarn "Skipping libstdc++ manpage generation since you don't have doxygen installed"
1738                 fi
1739         fi
1740
1741         popd >/dev/null
1742 }
1743
1744 #---->> src_test <<----
1745
1746 toolchain_src_test() {
1747         if use ${GCC_EBUILD_TEST_FLAG} ; then
1748                 cd "${WORKDIR}"/build
1749                 # enable verbose test run and result logging
1750                 emake -k check RUNTESTFLAGS='-a -v'
1751         fi
1752 }
1753
1754 #---->> src_install <<----
1755
1756 toolchain_src_install() {
1757         cd "${WORKDIR}"/build
1758
1759         # Don't allow symlinks in private gcc include dir as this can break the build
1760         find gcc/include*/ -type l -delete
1761
1762         # Copy over the info pages.  We disabled their generation earlier, but the
1763         # build system only expects to install out of the build dir, not the source.  #464008
1764         mkdir -p gcc/doc
1765         local x=
1766         for x in "${S}"/gcc/doc/*.info* ; do
1767                 if [[ -f ${x} ]] ; then
1768                         cp "${x}" gcc/doc/ || die
1769                 fi
1770         done
1771
1772         # We remove the generated fixincludes, as they can cause things to break
1773         # (ncurses, openssl, etc).  We do not prevent them from being built, as
1774         # in the following commit which we revert:
1775         # https://sources.gentoo.org/cgi-bin/viewvc.cgi/gentoo-x86/eclass/toolchain.eclass?r1=1.647&r2=1.648
1776         # This is because bsd userland needs fixedincludes to build gcc, while
1777         # linux does not.  Both can dispose of them afterwards.
1778         while read x ; do
1779                 grep -q 'It has been auto-edited by fixincludes from' "${x}" \
1780                         && rm -f "${x}"
1781         done < <(find gcc/include*/ -name '*.h')
1782
1783         # Do the 'make install' from the build directory
1784         S="${WORKDIR}"/build emake -j1 DESTDIR="${D}" install || die
1785
1786         # Punt some tools which are really only useful while building gcc
1787         find "${ED}" -name install-tools -prune -type d -exec rm -rf "{}" \;
1788         # This one comes with binutils
1789         find "${ED}" -name libiberty.a -delete
1790
1791         # Move the libraries to the proper location
1792         gcc_movelibs
1793
1794         # Basic sanity check
1795         if ! is_crosscompile ; then
1796                 local EXEEXT
1797                 eval $(grep ^EXEEXT= "${WORKDIR}"/build/gcc/config.log)
1798                 [[ -r ${D}${BINPATH}/gcc${EXEEXT} ]] || die "gcc not found in ${ED}"
1799         fi
1800
1801         dodir /etc/env.d/gcc
1802         create_gcc_env_entry
1803         create_revdep_rebuild_entry
1804
1805         # Setup the gcc_env_entry for hardened gcc 4 with minispecs
1806         want_minispecs && copy_minispecs_gcc_specs
1807
1808         # Make sure we dont have stuff lying around that
1809         # can nuke multiple versions of gcc
1810         gcc_slot_java
1811
1812         dodir /usr/bin
1813         cd "${D}"${BINPATH}
1814         # Ugh: we really need to auto-detect this list.
1815         #      It's constantly out of date.
1816         for x in cpp gcc g++ c++ gcov g77 gcj gcjh gfortran gccgo ; do
1817                 # For some reason, g77 gets made instead of ${CTARGET}-g77...
1818                 # this should take care of that
1819                 if [[ -f ${x} ]] ; then
1820                         # In case they're hardlinks, clear out the target first
1821                         # otherwise the mv below will complain.
1822                         rm -f ${CTARGET}-${x}
1823                         mv ${x} ${CTARGET}-${x}
1824                 fi
1825
1826                 if [[ -f ${CTARGET}-${x} ]] ; then
1827                         if ! is_crosscompile ; then
1828                                 ln -sf ${CTARGET}-${x} ${x}
1829                                 dosym ${BINPATH#${EPREFIX}}/${CTARGET}-${x} \
1830                                         /usr/bin/${x}-${GCC_CONFIG_VER}
1831                         fi
1832                         # Create versioned symlinks
1833                         dosym ${BINPATH#${EPREFIX}}/${CTARGET}-${x} \
1834                                 /usr/bin/${CTARGET}-${x}-${GCC_CONFIG_VER}
1835                 fi
1836
1837                 if [[ -f ${CTARGET}-${x}-${GCC_CONFIG_VER} ]] ; then
1838                         rm -f ${CTARGET}-${x}-${GCC_CONFIG_VER}
1839                         ln -sf ${CTARGET}-${x} ${CTARGET}-${x}-${GCC_CONFIG_VER}
1840                 fi
1841         done
1842
1843         # When gcc builds a crosscompiler it does not install unprefixed tools.
1844         # When cross-building gcc does install native tools.
1845         if ! is_crosscompile; then
1846                 # Rename the main go binaries as we don't want to clobber dev-lang/go
1847                 # when gcc-config runs. #567806
1848                 if tc_version_is_at_least 5 && is_go ; then
1849                         for x in go gofmt; do
1850                                 mv ${x} ${x}-${GCCMAJOR} || die
1851                         done
1852                 fi
1853         fi
1854
1855         # TODO(bug #686512): implement stripping (we use RESTRICT=strip)
1856         # As gcc installs object files both build against ${CHOST} and ${CTARGET}
1857         # we will ned to run stripping using different tools:
1858         # Using ${CHOST} tools:
1859         #  - "${D}${BINPATH}"
1860         #  - (for is_crosscompile) "${D}${HOSTLIBPATH}"
1861         #  - "${D}${PREFIX}/libexec/gcc/${CTARGET}/${GCC_CONFIG_VER}"
1862         # Using ${CTARGET} tools:
1863         #  - "${D}${LIBPATH}"
1864
1865         cd "${S}"
1866         if is_crosscompile; then
1867                 rm -rf "${ED}"/usr/share/{man,info}
1868                 rm -rf "${D}"${DATAPATH}/{man,info}
1869         else
1870                 if tc_version_is_at_least 3.0 ; then
1871                         local cxx_mandir=$(find "${WORKDIR}/build/${CTARGET}/libstdc++-v3" -name man)
1872                         if [[ -d ${cxx_mandir} ]] ; then
1873                                 cp -r "${cxx_mandir}"/man? "${D}${DATAPATH}"/man/
1874                         fi
1875                 fi
1876         fi
1877
1878         # portage regenerates 'dir' files on it's own: bug #672408
1879         # Drop 'dir' files to avoid collisions.
1880         if [[ -f "${D}${DATAPATH}"/info/dir ]]; then
1881                 einfo "Deleting '${D}${DATAPATH}/info/dir'"
1882                 rm "${D}${DATAPATH}"/info/dir || die
1883         fi
1884
1885         # prune empty dirs left behind
1886         find "${ED}" -depth -type d -delete 2>/dev/null
1887
1888         # install testsuite results
1889         if use ${GCC_EBUILD_TEST_FLAG}; then
1890                 docinto testsuite
1891                 find "${WORKDIR}"/build -type f -name "*.sum" -exec dodoc {} +
1892                 find "${WORKDIR}"/build -type f -path "*/testsuite/*.log" -exec dodoc {} +
1893         fi
1894
1895         # Rather install the script, else portage with changing $FILESDIR
1896         # between binary and source package borks things ....
1897         if ! is_crosscompile && [[ ${PN} != "kgcc64" ]] ; then
1898                 insinto "${DATAPATH#${EPREFIX}}"
1899                 newins "$(prefixify_ro "${FILESDIR}"/awk/fixlafiles.awk-no_gcc_la)" fixlafiles.awk || die
1900                 exeinto "${DATAPATH#${EPREFIX}}"
1901                 doexe "$(prefixify_ro "${FILESDIR}"/fix_libtool_files.sh)" || die
1902                 doexe "${FILESDIR}"/c{89,99} || die
1903         fi
1904
1905         # libstdc++.la: Delete as it doesn't add anything useful: g++ itself
1906         # handles linkage correctly in the dynamic & static case.  It also just
1907         # causes us pain: any C++ progs/libs linking with libtool will gain a
1908         # reference to the full libstdc++.la file which is gcc version specific.
1909         # libstdc++fs.la: It doesn't link against anything useful.
1910         # libsupc++.la: This has no dependencies.
1911         # libcc1.la: There is no static library, only dynamic.
1912         # libcc1plugin.la: Same as above, and it's loaded via dlopen.
1913         # libcp1plugin.la: Same as above, and it's loaded via dlopen.
1914         # libgomp.la: gcc itself handles linkage (libgomp.spec).
1915         # libgomp-plugin-*.la: Same as above, and it's an internal plugin only
1916         # loaded via dlopen.
1917         # libgfortran.la: gfortran itself handles linkage correctly in the
1918         # dynamic & static case (libgfortran.spec). #573302
1919         # libgfortranbegin.la: Same as above, and it's an internal lib.
1920         # libmpx.la: gcc itself handles linkage correctly (libmpx.spec).
1921         # libmpxwrappers.la: See above.
1922         # libitm.la: gcc itself handles linkage correctly (libitm.spec).
1923         # libvtv.la: gcc itself handles linkage correctly.
1924         # lib*san.la: Sanitizer linkage is handled internally by gcc, and they
1925         # do not support static linking. #487550 #546700
1926         find "${D}${LIBPATH}" \
1927                 '(' \
1928                         -name libstdc++.la -o \
1929                         -name libstdc++fs.la -o \
1930                         -name libsupc++.la -o \
1931                         -name libcc1.la -o \
1932                         -name libcc1plugin.la -o \
1933                         -name libcp1plugin.la -o \
1934                         -name 'libgomp.la' -o \
1935                         -name 'libgomp-plugin-*.la' -o \
1936                         -name libgfortran.la -o \
1937                         -name libgfortranbegin.la -o \
1938                         -name libmpx.la -o \
1939                         -name libmpxwrappers.la -o \
1940                         -name libitm.la -o \
1941                         -name libvtv.la -o \
1942                         -name 'lib*san.la' \
1943                 ')' -type f -delete
1944
1945         # Use gid of 0 because some stupid ports don't have
1946         # the group 'root' set to gid 0.  Send to /dev/null
1947         # for people who are testing as non-root.
1948         chown -R root:0 "${D}${LIBPATH}" 2>/dev/null
1949
1950         # Installing gdb pretty-printers into gdb-specific location.
1951         local py gdbdir=/usr/share/gdb/auto-load${LIBPATH}
1952         pushd "${D}${LIBPATH}" >/dev/null
1953         for py in $(find . -name '*-gdb.py') ; do
1954                 local multidir=${py%/*}
1955                 insinto "${gdbdir}/${multidir}"
1956                 sed -i "/^libdir =/s:=.*:= '${LIBPATH}/${multidir}':" "${py}" || die #348128
1957                 doins "${py}" || die
1958                 rm "${py}" || die
1959         done
1960         popd >/dev/null
1961
1962         # Don't scan .gox files for executable stacks - false positives
1963         export QA_EXECSTACK="usr/lib*/go/*/*.gox"
1964         export QA_WX_LOAD="usr/lib*/go/*/*.gox"
1965
1966         # Disable RANDMMAP so PCH works. #301299
1967         if tc_version_is_at_least 4.3 ; then
1968                 pax-mark -r "${D}${PREFIX}/libexec/gcc/${CTARGET}/${GCC_CONFIG_VER}/cc1"
1969                 pax-mark -r "${D}${PREFIX}/libexec/gcc/${CTARGET}/${GCC_CONFIG_VER}/cc1plus"
1970         fi
1971
1972         # Disable MPROTECT so java works. #574808
1973         if is_gcj ; then
1974                 pax-mark -m "${D}${PREFIX}/libexec/gcc/${CTARGET}/${GCC_CONFIG_VER}/ecj1"
1975                 pax-mark -m "${D}${PREFIX}/${CTARGET}/gcc-bin/${GCC_CONFIG_VER}/gij"
1976         fi
1977 }
1978
1979 # Move around the libs to the right location.  For some reason,
1980 # when installing gcc, it dumps internal libraries into /usr/lib
1981 # instead of the private gcc lib path
1982 gcc_movelibs() {
1983         # older versions of gcc did not support --print-multi-os-directory
1984         tc_version_is_at_least 3.2 || return 0
1985
1986         # For non-target libs which are for CHOST and not CTARGET, we want to
1987         # move them to the compiler-specific CHOST internal dir.  This is stuff
1988         # that you want to link against when building tools rather than building
1989         # code to run on the target.
1990         if tc_version_is_at_least 5 && is_crosscompile ; then
1991                 dodir "${HOSTLIBPATH#${EPREFIX}}"
1992                 mv "${ED}"/usr/$(get_libdir)/libcc1* "${D}${HOSTLIBPATH}" || die
1993         fi
1994
1995         # For all the libs that are built for CTARGET, move them into the
1996         # compiler-specific CTARGET internal dir.
1997         local x multiarg removedirs=""
1998         for multiarg in $($(XGCC) -print-multi-lib) ; do
1999                 multiarg=${multiarg#*;}
2000                 multiarg=${multiarg//@/ -}
2001
2002                 local OS_MULTIDIR=$($(XGCC) ${multiarg} --print-multi-os-directory)
2003                 local MULTIDIR=$($(XGCC) ${multiarg} --print-multi-directory)
2004                 local TODIR="${D}${LIBPATH}"/${MULTIDIR}
2005                 local FROMDIR=
2006
2007                 [[ -d ${TODIR} ]] || mkdir -p ${TODIR}
2008
2009                 for FROMDIR in \
2010                         "${LIBPATH}"/${OS_MULTIDIR} \
2011                         "${LIBPATH}"/../${MULTIDIR} \
2012                         "${PREFIX}"/lib/${OS_MULTIDIR} \
2013                         "${PREFIX}"/${CTARGET}/lib/${OS_MULTIDIR}
2014                 do
2015                         removedirs="${removedirs} ${FROMDIR}"
2016                         FROMDIR=${D}${FROMDIR}
2017                         if [[ ${FROMDIR} != "${TODIR}" && -d ${FROMDIR} ]] ; then
2018                                 local files=$(find "${FROMDIR}" -maxdepth 1 ! -type d 2>/dev/null)
2019                                 if [[ -n ${files} ]] ; then
2020                                         mv ${files} "${TODIR}" || die
2021                                 fi
2022                         fi
2023                 done
2024                 fix_libtool_libdir_paths "${LIBPATH}/${MULTIDIR}"
2025
2026                 # SLOT up libgcj.pc if it's available (and let gcc-config worry about links)
2027                 FROMDIR="${PREFIX}/lib/${OS_MULTIDIR}"
2028                 for x in "${D}${FROMDIR}"/pkgconfig/libgcj*.pc ; do
2029                         [[ -f ${x} ]] || continue
2030                         sed -i "/^libdir=/s:=.*:=${LIBPATH}/${MULTIDIR}:" "${x}" || die
2031                         mv "${x}" "${D}${FROMDIR}"/pkgconfig/libgcj-${GCC_PV}.pc || die
2032                 done
2033         done
2034
2035         # We remove directories separately to avoid this case:
2036         #       mv SRC/lib/../lib/*.o DEST
2037         #       rmdir SRC/lib/../lib/
2038         #       mv SRC/lib/../lib32/*.o DEST  # Bork
2039         for FROMDIR in ${removedirs} ; do
2040                 rmdir "${D}"${FROMDIR} >& /dev/null
2041         done
2042         find -depth "${ED}" -type d -exec rmdir {} + >& /dev/null
2043 }
2044
2045 # make sure the libtool archives have libdir set to where they actually
2046 # -are-, and not where they -used- to be.  also, any dependencies we have
2047 # on our own .la files need to be updated.
2048 fix_libtool_libdir_paths() {
2049         local libpath="$1"
2050
2051         pushd "${D}" >/dev/null
2052
2053         pushd "./${libpath}" >/dev/null
2054         local dir="${PWD#${D%/}}"
2055         local allarchives=$(echo *.la)
2056         allarchives="\(${allarchives// /\\|}\)"
2057         popd >/dev/null
2058
2059         # The libdir might not have any .la files. #548782
2060         find "./${dir}" -maxdepth 1 -name '*.la' \
2061                 -exec sed -i -e "/^libdir=/s:=.*:='${dir}':" {} + || die
2062         # Would be nice to combine these, but -maxdepth can not be specified
2063         # on sub-expressions.
2064         find "./${PREFIX}"/lib* -maxdepth 3 -name '*.la' \
2065                 -exec sed -i -e "/^dependency_libs=/s:/[^ ]*/${allarchives}:${libpath}/\1:g" {} + || die
2066         find "./${dir}/" -maxdepth 1 -name '*.la' \
2067                 -exec sed -i -e "/^dependency_libs=/s:/[^ ]*/${allarchives}:${libpath}/\1:g" {} + || die
2068
2069         popd >/dev/null
2070 }
2071
2072 create_gcc_env_entry() {
2073         dodir /etc/env.d/gcc
2074         local gcc_envd_base="/etc/env.d/gcc/${CTARGET}-${GCC_CONFIG_VER}"
2075
2076         local gcc_specs_file
2077         local gcc_envd_file="${ED}${gcc_envd_base}"
2078         if [[ -z $1 ]] ; then
2079                 # I'm leaving the following commented out to remind me that it
2080                 # was an insanely -bad- idea. Stuff broke. GCC_SPECS isnt unset
2081                 # on chroot or in non-toolchain.eclass gcc ebuilds!
2082                 #gcc_specs_file="${LIBPATH}/specs"
2083                 gcc_specs_file=""
2084         else
2085                 gcc_envd_file+="-$1"
2086                 gcc_specs_file="${LIBPATH}/$1.specs"
2087         fi
2088
2089         # We want to list the default ABI's LIBPATH first so libtool
2090         # searches that directory first.  This is a temporary
2091         # workaround for libtool being stupid and using .la's from
2092         # conflicting ABIs by using the first one in the search path
2093         local ldpaths mosdirs
2094         if tc_version_is_at_least 3.2 ; then
2095                 local mdir mosdir abi ldpath
2096                 for abi in $(get_all_abis TARGET) ; do
2097                         mdir=$($(XGCC) $(get_abi_CFLAGS ${abi}) --print-multi-directory)
2098                         ldpath=${LIBPATH}
2099                         [[ ${mdir} != "." ]] && ldpath+="/${mdir}"
2100                         ldpaths="${ldpath}${ldpaths:+:${ldpaths}}"
2101
2102                         mosdir=$($(XGCC) $(get_abi_CFLAGS ${abi}) -print-multi-os-directory)
2103                         mosdirs="${mosdir}${mosdirs:+:${mosdirs}}"
2104                 done
2105         else
2106                 # Older gcc's didn't do multilib, so logic is simple.
2107                 ldpaths=${LIBPATH}
2108         fi
2109
2110         cat <<-EOF > ${gcc_envd_file}
2111         GCC_PATH="${BINPATH}"
2112         LDPATH="${ldpaths}"
2113         MANPATH="${DATAPATH}/man"
2114         INFOPATH="${DATAPATH}/info"
2115         STDCXX_INCDIR="${STDCXX_INCDIR##*/}"
2116         CTARGET="${CTARGET}"
2117         GCC_SPECS="${gcc_specs_file}"
2118         MULTIOSDIRS="${mosdirs}"
2119         EOF
2120 }
2121
2122 create_revdep_rebuild_entry() {
2123         local revdep_rebuild_base="/etc/revdep-rebuild/05cross-${CTARGET}-${GCC_CONFIG_VER}"
2124         local revdep_rebuild_file="${ED}${revdep_rebuild_base}"
2125
2126         is_crosscompile || return 0
2127
2128         dodir /etc/revdep-rebuild
2129         cat <<-EOF > "${revdep_rebuild_file}"
2130         # Generated by ${CATEGORY}/${PF}
2131         # Ignore libraries built for ${CTARGET}, https://bugs.gentoo.org/692844.
2132         SEARCH_DIRS_MASK="${LIBPATH}"
2133         EOF
2134 }
2135
2136 copy_minispecs_gcc_specs() {
2137         # on gcc 6 we don't need minispecs
2138         if tc_version_is_at_least 6.0 ; then
2139                 return 0
2140         fi
2141
2142         # setup the hardenedno* specs files and the vanilla specs file.
2143         if hardened_gcc_works ; then
2144                 create_gcc_env_entry hardenednopiessp
2145         fi
2146         if hardened_gcc_works pie ; then
2147                 create_gcc_env_entry hardenednopie
2148         fi
2149         if hardened_gcc_works ssp ; then
2150                 create_gcc_env_entry hardenednossp
2151         fi
2152         create_gcc_env_entry vanilla
2153         insinto ${LIBPATH#${EPREFIX}}
2154         doins "${WORKDIR}"/specs/*.specs || die "failed to install specs"
2155         # Build system specs file which, if it exists, must be a complete set of
2156         # specs as it completely and unconditionally overrides the builtin specs.
2157         if ! tc_version_is_at_least 4.4 ; then
2158                 $(XGCC) -dumpspecs > "${WORKDIR}"/specs/specs
2159                 cat "${WORKDIR}"/build.specs >> "${WORKDIR}"/specs/specs
2160                 doins "${WORKDIR}"/specs/specs || die "failed to install the specs file"
2161         fi
2162 }
2163
2164 gcc_slot_java() {
2165         local x
2166
2167         # Move Java headers to compiler-specific dir
2168         for x in "${D}${PREFIX}"/include/gc*.h "${D}${PREFIX}"/include/j*.h ; do
2169                 [[ -f ${x} ]] && mv -f "${x}" "${D}${LIBPATH}"/include/
2170         done
2171         for x in gcj gnu java javax org ; do
2172                 if [[ -d ${D}${PREFIX}/include/${x} ]] ; then
2173                         dodir /${LIBPATH#${EPREFIX}}/include/${x}
2174                         mv -f "${D}${PREFIX}"/include/${x}/* "${D}${LIBPATH}"/include/${x}/
2175                         rm -rf "${D}${PREFIX}"/include/${x}
2176                 fi
2177         done
2178
2179         if [[ -d ${D}${PREFIX}/lib/security ]] || [[ -d ${D}${PREFIX}/$(get_libdir)/security ]] ; then
2180                 dodir /${LIBPATH#${EPREFIX}}/security
2181                 mv -f "${D}${PREFIX}"/lib*/security/* "${D}${LIBPATH}"/security
2182                 rm -rf "${D}${PREFIX}"/lib*/security
2183         fi
2184
2185         # Move random gcj files to compiler-specific directories
2186         for x in libgcj.spec logging.properties ; do
2187                 x="${D}${PREFIX}/lib/${x}"
2188                 [[ -f ${x} ]] && mv -f "${x}" "${D}${LIBPATH}"/
2189         done
2190
2191         # Rename jar because it could clash with Kaffe's jar if this gcc is
2192         # primary compiler (aka don't have the -<version> extension)
2193         cd "${D}${BINPATH}"
2194         [[ -f jar ]] && mv -f jar gcj-jar
2195 }
2196
2197 #---->> pkg_post* <<----
2198
2199 toolchain_pkg_postinst() {
2200         do_gcc_config
2201         if [[ ! ${ROOT%/} && -f ${EPREFIX}/usr/share/eselect/modules/compiler-shadow.eselect ]] ; then
2202                 eselect compiler-shadow update all
2203         fi
2204
2205         if ! is_crosscompile && [[ ${PN} != "kgcc64" ]] ; then
2206                 echo
2207                 ewarn "If you have issues with packages unable to locate libstdc++.la,"
2208                 ewarn "then try running 'fix_libtool_files.sh' on the old gcc versions."
2209                 echo
2210                 ewarn "You might want to review the GCC upgrade guide when moving between"
2211                 ewarn "major versions (like 4.2 to 4.3):"
2212                 ewarn "https://wiki.gentoo.org/wiki/Upgrading_GCC"
2213                 echo
2214
2215                 # Clean up old paths
2216                 rm -f "${EROOT%/}"/*/rcscripts/awk/fixlafiles.awk "${EROOT%/}"/sbin/fix_libtool_files.sh
2217                 rmdir "${EROOT%/}"/*/rcscripts{/awk,} 2>/dev/null
2218
2219                 mkdir -p "${EROOT%/}"/usr/{share/gcc-data,sbin,bin}
2220                 # DATAPATH has EPREFIX already, use ROOT with it
2221                 cp "${ROOT%/}${DATAPATH}"/fixlafiles.awk "${EROOT%/}"/usr/share/gcc-data/ || die
2222                 cp "${ROOT%/}${DATAPATH}"/fix_libtool_files.sh "${EROOT%/}"/usr/sbin/ || die
2223
2224                 # Since these aren't critical files and portage sucks with
2225                 # handling of binpkgs, don't require these to be found
2226                 cp "${ROOT%/}${DATAPATH}"/c{89,99} "${EROOT%/}"/usr/bin/ 2>/dev/null
2227         fi
2228
2229         if use ${GCC_EBUILD_TEST_FLAG} ; then
2230                 elog "Testsuite results have been installed into /usr/share/doc/${PF}/testsuite"
2231                 echo
2232         fi
2233
2234         if [[ -n ${PRERELEASE}${SNAPSHOT} ]] ; then
2235                 einfo "This GCC ebuild is provided for your convenience, and the use"
2236                 einfo "of this compiler is not supported by the Gentoo Developers."
2237                 einfo "Please report bugs to upstream at http://gcc.gnu.org/bugzilla/"
2238         fi
2239 }
2240
2241 toolchain_pkg_postrm() {
2242         do_gcc_config
2243         if [[ ! ${ROOT%/} && -f ${EPREFIX}/usr/share/eselect/modules/compiler-shadow.eselect ]] ; then
2244                 eselect compiler-shadow clean all
2245         fi
2246
2247         # to make our lives easier (and saner), we do the fix_libtool stuff here.
2248         # rather than checking SLOT's and trying in upgrade paths, we just see if
2249         # the common libstdc++.la exists in the ${LIBPATH} of the gcc that we are
2250         # unmerging.  if it does, that means this was a simple re-emerge.
2251
2252         # clean up the cruft left behind by cross-compilers
2253         if is_crosscompile ; then
2254                 if [[ -z $(ls "${EROOT%/}"/etc/env.d/gcc/${CTARGET}* 2>/dev/null) ]] ; then
2255                         einfo "Removing last cross-compiler instance. Deleting dangling symlinks."
2256                         rm -f "${EROOT%/}"/etc/env.d/gcc/config-${CTARGET}
2257                         rm -f "${EROOT%/}"/etc/env.d/??gcc-${CTARGET}
2258                         rm -f "${EROOT%/}"/usr/bin/${CTARGET}-{gcc,{g,c}++}{,32,64}
2259                 fi
2260                 return 0
2261         fi
2262
2263         # ROOT isnt handled by the script
2264         [[ ${ROOT%/} ]] && return 0
2265
2266         if [[ ! -e ${LIBPATH}/libstdc++.so ]] ; then
2267                 einfo "Running 'fix_libtool_files.sh ${GCC_RELEASE_VER}'"
2268                 fix_libtool_files.sh ${GCC_RELEASE_VER}
2269         fi
2270
2271         return 0
2272 }
2273
2274 do_gcc_config() {
2275         if ! should_we_gcc_config ; then
2276                 gcc-config --use-old --force
2277                 return 0
2278         fi
2279
2280         local current_gcc_config target
2281
2282         current_gcc_config=$(gcc-config -c ${CTARGET} 2>/dev/null)
2283         if [[ -n ${current_gcc_config} ]] ; then
2284                 local current_specs use_specs
2285                 # figure out which specs-specific config is active
2286                 current_specs=$(gcc-config -S ${current_gcc_config} | awk '{print $3}')
2287                 [[ -n ${current_specs} ]] && use_specs=-${current_specs}
2288
2289                 if [[ -n ${use_specs} ]] && \
2290                    [[ ! -e ${EROOT%/}/etc/env.d/gcc/${CTARGET}-${GCC_CONFIG_VER}${use_specs} ]]
2291                 then
2292                         ewarn "The currently selected specs-specific gcc config,"
2293                         ewarn "${current_specs}, doesn't exist anymore. This is usually"
2294                         ewarn "due to enabling/disabling hardened or switching to a version"
2295                         ewarn "of gcc that doesnt create multiple specs files. The default"
2296                         ewarn "config will be used, and the previous preference forgotten."
2297                         use_specs=""
2298                 fi
2299
2300                 target="${CTARGET}-${GCC_CONFIG_VER}${use_specs}"
2301         else
2302                 # The curent target is invalid.  Attempt to switch to a valid one.
2303                 # Blindly pick the latest version.  #529608
2304                 # TODO: Should update gcc-config to accept `-l ${CTARGET}` rather than
2305                 # doing a partial grep like this.
2306                 target=$(gcc-config -l 2>/dev/null | grep " ${CTARGET}-[0-9]" | tail -1 | awk '{print $2}')
2307         fi
2308
2309         gcc-config "${target}"
2310 }
2311
2312 should_we_gcc_config() {
2313         # if the current config is invalid, we definitely want a new one
2314         # Note: due to bash quirkiness, the following must not be 1 line
2315         local curr_config
2316         curr_config=$(gcc-config -c ${CTARGET} 2>&1) || return 0
2317
2318         # if the previously selected config has the same major.minor (branch) as
2319         # the version we are installing, then it will probably be uninstalled
2320         # for being in the same SLOT, make sure we run gcc-config.
2321         local curr_config_ver=$(gcc-config -S ${curr_config} | awk '{print $2}')
2322
2323         local curr_branch_ver=$(ver_cut 1-2 ${curr_config_ver})
2324
2325         if [[ ${curr_branch_ver} == ${GCC_BRANCH_VER} ]] ; then
2326                 return 0
2327         else
2328                 # if we're installing a genuinely different compiler version,
2329                 # we should probably tell the user -how- to switch to the new
2330                 # gcc version, since we're not going to do it for him/her.
2331                 # We don't want to switch from say gcc-3.3 to gcc-3.4 right in
2332                 # the middle of an emerge operation (like an 'emerge -e world'
2333                 # which could install multiple gcc versions).
2334                 # Only warn if we're installing a pkg as we might be called from
2335                 # the pkg_{pre,post}rm steps.  #446830
2336                 if [[ ${EBUILD_PHASE} == *"inst" ]] ; then
2337                         einfo "The current gcc config appears valid, so it will not be"
2338                         einfo "automatically switched for you.  If you would like to"
2339                         einfo "switch to the newly installed gcc version, do the"
2340                         einfo "following:"
2341                         echo
2342                         einfo "gcc-config ${CTARGET}-${GCC_CONFIG_VER}"
2343                         einfo "source /etc/profile"
2344                         echo
2345                 fi
2346                 return 1
2347         fi
2348 }
2349
2350 #---->> support and misc functions <<----
2351
2352 # This is to make sure we don't accidentally try to enable support for a
2353 # language that doesnt exist. GCC 3.4 supports f77, while 4.0 supports f95, etc.
2354 #
2355 # Also add a hook so special ebuilds (kgcc64) can control which languages
2356 # exactly get enabled
2357 gcc-lang-supported() {
2358         grep ^language=\"${1}\" "${S}"/gcc/*/config-lang.in > /dev/null || return 1
2359         [[ -z ${TOOLCHAIN_ALLOWED_LANGS} ]] && return 0
2360         has $1 ${TOOLCHAIN_ALLOWED_LANGS}
2361 }
2362
2363 is_ada() {
2364         gcc-lang-supported ada || return 1
2365         use_if_iuse ada
2366 }
2367
2368 is_cxx() {
2369         gcc-lang-supported 'c++' || return 1
2370         use_if_iuse cxx
2371 }
2372
2373 is_d() {
2374         gcc-lang-supported d || return 1
2375         use_if_iuse d
2376 }
2377
2378 is_f77() {
2379         gcc-lang-supported f77 || return 1
2380         use_if_iuse fortran
2381 }
2382
2383 is_f95() {
2384         gcc-lang-supported f95 || return 1
2385         use_if_iuse fortran
2386 }
2387
2388 is_fortran() {
2389         gcc-lang-supported fortran || return 1
2390         use_if_iuse fortran
2391 }
2392
2393 is_gcj() {
2394         gcc-lang-supported java || return 1
2395         use_if_iuse cxx && use_if_iuse gcj
2396 }
2397
2398 is_go() {
2399         gcc-lang-supported go || return 1
2400         use_if_iuse cxx && use_if_iuse go
2401 }
2402
2403 is_jit() {
2404         gcc-lang-supported jit || return 1
2405         # cross-compiler does not really support jit as it has
2406         # to generate code for a target. On target like avr
2407         # libgcclit.so can't link at all: bug #594572
2408         is_crosscompile && return 1
2409         use_if_iuse jit
2410 }
2411
2412 is_multilib() {
2413         tc_version_is_at_least 3 || return 1
2414         use_if_iuse multilib
2415 }
2416
2417 is_objc() {
2418         gcc-lang-supported objc || return 1
2419         use_if_iuse objc
2420 }
2421
2422 is_objcxx() {
2423         gcc-lang-supported 'obj-c++' || return 1
2424         use_if_iuse cxx && use_if_iuse objc++
2425 }
2426
2427 # Grab a variable from the build system (taken from linux-info.eclass)
2428 get_make_var() {
2429         local var=$1 makefile=${2:-${WORKDIR}/build/Makefile}
2430         echo -e "e:\\n\\t@echo \$(${var})\\ninclude ${makefile}" | \
2431                 r=${makefile%/*} emake --no-print-directory -s -f - 2>/dev/null
2432 }
2433
2434 XGCC() { get_make_var GCC_FOR_TARGET ; }
2435
2436 # The gentoo piessp patches allow for 3 configurations:
2437 # 1) PIE+SSP by default
2438 # 2) PIE by default
2439 # 3) SSP by default
2440 hardened_gcc_works() {
2441         if [[ $1 == "pie" ]] ; then
2442                 # $gcc_cv_ld_pie is unreliable as it simply take the output of
2443                 # `ld --help | grep -- -pie`, that reports the option in all cases, also if
2444                 # the loader doesn't actually load the resulting executables.
2445                 # To avoid breakage, blacklist FreeBSD here at least
2446                 [[ ${CTARGET} == *-freebsd* ]] && return 1
2447
2448                 want_pie || return 1
2449                 use_if_iuse nopie && return 1
2450                 hardened_gcc_is_stable pie
2451                 return $?
2452         elif [[ $1 == "ssp" ]] ; then
2453                 [[ -n ${SPECS_VER} ]] || return 1
2454                 use_if_iuse nossp && return 1
2455                 hardened_gcc_is_stable ssp
2456                 return $?
2457         else
2458                 # laziness ;)
2459                 hardened_gcc_works pie || return 1
2460                 hardened_gcc_works ssp || return 1
2461                 return 0
2462         fi
2463 }
2464
2465 hardened_gcc_is_stable() {
2466         local tocheck
2467         if [[ $1 == "pie" ]] ; then
2468                 if [[ ${CTARGET} == *-uclibc* ]] ; then
2469                         tocheck=${PIE_UCLIBC_STABLE}
2470                 else
2471                         tocheck=${PIE_GLIBC_STABLE}
2472                 fi
2473         elif [[ $1 == "ssp" ]] ; then
2474                 if [[ ${CTARGET} == *-uclibc* ]] ; then
2475                         tocheck=${SSP_UCLIBC_STABLE}
2476                 elif  [[ ${CTARGET} == *-gnu* ]] ; then
2477                         tocheck=${SSP_STABLE}
2478                 fi
2479         else
2480                 die "hardened_gcc_stable needs to be called with pie or ssp"
2481         fi
2482
2483         has $(tc-arch) ${tocheck} && return 0
2484         return 1
2485 }
2486
2487 want_minispecs() {
2488         # on gcc 6 we don't need minispecs
2489         if tc_version_is_at_least 6.0 ; then
2490                 return 0
2491         fi
2492         if tc_version_is_at_least 4.3.2 && use_if_iuse hardened ; then
2493                 if ! want_pie ; then
2494                         ewarn "PIE_VER or SPECS_VER is not defined in the GCC ebuild."
2495                 elif use vanilla ; then
2496                         ewarn "You will not get hardened features if you have the vanilla USE-flag."
2497                 elif use_if_iuse nopie && use_if_iuse nossp ; then
2498                         ewarn "You will not get hardened features if you have the nopie and nossp USE-flag."
2499                 elif ! hardened_gcc_works ; then
2500                         ewarn "Your $(tc-arch) arch is not supported."
2501                 else
2502                         return 0
2503                 fi
2504                 ewarn "Hope you know what you are doing. Hardened will not work."
2505                 return 0
2506         fi
2507         return 1
2508 }
2509
2510 want_pie() {
2511         ! use_if_iuse hardened && [[ -n ${PIE_VER} ]] && use_if_iuse nopie && return 1
2512         [[ -n ${PIE_VER} ]] && [[ -n ${SPECS_VER} ]] && return 0
2513         tc_version_is_at_least 4.3.2 && return 1
2514         [[ -z ${PIE_VER} ]] && return 1
2515         use_if_iuse nopie || return 0
2516         return 1
2517 }
2518
2519 has toolchain_death_notice ${EBUILD_DEATH_HOOKS} || EBUILD_DEATH_HOOKS+=" toolchain_death_notice"
2520 toolchain_death_notice() {
2521         if [[ -e "${WORKDIR}"/build ]] ; then
2522                 pushd "${WORKDIR}"/build >/dev/null
2523                 (echo '' | $(tc-getCC ${CTARGET}) ${CFLAGS} -v -E - 2>&1) > gccinfo.log
2524                 [[ -e "${T}"/build.log ]] && cp "${T}"/build.log .
2525                 tar jcf "${WORKDIR}"/gcc-build-logs.tar.bz2 \
2526                         gccinfo.log build.log $(find -name config.log)
2527                 rm gccinfo.log build.log
2528                 eerror
2529                 eerror "Please include ${WORKDIR}/gcc-build-logs.tar.bz2 in your bug report."
2530                 eerror
2531                 popd >/dev/null
2532         fi
2533 }
2534
2535 # Note [implicitly enabled flags]
2536 # -------------------------------
2537 # Usually configure-based packages handle explicit feature requests
2538 # like
2539 #     ./configure --enable-foo
2540 # as explicit request to check for support of 'foo' and bail out at
2541 # configure time.
2542 #
2543 # GCC does not follow this pattern and instead overrides autodetection
2544 # of the feature and enables it unconditionally.
2545 # See bugs:
2546 #    https://gcc.gnu.org/PR85663 (libsanitizer on mips)
2547 #    https://bugs.gentoo.org/661252 (libvtv on powerpc64)
2548 #
2549 # Thus safer way to enable/disable the feature is to rely on implicit
2550 # enabled-by-default state:
2551 #    econf $(usex foo '' --disable-foo)