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