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