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