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