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