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