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