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