d93751d9a5f61854d15032d0152faa2af1792689
[gentoo.git] / sys-libs / glibc / glibc-2.26-r7.ebuild
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 EAPI=6
5
6 inherit prefix toolchain-funcs toolchain-glibc
7
8 DESCRIPTION="GNU libc C library"
9 HOMEPAGE="https://www.gnu.org/software/libc/"
10
11 LICENSE="LGPL-2.1+ BSD HPND ISC inner-net rc PCRE"
12 EMULTILIB_PKG="true"
13
14 # Configuration variables
15
16 if [[ ${PV} == 9999* ]]; then
17         EGIT_REPO_URI="https://sourceware.org/git/glibc.git"
18         inherit git-r3
19 else
20         KEYWORDS="~alpha amd64 arm arm64 hppa ~ia64 m68k ~mips ppc ppc64 s390 sparc x86"
21         SRC_URI="mirror://gnu/glibc/${P}.tar.xz"
22 fi
23
24 RELEASE_VER=${PV}
25
26 GCC_BOOTSTRAP_VER=20180511
27
28 # Gentoo patchset
29 PATCH_VER=7
30
31 SRC_URI+=" https://dev.gentoo.org/~dilfridge/distfiles/${P}-patches-${PATCH_VER}.tar.bz2"
32 SRC_URI+=" multilib? ( https://dev.gentoo.org/~dilfridge/distfiles/gcc-multilib-bootstrap-${GCC_BOOTSTRAP_VER}.tar.xz )"
33
34 IUSE="audit caps debug doc gd hardened multilib nscd selinux systemtap profile suid vanilla headers-only"
35
36 # Min kernel version glibc requires
37 : ${NPTL_KERN_VER:="3.2.0"}
38
39 # Here's how the cross-compile logic breaks down ...
40 #  CTARGET - machine that will target the binaries
41 #  CHOST   - machine that will host the binaries
42 #  CBUILD  - machine that will build the binaries
43 # If CTARGET != CHOST, it means you want a libc for cross-compiling.
44 # If CHOST != CBUILD, it means you want to cross-compile the libc.
45 #  CBUILD = CHOST = CTARGET    - native build/install
46 #  CBUILD != (CHOST = CTARGET) - cross-compile a native build
47 #  (CBUILD = CHOST) != CTARGET - libc for cross-compiler
48 #  CBUILD != CHOST != CTARGET  - cross-compile a libc for a cross-compiler
49 # For install paths:
50 #  CHOST = CTARGET  - install into /
51 #  CHOST != CTARGET - install into /usr/CTARGET/
52
53 export CBUILD=${CBUILD:-${CHOST}}
54 export CTARGET=${CTARGET:-${CHOST}}
55 if [[ ${CTARGET} == ${CHOST} ]] ; then
56         if [[ ${CATEGORY} == cross-* ]] ; then
57                 export CTARGET=${CATEGORY#cross-}
58         fi
59 fi
60
61 # Note [Disable automatic stripping]
62 # Disabling automatic stripping for a few reasons:
63 # - portage's attempt to strip breaks non-native binaries at least on
64 #   arm: bug #697428
65 # - portage's attempt to strip libpthread.so.0 breaks gdb thread
66 #   enumeration: bug #697910. This is quite subtle:
67 #   * gdb uses glibc's libthread_db-1.0.so to enumerate threads.
68 #   * libthread_db-1.0.so needs access to libpthread.so.0 local symbols
69 #     via 'ps_pglobal_lookup' symbol defined in gdb.
70 #   * 'ps_pglobal_lookup' uses '.symtab' section table to resolve all
71 #     known symbols in 'libpthread.so.0'. Specifically 'nptl_version'
72 #     (unexported) is used to sanity check compatibility before enabling
73 #     debugging.
74 #     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
75 #   * normal 'strip' command trims '.symtab'
76 #   Thus our main goal here is to prevent 'libpthread.so.0' from
77 #   losing it's '.symtab' entries.
78 # As Gentoo's strip does not allow us to pass less aggressive stripping
79 # options and does not check the machine target we disable stripping
80 # entirely.
81 RESTRICT=strip
82
83 is_crosscompile() {
84         [[ ${CHOST} != ${CTARGET} ]]
85 }
86
87 SLOT="2.2"
88
89 # General: We need a new-enough binutils/gcc to match upstream baseline.
90 # arch: we need to make sure our binutils/gcc supports TLS.
91 COMMON_DEPEND="
92         nscd? ( selinux? (
93                 audit? ( sys-process/audit )
94                 caps? ( sys-libs/libcap )
95         ) )
96         suid? ( caps? ( sys-libs/libcap ) )
97         selinux? ( sys-libs/libselinux )
98         systemtap? ( dev-util/systemtap )
99 "
100 DEPEND="${COMMON_DEPEND}
101         >=app-misc/pax-utils-0.1.10
102         doc? ( sys-apps/texinfo )
103 "
104 RDEPEND="${COMMON_DEPEND}
105         sys-apps/gentoo-functions
106 "
107
108 if [[ ${CATEGORY} == cross-* ]] ; then
109         DEPEND+=" !headers-only? (
110                 >=${CATEGORY}/binutils-2.24
111                 >=${CATEGORY}/gcc-4.9
112         )"
113         [[ ${CATEGORY} == *-linux* ]] && DEPEND+=" ${CATEGORY}/linux-headers"
114 else
115         DEPEND+="
116                 >=sys-devel/binutils-2.24
117                 >=sys-devel/gcc-4.9
118                 virtual/os-headers
119         "
120         RDEPEND+=" vanilla? ( !sys-libs/timezone-data )"
121         PDEPEND+=" !vanilla? ( sys-libs/timezone-data )"
122 fi
123
124 #
125 # the phases
126 #
127
128 pkg_pretend() {
129         # Make sure devpts is mounted correctly for use w/out setuid pt_chown
130         check_devpts
131
132         # Prevent native builds from downgrading
133         if [[ ${MERGE_TYPE} != "buildonly" ]] && \
134            [[ ${ROOT} == "/" ]] && \
135            [[ ${CBUILD} == ${CHOST} ]] && \
136            [[ ${CHOST} == ${CTARGET} ]] ; then
137                 # The high rev # is to allow people to downgrade between -r# versions.
138                 # We want to block 2.20->2.19, but 2.20-r3->2.20-r2 should be fine.
139                 # Hopefully we never actually use a r# this high.
140                 if has_version ">${CATEGORY}/${P}-r10000" ; then
141                         eerror "Sanity check to keep you from breaking your system:"
142                         eerror " Downgrading glibc is not supported and a sure way to destruction"
143                         [[ ${I_ALLOW_TO_BREAK_MY_SYSTEM} = yes ]] || die "Aborting to save your system."
144                 fi
145
146                 if ! glibc_run_test '#include <pwd.h>\nint main(){return getpwuid(0)==0;}\n'
147                 then
148                         eerror "Your patched vendor kernel is broken.  You need to get an"
149                         eerror "update from whoever is providing the kernel to you."
150                         eerror "https://sourceware.org/bugzilla/show_bug.cgi?id=5227"
151                         eerror "https://bugs.gentoo.org/262698"
152                         die "Keeping your system alive, say thank you"
153                 fi
154
155                 if ! glibc_run_test '#include <unistd.h>\n#include <sys/syscall.h>\nint main(){return syscall(1000)!=-1;}\n'
156                 then
157                         eerror "Your old kernel is broken.  You need to update it to"
158                         eerror "a newer version as syscall(<bignum>) will break."
159                         eerror "https://bugs.gentoo.org/279260"
160                         die "Keeping your system alive, say thank you"
161                 fi
162         fi
163
164         # Users have had a chance to phase themselves, time to give em the boot
165         if [[ -e ${EROOT}/etc/locale.gen ]] && [[ -e ${EROOT}/etc/locales.build ]] ; then
166                 eerror "You still haven't deleted ${EROOT}/etc/locales.build."
167                 eerror "Do so now after making sure ${EROOT}/etc/locale.gen is kosher."
168                 die "Lazy upgrader detected"
169         fi
170
171         if [[ ${CTARGET} == i386-* ]] ; then
172                 eerror "i386 CHOSTs are no longer supported."
173                 eerror "Chances are you don't actually want/need i386."
174                 eerror "Please read https://www.gentoo.org/doc/en/change-chost.xml"
175                 die "Please fix your CHOST"
176         fi
177
178         if [[ -e /proc/xen ]] && [[ $(tc-arch) == "x86" ]] && ! is-flag -mno-tls-direct-seg-refs ; then
179                 ewarn "You are using Xen but don't have -mno-tls-direct-seg-refs in your CFLAGS."
180                 ewarn "This will result in a 50% performance penalty when running with a 32bit"
181                 ewarn "hypervisor, which is probably not what you want."
182         fi
183
184         # Make sure host system is up to date #394453
185         if has_version '<sys-libs/glibc-2.13' && \
186            [[ -n $(scanelf -qys__guard -F'#s%F' "${EROOT}"/lib*/l*-*.so) ]]
187         then
188                 ebegin "Scanning system for __guard to see if you need to rebuild first ..."
189                 local files=$(
190                         scanelf -qys__guard -F'#s%F' \
191                                 "${EROOT}"/*bin/ \
192                                 "${EROOT}"/lib* \
193                                 "${EROOT}"/usr/*bin/ \
194                                 "${EROOT}"/usr/lib* | \
195                                 egrep -v \
196                                         -e "^${EROOT}/lib.*/(libc|ld)-2.*.so$" \
197                                         -e "^${EROOT}/sbin/(ldconfig|sln)$"
198                 )
199                 [[ -z ${files} ]]
200                 if ! eend $? ; then
201                         eerror "Your system still has old SSP __guard symbols.  You need to"
202                         eerror "rebuild all the packages that provide these files first:"
203                         eerror "${files}"
204                         die "old __guard detected"
205                 fi
206         fi
207
208         # Check for sanity of /etc/nsswitch.conf
209         if [[ -e ${EROOT}/etc/nsswitch.conf ]] ; then
210                 local entry
211                 for entry in passwd group shadow; do
212                         if ! egrep -q "^[ \t]*${entry}:.*files" "${EROOT}"/etc/nsswitch.conf; then
213                                 eerror "Your ${EROOT}/etc/nsswitch.conf is out of date."
214                                 eerror "Please make sure you have 'files' entries for"
215                                 eerror "'passwd:', 'group:' and 'shadow:' databases."
216                                 eerror "For more details see:"
217                                 eerror "  https://wiki.gentoo.org/wiki/Project:Toolchain/nsswitch.conf_in_glibc-2.26"
218                                 die "nsswitch.conf has no 'files' provider in '${entry}'."
219                         fi
220                 done
221         fi
222 }
223
224 src_unpack() {
225         use multilib && unpack gcc-multilib-bootstrap-${GCC_BOOTSTRAP_VER}.tar.xz
226
227         setup_env
228
229         # Check NPTL support _before_ we unpack things to save some time
230         check_nptl_support
231
232         if [[ -n ${EGIT_REPO_URI} ]] ; then
233                 git-r3_src_unpack
234         else
235                 unpack ${P}.tar.xz
236         fi
237
238         cd "${S}"
239         touch locale/C-translit.h #185476 #218003
240
241         cd "${WORKDIR}"
242         unpack glibc-${RELEASE_VER}-patches-${PATCH_VER}.tar.bz2
243 }
244
245 src_prepare() {
246         if ! use vanilla ; then
247                 elog "Applying Gentoo Glibc Patchset ${RELEASE_VER}-${PATCH_VER}"
248                 eapply "${WORKDIR}"/patches
249                 einfo "Done."
250         fi
251
252         if just_headers ; then
253                 if [[ -e ports/sysdeps/mips/preconfigure ]] ; then
254                         # mips peeps like to screw with us.  if building headers,
255                         # we don't have a real compiler, so we can't let them
256                         # insert -mabi on us.
257                         sed -i '/CPPFLAGS=.*-mabi/s|.*|:|' ports/sysdeps/mips/preconfigure || die
258                         find ports/sysdeps/mips/ -name Makefile -exec sed -i '/^CC.*-mabi=/s:-mabi=.*:-D_MIPS_SZPTR=32:' {} +
259                 fi
260         fi
261
262         default
263
264         gnuconfig_update
265
266         cd "${WORKDIR}"
267         find . -name configure -exec touch {} +
268
269         eprefixify extra/locale/locale-gen
270
271         # Fix permissions on some of the scripts.
272         chmod u+x "${S}"/scripts/*.sh
273
274         cd "${S}"
275
276         if use hardened ; then
277                 # We don't enable these for non-hardened as the output is very terse --
278                 # it only states that a crash happened.  The default upstream behavior
279                 # includes backtraces and symbols.
280                 einfo "Installing Hardened Gentoo SSP and FORTIFY_SOURCE handler"
281                 cp "${FILESDIR}"/2.20/glibc-2.20-gentoo-stack_chk_fail.c debug/stack_chk_fail.c || die
282                 cp "${FILESDIR}"/2.25/glibc-2.25-gentoo-chk_fail.c debug/chk_fail.c || die
283
284                 if use debug ; then
285                         # Allow SIGABRT to dump core on non-hardened systems, or when debug is requested.
286                         sed -i \
287                                 -e '/^CFLAGS-backtrace.c/ iCPPFLAGS-stack_chk_fail.c = -DSSP_SMASH_DUMPS_CORE' \
288                                 -e '/^CFLAGS-backtrace.c/ iCPPFLAGS-chk_fail.c = -DSSP_SMASH_DUMPS_CORE' \
289                                 debug/Makefile || die
290                 fi
291         fi
292 }
293
294 glibc_do_configure() {
295         # Glibc does not work with gold (for various reasons) #269274.
296         tc-ld-disable-gold
297
298         # CXX isnt handled by the multilib system, so if we dont unset here
299         # we accumulate crap across abis
300         unset CXX
301
302         einfo "Configuring glibc for $1"
303
304         if use doc ; then
305                 export MAKEINFO=makeinfo
306         else
307                 export MAKEINFO=/dev/null
308         fi
309
310         local v
311         for v in ABI CBUILD CHOST CTARGET CBUILD_OPT CTARGET_OPT CC CXX LD {AS,C,CPP,CXX,LD}FLAGS MAKEINFO ; do
312                 einfo " $(printf '%15s' ${v}:)   ${!v}"
313         done
314
315         # The glibc configure script doesn't properly use LDFLAGS all the time.
316         export CC="$(tc-getCC ${CTARGET}) ${LDFLAGS}"
317         einfo " $(printf '%15s' 'Manual CC:')   ${CC}"
318
319         # Some of the tests are written in C++, so we need to force our multlib abis in, bug 623548
320         export CXX="$(tc-getCXX ${CTARGET}) $(get_abi_CFLAGS)"
321         einfo " $(printf '%15s' 'Manual CXX:')   ${CXX}"
322
323         echo
324
325         local myconf=()
326
327         # set addons
328         pushd "${S}" > /dev/null
329         local addons=$(echo */configure | sed \
330                 -e 's:/configure::g' \
331                 -e 's:\(linuxthreads\|nptl\|rtkaio\|glibc-compat\)\( \|$\)::g' \
332                 -e 's: \+$::' \
333                 -e 's! !,!g' \
334                 -e 's!^!,!' \
335                 -e '/^,\*$/d')
336         [[ -d ports ]] && addons+=",ports"
337         popd > /dev/null
338
339         case ${CTARGET} in
340                 powerpc-*)
341                         # Currently gcc on powerpc32 generates invalid code for
342                         # __builtin_return_address(0) calls. Normally programs
343                         # don't do that but malloc hooks in glibc do:
344                         # https://gcc.gnu.org/PR81996
345                         # https://bugs.gentoo.org/629054
346                         myconf+=( --enable-stack-protector=no )
347                         ;;
348                 *)
349                         myconf+=( --enable-stack-protector=all )
350                         ;;
351         esac
352         myconf+=( --enable-stackguard-randomization )
353
354         # Keep a whitelist of targets supporing IFUNC. glibc's ./configure
355         # is not robust enough to detect proper support:
356         #    https://bugs.gentoo.org/641216
357         #    https://sourceware.org/PR22634#c0
358         case $(tc-arch ${CTARGET}) in
359                 # Keep whitelist of targets where autodetection mostly works.
360                 amd64|x86|sparc|ppc|ppc64|arm|arm64|s390) ;;
361                 # Blacklist everywhere else
362                 *) myconf+=( libc_cv_ld_gnu_indirect_function=no ) ;;
363         esac
364
365         [[ $(tc-is-softfloat) == "yes" ]] && myconf+=( --without-fp )
366
367         if [[ $1 == "nptl" ]] ; then
368                 myconf+=( --enable-kernel=${NPTL_KERN_VER} )
369         else
370                 die "invalid pthread option"
371         fi
372         myconf+=( --enable-add-ons="${addons#,}" )
373
374         # Since SELinux support is only required for nscd, only enable it if:
375         # 1. USE selinux
376         # 2. only for the primary ABI on multilib systems
377         # 3. Not a crosscompile
378         if ! is_crosscompile && use selinux ; then
379                 if use multilib ; then
380                         if is_final_abi ; then
381                                 myconf+=( --with-selinux )
382                         else
383                                 myconf+=( --without-selinux )
384                         fi
385                 else
386                         myconf+=( --with-selinux )
387                 fi
388         else
389                 myconf+=( --without-selinux )
390         fi
391
392         # Force a few tests where we always know the answer but
393         # configure is incapable of finding it.
394         if is_crosscompile ; then
395                 export \
396                         libc_cv_c_cleanup=yes \
397                         libc_cv_forced_unwind=yes
398         fi
399
400         myconf+=(
401                 --without-cvs
402                 --disable-werror
403                 --enable-bind-now
404                 --build=${CBUILD_OPT:-${CBUILD}}
405                 --host=${CTARGET_OPT:-${CTARGET}}
406                 $(use_enable profile)
407                 $(use_with gd)
408                 --with-headers=$(alt_build_headers)
409                 --prefix="${EPREFIX}/usr"
410                 --sysconfdir="${EPREFIX}/etc"
411                 --localstatedir="${EPREFIX}/var"
412                 --libdir='$(prefix)'/$(get_libdir)
413                 --mandir='$(prefix)'/share/man
414                 --infodir='$(prefix)'/share/info
415                 --libexecdir='$(libdir)'/misc/glibc
416                 --with-bugurl=https://bugs.gentoo.org/
417                 --with-pkgversion="$(glibc_banner)"
418                 $(use_multiarch || echo --disable-multi-arch)
419                 $(in_iuse systemtap && use_enable systemtap)
420                 $(in_iuse nscd && use_enable nscd)
421                 ${EXTRA_ECONF}
422         )
423
424         # We rely on sys-libs/timezone-data for timezone tools normally.
425         myconf+=( $(use_enable vanilla timezone-tools) )
426
427         # These libs don't have configure flags.
428         ac_cv_lib_audit_audit_log_user_avc_message=$(in_iuse audit && usex audit || echo no)
429         ac_cv_lib_cap_cap_init=$(in_iuse caps && usex caps || echo no)
430
431         # There is no configure option for this and we need to export it
432         # since the glibc build will re-run configure on itself
433         export libc_cv_rootsbindir="${EPREFIX}/sbin"
434         export libc_cv_slibdir="${EPREFIX}/$(get_libdir)"
435
436         # We take care of patching our binutils to use both hash styles,
437         # and many people like to force gnu hash style only, so disable
438         # this overriding check.  #347761
439         export libc_cv_hashstyle=no
440
441         local builddir=$(builddir "$1")
442         mkdir -p "${builddir}"
443         cd "${builddir}"
444         set -- "${S}"/configure "${myconf[@]}"
445         echo "$@"
446         "$@" || die "failed to configure glibc"
447
448         # ia64 static cross-compilers are a pita in so much that they
449         # can't produce static ELFs (as the libgcc.a is broken).  so
450         # disable building of the programs for those targets if it
451         # doesn't work.
452         # XXX: We could turn this into a compiler test, but ia64 is
453         # the only one that matters, so this should be fine for now.
454         if is_crosscompile && [[ ${CTARGET} == ia64* ]] ; then
455                 sed -i '1i+link-static = touch $@' config.make
456         fi
457
458         # If we're trying to migrate between ABI sets, we need
459         # to lie and use a local copy of gcc.  Like if the system
460         # is built with MULTILIB_ABIS="amd64 x86" but we want to
461         # add x32 to it, gcc/glibc don't yet support x32.
462         if [[ -n ${GCC_BOOTSTRAP_VER} ]] && use multilib ; then
463                 echo 'main(){}' > "${T}"/test.c
464                 if ! $(tc-getCC ${CTARGET}) ${CFLAGS} ${LDFLAGS} "${T}"/test.c -Wl,-emain -lgcc 2>/dev/null ; then
465                         sed -i -e '/^CC = /s:$: -B$(objdir)/../'"gcc-multilib-bootstrap-${GCC_BOOTSTRAP_VER}/${ABI}:" config.make || die
466                 fi
467         fi
468 }
469
470 glibc_headers_configure() {
471         export ABI=default
472
473         local builddir=$(builddir "headers")
474         mkdir -p "${builddir}"
475         cd "${builddir}"
476
477         # if we don't have a compiler yet, we can't really test it now ...
478         # hopefully they don't affect header generation, so let's hope for
479         # the best here ...
480         local v vars=(
481                 ac_cv_header_cpuid_h=yes
482                 libc_cv_{386,390,alpha,arm,hppa,ia64,mips,{powerpc,sparc}{,32,64},sh,x86_64}_tls=yes
483                 libc_cv_asm_cfi_directives=yes
484                 libc_cv_broken_visibility_attribute=no
485                 libc_cv_c_cleanup=yes
486                 libc_cv_forced_unwind=yes
487                 libc_cv_gcc___thread=yes
488                 libc_cv_mlong_double_128=yes
489                 libc_cv_mlong_double_128ibm=yes
490                 libc_cv_ppc_machine=yes
491                 libc_cv_ppc_rel16=yes
492                 libc_cv_predef_fortify_source=no
493                 libc_cv_visibility_attribute=yes
494                 libc_cv_z_combreloc=yes
495                 libc_cv_z_execstack=yes
496                 libc_cv_z_initfirst=yes
497                 libc_cv_z_nodelete=yes
498                 libc_cv_z_nodlopen=yes
499                 libc_cv_z_relro=yes
500                 libc_mips_abi=${ABI}
501                 libc_mips_float=$([[ $(tc-is-softfloat) == "yes" ]] && echo soft || echo hard)
502                 # These libs don't have configure flags.
503                 ac_cv_lib_audit_audit_log_user_avc_message=no
504                 ac_cv_lib_cap_cap_init=no
505         )
506
507         einfo "Forcing cached settings:"
508         for v in "${vars[@]}" ; do
509                 einfo " ${v}"
510                 export ${v}
511         done
512
513         # Blow away some random CC settings that screw things up. #550192
514         if [[ -d ${S}/sysdeps/mips ]]; then
515                 pushd "${S}"/sysdeps/mips >/dev/null
516                 sed -i -e '/^CC +=/s:=.*:= -D_MIPS_SZPTR=32:' mips32/Makefile mips64/n32/Makefile || die
517                 sed -i -e '/^CC +=/s:=.*:= -D_MIPS_SZPTR=64:' mips64/n64/Makefile || die
518
519                 # Force the mips ABI to the default.  This is OK because the set of
520                 # installed headers in this phase is the same between the 3 ABIs.
521                 # If this ever changes, this hack will break, but that's unlikely
522                 # as glibc discourages that behavior.
523                 # https://crbug.com/647033
524                 sed -i -e 's:abiflag=.*:abiflag=_ABIO32:' preconfigure || die
525
526                 popd >/dev/null
527         fi
528
529         local myconf=()
530         myconf+=(
531                 --disable-sanity-checks
532                 --enable-hacker-mode
533                 --without-cvs
534                 --disable-werror
535                 --enable-bind-now
536                 --build=${CBUILD_OPT:-${CBUILD}}
537                 --host=${CTARGET_OPT:-${CTARGET}}
538                 --with-headers=$(alt_build_headers)
539                 --prefix="${EPREFIX}/usr"
540                 ${EXTRA_ECONF}
541         )
542
543         local addons
544         [[ -d ${S}/ports ]] && addons+=",ports"
545         myconf+=( --enable-add-ons="${addons#,}" )
546
547         # Nothing is compiled here which would affect the headers for the target.
548         # So forcing CC/CFLAGS is sane.
549         local headers_only_CC=$(tc-getBUILD_CC)
550         local headers_only_CFLAGS="-O1 -pipe"
551         local headers_only_CPPFLAGS="-U_FORTIFY_SOURCE"
552         local headers_only_LDFLAGS=""
553         set -- "${S}"/configure "${myconf[@]}"
554         echo \
555                 "CC=${headers_only_CC}" \
556                 "CFLAGS=${headers_only_CFLAGS}" \
557                 "CPPFLAGS=${headers_only_CPPFLAGS}" \
558                 "LDFLAGS=${headers_only_LDFLAGS}" \
559                 "$@"
560         CC=${headers_only_CC} \
561         CFLAGS=${headers_only_CFLAGS} \
562         CPPFLAGS=${headers_only_CPPFLAGS} \
563         LDFLAGS="" \
564         "$@" || die "failed to configure glibc"
565 }
566
567 do_src_configure() {
568         if just_headers ; then
569                 glibc_headers_configure
570         else
571                 glibc_do_configure nptl
572         fi
573 }
574
575 src_configure() {
576         foreach_abi do_src_configure
577 }
578
579 do_src_compile() {
580         emake -C "$(builddir nptl)"
581 }
582
583 src_compile() {
584         if just_headers ; then
585                 return
586         fi
587
588         foreach_abi do_src_compile
589 }
590
591 glibc_src_test() {
592         cd "$(builddir $1)"
593         emake check
594 }
595
596 do_src_test() {
597         local ret=0
598
599         glibc_src_test nptl
600         : $(( ret |= $? ))
601
602         return ${ret}
603 }
604
605 src_test() {
606         if just_headers ; then
607                 return
608         fi
609         # Give tests more time to complete.
610         export TIMEOUTFACTOR=5
611
612         foreach_abi do_src_test || die "tests failed"
613 }
614
615 glibc_do_src_install() {
616         local builddir=$(builddir nptl)
617         cd "${builddir}"
618
619         emake install_root="${D}$(alt_prefix)" install
620
621         # This version (2.26) provides some compatibility libraries for the NIS/NIS+ support
622         # which come without headers etc. Only needed for binary packages since the
623         # external net-libs/libnsl has increased soversion. Keep only versioned libraries.
624         find "${D}" -name "libnsl.a" -delete
625         find "${D}" -name "libnsl.so" -delete
626
627         # Normally upstream_pv is ${PV}. Live ebuilds are exception, there we need
628         # to infer upstream version:
629         # '#define VERSION "2.26.90"' -> '2.26.90'
630         local upstream_pv=$(sed -n -r 's/#define VERSION "(.*)"/\1/p' "${S}"/version.h)
631
632         if [[ -e ${ED}$(alt_usrlibdir)/libm-${upstream_pv}.a ]] ; then
633                 # Move versioned .a file out of libdir to evade portage QA checks
634                 # instead of using gen_usr_ldscript(). We fix ldscript as:
635                 # "GROUP ( /usr/lib64/libm-<pv>.a ..." -> "GROUP ( /usr/lib64/glibc-<pv>/libm-<pv>.a ..."
636                 sed -i "s@\(libm-${upstream_pv}.a\)@${P}/\1@" "${ED}"$(alt_usrlibdir)/libm.a || die
637                 dodir $(alt_usrlibdir)/${P}
638                 mv "${ED}"$(alt_usrlibdir)/libm-${upstream_pv}.a "${ED}"$(alt_usrlibdir)/${P}/libm-${upstream_pv}.a || die
639         fi
640
641         # We'll take care of the cache ourselves
642         rm -f "${ED}"/etc/ld.so.cache
643
644         # Everything past this point just needs to be done once ...
645         is_final_abi || return 0
646
647         # Make sure the non-native interp can be found on multilib systems even
648         # if the main library set isn't installed into the right place.  Maybe
649         # we should query the active gcc for info instead of hardcoding it ?
650         local i ldso_abi ldso_name
651         local ldso_abi_list=(
652                 # x86
653                 amd64   /lib64/ld-linux-x86-64.so.2
654                 x32     /libx32/ld-linux-x32.so.2
655                 x86     /lib/ld-linux.so.2
656                 # mips
657                 o32     /lib/ld.so.1
658                 n32     /lib32/ld.so.1
659                 n64     /lib64/ld.so.1
660                 # powerpc
661                 ppc     /lib/ld.so.1
662                 ppc64   /lib64/ld64.so.1
663                 # s390
664                 s390    /lib/ld.so.1
665                 s390x   /lib/ld64.so.1
666                 # sparc
667                 sparc32 /lib/ld-linux.so.2
668                 sparc64 /lib64/ld-linux.so.2
669         )
670         case $(tc-endian) in
671         little)
672                 ldso_abi_list+=(
673                         # arm
674                         arm64   /lib/ld-linux-aarch64.so.1
675                 )
676                 ;;
677         big)
678                 ldso_abi_list+=(
679                         # arm
680                         arm64   /lib/ld-linux-aarch64_be.so.1
681                 )
682                 ;;
683         esac
684         if [[ ${SYMLINK_LIB} == "yes" ]] && [[ ! -e ${ED}/$(alt_prefix)/lib ]] ; then
685                 dosym $(get_abi_LIBDIR ${DEFAULT_ABI}) $(alt_prefix)/lib
686         fi
687         for (( i = 0; i < ${#ldso_abi_list[@]}; i += 2 )) ; do
688                 ldso_abi=${ldso_abi_list[i]}
689                 has ${ldso_abi} $(get_install_abis) || continue
690
691                 ldso_name="$(alt_prefix)${ldso_abi_list[i+1]}"
692                 if [[ ! -L ${ED}/${ldso_name} && ! -e ${ED}/${ldso_name} ]] ; then
693                         dosym ../$(get_abi_LIBDIR ${ldso_abi})/${ldso_name##*/} ${ldso_name}
694                 fi
695         done
696
697         # With devpts under Linux mounted properly, we do not need the pt_chown
698         # binary to be setuid.  This is because the default owners/perms will be
699         # exactly what we want.
700         if in_iuse suid && ! use suid ; then
701                 find "${ED}" -name pt_chown -exec chmod -s {} +
702         fi
703
704         #################################################################
705         # EVERYTHING AFTER THIS POINT IS FOR NATIVE GLIBC INSTALLS ONLY #
706         # Make sure we install some symlink hacks so that when we build
707         # a 2nd stage cross-compiler, gcc finds the target system
708         # headers correctly.  See gcc/doc/gccinstall.info
709         if is_crosscompile ; then
710                 # We need to make sure that /lib and /usr/lib always exists.
711                 # gcc likes to use relative paths to get to its multilibs like
712                 # /usr/lib/../lib64/.  So while we don't install any files into
713                 # /usr/lib/, we do need it to exist.
714                 cd "${ED}"$(alt_libdir)/..
715                 [[ -e lib ]] || mkdir lib
716                 cd "${ED}"$(alt_usrlibdir)/..
717                 [[ -e lib ]] || mkdir lib
718
719                 dosym usr/include $(alt_prefix)/sys-include
720                 return 0
721         fi
722
723         # Files for Debian-style locale updating
724         dodir /usr/share/i18n
725         sed \
726                 -e "/^#/d" \
727                 -e "/SUPPORTED-LOCALES=/d" \
728                 -e "s: \\\\::g" -e "s:/: :g" \
729                 "${S}"/localedata/SUPPORTED > "${ED}"/usr/share/i18n/SUPPORTED \
730                 || die "generating /usr/share/i18n/SUPPORTED failed"
731         cd "${WORKDIR}"/extra/locale
732         dosbin locale-gen
733         doman *.[0-8]
734         insinto /etc
735         doins locale.gen
736
737         # Make sure all the ABI's can find the locales and so we only
738         # have to generate one set
739         local a
740         keepdir /usr/$(get_libdir)/locale
741         for a in $(get_install_abis) ; do
742                 if [[ ! -e ${ED}/usr/$(get_abi_LIBDIR ${a})/locale ]] ; then
743                         dosym ../$(get_libdir)/locale /usr/$(get_abi_LIBDIR ${a})/locale
744                 fi
745         done
746
747         cd "${S}"
748
749         # Install misc network config files
750         insinto /etc
751         doins nscd/nscd.conf posix/gai.conf nss/nsswitch.conf
752         doins "${WORKDIR}"/extra/etc/*.conf
753
754         if use nscd ; then
755                 doinitd "$(prefixify_ro "${WORKDIR}"/extra/etc/nscd)"
756
757                 local nscd_args=(
758                         -e "s:@PIDFILE@:$(strings "${ED}"/usr/sbin/nscd | grep nscd.pid):"
759                 )
760
761                 sed -i "${nscd_args[@]}" "${ED}"/etc/init.d/nscd
762
763                 systemd_dounit nscd/nscd.service
764                 systemd_newtmpfilesd nscd/nscd.tmpfiles nscd.conf
765         else
766                 # Do this since extra/etc/*.conf above might have nscd.conf.
767                 rm -f "${ED}"/etc/nscd.conf
768         fi
769
770         echo 'LDPATH="include ld.so.conf.d/*.conf"' > "${T}"/00glibc
771         doenvd "${T}"/00glibc
772
773         for d in BUGS ChangeLog* CONFORMANCE FAQ NEWS NOTES PROJECTS README* ; do
774                 [[ -s ${d} ]] && dodoc ${d}
775         done
776
777         # Prevent overwriting of the /etc/localtime symlink.  We'll handle the
778         # creation of the "factory" symlink in pkg_postinst().
779         rm -f "${ED}"/etc/localtime
780 }
781
782 glibc_headers_install() {
783         local builddir=$(builddir "headers")
784         cd "${builddir}"
785         emake install_root="${D}$(alt_prefix)" install-headers
786
787         insinto $(alt_headers)/gnu
788         doins "${S}"/include/gnu/stubs.h
789
790         # Make sure we install the sys-include symlink so that when
791         # we build a 2nd stage cross-compiler, gcc finds the target
792         # system headers correctly.  See gcc/doc/gccinstall.info
793         dosym usr/include $(alt_prefix)/sys-include
794 }
795
796 src_install() {
797         if just_headers ; then
798                 export ABI=default
799                 glibc_headers_install
800                 return
801         fi
802
803         foreach_abi glibc_do_src_install
804 }
805
806 pkg_preinst() {
807         # nothing to do if just installing headers
808         just_headers && return
809
810         # prepare /etc/ld.so.conf.d/ for files
811         mkdir -p "${EROOT}"/etc/ld.so.conf.d
812
813         # Default /etc/hosts.conf:multi to on for systems with small dbs.
814         if [[ $(wc -l < "${EROOT}"/etc/hosts) -lt 1000 ]] ; then
815                 sed -i '/^multi off/s:off:on:' "${ED}"/etc/host.conf
816                 einfo "Defaulting /etc/host.conf:multi to on"
817         fi
818
819         [[ ${ROOT} != "/" ]] && return 0
820         [[ -d ${ED}/$(get_libdir) ]] || return 0
821         [[ -z ${BOOTSTRAP_RAP} ]] && glibc_sanity_check
822 }
823
824 pkg_postinst() {
825         # nothing to do if just installing headers
826         just_headers && return
827
828         if ! tc-is-cross-compiler && [[ -x ${EROOT}/usr/sbin/iconvconfig ]] ; then
829                 # Generate fastloading iconv module configuration file.
830                 "${EROOT}"/usr/sbin/iconvconfig --prefix="${ROOT}"
831         fi
832
833         if ! is_crosscompile && [[ ${ROOT} == "/" ]] ; then
834                 # if the host locales.gen contains no entries, we'll install everything
835                 local locale_list="${EROOT}etc/locale.gen"
836                 if [[ -z $(locale-gen --list --config "${locale_list}") ]] ; then
837                         ewarn "Generating all locales; edit /etc/locale.gen to save time/space"
838                         locale_list="${EROOT}usr/share/i18n/SUPPORTED"
839                 fi
840                 locale-gen -j $(makeopts_jobs) --config "${locale_list}"
841         fi
842
843         # Check for sanity of /etc/nsswitch.conf, take 2
844         if [[ -e ${EROOT}/etc/nsswitch.conf ]] && ! has_version sys-auth/libnss-nis ; then
845                 local entry
846                 for entry in passwd group shadow; do
847                         if egrep -q "^[ \t]*${entry}:.*nis" "${EROOT}"/etc/nsswitch.conf; then
848                                 ewarn ""
849                                 ewarn "Your ${EROOT}/etc/nsswitch.conf uses NIS. Support for that has been"
850                                 ewarn "removed from glibc and is now provided by the package"
851                                 ewarn "  sys-auth/libnss-nis"
852                                 ewarn "Install it now to keep your NIS setup working."
853                                 ewarn ""
854                         fi
855                 done
856         fi
857 }