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