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