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