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