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