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