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