net-proxy/haproxy: Cleanup old versions, also re bug 711914
[gentoo.git] / sys-libs / glibc / glibc-2.30-r3.ebuild
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 EAPI=7
5
6 PYTHON_COMPAT=( python3_{6,7,8} )
7
8 inherit python-any-r1 prefix eutils toolchain-funcs flag-o-matic gnuconfig \
9         multilib systemd multiprocessing
10
11 DESCRIPTION="GNU libc C library"
12 HOMEPAGE="https://www.gnu.org/software/libc/"
13 LICENSE="LGPL-2.1+ BSD HPND ISC inner-net rc PCRE"
14 SLOT="2.2"
15
16 EMULTILIB_PKG="true"
17
18 if [[ ${PV} == 9999* ]]; then
19         EGIT_REPO_URI="https://sourceware.org/git/glibc.git"
20         inherit git-r3
21 else
22         KEYWORDS="~alpha ~amd64 ~arm ~arm64 ~hppa ~ia64 ~m68k ~mips ~ppc ~ppc64 ~riscv ~s390 ~sh ~sparc ~x86"
23         SRC_URI="mirror://gnu/glibc/${P}.tar.xz"
24 fi
25
26 RELEASE_VER=${PV}
27
28 GCC_BOOTSTRAP_VER=20180511
29
30 # Gentoo patchset
31 PATCH_VER=4
32 PATCH_DEV=dilfridge
33
34 SRC_URI+=" https://dev.gentoo.org/~${PATCH_DEV}/distfiles/${P}-patches-${PATCH_VER}.tar.xz"
35 SRC_URI+=" multilib? ( https://dev.gentoo.org/~dilfridge/distfiles/gcc-multilib-bootstrap-${GCC_BOOTSTRAP_VER}.tar.xz )"
36
37 IUSE="audit caps cet compile-locales +crypt custom-cflags doc gd headers-only +multiarch multilib nscd profile selinux +ssp +static-libs suid systemtap test vanilla"
38
39 # Minimum kernel version that glibc requires
40 MIN_KERN_VER="3.2.0"
41
42 # Here's how the cross-compile logic breaks down ...
43 #  CTARGET - machine that will target the binaries
44 #  CHOST   - machine that will host the binaries
45 #  CBUILD  - machine that will build the binaries
46 # If CTARGET != CHOST, it means you want a libc for cross-compiling.
47 # If CHOST != CBUILD, it means you want to cross-compile the libc.
48 #  CBUILD = CHOST = CTARGET    - native build/install
49 #  CBUILD != (CHOST = CTARGET) - cross-compile a native build
50 #  (CBUILD = CHOST) != CTARGET - libc for cross-compiler
51 #  CBUILD != CHOST != CTARGET  - cross-compile a libc for a cross-compiler
52 # For install paths:
53 #  CHOST = CTARGET  - install into /
54 #  CHOST != CTARGET - install into /usr/CTARGET/
55 #
56 export CBUILD=${CBUILD:-${CHOST}}
57 export CTARGET=${CTARGET:-${CHOST}}
58 if [[ ${CTARGET} == ${CHOST} ]] ; then
59         if [[ ${CATEGORY} == cross-* ]] ; then
60                 export CTARGET=${CATEGORY#cross-}
61         fi
62 fi
63
64 # Note [Disable automatic stripping]
65 # Disabling automatic stripping for a few reasons:
66 # - portage's attempt to strip breaks non-native binaries at least on
67 #   arm: bug #697428
68 # - portage's attempt to strip libpthread.so.0 breaks gdb thread
69 #   enumeration: bug #697910. This is quite subtle:
70 #   * gdb uses glibc's libthread_db-1.0.so to enumerate threads.
71 #   * libthread_db-1.0.so needs access to libpthread.so.0 local symbols
72 #     via 'ps_pglobal_lookup' symbol defined in gdb.
73 #   * 'ps_pglobal_lookup' uses '.symtab' section table to resolve all
74 #     known symbols in 'libpthread.so.0'. Specifically 'nptl_version'
75 #     (unexported) is used to sanity check compatibility before enabling
76 #     debugging.
77 #     Also see https://sourceware.org/gdb/wiki/FAQ#GDB_does_not_see_any_threads_besides_the_one_in_which_crash_occurred.3B_or_SIGTRAP_kills_my_program_when_I_set_a_breakpoint
78 #   * normal 'strip' command trims '.symtab'
79 #   Thus our main goal here is to prevent 'libpthread.so.0' from
80 #   losing it's '.symtab' entries.
81 # As Gentoo's strip does not allow us to pass less aggressive stripping
82 # options and does not check the machine target we strip selectively.
83
84 # We need a new-enough binutils/gcc to match upstream baseline.
85 # Also we need to make sure our binutils/gcc supports TLS,
86 # and that gcc already contains the hardened patches.
87 BDEPEND="
88         ${PYTHON_DEPS}
89         >=app-misc/pax-utils-0.1.10
90         sys-devel/bison
91         !<sys-devel/bison-2.7
92         !<sys-devel/make-4
93         doc? ( sys-apps/texinfo )
94 "
95 COMMON_DEPEND="
96         gd? ( media-libs/gd:2= )
97         nscd? ( selinux? (
98                 audit? ( sys-process/audit )
99                 caps? ( sys-libs/libcap )
100         ) )
101         suid? ( caps? ( sys-libs/libcap ) )
102         selinux? ( sys-libs/libselinux )
103         systemtap? ( dev-util/systemtap )
104 "
105 DEPEND="${COMMON_DEPEND}
106         test? ( >=net-dns/libidn2-2.0.5 )
107 "
108 RDEPEND="${COMMON_DEPEND}
109         sys-apps/gentoo-functions
110 "
111
112 RESTRICT="!test? ( test )"
113
114 if [[ ${CATEGORY} == cross-* ]] ; then
115         BDEPEND+=" !headers-only? (
116                 >=${CATEGORY}/binutils-2.24
117                 >=${CATEGORY}/gcc-6
118         )"
119         [[ ${CATEGORY} == *-linux* ]] && DEPEND+=" ${CATEGORY}/linux-headers"
120 else
121         BDEPEND+="
122                 >=sys-devel/binutils-2.24
123                 >=sys-devel/gcc-6
124         "
125         DEPEND+=" virtual/os-headers "
126         RDEPEND+="
127                 >=net-dns/libidn2-2.0.5
128                 vanilla? ( !sys-libs/timezone-data )
129         "
130         PDEPEND+=" !vanilla? ( sys-libs/timezone-data )"
131 fi
132
133 #
134 # Small helper functions
135 #
136
137 is_crosscompile() {
138         [[ ${CHOST} != ${CTARGET} ]]
139 }
140
141 just_headers() {
142         is_crosscompile && use headers-only
143 }
144
145 alt_prefix() {
146         is_crosscompile && echo /usr/${CTARGET}
147 }
148
149 # This prefix is applicable to CHOST when building against this
150 # glibc. It is baked into the library at configure time.
151 host_eprefix() {
152         is_crosscompile || echo "${EPREFIX}"
153 }
154
155 # This prefix is applicable to CBUILD when building against this
156 # glibc. It determines the destination path at install time.
157 build_eprefix() {
158         is_crosscompile && echo "${EPREFIX}"
159 }
160
161 # We need to be able to set alternative headers for compiling for non-native
162 # platform. Will also become useful for testing kernel-headers without screwing
163 # up the whole system.
164 alt_headers() {
165         echo ${ALT_HEADERS:=$(alt_prefix)/usr/include}
166 }
167
168 alt_build_headers() {
169         if [[ -z ${ALT_BUILD_HEADERS} ]] ; then
170                 ALT_BUILD_HEADERS="$(host_eprefix)$(alt_headers)"
171                 if tc-is-cross-compiler ; then
172                         ALT_BUILD_HEADERS=${SYSROOT}$(alt_headers)
173                         if [[ ! -e ${ALT_BUILD_HEADERS}/linux/version.h ]] ; then
174                                 local header_path=$(echo '#include <linux/version.h>' | $(tc-getCPP ${CTARGET}) ${CFLAGS} 2>&1 | grep -o '[^"]*linux/version.h')
175                                 ALT_BUILD_HEADERS=${header_path%/linux/version.h}
176                         fi
177                 fi
178         fi
179         echo "${ALT_BUILD_HEADERS}"
180 }
181
182 alt_libdir() {
183         echo $(alt_prefix)/$(get_libdir)
184 }
185 alt_usrlibdir() {
186         echo $(alt_prefix)/usr/$(get_libdir)
187 }
188
189 builddir() {
190         echo "${WORKDIR}/build-${ABI}-${CTARGET}-$1"
191 }
192
193 do_compile_test() {
194         local ret save_cflags=${CFLAGS}
195         CFLAGS+=" $1"
196         shift
197
198         pushd "${T}" >/dev/null
199
200         rm -f glibc-test*
201         printf '%b' "$*" > glibc-test.c
202
203         nonfatal emake glibc-test
204         ret=$?
205
206         popd >/dev/null
207
208         CFLAGS=${save_cflags}
209         return ${ret}
210 }
211
212 do_run_test() {
213         local ret
214
215         if [[ ${MERGE_TYPE} == "binary" ]] ; then
216                 # ignore build failures when installing a binary package #324685
217                 do_compile_test "" "$@" 2>/dev/null || return 0
218         else
219                 if ! do_compile_test "" "$@" ; then
220                         ewarn "Simple build failed ... assuming this is desired #324685"
221                         return 0
222                 fi
223         fi
224
225         pushd "${T}" >/dev/null
226
227         ./glibc-test
228         ret=$?
229         rm -f glibc-test*
230
231         popd >/dev/null
232
233         return ${ret}
234 }
235
236 setup_target_flags() {
237         # This largely mucks with compiler flags.  None of which should matter
238         # when building up just the headers.
239         just_headers && return 0
240
241         case $(tc-arch) in
242                 x86)
243                         # -march needed for #185404 #199334
244                         # TODO: When creating the first glibc cross-compile, this test will
245                         # always fail as it does a full link which in turn requires glibc.
246                         # Probably also applies when changing multilib profile settings (e.g.
247                         # enabling x86 when the profile was amd64-only previously).
248                         # We could change main to _start and pass -nostdlib here so that we
249                         # only test the gcc code compilation.  Or we could do a compile and
250                         # then look for the symbol via scanelf.
251                         if ! do_compile_test "" 'void f(int i, void *p) {if (__sync_fetch_and_add(&i, 1)) f(i, p);}\nint main(){return 0;}\n'; then
252                                 local t=${CTARGET_OPT:-${CTARGET}}
253                                 t=${t%%-*}
254                                 filter-flags '-march=*'
255                                 export CFLAGS="-march=${t} ${CFLAGS}"
256                                 einfo "Auto adding -march=${t} to CFLAGS #185404"
257                         fi
258                 ;;
259                 amd64)
260                         # -march needed for #185404 #199334
261                         # TODO: See cross-compile issues listed above for x86.
262                         [[ ${ABI} == x86 ]] &&
263                         if ! do_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'; then
264                                 local t=${CTARGET_OPT:-${CTARGET}}
265                                 t=${t%%-*}
266                                 # Normally the target is x86_64-xxx, so turn that into the -march that
267                                 # gcc actually accepts. #528708
268                                 [[ ${t} == "x86_64" ]] && t="x86-64"
269                                 filter-flags '-march=*'
270                                 # ugly, ugly, ugly.  ugly.
271                                 CFLAGS_x86=$(CFLAGS=${CFLAGS_x86} filter-flags '-march=*'; echo "${CFLAGS}")
272                                 export CFLAGS_x86="${CFLAGS_x86} -march=${t}"
273                                 einfo "Auto adding -march=${t} to CFLAGS_x86 #185404 (ABI=${ABI})"
274                         fi
275                 ;;
276                 mips)
277                         # The mips abi cannot support the GNU style hashes. #233233
278                         filter-ldflags -Wl,--hash-style=gnu -Wl,--hash-style=both
279                 ;;
280                 sparc)
281                         # Both sparc and sparc64 can use -fcall-used-g6.  -g7 is bad, though.
282                         filter-flags "-fcall-used-g7"
283                         append-flags "-fcall-used-g6"
284
285                         # If the CHOST is the basic one (e.g. not sparcv9-xxx already),
286                         # try to pick a better one so glibc can use cpu-specific .S files.
287                         # We key off the CFLAGS to get a good value.  Also need to handle
288                         # version skew.
289                         # We can't force users to set their CHOST to their exact machine
290                         # as many of these are not recognized by config.sub/gcc and such :(.
291                         # Note: If the mcpu values don't scale, we might try probing CPP defines.
292                         # Note: Should we factor in -Wa,-AvXXX flags too ?  Or -mvis/etc... ?
293
294                         local cpu
295                         case ${CTARGET} in
296                         sparc64-*)
297                                 case $(get-flag mcpu) in
298                                 niagara[234])
299                                         if ver_test -ge 2.8 ; then
300                                                 cpu="sparc64v2"
301                                         elif ver_test -ge 2.4 ; then
302                                                 cpu="sparc64v"
303                                         elif ver_test -ge 2.2.3 ; then
304                                                 cpu="sparc64b"
305                                         fi
306                                         ;;
307                                 niagara)
308                                         if ver_test -ge 2.4 ; then
309                                                 cpu="sparc64v"
310                                         elif ver_test -ge 2.2.3 ; then
311                                                 cpu="sparc64b"
312                                         fi
313                                         ;;
314                                 ultrasparc3)
315                                         cpu="sparc64b"
316                                         ;;
317                                 *)
318                                         # We need to force at least v9a because the base build doesn't
319                                         # work with just v9.
320                                         # https://sourceware.org/bugzilla/show_bug.cgi?id=19477
321                                         [[ -z ${cpu} ]] && append-flags "-Wa,-xarch=v9a"
322                                         ;;
323                                 esac
324                                 ;;
325                         sparc-*)
326                                 case $(get-flag mcpu) in
327                                 niagara[234])
328                                         if ver_test -ge 2.8 ; then
329                                                 cpu="sparcv9v2"
330                                         elif ver_test -ge 2.4 ; then
331                                                 cpu="sparcv9v"
332                                         elif ver_test -ge 2.2.3 ; then
333                                                 cpu="sparcv9b"
334                                         else
335                                                 cpu="sparcv9"
336                                         fi
337                                         ;;
338                                 niagara)
339                                         if ver_test -ge 2.4 ; then
340                                                 cpu="sparcv9v"
341                                         elif ver_test -ge 2.2.3 ; then
342                                                 cpu="sparcv9b"
343                                         else
344                                                 cpu="sparcv9"
345                                         fi
346                                         ;;
347                                 ultrasparc3)
348                                         cpu="sparcv9b"
349                                         ;;
350                                 v9|ultrasparc)
351                                         cpu="sparcv9"
352                                         ;;
353                                 v8|supersparc|hypersparc|leon|leon3)
354                                         cpu="sparcv8"
355                                         ;;
356                                 esac
357                         ;;
358                         esac
359                         [[ -n ${cpu} ]] && CTARGET_OPT="${cpu}-${CTARGET#*-}"
360                 ;;
361         esac
362 }
363
364 setup_flags() {
365         # Make sure host make.conf doesn't pollute us
366         if is_crosscompile || tc-is-cross-compiler ; then
367                 CHOST=${CTARGET} strip-unsupported-flags
368         fi
369
370         # Store our CFLAGS because it's changed depending on which CTARGET
371         # we are building when pulling glibc on a multilib profile
372         CFLAGS_BASE=${CFLAGS_BASE-${CFLAGS}}
373         CFLAGS=${CFLAGS_BASE}
374         CXXFLAGS_BASE=${CXXFLAGS_BASE-${CXXFLAGS}}
375         CXXFLAGS=${CXXFLAGS_BASE}
376         ASFLAGS_BASE=${ASFLAGS_BASE-${ASFLAGS}}
377         ASFLAGS=${ASFLAGS_BASE}
378
379         # Allow users to explicitly avoid flag sanitization via
380         # USE=custom-cflags.
381         if ! use custom-cflags; then
382                 # Over-zealous CFLAGS can often cause problems.  What may work for one
383                 # person may not work for another.  To avoid a large influx of bugs
384                 # relating to failed builds, we strip most CFLAGS out to ensure as few
385                 # problems as possible.
386                 strip-flags
387                 # Lock glibc at -O2; we want to be conservative here.
388                 filter-flags '-O?'
389                 append-flags -O2
390         fi
391         strip-unsupported-flags
392         filter-flags -m32 -m64 '-mabi=*'
393
394         # glibc aborts if rpath is set by LDFLAGS
395         filter-ldflags '-Wl,-rpath=*'
396
397         # #492892
398         filter-flags -frecord-gcc-switches
399
400         unset CBUILD_OPT CTARGET_OPT
401         if use multilib ; then
402                 CTARGET_OPT=$(get_abi_CTARGET)
403                 [[ -z ${CTARGET_OPT} ]] && CTARGET_OPT=$(get_abi_CHOST)
404         fi
405
406         setup_target_flags
407
408         if [[ -n ${CTARGET_OPT} && ${CBUILD} == ${CHOST} ]] && ! is_crosscompile; then
409                 CBUILD_OPT=${CTARGET_OPT}
410         fi
411
412         # glibc's headers disallow -O0 and fail at build time:
413         #  include/libc-symbols.h:75:3: #error "glibc cannot be compiled without optimization"
414         replace-flags -O0 -O1
415
416         filter-flags '-fstack-protector*'
417 }
418
419 want_tls() {
420         # Archs that can use TLS (Thread Local Storage)
421         case $(tc-arch) in
422                 x86)
423                         # requires i486 or better #106556
424                         [[ ${CTARGET} == i[4567]86* ]] && return 0
425                         return 1
426                 ;;
427         esac
428         return 0
429 }
430
431 want__thread() {
432         want_tls || return 1
433
434         # For some reason --with-tls --with__thread is causing segfaults on sparc32.
435         [[ ${PROFILE_ARCH} == "sparc" ]] && return 1
436
437         [[ -n ${WANT__THREAD} ]] && return ${WANT__THREAD}
438
439         # only test gcc -- can't test linking yet
440         tc-has-tls -c ${CTARGET}
441         WANT__THREAD=$?
442
443         return ${WANT__THREAD}
444 }
445
446 use_multiarch() {
447         # Allow user to disable runtime arch detection in multilib.
448         use multiarch || return 1
449         # Make sure binutils is new enough to support indirect functions,
450         # #336792. This funky sed supports gold and bfd linkers.
451         local bver nver
452         bver=$($(tc-getLD ${CTARGET}) -v | sed -n -r '1{s:[^0-9]*::;s:^([0-9.]*).*:\1:;p}')
453         case $(tc-arch ${CTARGET}) in
454         amd64|x86) nver="2.20" ;;
455         arm)       nver="2.22" ;;
456         hppa)      nver="2.23" ;;
457         ppc|ppc64) nver="2.20" ;;
458         # ifunc support was added in 2.23, but glibc also needs
459         # machinemode which is in 2.24.
460         s390)      nver="2.24" ;;
461         sparc)     nver="2.21" ;;
462         *)         return 1 ;;
463         esac
464         ver_test ${bver} -ge ${nver}
465 }
466
467 # Setup toolchain variables that had historically been defined in the
468 # profiles for these archs.
469 setup_env() {
470         # silly users
471         unset LD_RUN_PATH
472         unset LD_ASSUME_KERNEL
473
474         if is_crosscompile || tc-is-cross-compiler ; then
475                 multilib_env ${CTARGET_OPT:-${CTARGET}}
476
477                 if ! use multilib ; then
478                         MULTILIB_ABIS=${DEFAULT_ABI}
479                 else
480                         MULTILIB_ABIS=${MULTILIB_ABIS:-${DEFAULT_ABI}}
481                 fi
482
483                 # If the user has CFLAGS_<CTARGET> in their make.conf, use that,
484                 # and fall back on CFLAGS.
485                 local VAR=CFLAGS_${CTARGET//[-.]/_}
486                 CFLAGS=${!VAR-${CFLAGS}}
487                 einfo " $(printf '%15s' 'Manual CFLAGS:')   ${CFLAGS}"
488         fi
489
490         setup_flags
491
492         export ABI=${ABI:-${DEFAULT_ABI:-default}}
493
494         if just_headers ; then
495                 # Avoid mixing host's CC and target's CFLAGS_${ABI}:
496                 # At this bootstrap stage we have only binutils for
497                 # target but not compiler yet.
498                 einfo "Skip CC ABI injection. We can't use (cross-)compiler yet."
499                 return 0
500         fi
501         local VAR=CFLAGS_${ABI}
502         # We need to export CFLAGS with abi information in them because glibc's
503         # configure script checks CFLAGS for some targets (like mips).  Keep
504         # around the original clean value to avoid appending multiple ABIs on
505         # top of each other.
506         : ${__GLIBC_CC:=$(tc-getCC ${CTARGET_OPT:-${CTARGET}})}
507         export __GLIBC_CC CC="${__GLIBC_CC} ${!VAR}"
508         einfo " $(printf '%15s' 'Manual CC:')   ${CC}"
509 }
510
511 foreach_abi() {
512         setup_env
513
514         local ret=0
515         local abilist=""
516         if use multilib ; then
517                 abilist=$(get_install_abis)
518         else
519                 abilist=${DEFAULT_ABI}
520         fi
521         local -x ABI
522         for ABI in ${abilist:-default} ; do
523                 setup_env
524                 einfo "Running $1 for ABI ${ABI}"
525                 $1
526                 : $(( ret |= $? ))
527         done
528         return ${ret}
529 }
530
531 glibc_banner() {
532         local b="Gentoo ${PVR}"
533         [[ -n ${PATCH_VER} ]] && ! use vanilla && b+=" p${PATCH_VER}"
534         echo "${b}"
535 }
536
537 check_devpts() {
538         # Make sure devpts is mounted correctly for use w/out setuid pt_chown.
539
540         # If merely building the binary package, then there's nothing to verify.
541         [[ ${MERGE_TYPE} == "buildonly" ]] && return
542
543         # Only sanity check when installing the native glibc.
544         [[ -n ${ROOT} ]] && return
545
546         # If they're opting in to the old suid code, then no need to check.
547         use suid && return
548
549         if awk '$3 == "devpts" && $4 ~ /[, ]gid=5[, ]/ { exit 1 }' /proc/mounts ; then
550                 eerror "In order to use glibc with USE=-suid, you must make sure that"
551                 eerror "you have devpts mounted at /dev/pts with the gid=5 option."
552                 eerror "Openrc should do this for you, so you should check /etc/fstab"
553                 eerror "and make sure you do not have any invalid settings there."
554                 die "mount & fix your /dev/pts settings"
555         fi
556 }
557
558 # The following Kernel version handling functions are mostly copied from portage
559 # source. It's better not to use linux-info.eclass here since a) it adds too
560 # much magic, see bug 326693 for some of the arguments, and b) some of the
561 # functions are just not provided.
562
563 g_get_running_KV() {
564         uname -r
565         return $?
566 }
567
568 g_KV_major() {
569         [[ -z $1 ]] && return 1
570         local KV=$@
571         echo "${KV%%.*}"
572 }
573
574 g_KV_minor() {
575         [[ -z $1 ]] && return 1
576         local KV=$@
577         KV=${KV#*.}
578         echo "${KV%%.*}"
579 }
580
581 g_KV_micro() {
582         [[ -z $1 ]] && return 1
583         local KV=$@
584         KV=${KV#*.*.}
585         echo "${KV%%[^[:digit:]]*}"
586 }
587
588 g_KV_to_int() {
589         [[ -z $1 ]] && return 1
590         local KV_MAJOR=$(g_KV_major "$1")
591         local KV_MINOR=$(g_KV_minor "$1")
592         local KV_MICRO=$(g_KV_micro "$1")
593         local KV_int=$(( KV_MAJOR * 65536 + KV_MINOR * 256 + KV_MICRO ))
594
595         # We make version 2.2.0 the minimum version we will handle as
596         # a sanity check ... if its less, we fail ...
597         if [[ ${KV_int} -ge 131584 ]] ; then
598                 echo "${KV_int}"
599                 return 0
600         fi
601         return 1
602 }
603
604 g_int_to_KV() {
605         local version=$1 major minor micro
606         major=$((version / 65536))
607         minor=$(((version % 65536) / 256))
608         micro=$((version % 256))
609         echo ${major}.${minor}.${micro}
610 }
611
612 eend_KV() {
613         [[ $(g_KV_to_int $1) -ge $(g_KV_to_int $2) ]]
614         eend $?
615 }
616
617 get_kheader_version() {
618         printf '#include <linux/version.h>\nLINUX_VERSION_CODE\n' | \
619         $(tc-getCPP ${CTARGET}) -I "$(build_eprefix)$(alt_build_headers)" - | \
620         tail -n 1
621 }
622
623 # We collect all sanity checks here. Consistency is not guranteed between
624 # pkg_ and src_ phases, so we call this function both in pkg_pretend and in
625 # src_unpack.
626 sanity_prechecks() {
627         # Make sure devpts is mounted correctly for use w/out setuid pt_chown
628         check_devpts
629
630         # Prevent native builds from downgrading
631         if [[ ${MERGE_TYPE} != "buildonly" ]] && \
632            [[ -z ${ROOT} ]] && \
633            [[ ${CBUILD} == ${CHOST} ]] && \
634            [[ ${CHOST} == ${CTARGET} ]] ; then
635
636                 # The high rev # is to allow people to downgrade between -r#
637                 # versions. We want to block 2.20->2.19, but 2.20-r3->2.20-r2
638                 # should be fine. Hopefully we never actually use a r# this
639                 # high.
640                 if has_version ">${CATEGORY}/${P}-r10000" ; then
641                         eerror "Sanity check to keep you from breaking your system:"
642                         eerror " Downgrading glibc is not supported and a sure way to destruction."
643                         [[ ${I_ALLOW_TO_BREAK_MY_SYSTEM} = yes ]] || die "Aborting to save your system."
644                 fi
645
646                 if ! do_run_test '#include <unistd.h>\n#include <sys/syscall.h>\nint main(){return syscall(1000)!=-1;}\n' ; then
647                         eerror "Your old kernel is broken. You need to update it to a newer"
648                         eerror "version as syscall(<bignum>) will break. See bug 279260."
649                         die "Old and broken kernel."
650                 fi
651         fi
652
653         # Users have had a chance to phase themselves, time to give em the boot
654         if [[ -e ${EROOT}/etc/locale.gen ]] && [[ -e ${EROOT}/etc/locales.build ]] ; then
655                 eerror "You still haven't deleted ${EROOT}/etc/locales.build."
656                 eerror "Do so now after making sure ${EROOT}/etc/locale.gen is kosher."
657                 die "Lazy upgrader detected"
658         fi
659
660         if [[ ${CTARGET} == i386-* ]] ; then
661                 eerror "i386 CHOSTs are no longer supported."
662                 eerror "Chances are you don't actually want/need i386."
663                 eerror "Please read https://www.gentoo.org/doc/en/change-chost.xml"
664                 die "Please fix your CHOST"
665         fi
666
667         if [[ -e /proc/xen ]] && [[ $(tc-arch) == "x86" ]] && ! is-flag -mno-tls-direct-seg-refs ; then
668                 ewarn "You are using Xen but don't have -mno-tls-direct-seg-refs in your CFLAGS."
669                 ewarn "This will result in a 50% performance penalty when running with a 32bit"
670                 ewarn "hypervisor, which is probably not what you want."
671         fi
672
673         # Check for sanity of /etc/nsswitch.conf
674         if [[ -e ${EROOT}/etc/nsswitch.conf ]] ; then
675                 local entry
676                 for entry in passwd group shadow; do
677                         if ! egrep -q "^[ \t]*${entry}:.*files" "${EROOT}"/etc/nsswitch.conf; then
678                                 eerror "Your ${EROOT}/etc/nsswitch.conf is out of date."
679                                 eerror "Please make sure you have 'files' entries for"
680                                 eerror "'passwd:', 'group:' and 'shadow:' databases."
681                                 eerror "For more details see:"
682                                 eerror "  https://wiki.gentoo.org/wiki/Project:Toolchain/nsswitch.conf_in_glibc-2.26"
683                                 die "nsswitch.conf has no 'files' provider in '${entry}'."
684                         fi
685                 done
686         fi
687
688         # ABI-specific checks follow here. Hey, we have a lot more specific conditions that
689         # we test for...
690         if ! is_crosscompile ; then
691                 if use amd64 && use multilib && [[ ${MERGE_TYPE} != "binary" ]] ; then
692                         ebegin "Checking that IA32 emulation is enabled in the running kernel"
693                         echo 'int main(){return 0;}' > "${T}/check-ia32-emulation.c"
694                         local STAT
695                         if "${CC-${CHOST}-gcc}" ${CFLAGS_x86} "${T}/check-ia32-emulation.c" -o "${T}/check-ia32-emulation.elf32"; then
696                                 "${T}/check-ia32-emulation.elf32"
697                                 STAT=$?
698                         else
699                                 # Don't fail here to allow single->multi ABI switch
700                                 # or recover from breakage like bug #646424
701                                 ewarn "Failed to compile the ABI test. Broken host glibc?"
702                                 STAT=0
703                         fi
704                         rm -f "${T}/check-ia32-emulation.elf32"
705                         eend $STAT
706                         [[ $STAT -eq 0 ]] || die "CONFIG_IA32_EMULATION must be enabled in the kernel to compile a multilib glibc."
707                 fi
708
709         fi
710
711         # When we actually have to compile something...
712         if ! just_headers ; then
713                 ebegin "Checking gcc for __thread support"
714                 if ! eend $(want__thread ; echo $?) ; then
715                         echo
716                         eerror "Could not find a gcc that supports the __thread directive!"
717                         eerror "Please update your binutils/gcc and try again."
718                         die "No __thread support in gcc!"
719                 fi
720
721                 if [[ ${CTARGET} == *-linux* ]] ; then
722                         local run_kv build_kv want_kv
723
724                         run_kv=$(g_get_running_KV)
725                         build_kv=$(g_int_to_KV $(get_kheader_version))
726                         want_kv=${MIN_KERN_VER}
727
728                         if ! is_crosscompile && ! tc-is-cross-compiler ; then
729                                 # Building fails on an non-supporting kernel
730                                 ebegin "Checking running kernel version (${run_kv} >= ${want_kv})"
731                                 if ! eend_KV ${run_kv} ${want_kv} ; then
732                                         echo
733                                         eerror "You need a kernel of at least ${want_kv}!"
734                                         die "Kernel version too low!"
735                                 fi
736                         fi
737
738                         ebegin "Checking linux-headers version (${build_kv} >= ${want_kv})"
739                         if ! eend_KV ${build_kv} ${want_kv} ; then
740                                 echo
741                                 eerror "You need linux-headers of at least ${want_kv}!"
742                                 die "linux-headers version too low!"
743                         fi
744                 fi
745         fi
746 }
747
748 #
749 # the phases
750 #
751
752 # pkg_pretend
753
754 pkg_pretend() {
755         # All the checks...
756         einfo "Checking general environment sanity."
757         sanity_prechecks
758 }
759
760 pkg_setup() {
761         # see bug 682570
762         [[ -z ${BOOTSTRAP_RAP} ]] && python-any-r1_pkg_setup
763 }
764
765 # src_unpack
766
767 src_unpack() {
768         # Consistency is not guaranteed between pkg_ and src_ ...
769         sanity_prechecks
770
771         use multilib && unpack gcc-multilib-bootstrap-${GCC_BOOTSTRAP_VER}.tar.xz
772
773         setup_env
774
775         if [[ -n ${EGIT_REPO_URI} ]] ; then
776                 git-r3_src_unpack
777         else
778                 unpack ${P}.tar.xz
779         fi
780
781         cd "${WORKDIR}" || die
782         unpack glibc-${RELEASE_VER}-patches-${PATCH_VER}.tar.xz
783 }
784
785 src_prepare() {
786         if ! use vanilla ; then
787                 elog "Applying Gentoo Glibc Patchset ${RELEASE_VER}-${PATCH_VER}"
788                 eapply "${WORKDIR}"/patches
789                 einfo "Done."
790         fi
791
792         default
793
794         gnuconfig_update
795
796         cd "${WORKDIR}"
797         find . -name configure -exec touch {} +
798
799         eprefixify extra/locale/locale-gen
800
801         # Fix permissions on some of the scripts.
802         chmod u+x "${S}"/scripts/*.sh
803
804         cd "${S}"
805 }
806
807 glibc_do_configure() {
808         # Glibc does not work with gold (for various reasons) #269274.
809         tc-ld-disable-gold
810
811         # CXX isnt handled by the multilib system, so if we dont unset here
812         # we accumulate crap across abis
813         unset CXX
814
815         einfo "Configuring glibc for nptl"
816
817         if use doc ; then
818                 export MAKEINFO=makeinfo
819         else
820                 export MAKEINFO=/dev/null
821         fi
822
823         local v
824         for v in ABI CBUILD CHOST CTARGET CBUILD_OPT CTARGET_OPT CC CXX LD {AS,C,CPP,CXX,LD}FLAGS MAKEINFO ; do
825                 einfo " $(printf '%15s' ${v}:)   ${!v}"
826         done
827
828         # CFLAGS can contain ABI-specific flags like -mfpu=neon, see bug #657760
829         # To build .S (assembly) files with the same ABI-specific flags
830         # upstream currently recommends adding CFLAGS to CC/CXX:
831         #    https://sourceware.org/PR23273
832         # Note: Passing CFLAGS via CPPFLAGS overrides glibc's arch-specific CFLAGS
833         # and breaks multiarch support. See 659030#c3 for an example.
834         # The glibc configure script doesn't properly use LDFLAGS all the time.
835         export CC="$(tc-getCC ${CTARGET}) ${CFLAGS} ${LDFLAGS}"
836         einfo " $(printf '%15s' 'Manual CC:')   ${CC}"
837
838         # Some of the tests are written in C++, so we need to force our multlib abis in, bug 623548
839         export CXX="$(tc-getCXX ${CTARGET}) $(get_abi_CFLAGS) ${CFLAGS}"
840
841         if is_crosscompile; then
842                 # Assume worst-case bootstrap: glibc is buil first time
843                 # when ${CTARGET}-g++ is not available yet. We avoid
844                 # building auxiliary programs that require C++: bug #683074
845                 # It should not affect final result.
846                 export libc_cv_cxx_link_ok=no
847                 # The line above has the same effect. We set CXX explicitly
848                 # to make build logs less confusing.
849                 export CXX=
850         fi
851         einfo " $(printf '%15s' 'Manual CXX:')   ${CXX}"
852
853         echo
854
855         local myconf=()
856
857         case ${CTARGET} in
858                 m68k*)
859                         # setjmp() is not compatible with stack protection:
860                         # https://sourceware.org/PR24202
861                         myconf+=( --enable-stack-protector=no )
862                         ;;
863                 powerpc-*)
864                         # Currently gcc on powerpc32 generates invalid code for
865                         # __builtin_return_address(0) calls. Normally programs
866                         # don't do that but malloc hooks in glibc do:
867                         # https://gcc.gnu.org/PR81996
868                         # https://bugs.gentoo.org/629054
869                         myconf+=( --enable-stack-protector=no )
870                         ;;
871                 *)
872                         myconf+=( --enable-stack-protector=$(usex ssp all no) )
873                         ;;
874         esac
875         myconf+=( --enable-stackguard-randomization )
876
877         # Keep a whitelist of targets supporing IFUNC. glibc's ./configure
878         # is not robust enough to detect proper support:
879         #    https://bugs.gentoo.org/641216
880         #    https://sourceware.org/PR22634#c0
881         case $(tc-arch ${CTARGET}) in
882                 # Keep whitelist of targets where autodetection mostly works.
883                 amd64|x86|sparc|ppc|ppc64|arm|arm64|s390) ;;
884                 # Blacklist everywhere else
885                 *) myconf+=( libc_cv_ld_gnu_indirect_function=no ) ;;
886         esac
887
888         # Enable Intel Control-flow Enforcement Technology on amd64 if requested
889         case ${CTARGET} in
890                 x86_64-*) myconf+=( $(use_enable cet) ) ;;
891                 *) ;;
892         esac
893
894         [[ $(tc-is-softfloat) == "yes" ]] && myconf+=( --without-fp )
895
896         myconf+=( --enable-kernel=${MIN_KERN_VER} )
897
898         # Since SELinux support is only required for nscd, only enable it if:
899         # 1. USE selinux
900         # 2. only for the primary ABI on multilib systems
901         # 3. Not a crosscompile
902         if ! is_crosscompile && use selinux ; then
903                 if use multilib ; then
904                         if is_final_abi ; then
905                                 myconf+=( --with-selinux )
906                         else
907                                 myconf+=( --without-selinux )
908                         fi
909                 else
910                         myconf+=( --with-selinux )
911                 fi
912         else
913                 myconf+=( --without-selinux )
914         fi
915
916         # Force a few tests where we always know the answer but
917         # configure is incapable of finding it.
918         if is_crosscompile ; then
919                 export \
920                         libc_cv_c_cleanup=yes \
921                         libc_cv_forced_unwind=yes
922         fi
923
924         myconf+=(
925                 --without-cvs
926                 --disable-werror
927                 --enable-bind-now
928                 --build=${CBUILD_OPT:-${CBUILD}}
929                 --host=${CTARGET_OPT:-${CTARGET}}
930                 $(use_enable profile)
931                 $(use_with gd)
932                 --with-headers=$(build_eprefix)$(alt_build_headers)
933                 --prefix="$(host_eprefix)/usr"
934                 --sysconfdir="$(host_eprefix)/etc"
935                 --localstatedir="$(host_eprefix)/var"
936                 --libdir='$(prefix)'/$(get_libdir)
937                 --mandir='$(prefix)'/share/man
938                 --infodir='$(prefix)'/share/info
939                 --libexecdir='$(libdir)'/misc/glibc
940                 --with-bugurl=https://bugs.gentoo.org/
941                 --with-pkgversion="$(glibc_banner)"
942                 $(use_enable crypt)
943                 $(use_multiarch || echo --disable-multi-arch)
944                 $(use_enable systemtap)
945                 $(use_enable nscd)
946                 ${EXTRA_ECONF}
947         )
948
949         # We rely on sys-libs/timezone-data for timezone tools normally.
950         myconf+=( $(use_enable vanilla timezone-tools) )
951
952         # These libs don't have configure flags.
953         ac_cv_lib_audit_audit_log_user_avc_message=$(usex audit || echo no)
954         ac_cv_lib_cap_cap_init=$(usex caps || echo no)
955
956         # There is no configure option for this and we need to export it
957         # since the glibc build will re-run configure on itself
958         export libc_cv_rootsbindir="$(host_eprefix)/sbin"
959         export libc_cv_slibdir="$(host_eprefix)/$(get_libdir)"
960
961         # We take care of patching our binutils to use both hash styles,
962         # and many people like to force gnu hash style only, so disable
963         # this overriding check.  #347761
964         export libc_cv_hashstyle=no
965
966         local builddir=$(builddir nptl)
967         mkdir -p "${builddir}"
968         cd "${builddir}"
969         set -- "${S}"/configure "${myconf[@]}"
970         echo "$@"
971         "$@" || die "failed to configure glibc"
972
973         # ia64 static cross-compilers are a pita in so much that they
974         # can't produce static ELFs (as the libgcc.a is broken).  so
975         # disable building of the programs for those targets if it
976         # doesn't work.
977         # XXX: We could turn this into a compiler test, but ia64 is
978         # the only one that matters, so this should be fine for now.
979         if is_crosscompile && [[ ${CTARGET} == ia64* ]] ; then
980                 sed -i '1i+link-static = touch $@' config.make
981         fi
982
983         # If we're trying to migrate between ABI sets, we need
984         # to lie and use a local copy of gcc.  Like if the system
985         # is built with MULTILIB_ABIS="amd64 x86" but we want to
986         # add x32 to it, gcc/glibc don't yet support x32.
987         #
988         if [[ -n ${GCC_BOOTSTRAP_VER} ]] && use multilib ; then
989                 echo 'main(){}' > "${T}"/test.c
990                 if ! $(tc-getCC ${CTARGET}) ${CFLAGS} ${LDFLAGS} "${T}"/test.c -Wl,-emain -lgcc 2>/dev/null ; then
991                         sed -i -e '/^CC = /s:$: -B$(objdir)/../'"gcc-multilib-bootstrap-${GCC_BOOTSTRAP_VER}/${ABI}:" config.make || die
992                 fi
993         fi
994 }
995
996 glibc_headers_configure() {
997         export ABI=default
998
999         local builddir=$(builddir "headers")
1000         mkdir -p "${builddir}"
1001         cd "${builddir}"
1002
1003         # if we don't have a compiler yet, we can't really test it now ...
1004         # hopefully they don't affect header generation, so let's hope for
1005         # the best here ...
1006         local v vars=(
1007                 ac_cv_header_cpuid_h=yes
1008                 libc_cv_{386,390,alpha,arm,hppa,ia64,mips,{powerpc,sparc}{,32,64},sh,x86_64}_tls=yes
1009                 libc_cv_asm_cfi_directives=yes
1010                 libc_cv_broken_visibility_attribute=no
1011                 libc_cv_c_cleanup=yes
1012                 libc_cv_compiler_powerpc64le_binary128_ok=yes
1013                 libc_cv_forced_unwind=yes
1014                 libc_cv_gcc___thread=yes
1015                 libc_cv_mlong_double_128=yes
1016                 libc_cv_mlong_double_128ibm=yes
1017                 libc_cv_ppc_machine=yes
1018                 libc_cv_ppc_rel16=yes
1019                 libc_cv_predef_fortify_source=no
1020                 libc_cv_target_power8_ok=yes
1021                 libc_cv_visibility_attribute=yes
1022                 libc_cv_z_combreloc=yes
1023                 libc_cv_z_execstack=yes
1024                 libc_cv_z_initfirst=yes
1025                 libc_cv_z_nodelete=yes
1026                 libc_cv_z_nodlopen=yes
1027                 libc_cv_z_relro=yes
1028                 libc_mips_abi=${ABI}
1029                 libc_mips_float=$([[ $(tc-is-softfloat) == "yes" ]] && echo soft || echo hard)
1030                 # These libs don't have configure flags.
1031                 ac_cv_lib_audit_audit_log_user_avc_message=no
1032                 ac_cv_lib_cap_cap_init=no
1033         )
1034
1035         einfo "Forcing cached settings:"
1036         for v in "${vars[@]}" ; do
1037                 einfo " ${v}"
1038                 export ${v}
1039         done
1040
1041         local headers_only_arch_CPPFLAGS=()
1042
1043         # Blow away some random CC settings that screw things up. #550192
1044         if [[ -d ${S}/sysdeps/mips ]]; then
1045                 pushd "${S}"/sysdeps/mips >/dev/null
1046                 sed -i -e '/^CC +=/s:=.*:= -D_MIPS_SZPTR=32:' mips32/Makefile mips64/n32/Makefile || die
1047                 sed -i -e '/^CC +=/s:=.*:= -D_MIPS_SZPTR=64:' mips64/n64/Makefile || die
1048
1049                 # Force the mips ABI to the default.  This is OK because the set of
1050                 # installed headers in this phase is the same between the 3 ABIs.
1051                 # If this ever changes, this hack will break, but that's unlikely
1052                 # as glibc discourages that behavior.
1053                 # https://crbug.com/647033
1054                 sed -i -e 's:abiflag=.*:abiflag=_ABIO32:' preconfigure || die
1055
1056                 popd >/dev/null
1057         fi
1058
1059         case ${CTARGET} in
1060         riscv*)
1061                 # RISC-V interrogates the compiler to determine which target to
1062                 # build.  If building the headers then we don't strictly need a
1063                 # RISC-V compiler, so the built-in definitions that are provided
1064                 # along with all RISC-V compiler might not exist.  This causes
1065                 # glibc's RISC-V preconfigure script to blow up.  Since we're just
1066                 # building the headers any value will actually work here, so just
1067                 # pick the standard one (rv64g/lp64d) to make the build scripts
1068                 # happy for now -- the headers are all the same anyway so it
1069                 # doesn't matter.
1070                 headers_only_arch_CPPFLAGS+=(
1071                         -D__riscv_xlen=64
1072                         -D__riscv_flen=64
1073                         -D__riscv_float_abi_double=1
1074                         -D__riscv_atomic=1
1075                 ) ;;
1076         esac
1077
1078         local myconf=()
1079         myconf+=(
1080                 --disable-sanity-checks
1081                 --enable-hacker-mode
1082                 --without-cvs
1083                 --disable-werror
1084                 --enable-bind-now
1085                 --build=${CBUILD_OPT:-${CBUILD}}
1086                 --host=${CTARGET_OPT:-${CTARGET}}
1087                 --with-headers=$(build_eprefix)$(alt_build_headers)
1088                 --prefix="$(host_eprefix)/usr"
1089                 ${EXTRA_ECONF}
1090         )
1091
1092         # Nothing is compiled here which would affect the headers for the target.
1093         # So forcing CC/CFLAGS is sane.
1094         local headers_only_CC=$(tc-getBUILD_CC)
1095         local headers_only_CFLAGS="-O1 -pipe"
1096         local headers_only_CPPFLAGS="-U_FORTIFY_SOURCE ${headers_only_arch_CPPFLAGS[*]}"
1097         local headers_only_LDFLAGS=""
1098         set -- "${S}"/configure "${myconf[@]}"
1099         echo \
1100                 "CC=${headers_only_CC}" \
1101                 "CFLAGS=${headers_only_CFLAGS}" \
1102                 "CPPFLAGS=${headers_only_CPPFLAGS}" \
1103                 "LDFLAGS=${headers_only_LDFLAGS}" \
1104                 "$@"
1105         CC=${headers_only_CC} \
1106         CFLAGS=${headers_only_CFLAGS} \
1107         CPPFLAGS=${headers_only_CPPFLAGS} \
1108         LDFLAGS="" \
1109         "$@" || die "failed to configure glibc"
1110 }
1111
1112 do_src_configure() {
1113         if just_headers ; then
1114                 glibc_headers_configure
1115         else
1116                 glibc_do_configure nptl
1117         fi
1118 }
1119
1120 src_configure() {
1121         foreach_abi do_src_configure
1122 }
1123
1124 do_src_compile() {
1125         emake -C "$(builddir nptl)"
1126 }
1127
1128 src_compile() {
1129         if just_headers ; then
1130                 return
1131         fi
1132
1133         foreach_abi do_src_compile
1134 }
1135
1136 glibc_src_test() {
1137         cd "$(builddir nptl)"
1138         emake check
1139 }
1140
1141 do_src_test() {
1142         local ret=0
1143
1144         glibc_src_test
1145         : $(( ret |= $? ))
1146
1147         return ${ret}
1148 }
1149
1150 src_test() {
1151         if just_headers ; then
1152                 return
1153         fi
1154
1155         # Give tests more time to complete.
1156         export TIMEOUTFACTOR=5
1157
1158         foreach_abi do_src_test || die "tests failed"
1159 }
1160
1161 run_locale_gen() {
1162         # if the host locales.gen contains no entries, we'll install everything
1163         local root="$1"
1164         local inplace=""
1165
1166         if [[ "${root}" == "--inplace-glibc" ]] ; then
1167                 inplace="--inplace-glibc"
1168                 root="$2"
1169         fi
1170
1171         local locale_list="${root}/etc/locale.gen"
1172
1173         pushd "${ED}"/$(get_libdir) >/dev/null
1174
1175         if [[ -z $(locale-gen --list --config "${locale_list}") ]] ; then
1176                 [[ -z ${inplace} ]] && ewarn "Generating all locales; edit /etc/locale.gen to save time/space"
1177                 locale_list="${root}/usr/share/i18n/SUPPORTED"
1178         fi
1179
1180         locale-gen ${inplace} --jobs $(makeopts_jobs) --config "${locale_list}" \
1181                 --destdir "${root}"
1182
1183         popd >/dev/null
1184 }
1185
1186 glibc_do_src_install() {
1187         local builddir=$(builddir nptl)
1188         cd "${builddir}"
1189
1190         emake install_root="${D}/$(build_eprefix)$(alt_prefix)" install
1191
1192         # This version (2.26) provides some compatibility libraries for the NIS/NIS+ support
1193         # which come without headers etc. Only needed for binary packages since the
1194         # external net-libs/libnsl has increased soversion. Keep only versioned libraries.
1195         find "${D}" -name "libnsl.a" -delete
1196         find "${D}" -name "libnsl.so" -delete
1197
1198         # Normally upstream_pv is ${PV}. Live ebuilds are exception, there we need
1199         # to infer upstream version:
1200         # '#define VERSION "2.26.90"' -> '2.26.90'
1201         local upstream_pv=$(sed -n -r 's/#define VERSION "(.*)"/\1/p' "${S}"/version.h)
1202
1203         # Avoid stripping binaries not targeted by ${CHOST}. Or else
1204         # ${CHOST}-strip would break binaries build for ${CTARGET}.
1205         is_crosscompile && dostrip -x /
1206         # gdb thread introspection relies on local libpthreas symbols. stripping breaks it
1207         # See Note [Disable automatic stripping]
1208         dostrip -x $(alt_libdir)/libpthread-${upstream_pv}.so
1209
1210         if [[ -e ${ED}/$(alt_usrlibdir)/libm-${upstream_pv}.a ]] ; then
1211                 # Move versioned .a file out of libdir to evade portage QA checks
1212                 # instead of using gen_usr_ldscript(). We fix ldscript as:
1213                 # "GROUP ( /usr/lib64/libm-<pv>.a ..." -> "GROUP ( /usr/lib64/glibc-<pv>/libm-<pv>.a ..."
1214                 sed -i "s@\(libm-${upstream_pv}.a\)@${P}/\1@" "${ED}"/$(alt_usrlibdir)/libm.a || die
1215                 dodir $(alt_usrlibdir)/${P}
1216                 mv "${ED}"/$(alt_usrlibdir)/libm-${upstream_pv}.a "${ED}"/$(alt_usrlibdir)/${P}/libm-${upstream_pv}.a || die
1217         fi
1218
1219         # We'll take care of the cache ourselves
1220         rm -f "${ED}"/etc/ld.so.cache
1221
1222         # Everything past this point just needs to be done once ...
1223         is_final_abi || return 0
1224
1225         # Make sure the non-native interp can be found on multilib systems even
1226         # if the main library set isn't installed into the right place.  Maybe
1227         # we should query the active gcc for info instead of hardcoding it ?
1228         local i ldso_abi ldso_name
1229         local ldso_abi_list=(
1230                 # x86
1231                 amd64   /lib64/ld-linux-x86-64.so.2
1232                 x32     /libx32/ld-linux-x32.so.2
1233                 x86     /lib/ld-linux.so.2
1234                 # mips
1235                 o32     /lib/ld.so.1
1236                 n32     /lib32/ld.so.1
1237                 n64     /lib64/ld.so.1
1238                 # powerpc
1239                 ppc     /lib/ld.so.1
1240                 ppc64   /lib64/ld64.so.1
1241                 # riscv
1242                 lp64d   /lib/ld-linux-riscv64-lp64d.so.1
1243                 lp64    /lib/ld-linux-riscv64-lp64.so.1
1244                 # s390
1245                 s390    /lib/ld.so.1
1246                 s390x   /lib/ld64.so.1
1247                 # sparc
1248                 sparc32 /lib/ld-linux.so.2
1249                 sparc64 /lib64/ld-linux.so.2
1250         )
1251         case $(tc-endian) in
1252         little)
1253                 ldso_abi_list+=(
1254                         # arm
1255                         arm64   /lib/ld-linux-aarch64.so.1
1256                 )
1257                 ;;
1258         big)
1259                 ldso_abi_list+=(
1260                         # arm
1261                         arm64   /lib/ld-linux-aarch64_be.so.1
1262                 )
1263                 ;;
1264         esac
1265         if [[ ${SYMLINK_LIB} == "yes" ]] && [[ ! -e ${ED}/$(alt_prefix)/lib ]] ; then
1266                 dosym $(get_abi_LIBDIR ${DEFAULT_ABI}) $(alt_prefix)/lib
1267         fi
1268         for (( i = 0; i < ${#ldso_abi_list[@]}; i += 2 )) ; do
1269                 ldso_abi=${ldso_abi_list[i]}
1270                 has ${ldso_abi} $(get_install_abis) || continue
1271
1272                 ldso_name="$(alt_prefix)${ldso_abi_list[i+1]}"
1273                 if [[ ! -L ${ED}/${ldso_name} && ! -e ${ED}/${ldso_name} ]] ; then
1274                         dosym ../$(get_abi_LIBDIR ${ldso_abi})/${ldso_name##*/} ${ldso_name}
1275                 fi
1276         done
1277
1278         # With devpts under Linux mounted properly, we do not need the pt_chown
1279         # binary to be setuid.  This is because the default owners/perms will be
1280         # exactly what we want.
1281         if ! use suid ; then
1282                 find "${ED}" -name pt_chown -exec chmod -s {} +
1283         fi
1284
1285         #################################################################
1286         # EVERYTHING AFTER THIS POINT IS FOR NATIVE GLIBC INSTALLS ONLY #
1287         # Make sure we install some symlink hacks so that when we build
1288         # a 2nd stage cross-compiler, gcc finds the target system
1289         # headers correctly.  See gcc/doc/gccinstall.info
1290         if is_crosscompile ; then
1291                 # We need to make sure that /lib and /usr/lib always exists.
1292                 # gcc likes to use relative paths to get to its multilibs like
1293                 # /usr/lib/../lib64/.  So while we don't install any files into
1294                 # /usr/lib/, we do need it to exist.
1295                 keepdir $(alt_prefix)/lib
1296                 keepdir $(alt_prefix)/usr/lib
1297
1298                 dosym usr/include $(alt_prefix)/sys-include
1299                 return 0
1300         fi
1301
1302         # Files for Debian-style locale updating
1303         dodir /usr/share/i18n
1304         sed \
1305                 -e "/^#/d" \
1306                 -e "/SUPPORTED-LOCALES=/d" \
1307                 -e "s: \\\\::g" -e "s:/: :g" \
1308                 "${S}"/localedata/SUPPORTED > "${ED}"/usr/share/i18n/SUPPORTED \
1309                 || die "generating /usr/share/i18n/SUPPORTED failed"
1310         cd "${WORKDIR}"/extra/locale
1311         dosbin locale-gen
1312         doman *.[0-8]
1313         insinto /etc
1314         doins locale.gen
1315
1316         # Make sure all the ABI's can find the locales and so we only
1317         # have to generate one set
1318         local a
1319         keepdir /usr/$(get_libdir)/locale
1320         for a in $(get_install_abis) ; do
1321                 if [[ ! -e ${ED}/usr/$(get_abi_LIBDIR ${a})/locale ]] ; then
1322                         dosym ../$(get_libdir)/locale /usr/$(get_abi_LIBDIR ${a})/locale
1323                 fi
1324         done
1325
1326         # HACK: If we're building for riscv, we need to additionally make sure that
1327         # we can find the locale archive afterwards
1328         case ${CTARGET} in
1329                 riscv*)
1330                         if [[ ! -e ${ED}/usr/lib/locale ]] ; then
1331                                 dosym ../$(get_libdir)/locale /usr/lib/locale
1332                         fi
1333                         ;;
1334                 *) ;;
1335         esac
1336
1337         cd "${S}"
1338
1339         # Install misc network config files
1340         insinto /etc
1341         doins nscd/nscd.conf posix/gai.conf nss/nsswitch.conf
1342         doins "${WORKDIR}"/extra/etc/*.conf
1343
1344         if use nscd ; then
1345                 doinitd "$(prefixify_ro "${WORKDIR}"/extra/etc/nscd)"
1346
1347                 local nscd_args=(
1348                         -e "s:@PIDFILE@:$(strings "${ED}"/usr/sbin/nscd | grep nscd.pid):"
1349                 )
1350
1351                 sed -i "${nscd_args[@]}" "${ED}"/etc/init.d/nscd
1352
1353                 systemd_dounit nscd/nscd.service
1354                 systemd_newtmpfilesd nscd/nscd.tmpfiles nscd.conf
1355         else
1356                 # Do this since extra/etc/*.conf above might have nscd.conf.
1357                 rm -f "${ED}"/etc/nscd.conf
1358         fi
1359
1360         echo 'LDPATH="include ld.so.conf.d/*.conf"' > "${T}"/00glibc
1361         doenvd "${T}"/00glibc
1362
1363         for d in BUGS ChangeLog CONFORMANCE FAQ NEWS NOTES PROJECTS README* ; do
1364                 [[ -s ${d} ]] && dodoc ${d}
1365         done
1366         dodoc -r ChangeLog.old
1367
1368         # Prevent overwriting of the /etc/localtime symlink.  We'll handle the
1369         # creation of the "factory" symlink in pkg_postinst().
1370         rm -f "${ED}"/etc/localtime
1371
1372         # Generate all locales if this is a native build as locale generation
1373         if use compile-locales && ! is_crosscompile ; then
1374                 run_locale_gen --inplace-glibc "${ED}/"
1375                 sed -e 's:COMPILED_LOCALES="":COMPILED_LOCALES="1":' -i "${ED}"/usr/sbin/locale-gen || die
1376         fi
1377 }
1378
1379 glibc_headers_install() {
1380         local builddir=$(builddir "headers")
1381         cd "${builddir}"
1382         emake install_root="${D}/$(build_eprefix)$(alt_prefix)" install-headers
1383
1384         insinto $(alt_headers)/gnu
1385         doins "${S}"/include/gnu/stubs.h
1386
1387         # Make sure we install the sys-include symlink so that when
1388         # we build a 2nd stage cross-compiler, gcc finds the target
1389         # system headers correctly.  See gcc/doc/gccinstall.info
1390         dosym usr/include $(alt_prefix)/sys-include
1391 }
1392
1393 src_install() {
1394         if just_headers ; then
1395                 export ABI=default
1396                 glibc_headers_install
1397                 return
1398         fi
1399
1400         foreach_abi glibc_do_src_install
1401
1402         if ! use static-libs ; then
1403                 elog "Not installing static glibc libraries"
1404                 find "${ED}" -name "*.a" -and -not -name "*_nonshared.a" -delete
1405         fi
1406 }
1407
1408 # Simple test to make sure our new glibc isn't completely broken.
1409 # Make sure we don't test with statically built binaries since
1410 # they will fail.  Also, skip if this glibc is a cross compiler.
1411 #
1412 # If coreutils is built with USE=multicall, some of these files
1413 # will just be wrapper scripts, not actual ELFs we can test.
1414 glibc_sanity_check() {
1415         cd / #228809
1416
1417         # We enter ${ED} so to avoid trouble if the path contains
1418         # special characters; for instance if the path contains the
1419         # colon character (:), then the linker will try to split it
1420         # and look for the libraries in an unexpected place. This can
1421         # lead to unsafe code execution if the generated prefix is
1422         # within a world-writable directory.
1423         # (e.g. /var/tmp/portage:${HOSTNAME})
1424         pushd "${ED}"/$(get_libdir) >/dev/null
1425
1426         local x striptest
1427         for x in cal date env free ls true uname uptime ; do
1428                 x=$(type -p ${x})
1429                 [[ -z ${x} || ${x} != ${EPREFIX}/* ]] && continue
1430                 striptest=$(LC_ALL="C" file -L ${x} 2>/dev/null) || continue
1431                 case ${striptest} in
1432                 *"statically linked"*) continue;;
1433                 *"ASCII text"*) continue;;
1434                 esac
1435                 # We need to clear the locale settings as the upgrade might want
1436                 # incompatible locale data.  This test is not for verifying that.
1437                 LC_ALL=C \
1438                 ./ld-*.so --library-path . ${x} > /dev/null \
1439                         || die "simple run test (${x}) failed"
1440         done
1441
1442         popd >/dev/null
1443 }
1444
1445 pkg_preinst() {
1446         # nothing to do if just installing headers
1447         just_headers && return
1448
1449         # prepare /etc/ld.so.conf.d/ for files
1450         mkdir -p "${EROOT}"/etc/ld.so.conf.d
1451
1452         # Default /etc/hosts.conf:multi to on for systems with small dbs.
1453         if [[ $(wc -l < "${EROOT}"/etc/hosts) -lt 1000 ]] ; then
1454                 sed -i '/^multi off/s:off:on:' "${ED}"/etc/host.conf
1455                 einfo "Defaulting /etc/host.conf:multi to on"
1456         fi
1457
1458         [[ -n ${ROOT} ]] && return 0
1459         [[ -d ${ED}/$(get_libdir) ]] || return 0
1460         [[ -z ${BOOTSTRAP_RAP} ]] && glibc_sanity_check
1461 }
1462
1463 pkg_postinst() {
1464         # nothing to do if just installing headers
1465         just_headers && return
1466
1467         if ! tc-is-cross-compiler && [[ -x ${EROOT}/usr/sbin/iconvconfig ]] ; then
1468                 # Generate fastloading iconv module configuration file.
1469                 "${EROOT}"/usr/sbin/iconvconfig --prefix="${ROOT}/"
1470         fi
1471
1472         if ! is_crosscompile && [[ -z ${ROOT} ]] ; then
1473                 use compile-locales || run_locale_gen "${EROOT}/"
1474         fi
1475
1476         # Check for sanity of /etc/nsswitch.conf, take 2
1477         if [[ -e ${EROOT}/etc/nsswitch.conf ]] && ! has_version sys-auth/libnss-nis ; then
1478                 local entry
1479                 for entry in passwd group shadow; do
1480                         if egrep -q "^[ \t]*${entry}:.*nis" "${EROOT}"/etc/nsswitch.conf; then
1481                                 ewarn ""
1482                                 ewarn "Your ${EROOT}/etc/nsswitch.conf uses NIS. Support for that has been"
1483                                 ewarn "removed from glibc and is now provided by the package"
1484                                 ewarn "  sys-auth/libnss-nis"
1485                                 ewarn "Install it now to keep your NIS setup working."
1486                                 ewarn ""
1487                         fi
1488                 done
1489         fi
1490 }