sys-cluster/openmpi: bump 2.0.3
[gentoo.git] / eclass / toolchain-glibc.eclass
1 # Copyright 1999-2017 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: toolchain-glibc.eclass
5 # @MAINTAINER:
6 # <toolchain@gentoo.org>
7 # @BLURB: Common code for sys-libs/glibc ebuilds
8 # @DESCRIPTION:
9 # This eclass contains the common phase functions migrated from
10 # sys-libs/glibc eblits.
11
12 if [[ -z ${_TOOLCHAIN_GLIBC_ECLASS} ]]; then
13
14 inherit eutils versionator toolchain-funcs flag-o-matic gnuconfig \
15         multilib systemd unpacker multiprocessing prefix
16
17 case ${EAPI:-0} in
18         0|1|2|3) EXPORT_FUNCTIONS pkg_setup src_unpack src_compile src_test \
19                 src_install pkg_preinst pkg_postinst;;
20         2|3) EXPORT_FUNCTIONS pkg_setup src_unpack src_prepare src_configure \
21                 src_compile src_test src_install pkg_preinst pkg_postinst;;
22         4|5|6) EXPORT_FUNCTIONS pkg_pretend pkg_setup src_unpack src_prepare \
23                 src_configure src_compile src_test src_install \
24                 pkg_preinst pkg_postinst;;
25         *) die "Unsupported EAPI=${EAPI}";;
26 esac
27
28 # == common ==
29
30 alt_prefix() {
31         is_crosscompile && echo /usr/${CTARGET}
32 }
33
34 if [[ ${EAPI:-0} == [012] ]] ; then
35         : ${ED:=${D}}
36         : ${EROOT:=${ROOT}}
37 fi
38 # This indirection is for binpkgs. #523332
39 _nonfatal() { nonfatal "$@" ; }
40 if [[ ${EAPI:-0} == [0123] ]] ; then
41         nonfatal() { "$@" ; }
42         _nonfatal() { "$@" ; }
43 fi
44
45 # We need to be able to set alternative headers for
46 # compiling for non-native platform
47 # Will also become useful for testing kernel-headers without screwing up
48 # the whole system.
49 # note: intentionally undocumented.
50 alt_headers() {
51         echo ${ALT_HEADERS:=$(alt_prefix)/usr/include}
52 }
53 alt_build_headers() {
54         if [[ -z ${ALT_BUILD_HEADERS} ]] ; then
55                 ALT_BUILD_HEADERS="${EPREFIX}$(alt_headers)"
56                 if tc-is-cross-compiler ; then
57                         ALT_BUILD_HEADERS=${SYSROOT}$(alt_headers)
58                         if [[ ! -e ${ALT_BUILD_HEADERS}/linux/version.h ]] ; then
59                                 local header_path=$(echo '#include <linux/version.h>' | $(tc-getCPP ${CTARGET}) ${CFLAGS} 2>&1 | grep -o '[^"]*linux/version.h')
60                                 ALT_BUILD_HEADERS=${header_path%/linux/version.h}
61                         fi
62                 fi
63         fi
64         echo "${ALT_BUILD_HEADERS}"
65 }
66
67 alt_libdir() {
68         echo $(alt_prefix)/$(get_libdir)
69 }
70 alt_usrlibdir() {
71         echo $(alt_prefix)/usr/$(get_libdir)
72 }
73
74 builddir() {
75         echo "${WORKDIR}/build-${ABI}-${CTARGET}-$1"
76 }
77
78 setup_target_flags() {
79         # This largely mucks with compiler flags.  None of which should matter
80         # when building up just the headers.
81         just_headers && return 0
82
83         case $(tc-arch) in
84                 x86)
85                         # -march needed for #185404 #199334
86                         # TODO: When creating the first glibc cross-compile, this test will
87                         # always fail as it does a full link which in turn requires glibc.
88                         # Probably also applies when changing multilib profile settings (e.g.
89                         # enabling x86 when the profile was amd64-only previously).
90                         # We could change main to _start and pass -nostdlib here so that we
91                         # only test the gcc code compilation.  Or we could do a compile and
92                         # then look for the symbol via scanelf.
93                         if ! glibc_compile_test "" 'void f(int i, void *p) {if (__sync_fetch_and_add(&i, 1)) f(i, p);}\nint main(){return 0;}\n' 2>/dev/null ; then
94                                 local t=${CTARGET_OPT:-${CTARGET}}
95                                 t=${t%%-*}
96                                 filter-flags '-march=*'
97                                 export CFLAGS="-march=${t} ${CFLAGS}"
98                                 einfo "Auto adding -march=${t} to CFLAGS #185404"
99                         fi
100                 ;;
101                 amd64)
102                         # -march needed for #185404 #199334
103                         # Note: This test only matters when the x86 ABI is enabled, so we could
104                         # optimize a bit and elide it.
105                         # TODO: See cross-compile issues listed above for x86.
106                         if ! glibc_compile_test "${CFLAGS_x86}" 'void f(int i, void *p) {if (__sync_fetch_and_add(&i, 1)) f(i, p);}\nint main(){return 0;}\n' 2>/dev/null ; then
107                                 local t=${CTARGET_OPT:-${CTARGET}}
108                                 t=${t%%-*}
109                                 # Normally the target is x86_64-xxx, so turn that into the -march that
110                                 # gcc actually accepts. #528708
111                                 [[ ${t} == "x86_64" ]] && t="x86-64"
112                                 filter-flags '-march=*'
113                                 # ugly, ugly, ugly.  ugly.
114                                 CFLAGS_x86=$(CFLAGS=${CFLAGS_x86} filter-flags '-march=*'; echo "${CFLAGS}")
115                                 export CFLAGS_x86="${CFLAGS_x86} -march=${t}"
116                                 einfo "Auto adding -march=${t} to CFLAGS_x86 #185404"
117                         fi
118                 ;;
119                 mips)
120                         # The mips abi cannot support the GNU style hashes. #233233
121                         filter-ldflags -Wl,--hash-style=gnu -Wl,--hash-style=both
122                 ;;
123                 ppc)
124                         append-flags "-freorder-blocks"
125                 ;;
126                 sparc)
127                         # Both sparc and sparc64 can use -fcall-used-g6.  -g7 is bad, though.
128                         filter-flags "-fcall-used-g7"
129                         append-flags "-fcall-used-g6"
130
131                         # If the CHOST is the basic one (e.g. not sparcv9-xxx already),
132                         # try to pick a better one so glibc can use cpu-specific .S files.
133                         # We key off the CFLAGS to get a good value.  Also need to handle
134                         # version skew.
135                         # We can't force users to set their CHOST to their exact machine
136                         # as many of these are not recognized by config.sub/gcc and such :(.
137                         # Note: If the mcpu values don't scale, we might try probing CPP defines.
138                         # Note: Should we factor in -Wa,-AvXXX flags too ?  Or -mvis/etc... ?
139
140                         local cpu
141                         case ${CTARGET} in
142                         sparc64-*)
143                                 case $(get-flag mcpu) in
144                                 niagara[234])
145                                         if version_is_at_least 2.8 ; then
146                                                 cpu="sparc64v2"
147                                         elif version_is_at_least 2.4 ; then
148                                                 cpu="sparc64v"
149                                         elif version_is_at_least 2.2.3 ; then
150                                                 cpu="sparc64b"
151                                         fi
152                                         ;;
153                                 niagara)
154                                         if version_is_at_least 2.4 ; then
155                                                 cpu="sparc64v"
156                                         elif version_is_at_least 2.2.3 ; then
157                                                 cpu="sparc64b"
158                                         fi
159                                         ;;
160                                 ultrasparc3)
161                                         cpu="sparc64b"
162                                         ;;
163                                 *)
164                                         # We need to force at least v9a because the base build doesn't
165                                         # work with just v9.
166                                         # https://sourceware.org/bugzilla/show_bug.cgi?id=19477
167                                         [[ -z ${cpu} ]] && append-flags "-Wa,-xarch=v9a"
168                                         ;;
169                                 esac
170                                 ;;
171                         sparc-*)
172                                 case $(get-flag mcpu) in
173                                 niagara[234])
174                                         if version_is_at_least 2.8 ; then
175                                                 cpu="sparcv9v2"
176                                         elif version_is_at_least 2.4 ; then
177                                                 cpu="sparcv9v"
178                                         elif version_is_at_least 2.2.3 ; then
179                                                 cpu="sparcv9b"
180                                         else
181                                                 cpu="sparcv9"
182                                         fi
183                                         ;;
184                                 niagara)
185                                         if version_is_at_least 2.4 ; then
186                                                 cpu="sparcv9v"
187                                         elif version_is_at_least 2.2.3 ; then
188                                                 cpu="sparcv9b"
189                                         else
190                                                 cpu="sparcv9"
191                                         fi
192                                         ;;
193                                 ultrasparc3)
194                                         cpu="sparcv9b"
195                                         ;;
196                                 v9|ultrasparc)
197                                         cpu="sparcv9"
198                                         ;;
199                                 v8|supersparc|hypersparc|leon|leon3)
200                                         cpu="sparcv8"
201                                         ;;
202                                 esac
203                         ;;
204                         esac
205                         [[ -n ${cpu} ]] && CTARGET_OPT="${cpu}-${CTARGET#*-}"
206                 ;;
207         esac
208 }
209
210 setup_flags() {
211         # Make sure host make.conf doesn't pollute us
212         if is_crosscompile || tc-is-cross-compiler ; then
213                 CHOST=${CTARGET} strip-unsupported-flags
214         fi
215
216         # Store our CFLAGS because it's changed depending on which CTARGET
217         # we are building when pulling glibc on a multilib profile
218         CFLAGS_BASE=${CFLAGS_BASE-${CFLAGS}}
219         CFLAGS=${CFLAGS_BASE}
220         CXXFLAGS_BASE=${CXXFLAGS_BASE-${CXXFLAGS}}
221         CXXFLAGS=${CXXFLAGS_BASE}
222         ASFLAGS_BASE=${ASFLAGS_BASE-${ASFLAGS}}
223         ASFLAGS=${ASFLAGS_BASE}
224
225         # Over-zealous CFLAGS can often cause problems.  What may work for one
226         # person may not work for another.  To avoid a large influx of bugs
227         # relating to failed builds, we strip most CFLAGS out to ensure as few
228         # problems as possible.
229         strip-flags
230         strip-unsupported-flags
231         filter-flags -m32 -m64 -mabi=*
232
233         # Bug 492892.
234         filter-flags -frecord-gcc-switches
235
236         unset CBUILD_OPT CTARGET_OPT
237         if use multilib ; then
238                 CTARGET_OPT=$(get_abi_CTARGET)
239                 [[ -z ${CTARGET_OPT} ]] && CTARGET_OPT=$(get_abi_CHOST)
240         fi
241
242         setup_target_flags
243
244         if [[ -n ${CTARGET_OPT} && ${CBUILD} == ${CHOST} ]] && ! is_crosscompile; then
245                 CBUILD_OPT=${CTARGET_OPT}
246         fi
247
248         # Lock glibc at -O2 -- linuxthreads needs it and we want to be
249         # conservative here.  -fno-strict-aliasing is to work around #155906
250         filter-flags -O?
251         append-flags -O2 -fno-strict-aliasing
252
253         # Can't build glibc itself with fortify code.  Newer versions add
254         # this flag for us, so no need to do it manually.
255         version_is_at_least 2.16 ${PV} || append-cppflags -U_FORTIFY_SOURCE
256
257         # building glibc with SSP is fraught with difficulty, especially
258         # due to __stack_chk_fail_local which would mean significant changes
259         # to the glibc build process. See bug #94325 #293721
260         # Note we have to handle both user-given CFLAGS and gcc defaults via
261         # spec rules here.  We can't simply add -fno-stack-protector as it gets
262         # added before user flags, and we can't just filter-flags because
263         # _filter_hardened doesn't support globs.
264         filter-flags -fstack-protector*
265         gcc-specs-ssp && append-flags $(test-flags -fno-stack-protector)
266
267         if use hardened && gcc-specs-pie ; then
268                 # Force PIC macro definition for all compilations since they're all
269                 # either -fPIC or -fPIE with the default-PIE compiler.
270                 append-cppflags -DPIC
271         else
272                 # Don't build -fPIE without the default-PIE compiler and the
273                 # hardened-pie patch
274                 filter-flags -fPIE
275         fi
276 }
277
278 want_nptl() {
279         [[ -z ${LT_VER} ]] && return 0
280         want_tls || return 1
281         use nptl || return 1
282
283         # Older versions of glibc had incomplete arch support for nptl.
284         # But if you're building those now, you can handle USE=nptl yourself.
285         return 0
286 }
287
288 want_linuxthreads() {
289         [[ -z ${LT_VER} ]] && return 1
290         use linuxthreads
291 }
292
293 want_tls() {
294         # Archs that can use TLS (Thread Local Storage)
295         case $(tc-arch) in
296                 x86)
297                         # requires i486 or better #106556
298                         [[ ${CTARGET} == i[4567]86* ]] && return 0
299                         return 1
300                 ;;
301         esac
302
303         return 0
304 }
305
306 want__thread() {
307         want_tls || return 1
308
309         # For some reason --with-tls --with__thread is causing segfaults on sparc32.
310         [[ ${PROFILE_ARCH} == "sparc" ]] && return 1
311
312         [[ -n ${WANT__THREAD} ]] && return ${WANT__THREAD}
313
314         # only test gcc -- cant test linking yet
315         tc-has-tls -c ${CTARGET}
316         WANT__THREAD=$?
317
318         return ${WANT__THREAD}
319 }
320
321 use_multiarch() {
322         # Make sure binutils is new enough to support indirect functions #336792
323         # This funky sed supports gold and bfd linkers.
324         local bver nver
325         bver=$($(tc-getLD ${CTARGET}) -v | sed -n -r '1{s:[^0-9]*::;s:^([0-9.]*).*:\1:;p}')
326         case $(tc-arch ${CTARGET}) in
327         amd64|x86) nver="2.20" ;;
328         arm)       nver="2.22" ;;
329         hppa)      nver="2.23" ;;
330         ppc|ppc64) nver="2.20" ;;
331         # ifunc was added in 2.23, but glibc also needs machinemode which is in 2.24.
332         s390)      nver="2.24" ;;
333         sparc)     nver="2.21" ;;
334         *)         return 1 ;;
335         esac
336         version_is_at_least ${nver} ${bver}
337 }
338
339 # Setup toolchain variables that had historically
340 # been defined in the profiles for these archs.
341 setup_env() {
342         # silly users
343         unset LD_RUN_PATH
344         unset LD_ASSUME_KERNEL
345
346         if is_crosscompile || tc-is-cross-compiler ; then
347                 multilib_env ${CTARGET_OPT:-${CTARGET}}
348
349                 if ! use multilib ; then
350                         MULTILIB_ABIS=${DEFAULT_ABI}
351                 else
352                         MULTILIB_ABIS=${MULTILIB_ABIS:-${DEFAULT_ABI}}
353                 fi
354
355                 # If the user has CFLAGS_<CTARGET> in their make.conf, use that,
356                 # and fall back on CFLAGS.
357                 local VAR=CFLAGS_${CTARGET//[-.]/_}
358                 CFLAGS=${!VAR-${CFLAGS}}
359         fi
360
361         setup_flags
362
363         export ABI=${ABI:-${DEFAULT_ABI:-default}}
364
365         local VAR=CFLAGS_${ABI}
366         # We need to export CFLAGS with abi information in them because glibc's
367         # configure script checks CFLAGS for some targets (like mips).  Keep
368         # around the original clean value to avoid appending multiple ABIs on
369         # top of each other.
370         : ${__GLIBC_CC:=$(tc-getCC ${CTARGET_OPT:-${CTARGET}})}
371         export __GLIBC_CC CC="${__GLIBC_CC} ${!VAR}"
372 }
373
374 foreach_abi() {
375         setup_env
376
377         local ret=0
378         local abilist=""
379         if use multilib ; then
380                 abilist=$(get_install_abis)
381         else
382                 abilist=${DEFAULT_ABI}
383         fi
384         local -x ABI
385         for ABI in ${abilist:-default} ; do
386                 setup_env
387                 einfo "Running $1 for ABI ${ABI}"
388                 $1
389                 : $(( ret |= $? ))
390         done
391         return ${ret}
392 }
393
394 just_headers() {
395         is_crosscompile && use crosscompile_opts_headers-only
396 }
397
398 glibc_banner() {
399         local b="Gentoo ${PVR}"
400         [[ -n ${SNAP_VER} ]] && b+=" snapshot ${SNAP_VER}"
401         [[ -n ${BRANCH_UPDATE} ]] && b+=" branch ${BRANCH_UPDATE}"
402         [[ -n ${PATCH_VER} ]] && ! use vanilla && b+=" p${PATCH_VER}"
403         echo "${b}"
404 }
405
406 # == phases ==
407
408 glibc_compile_test() {
409         local ret save_cflags=${CFLAGS}
410         CFLAGS+=" $1"
411         shift
412
413         pushd "${T}" >/dev/null
414
415         rm -f glibc-test*
416         printf '%b' "$*" > glibc-test.c
417
418         _nonfatal emake -s glibc-test
419         ret=$?
420
421         popd >/dev/null
422
423         CFLAGS=${save_cflags}
424         return ${ret}
425 }
426
427 glibc_run_test() {
428         local ret
429
430         if [[ ${EMERGE_FROM} == "binary" ]] ; then
431                 # ignore build failures when installing a binary package #324685
432                 glibc_compile_test "" "$@" 2>/dev/null || return 0
433         else
434                 if ! glibc_compile_test "" "$@" ; then
435                         ewarn "Simple build failed ... assuming this is desired #324685"
436                         return 0
437                 fi
438         fi
439
440         pushd "${T}" >/dev/null
441
442         ./glibc-test
443         ret=$?
444         rm -f glibc-test*
445
446         popd >/dev/null
447
448         return ${ret}
449 }
450
451 check_devpts() {
452         # Make sure devpts is mounted correctly for use w/out setuid pt_chown.
453
454         # If merely building the binary package, then there's nothing to verify.
455         [[ ${MERGE_TYPE} == "buildonly" ]] && return
456
457         # Only sanity check when installing the native glibc.
458         [[ ${ROOT} != "/" ]] && return
459
460         # Older versions always installed setuid, so no need to check.
461         in_iuse suid || return
462
463         # If they're opting in to the old suid code, then no need to check.
464         use suid && return
465
466         if awk '$3 == "devpts" && $4 ~ /[, ]gid=5[, ]/ { exit 1 }' /proc/mounts ; then
467                 eerror "In order to use glibc with USE=-suid, you must make sure that"
468                 eerror "you have devpts mounted at /dev/pts with the gid=5 option."
469                 eerror "Openrc should do this for you, so you should check /etc/fstab"
470                 eerror "and make sure you do not have any invalid settings there."
471                 # Do not die on older kernels as devpts did not export these settings #489520.
472                 if version_is_at_least 2.6.25 $(uname -r) ; then
473                         die "mount & fix your /dev/pts settings"
474                 fi
475         fi
476 }
477
478 toolchain-glibc_pkg_pretend() {
479         # For older EAPIs, this is run in pkg_preinst.
480         if [[ ${EAPI:-0} != [0123] ]] ; then
481                 check_devpts
482         fi
483
484         # Prevent native builds from downgrading.
485         if [[ ${MERGE_TYPE} != "buildonly" ]] && \
486            [[ ${ROOT} == "/" ]] && \
487            [[ ${CBUILD} == ${CHOST} ]] && \
488            [[ ${CHOST} == ${CTARGET} ]] ; then
489                 # The high rev # is to allow people to downgrade between -r# versions.
490                 # We want to block 2.20->2.19, but 2.20-r3->2.20-r2 should be fine.
491                 # Hopefully we never actually use a r# this high.
492                 if has_version ">${CATEGORY}/${P}-r10000" ; then
493                         eerror "Sanity check to keep you from breaking your system:"
494                         eerror " Downgrading glibc is not supported and a sure way to destruction"
495                         die "aborting to save your system"
496                 fi
497
498                 if ! glibc_run_test '#include <pwd.h>\nint main(){return getpwuid(0)==0;}\n'
499                 then
500                         eerror "Your patched vendor kernel is broken.  You need to get an"
501                         eerror "update from whoever is providing the kernel to you."
502                         eerror "https://sourceware.org/bugzilla/show_bug.cgi?id=5227"
503                         eerror "http://bugs.gentoo.org/262698"
504                         die "keeping your system alive, say thank you"
505                 fi
506
507                 if ! glibc_run_test '#include <unistd.h>\n#include <sys/syscall.h>\nint main(){return syscall(1000)!=-1;}\n'
508                 then
509                         eerror "Your old kernel is broken.  You need to update it to"
510                         eerror "a newer version as syscall(<bignum>) will break."
511                         eerror "http://bugs.gentoo.org/279260"
512                         die "keeping your system alive, say thank you"
513                 fi
514         fi
515
516         # users have had a chance to phase themselves, time to give em the boot
517         if [[ -e ${EROOT}/etc/locale.gen ]] && [[ -e ${EROOT}/etc/locales.build ]] ; then
518                 eerror "You still haven't deleted ${EROOT}/etc/locales.build."
519                 eerror "Do so now after making sure ${EROOT}/etc/locale.gen is kosher."
520                 die "lazy upgrader detected"
521         fi
522
523         if [[ ${CTARGET} == i386-* ]] ; then
524                 eerror "i386 CHOSTs are no longer supported."
525                 eerror "Chances are you don't actually want/need i386."
526                 eerror "Please read http://www.gentoo.org/doc/en/change-chost.xml"
527                 die "please fix your CHOST"
528         fi
529
530         if [[ -e /proc/xen ]] && [[ $(tc-arch) == "x86" ]] && ! is-flag -mno-tls-direct-seg-refs ; then
531                 ewarn "You are using Xen but don't have -mno-tls-direct-seg-refs in your CFLAGS."
532                 ewarn "This will result in a 50% performance penalty when running with a 32bit"
533                 ewarn "hypervisor, which is probably not what you want."
534         fi
535
536         use hardened && ! gcc-specs-pie && \
537                 ewarn "PIE hardening not applied, as your compiler doesn't default to PIE"
538
539         # Make sure host system is up to date #394453
540         if has_version '<sys-libs/glibc-2.13' && \
541            [[ -n $(scanelf -qys__guard -F'#s%F' "${EROOT}"/lib*/l*-*.so) ]]
542         then
543                 ebegin "Scanning system for __guard to see if you need to rebuild first ..."
544                 local files=$(
545                         scanelf -qys__guard -F'#s%F' \
546                                 "${EROOT}"/*bin/ \
547                                 "${EROOT}"/lib* \
548                                 "${EROOT}"/usr/*bin/ \
549                                 "${EROOT}"/usr/lib* | \
550                                 egrep -v \
551                                         -e "^${EROOT}/lib.*/(libc|ld)-2.*.so$" \
552                                         -e "^${EROOT}/sbin/(ldconfig|sln)$"
553                 )
554                 [[ -z ${files} ]]
555                 if ! eend $? ; then
556                         eerror "Your system still has old SSP __guard symbols.  You need to"
557                         eerror "rebuild all the packages that provide these files first:"
558                         eerror "${files}"
559                         die "old __guard detected"
560                 fi
561         fi
562 }
563
564 toolchain-glibc_pkg_setup() {
565         [[ ${EAPI:-0} == [0123] ]] && toolchain-glibc_pkg_pretend
566 }
567
568 int_to_KV() {
569         local version=$1 major minor micro
570         major=$((version / 65536))
571         minor=$(((version % 65536) / 256))
572         micro=$((version % 256))
573         echo ${major}.${minor}.${micro}
574 }
575
576 eend_KV() {
577         [[ $(KV_to_int $1) -ge $(KV_to_int $2) ]]
578         eend $?
579 }
580
581 get_kheader_version() {
582         printf '#include <linux/version.h>\nLINUX_VERSION_CODE\n' | \
583         $(tc-getCPP ${CTARGET}) -I "${EPREFIX}/$(alt_build_headers)" - | \
584         tail -n 1
585 }
586
587 check_nptl_support() {
588         # don't care about the compiler here as we arent using it
589         just_headers && return
590
591         local run_kv build_kv want_kv
592         run_kv=$(int_to_KV $(get_KV))
593         build_kv=$(int_to_KV $(get_kheader_version))
594         want_kv=${NPTL_KERN_VER}
595
596         ebegin "Checking gcc for __thread support"
597         if ! eend $(want__thread ; echo $?) ; then
598                 echo
599                 eerror "Could not find a gcc that supports the __thread directive!"
600                 eerror "Please update your binutils/gcc and try again."
601                 die "No __thread support in gcc!"
602         fi
603
604         if ! is_crosscompile && ! tc-is-cross-compiler ; then
605                 # Building fails on an non-supporting kernel
606                 ebegin "Checking kernel version (${run_kv} >= ${want_kv})"
607                 if ! eend_KV ${run_kv} ${want_kv} ; then
608                         echo
609                         eerror "You need a kernel of at least ${want_kv} for NPTL support!"
610                         die "Kernel version too low!"
611                 fi
612         fi
613
614         ebegin "Checking linux-headers version (${build_kv} >= ${want_kv})"
615         if ! eend_KV ${build_kv} ${want_kv} ; then
616                 echo
617                 eerror "You need linux-headers of at least ${want_kv} for NPTL support!"
618                 die "linux-headers version too low!"
619         fi
620 }
621
622 unpack_pkg() {
623         local a=${PN}
624         [[ -n ${SNAP_VER} ]] && a="${a}-${RELEASE_VER}"
625         [[ -n $1 ]] && a="${a}-$1"
626         if [[ -n ${SNAP_VER} ]] ; then
627                 a="${a}-${SNAP_VER}"
628         else
629                 if [[ -n $2 ]] ; then
630                         a="${a}-$2"
631                 else
632                         a="${a}-${RELEASE_VER}"
633                 fi
634         fi
635         if has ${a}.tar.xz ${A} ; then
636                 unpacker ${a}.tar.xz
637         else
638                 unpack ${a}.tar.bz2
639         fi
640         [[ -n $1 ]] && { mv ${a} $1 || die ; }
641 }
642
643 toolchain-glibc_do_src_unpack() {
644         # Check NPTL support _before_ we unpack things to save some time
645         want_nptl && check_nptl_support
646
647         if [[ -n ${EGIT_REPO_URIS} ]] ; then
648                 local i d
649                 for ((i=0; i<${#EGIT_REPO_URIS[@]}; ++i)) ; do
650                         EGIT_REPO_URI=${EGIT_REPO_URIS[$i]}
651                         EGIT_SOURCEDIR=${EGIT_SOURCEDIRS[$i]}
652                         git-2_src_unpack
653                 done
654         else
655                 unpack_pkg
656         fi
657
658         cd "${S}"
659         touch locale/C-translit.h #185476 #218003
660         [[ -n ${LT_VER}     ]] && unpack_pkg linuxthreads ${LT_VER}
661         [[ -n ${PORTS_VER}  ]] && unpack_pkg ports ${PORTS_VER}
662         [[ -n ${LIBIDN_VER} ]] && unpack_pkg libidn
663
664         if [[ -n ${PATCH_VER} ]] ; then
665                 cd "${WORKDIR}"
666                 unpack glibc-${RELEASE_VER}-patches-${PATCH_VER}.tar.bz2
667                 # pull out all the addons
668                 local d
669                 for d in extra/*/configure ; do
670                         d=${d%/configure}
671                         [[ -d ${S}/${d} ]] && die "${d} already exists in \${S}"
672                         mv "${d}" "${S}" || die "moving ${d} failed"
673                 done
674         fi
675 }
676
677 toolchain-glibc_src_unpack() {
678         setup_env
679
680         toolchain-glibc_do_src_unpack
681         [[ ${EAPI:-0} == [01] ]] && cd "${S}" && toolchain-glibc_src_prepare
682 }
683
684 toolchain-glibc_src_prepare() {
685         # XXX: We should do the branchupdate, before extracting the manpages and
686         # infopages else it does not help much (mtimes change if there is a change
687         # to them with branchupdate)
688         if [[ -n ${BRANCH_UPDATE} ]] ; then
689                 epatch "${DISTDIR}"/glibc-${RELEASE_VER}-branch-update-${BRANCH_UPDATE}.patch.bz2
690
691                 # Snapshot date patch
692                 einfo "Patching version to display snapshot date ..."
693                 sed -i -e "s:\(#define RELEASE\).*:\1 \"${BRANCH_UPDATE}\":" version.h
694         fi
695
696         # tag, glibc is it
697         if ! version_is_at_least 2.17 ; then
698                 [[ -e csu/Banner ]] && die "need new banner location"
699                 glibc_banner > csu/Banner
700         fi
701         if [[ -n ${PATCH_VER} ]] && ! use vanilla ; then
702                 EPATCH_MULTI_MSG="Applying Gentoo Glibc Patchset ${RELEASE_VER}-${PATCH_VER} ..." \
703                 EPATCH_EXCLUDE=${GLIBC_PATCH_EXCLUDE} \
704                 EPATCH_SUFFIX="patch" \
705                 ARCH=$(tc-arch) \
706                 epatch "${WORKDIR}"/patches
707         fi
708
709         if just_headers ; then
710                 if [[ -e ports/sysdeps/mips/preconfigure ]] ; then
711                         # mips peeps like to screw with us.  if building headers,
712                         # we don't have a real compiler, so we can't let them
713                         # insert -mabi on us.
714                         sed -i '/CPPFLAGS=.*-mabi/s|.*|:|' ports/sysdeps/mips/preconfigure || die
715                         find ports/sysdeps/mips/ -name Makefile -exec sed -i '/^CC.*-mabi=/s:-mabi=.*:-D_MIPS_SZPTR=32:' {} +
716                 fi
717         fi
718
719         epatch_user
720
721         gnuconfig_update
722
723         # Glibc is stupid sometimes, and doesn't realize that with a
724         # static C-Only gcc, -lgcc_eh doesn't exist.
725         # https://sourceware.org/ml/libc-alpha/2003-09/msg00100.html
726         # https://sourceware.org/ml/libc-alpha/2005-02/msg00042.html
727         # But! Finally fixed in recent versions:
728         # https://sourceware.org/ml/libc-alpha/2012-05/msg01865.html
729         if ! version_is_at_least 2.16 ; then
730                 echo 'int main(){}' > "${T}"/gcc_eh_test.c
731                 if ! $(tc-getCC ${CTARGET}) ${CFLAGS} ${LDFLAGS} "${T}"/gcc_eh_test.c -lgcc_eh 2>/dev/null ; then
732                         sed -i -e 's:-lgcc_eh::' Makeconfig || die "sed gcc_eh"
733                 fi
734         fi
735
736         cd "${WORKDIR}"
737         find . -type f '(' -size 0 -o -name "*.orig" ')' -delete
738         find . -name configure -exec touch {} +
739
740         eprefixify extra/locale/locale-gen
741
742         # Fix permissions on some of the scripts.
743         chmod u+x "${S}"/scripts/*.sh
744 }
745 dump_toolchain_settings() {
746         echo
747
748         einfo "$*"
749
750         local v
751         for v in ABI CBUILD CHOST CTARGET CBUILD_OPT CTARGET_OPT CC LD {AS,C,CPP,CXX,LD}FLAGS ; do
752                 einfo " $(printf '%15s' ${v}:)   ${!v}"
753         done
754
755         # The glibc configure script doesn't properly use LDFLAGS all the time.
756         export CC="$(tc-getCC ${CTARGET}) ${LDFLAGS}"
757         einfo " $(printf '%15s' 'Manual CC:')   ${CC}"
758         echo
759 }
760
761 glibc_do_configure() {
762         # Glibc does not work with gold (for various reasons) #269274.
763         tc-ld-disable-gold
764
765         dump_toolchain_settings "Configuring glibc for $1"
766
767         local myconf=()
768
769         # set addons
770         pushd "${S}" > /dev/null
771         local addons=$(echo */configure | sed \
772                 -e 's:/configure::g' \
773                 -e 's:\(linuxthreads\|nptl\|rtkaio\|glibc-compat\)\( \|$\)::g' \
774                 -e 's: \+$::' \
775                 -e 's! !,!g' \
776                 -e 's!^!,!' \
777                 -e '/^,\*$/d')
778         [[ -d ports ]] && addons+=",ports"
779         popd > /dev/null
780
781         myconf+=( $(use_enable hardened stackguard-randomization) )
782         if has_version '<sys-libs/glibc-2.13' ; then
783                 myconf+=( --enable-old-ssp-compat )
784         fi
785
786         [[ $(tc-is-softfloat) == "yes" ]] && myconf+=( --without-fp )
787
788         if [[ $1 == "linuxthreads" ]] ; then
789                 if want_tls ; then
790                         myconf+=( --with-tls )
791
792                         if ! want__thread || use glibc-compat20 || [[ ${LT_KER_VER} == 2.[02].* ]] ; then
793                                 myconf+=( --without-__thread )
794                         else
795                                 myconf+=( --with-__thread )
796                         fi
797                 else
798                         myconf+=( --without-tls --without-__thread )
799                 fi
800
801                 myconf+=( --disable-sanity-checks )
802                 addons="linuxthreads${addons}"
803                 myconf+=( --enable-kernel=${LT_KER_VER} )
804         elif [[ $1 == "nptl" ]] ; then
805                 # Newer versions require nptl, so there is no addon for it.
806                 version_is_at_least 2.20 || addons="nptl${addons}"
807                 myconf+=( --enable-kernel=${NPTL_KERN_VER} )
808         else
809                 die "invalid pthread option"
810         fi
811         myconf+=( --enable-add-ons="${addons#,}" )
812
813         # Since SELinux support is only required for nscd, only enable it if:
814         # 1. USE selinux
815         # 2. only for the primary ABI on multilib systems
816         # 3. Not a crosscompile
817         if ! is_crosscompile && use selinux ; then
818                 if use multilib ; then
819                         if is_final_abi ; then
820                                 myconf+=( --with-selinux )
821                         else
822                                 myconf+=( --without-selinux )
823                         fi
824                 else
825                         myconf+=( --with-selinux )
826                 fi
827         else
828                 myconf+=( --without-selinux )
829         fi
830
831         # Force a few tests where we always know the answer but
832         # configure is incapable of finding it.
833         if is_crosscompile ; then
834                 export \
835                         libc_cv_c_cleanup=yes \
836                         libc_cv_forced_unwind=yes
837         fi
838
839         myconf+=(
840                 --without-cvs
841                 --disable-werror
842                 --enable-bind-now
843                 --build=${CBUILD_OPT:-${CBUILD}}
844                 --host=${CTARGET_OPT:-${CTARGET}}
845                 $(use_enable profile)
846                 $(use_with gd)
847                 --with-headers=$(alt_build_headers)
848                 --prefix="${EPREFIX}/usr"
849                 --sysconfdir="${EPREFIX}/etc"
850                 --localstatedir="${EPREFIX}/var"
851                 --libdir='$(prefix)'/$(get_libdir)
852                 --mandir='$(prefix)'/share/man
853                 --infodir='$(prefix)'/share/info
854                 --libexecdir='$(libdir)'/misc/glibc
855                 --with-bugurl=http://bugs.gentoo.org/
856                 --with-pkgversion="$(glibc_banner)"
857                 $(use_multiarch || echo --disable-multi-arch)
858                 $(in_iuse rpc && use_enable rpc obsolete-rpc || echo --enable-obsolete-rpc)
859                 $(in_iuse systemtap && use_enable systemtap)
860                 $(in_iuse nscd && use_enable nscd)
861                 ${EXTRA_ECONF}
862         )
863
864         # We rely on sys-libs/timezone-data for timezone tools normally.
865         if version_is_at_least 2.23 ; then
866                 myconf+=( $(use_enable vanilla timezone-tools) )
867         fi
868
869         # These libs don't have configure flags.
870         ac_cv_lib_audit_audit_log_user_avc_message=$(in_iuse audit && usex audit || echo no)
871         ac_cv_lib_cap_cap_init=$(in_iuse caps && usex caps || echo no)
872
873         # There is no configure option for this and we need to export it
874         # since the glibc build will re-run configure on itself
875         export libc_cv_rootsbindir="${EPREFIX}/sbin"
876         export libc_cv_slibdir="${EPREFIX}/$(get_libdir)"
877
878         # We take care of patching our binutils to use both hash styles,
879         # and many people like to force gnu hash style only, so disable
880         # this overriding check.  #347761
881         export libc_cv_hashstyle=no
882
883         # Overtime, generating info pages can be painful.  So disable this for
884         # versions older than the latest stable to avoid the issue (this ver
885         # should be updated from time to time).  #464394 #465816
886         if ! version_is_at_least 2.17 ; then
887                 export ac_cv_prog_MAKEINFO=:
888         fi
889
890         local builddir=$(builddir "$1")
891         mkdir -p "${builddir}"
892         cd "${builddir}"
893         set -- "${S}"/configure "${myconf[@]}"
894         echo "$@"
895         "$@" || die "failed to configure glibc"
896
897         # ia64 static cross-compilers are a pita in so much that they
898         # can't produce static ELFs (as the libgcc.a is broken).  so
899         # disable building of the programs for those targets if it
900         # doesn't work.
901         # XXX: We could turn this into a compiler test, but ia64 is
902         # the only one that matters, so this should be fine for now.
903         if is_crosscompile && [[ ${CTARGET} == ia64* ]] ; then
904                 sed -i '1i+link-static = touch $@' config.make
905         fi
906
907         # If we're trying to migrate between ABI sets, we need
908         # to lie and use a local copy of gcc.  Like if the system
909         # is built with MULTILIB_ABIS="amd64 x86" but we want to
910         # add x32 to it, gcc/glibc don't yet support x32.
911         if [[ -n ${GCC_BOOTSTRAP_VER} ]] && use multilib ; then
912                 echo 'main(){}' > "${T}"/test.c
913                 if ! $(tc-getCC ${CTARGET}) ${CFLAGS} ${LDFLAGS} "${T}"/test.c -Wl,-emain -lgcc 2>/dev/null ; then
914                         sed -i -e '/^CC = /s:$: -B$(objdir)/../'"gcc-${GCC_BOOTSTRAP_VER}/${ABI}:" config.make || die
915                         mkdir -p sunrpc
916                         cp $(which rpcgen) sunrpc/cross-rpcgen || die
917                         touch -t 202001010101 sunrpc/cross-rpcgen || die
918                 fi
919         fi
920 }
921
922 toolchain-glibc_headers_configure() {
923         export ABI=default
924
925         local builddir=$(builddir "headers")
926         mkdir -p "${builddir}"
927         cd "${builddir}"
928
929         # if we don't have a compiler yet, we cant really test it now ...
930         # hopefully they don't affect header geneation, so let's hope for
931         # the best here ...
932         local v vars=(
933                 ac_cv_header_cpuid_h=yes
934                 libc_cv_{386,390,alpha,arm,hppa,ia64,mips,{powerpc,sparc}{,32,64},sh,x86_64}_tls=yes
935                 libc_cv_asm_cfi_directives=yes
936                 libc_cv_broken_visibility_attribute=no
937                 libc_cv_c_cleanup=yes
938                 libc_cv_forced_unwind=yes
939                 libc_cv_gcc___thread=yes
940                 libc_cv_mlong_double_128=yes
941                 libc_cv_mlong_double_128ibm=yes
942                 libc_cv_ppc_machine=yes
943                 libc_cv_ppc_rel16=yes
944                 libc_cv_predef_{fortify_source,stack_protector}=no
945                 libc_cv_visibility_attribute=yes
946                 libc_cv_z_combreloc=yes
947                 libc_cv_z_execstack=yes
948                 libc_cv_z_initfirst=yes
949                 libc_cv_z_nodelete=yes
950                 libc_cv_z_nodlopen=yes
951                 libc_cv_z_relro=yes
952                 libc_mips_abi=${ABI}
953                 libc_mips_float=$([[ $(tc-is-softfloat) == "yes" ]] && echo soft || echo hard)
954                 # These libs don't have configure flags.
955                 ac_cv_lib_audit_audit_log_user_avc_message=no
956                 ac_cv_lib_cap_cap_init=no
957         )
958         einfo "Forcing cached settings:"
959         for v in "${vars[@]}" ; do
960                 einfo " ${v}"
961                 export ${v}
962         done
963
964         # Blow away some random CC settings that screw things up. #550192
965         if [[ -d ${S}/sysdeps/mips ]]; then
966                 pushd "${S}"/sysdeps/mips >/dev/null
967                 sed -i -e '/^CC +=/s:=.*:= -D_MIPS_SZPTR=32:' mips32/Makefile mips64/n32/Makefile || die
968                 sed -i -e '/^CC +=/s:=.*:= -D_MIPS_SZPTR=64:' mips64/n64/Makefile || die
969                 if version_is_at_least 2.21 ; then
970                         # Force the mips ABI to the default.  This is OK because the set of
971                         # installed headers in this phase is the same between the 3 ABIs.
972                         # If this ever changes, this hack will break, but that's unlikely
973                         # as glibc discourages that behavior.
974                         # https://crbug.com/647033
975                         sed -i -e 's:abiflag=.*:abiflag=_ABIO32:' preconfigure || die
976                 fi
977                 popd >/dev/null
978         fi
979
980         local myconf=()
981         myconf+=(
982                 --disable-sanity-checks
983                 --enable-hacker-mode
984                 --without-cvs
985                 --disable-werror
986                 --enable-bind-now
987                 --build=${CBUILD_OPT:-${CBUILD}}
988                 --host=${CTARGET_OPT:-${CTARGET}}
989                 --with-headers=$(alt_build_headers)
990                 --prefix="${EPREFIX}/usr"
991                 ${EXTRA_ECONF}
992         )
993
994         local addons
995         [[ -d ${S}/ports ]] && addons+=",ports"
996         # Newer versions require nptl, so there is no addon for it.
997         version_is_at_least 2.20 || addons+=",nptl"
998         myconf+=( --enable-add-ons="${addons#,}" )
999
1000         # Nothing is compiled here which would affect the headers for the target.
1001         # So forcing CC/CFLAGS is sane.
1002         set -- "${S}"/configure "${myconf[@]}"
1003         echo "$@"
1004         CC="$(tc-getBUILD_CC)" \
1005         CFLAGS="-O1 -pipe" \
1006         CPPFLAGS="-U_FORTIFY_SOURCE" \
1007         LDFLAGS="" \
1008         "$@" || die "failed to configure glibc"
1009 }
1010
1011 toolchain-glibc_do_src_configure() {
1012         if just_headers ; then
1013                 toolchain-glibc_headers_configure
1014         else
1015                 want_linuxthreads && glibc_do_configure linuxthreads
1016                 want_nptl && glibc_do_configure nptl
1017         fi
1018 }
1019
1020 toolchain-glibc_src_configure() {
1021         foreach_abi toolchain-glibc_do_src_configure
1022 }
1023
1024 toolchain-glibc_do_src_compile() {
1025         local t
1026         for t in linuxthreads nptl ; do
1027                 if want_${t} ; then
1028                         [[ ${EAPI:-0} == [01] ]] && glibc_do_configure ${t}
1029                         emake -C "$(builddir ${t})" || die "make ${t} for ${ABI} failed"
1030                 fi
1031         done
1032 }
1033
1034 toolchain-glibc_src_compile() {
1035         if just_headers ; then
1036                 [[ ${EAPI:-0} == [01] ]] && toolchain-glibc_headers_configure
1037                 return
1038         fi
1039
1040         foreach_abi toolchain-glibc_do_src_compile
1041 }
1042
1043 glibc_src_test() {
1044         cd "$(builddir $1)"
1045         nonfatal emake -j1 check && return 0
1046         einfo "make check failed - re-running with --keep-going to get the rest of the results"
1047         nonfatal emake -j1 -k check
1048         ewarn "make check failed for ${ABI}-${CTARGET}-$1"
1049         return 1
1050 }
1051
1052 toolchain-glibc_do_src_test() {
1053         local ret=0 t
1054         for t in linuxthreads nptl ; do
1055                 if want_${t} ; then
1056                         glibc_src_test ${t}
1057                         : $(( ret |= $? ))
1058                 fi
1059         done
1060         return ${ret}
1061 }
1062
1063 toolchain-glibc_src_test() {
1064         # Give tests more time to complete.
1065         export TIMEOUTFACTOR=5
1066
1067         foreach_abi toolchain-glibc_do_src_test || die "tests failed"
1068 }
1069
1070 toolchain-glibc_do_src_install() {
1071         local builddir=$(builddir $(want_linuxthreads && echo linuxthreads || echo nptl))
1072         cd "${builddir}"
1073
1074         emake install_root="${D}$(alt_prefix)" install || die
1075
1076         if want_linuxthreads && want_nptl ; then
1077                 einfo "Installing NPTL to $(alt_libdir)/tls/..."
1078                 cd "$(builddir nptl)"
1079                 dodir $(alt_libdir)/tls $(alt_usrlibdir)/nptl
1080
1081                 local l src_lib
1082                 for l in libc libm librt libpthread libthread_db ; do
1083                         # take care of shared lib first ...
1084                         l=${l}.so
1085                         if [[ -e ${l} ]] ; then
1086                                 src_lib=${l}
1087                         else
1088                                 src_lib=$(eval echo */${l})
1089                         fi
1090                         cp -a ${src_lib} "${ED}"$(alt_libdir)/tls/${l} || die "copying nptl ${l}"
1091                         fperms a+rx $(alt_libdir)/tls/${l}
1092                         dosym ${l} $(alt_libdir)/tls/$(scanelf -qSF'%S#F' ${src_lib})
1093
1094                         # then grab the linker script or the symlink ...
1095                         if [[ -L ${ED}$(alt_usrlibdir)/${l} ]] ; then
1096                                 dosym $(alt_libdir)/tls/${l} $(alt_usrlibdir)/nptl/${l}
1097                         else
1098                                 sed \
1099                                         -e "s:/${l}:/tls/${l}:g" \
1100                                         -e "s:/${l/%.so/_nonshared.a}:/nptl/${l/%.so/_nonshared.a}:g" \
1101                                         "${ED}"$(alt_usrlibdir)/${l} > "${ED}"$(alt_usrlibdir)/nptl/${l}
1102                         fi
1103
1104                         # then grab the static lib ...
1105                         src_lib=${src_lib/%.so/.a}
1106                         [[ ! -e ${src_lib} ]] && src_lib=${src_lib/%.a/_pic.a}
1107                         cp -a ${src_lib} "${ED}"$(alt_usrlibdir)/nptl/ || die "copying nptl ${src_lib}"
1108                         src_lib=${src_lib/%.a/_nonshared.a}
1109                         if [[ -e ${src_lib} ]] ; then
1110                                 cp -a ${src_lib} "${ED}"$(alt_usrlibdir)/nptl/ || die "copying nptl ${src_lib}"
1111                         fi
1112                 done
1113
1114                 # use the nptl linker instead of the linuxthreads one as the linuxthreads
1115                 # one may lack TLS support and that can be really bad for business
1116                 cp -a elf/ld.so "${ED}"$(alt_libdir)/$(scanelf -qSF'%S#F' elf/ld.so) || die "copying nptl interp"
1117         fi
1118
1119         # Newer versions get fancy with libm linkage to include vectorized support.
1120         # While we don't really need a ldscript here, portage QA checks get upset.
1121         if [[ -e ${ED}$(alt_usrlibdir)/libm-${PV}.a ]] ; then
1122                 dosym ../../$(get_libdir)/libm-${PV}.so $(alt_usrlibdir)/libm-${PV}.so
1123         fi
1124
1125         # We'll take care of the cache ourselves
1126         rm -f "${ED}"/etc/ld.so.cache
1127
1128         # Everything past this point just needs to be done once ...
1129         is_final_abi || return 0
1130
1131         # Make sure the non-native interp can be found on multilib systems even
1132         # if the main library set isn't installed into the right place.  Maybe
1133         # we should query the active gcc for info instead of hardcoding it ?
1134         local i ldso_abi ldso_name
1135         local ldso_abi_list=(
1136                 # x86
1137                 amd64   /lib64/ld-linux-x86-64.so.2
1138                 x32     /libx32/ld-linux-x32.so.2
1139                 x86     /lib/ld-linux.so.2
1140                 # mips
1141                 o32     /lib/ld.so.1
1142                 n32     /lib32/ld.so.1
1143                 n64     /lib64/ld.so.1
1144                 # powerpc
1145                 ppc     /lib/ld.so.1
1146                 ppc64   /lib64/ld64.so.1
1147                 # s390
1148                 s390    /lib/ld.so.1
1149                 s390x   /lib/ld64.so.1
1150                 # sparc
1151                 sparc32 /lib/ld-linux.so.2
1152                 sparc64 /lib64/ld-linux.so.2
1153         )
1154         case $(tc-endian) in
1155         little)
1156                 ldso_abi_list+=(
1157                         # arm
1158                         arm64   /lib/ld-linux-aarch64.so.1
1159                 )
1160                 ;;
1161         big)
1162                 ldso_abi_list+=(
1163                         # arm
1164                         arm64   /lib/ld-linux-aarch64_be.so.1
1165                 )
1166                 ;;
1167         esac
1168         if [[ ${SYMLINK_LIB} == "yes" ]] && [[ ! -e ${ED}/$(alt_prefix)/lib ]] ; then
1169                 dosym $(get_abi_LIBDIR ${DEFAULT_ABI}) $(alt_prefix)/lib
1170         fi
1171         for (( i = 0; i < ${#ldso_abi_list[@]}; i += 2 )) ; do
1172                 ldso_abi=${ldso_abi_list[i]}
1173                 has ${ldso_abi} $(get_install_abis) || continue
1174
1175                 ldso_name="$(alt_prefix)${ldso_abi_list[i+1]}"
1176                 if [[ ! -L ${ED}/${ldso_name} && ! -e ${ED}/${ldso_name} ]] ; then
1177                         dosym ../$(get_abi_LIBDIR ${ldso_abi})/${ldso_name##*/} ${ldso_name}
1178                 fi
1179         done
1180
1181         # With devpts under Linux mounted properly, we do not need the pt_chown
1182         # binary to be setuid.  This is because the default owners/perms will be
1183         # exactly what we want.
1184         if in_iuse suid && ! use suid ; then
1185                 find "${ED}" -name pt_chown -exec chmod -s {} +
1186         fi
1187
1188         #################################################################
1189         # EVERYTHING AFTER THIS POINT IS FOR NATIVE GLIBC INSTALLS ONLY #
1190         # Make sure we install some symlink hacks so that when we build
1191         # a 2nd stage cross-compiler, gcc finds the target system
1192         # headers correctly.  See gcc/doc/gccinstall.info
1193         if is_crosscompile ; then
1194                 # We need to make sure that /lib and /usr/lib always exists.
1195                 # gcc likes to use relative paths to get to its multilibs like
1196                 # /usr/lib/../lib64/.  So while we don't install any files into
1197                 # /usr/lib/, we do need it to exist.
1198                 cd "${ED}"$(alt_libdir)/..
1199                 [[ -e lib ]] || mkdir lib
1200                 cd "${ED}"$(alt_usrlibdir)/..
1201                 [[ -e lib ]] || mkdir lib
1202
1203                 dosym usr/include $(alt_prefix)/sys-include
1204                 return 0
1205         fi
1206
1207         # Files for Debian-style locale updating
1208         dodir /usr/share/i18n
1209         sed \
1210                 -e "/^#/d" \
1211                 -e "/SUPPORTED-LOCALES=/d" \
1212                 -e "s: \\\\::g" -e "s:/: :g" \
1213                 "${S}"/localedata/SUPPORTED > "${ED}"/usr/share/i18n/SUPPORTED \
1214                 || die "generating /usr/share/i18n/SUPPORTED failed"
1215         cd "${WORKDIR}"/extra/locale
1216         dosbin locale-gen || die
1217         doman *.[0-8]
1218         insinto /etc
1219         doins locale.gen || die
1220
1221         # Make sure all the ABI's can find the locales and so we only
1222         # have to generate one set
1223         local a
1224         keepdir /usr/$(get_libdir)/locale
1225         for a in $(get_install_abis) ; do
1226                 if [[ ! -e ${ED}/usr/$(get_abi_LIBDIR ${a})/locale ]] ; then
1227                         dosym /usr/$(get_libdir)/locale /usr/$(get_abi_LIBDIR ${a})/locale
1228                 fi
1229         done
1230
1231         cd "${S}"
1232
1233         # Install misc network config files
1234         insinto /etc
1235         doins nscd/nscd.conf posix/gai.conf nss/nsswitch.conf || die
1236         doins "${WORKDIR}"/extra/etc/*.conf || die
1237
1238         if ! in_iuse nscd || use nscd ; then
1239                 doinitd "${WORKDIR}"/extra/etc/nscd || die
1240
1241                 local nscd_args=(
1242                         -e "s:@PIDFILE@:$(strings "${ED}"/usr/sbin/nscd | grep nscd.pid):"
1243                 )
1244                 version_is_at_least 2.16 || nscd_args+=( -e 's: --foreground : :' )
1245                 sed -i "${nscd_args[@]}" "${ED}"/etc/init.d/nscd
1246
1247                 # Newer versions of glibc include the nscd.service themselves.
1248                 # TODO: Drop the $FILESDIR copy once 2.19 goes stable.
1249                 if version_is_at_least 2.19 ; then
1250                         systemd_dounit nscd/nscd.service || die
1251                         systemd_newtmpfilesd nscd/nscd.tmpfiles nscd.conf || die
1252                 else
1253                         systemd_dounit "${FILESDIR}"/nscd.service || die
1254                         systemd_newtmpfilesd "${FILESDIR}"/nscd.tmpfilesd nscd.conf || die
1255                 fi
1256         else
1257                 # Do this since extra/etc/*.conf above might have nscd.conf.
1258                 rm -f "${ED}"/etc/nscd.conf
1259         fi
1260
1261         echo 'LDPATH="include ld.so.conf.d/*.conf"' > "${T}"/00glibc
1262         doenvd "${T}"/00glibc || die
1263
1264         for d in BUGS ChangeLog* CONFORMANCE FAQ NEWS NOTES PROJECTS README* ; do
1265                 [[ -s ${d} ]] && dodoc ${d}
1266         done
1267
1268         # Prevent overwriting of the /etc/localtime symlink.  We'll handle the
1269         # creation of the "factory" symlink in pkg_postinst().
1270         rm -f "${ED}"/etc/localtime
1271 }
1272
1273 toolchain-glibc_headers_install() {
1274         local builddir=$(builddir "headers")
1275         cd "${builddir}"
1276         emake install_root="${D}$(alt_prefix)" install-headers || die
1277         if ! version_is_at_least 2.16 ; then
1278                 insinto $(alt_headers)/bits
1279                 doins bits/stdio_lim.h || die
1280         fi
1281         insinto $(alt_headers)/gnu
1282         doins "${S}"/include/gnu/stubs.h || die "doins include gnu"
1283         # Make sure we install the sys-include symlink so that when
1284         # we build a 2nd stage cross-compiler, gcc finds the target
1285         # system headers correctly.  See gcc/doc/gccinstall.info
1286         dosym usr/include $(alt_prefix)/sys-include
1287 }
1288
1289 src_strip() {
1290         # gdb is lame and requires some debugging information to remain in
1291         # libpthread, so we need to strip it by hand.  libthread_db makes no
1292         # sense stripped as it is only used when debugging.
1293         local pthread=$(has splitdebug ${FEATURES} && echo "libthread_db" || echo "lib{pthread,thread_db}")
1294         env \
1295                 -uRESTRICT \
1296                 CHOST=${CTARGET} \
1297                 STRIP_MASK="/*/{,tls/}${pthread}*" \
1298                 prepallstrip
1299         # if user has stripping enabled and does not have split debug turned on,
1300         # then leave the debugging sections in libpthread.
1301         if ! has nostrip ${FEATURES} && ! has splitdebug ${FEATURES} ; then
1302                 ${STRIP:-${CTARGET}-strip} --strip-debug "${ED}"/*/libpthread-*.so
1303         fi
1304 }
1305
1306 toolchain-glibc_src_install() {
1307         if just_headers ; then
1308                 export ABI=default
1309                 toolchain-glibc_headers_install
1310                 return
1311         fi
1312
1313         foreach_abi toolchain-glibc_do_src_install
1314         src_strip
1315 }
1316
1317 # Simple test to make sure our new glibc isnt completely broken.
1318 # Make sure we don't test with statically built binaries since
1319 # they will fail.  Also, skip if this glibc is a cross compiler.
1320 #
1321 # If coreutils is built with USE=multicall, some of these files
1322 # will just be wrapper scripts, not actual ELFs we can test.
1323 glibc_sanity_check() {
1324         cd / #228809
1325
1326         # We enter ${ED} so to avoid trouble if the path contains
1327         # special characters; for instance if the path contains the
1328         # colon character (:), then the linker will try to split it
1329         # and look for the libraries in an unexpected place. This can
1330         # lead to unsafe code execution if the generated prefix is
1331         # within a world-writable directory.
1332         # (e.g. /var/tmp/portage:${HOSTNAME})
1333         pushd "${ED}"/$(get_libdir) >/dev/null
1334
1335         local x striptest
1336         for x in cal date env free ls true uname uptime ; do
1337                 x=$(type -p ${x})
1338                 [[ -z ${x} || ${x} != ${EPREFIX}/* ]] && continue
1339                 striptest=$(LC_ALL="C" file -L ${x} 2>/dev/null) || continue
1340                 case ${striptest} in
1341                 *"statically linked"*) continue;;
1342                 *"ASCII text"*) continue;;
1343                 esac
1344                 # We need to clear the locale settings as the upgrade might want
1345                 # incompatible locale data.  This test is not for verifying that.
1346                 LC_ALL=C \
1347                 ./ld-*.so --library-path . ${x} > /dev/null \
1348                         || die "simple run test (${x}) failed"
1349         done
1350
1351         popd >/dev/null
1352 }
1353
1354 toolchain-glibc_pkg_preinst() {
1355         # nothing to do if just installing headers
1356         just_headers && return
1357
1358         # prepare /etc/ld.so.conf.d/ for files
1359         mkdir -p "${EROOT}"/etc/ld.so.conf.d
1360
1361         # Default /etc/hosts.conf:multi to on for systems with small dbs.
1362         if [[ $(wc -l < "${EROOT}"/etc/hosts) -lt 1000 ]] ; then
1363                 sed -i '/^multi off/s:off:on:' "${ED}"/etc/host.conf
1364                 elog "Defaulting /etc/host.conf:multi to on"
1365         fi
1366
1367         [[ ${ROOT} != "/" ]] && return 0
1368         [[ -d ${ED}/$(get_libdir) ]] || return 0
1369         [[ -z ${BOOTSTRAP_RAP} ]] && glibc_sanity_check
1370
1371         # For newer EAPIs, this was run in pkg_pretend.
1372         if [[ ${EAPI:-0} == [0123] ]] ; then
1373                 check_devpts
1374         fi
1375 }
1376
1377 toolchain-glibc_pkg_postinst() {
1378         # nothing to do if just installing headers
1379         just_headers && return
1380
1381         if ! tc-is-cross-compiler && [[ -x ${EROOT}/usr/sbin/iconvconfig ]] ; then
1382                 # Generate fastloading iconv module configuration file.
1383                 "${EROOT}"/usr/sbin/iconvconfig --prefix="${ROOT}"
1384         fi
1385
1386         if ! is_crosscompile && [[ ${ROOT} == "/" ]] ; then
1387                 # Reload init ... if in a chroot or a diff init package, ignore
1388                 # errors from this step #253697
1389                 /sbin/telinit U 2>/dev/null
1390
1391                 # if the host locales.gen contains no entries, we'll install everything
1392                 local locale_list="${EROOT}etc/locale.gen"
1393                 if [[ -z $(locale-gen --list --config "${locale_list}") ]] ; then
1394                         ewarn "Generating all locales; edit /etc/locale.gen to save time/space"
1395                         locale_list="${EROOT}usr/share/i18n/SUPPORTED"
1396                 fi
1397                 locale-gen -j $(makeopts_jobs) --config "${locale_list}"
1398         fi
1399 }
1400
1401 _TOOLCHAIN_GLIBC_ECLASS=1
1402 fi