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