1 { lib, stdenv, callPackage
2 , withLinuxHeaders ? true
3 , profilingLibraries ? false
12 "-Wno-error=stringop-truncation"
13 "-Wno-error=missing-attributes"
14 "-Wno-error=array-bounds"
18 (callPackage ./common.nix { inherit stdenv; } {
19 inherit withLinuxHeaders withGd profilingLibraries withLibcrypt;
20 pname = "glibc" + lib.optionalString withGd "-gd" + lib.optionalString (stdenv.cc.isGNU && libgcc==null) "-nolibgcc";
21 }).overrideAttrs(previousAttrs: {
24 # Things you write here override, and do not add to,
25 # the values in `common.nix`.
26 # (For example, if you define `patches = [...]` here, it will
27 # override the patches in `common.nix` -- so instead you should
28 # write `patches = (previousAttrs.patches or []) ++ [ ... ]`.
30 NIX_NO_SELF_RPATH = true;
33 # Hack: get rid of the `-static' flag set by the bootstrap stdenv.
34 # This has to be done *after* `configure' because it builds some
36 export NIX_CFLAGS_LINK=
37 export NIX_LDFLAGS_BEFORE=
39 export NIX_DONT_SET_RPATH=1
42 # Apparently --bindir is not respected.
43 makeFlagsArray+=("bindir=$bin/bin" "sbindir=$bin/sbin" "rootsbindir=$bin/sbin")
44 '' + lib.optionalString stdenv.buildPlatform.isDarwin ''
45 # ld-wrapper will otherwise attempt to inject CoreFoundation into ld-linux's RUNPATH
46 export NIX_COREFOUNDATION_RPATH=
49 # The pie, stackprotector and fortify hardening flags are autodetected by
50 # glibc and enabled by default if supported. Setting it for every gcc
51 # invocation does not work.
52 hardeningDisable = [ "fortify" "pie" "stackprotector" ];
54 env = (previousAttrs.env or { }) // {
55 NIX_CFLAGS_COMPILE = (previousAttrs.env.NIX_CFLAGS_COMPILE or "") + lib.concatStringsSep " "
56 (builtins.concatLists [
57 (lib.optionals withGd gdCflags)
58 # Fix -Werror build failure when building glibc with musl with GCC >= 8, see:
59 # https://github.com/NixOS/nixpkgs/pull/68244#issuecomment-544307798
60 (lib.optional stdenv.hostPlatform.isMusl "-Wno-error=attribute-alias")
61 (lib.optionals ((stdenv.hostPlatform != stdenv.buildPlatform) || stdenv.hostPlatform.isMusl) [
62 # Ignore "error: '__EI___errno_location' specifies less restrictive attributes than its target '__errno_location'"
63 # New warning as of GCC 9
64 # Same for musl: https://github.com/NixOS/nixpkgs/issues/78805
65 "-Wno-error=missing-attributes"
67 (lib.optionals (stdenv.hostPlatform.isPower64) [
68 # Do not complain about the Processor Specific ABI (i.e. the
69 # choice to use IEEE-standard `long double`). We pass this
70 # flag in order to mute a `-Werror=psabi` passed by glibc;
71 # hopefully future glibc releases will not pass that flag.
77 # glibc needs to `dlopen()` `libgcc_s.so` but does not link
78 # against it. Furthermore, glibc doesn't use the ordinary
79 # `dlopen()` call to do this; instead it uses one which ignores
82 # https://sourceware.org/legacy-ml/libc-help/2013-11/msg00026.html
84 # In order to get it to not ignore `libgcc_s.so`, we have to add its path to
85 # `user-defined-trusted-dirs`:
87 # https://sourceware.org/git/?p=glibc.git;a=blob;f=elf/Makefile;h=b509b3eada1fb77bf81e2a0ca5740b94ad185764#l1355
89 # Conveniently, this will also inform Nix of the fact that glibc depends on
90 # gcc.libgcc, since the path will be embedded in the resulting binary.
93 (previousAttrs.makeFlags or [])
94 ++ lib.optionals (libgcc != null) [
95 "user-defined-trusted-dirs=${libgcc}/lib"
98 postInstall = previousAttrs.postInstall + (if stdenv.buildPlatform.canExecute stdenv.hostPlatform then ''
99 echo SUPPORTED-LOCALES=C.UTF-8/UTF-8 > ../glibc-2*/localedata/SUPPORTED
100 make -j''${NIX_BUILD_CORES:-1} localedata/install-locales
101 '' else lib.optionalString stdenv.buildPlatform.isLinux
102 # This is based on http://www.linuxfromscratch.org/lfs/view/development/chapter06/glibc.html
103 # Instead of using their patch to build a build-native localedef,
104 # we simply use the one from pkgsBuildBuild.
106 # Note that we can't use pkgsBuildHost (aka buildPackages) here, because
107 # that will cause an eval-time infinite recursion: "buildPackages.glibc
108 # depended on buildPackages.libgcc, which, since it's GCC, depends on the
109 # target's bintools, which depend on the target's glibc, which, again,
110 # depends on buildPackages.glibc, causing an infinute recursion when
111 # evaluating buildPackages.glibc when glibc hasn't come from stdenv
112 # (e.g. on musl)." https://github.com/NixOS/nixpkgs/pull/259964
114 pushd ../glibc-2*/localedata
115 export I18NPATH=$PWD GCONV_PATH=$PWD/../iconvdata
116 mkdir -p $NIX_BUILD_TOP/${pkgsBuildBuild.glibc}/lib/locale
117 ${lib.getBin pkgsBuildBuild.glibc}/bin/localedef \
118 --alias-file=../intl/locale.alias \
121 --prefix $NIX_BUILD_TOP \
122 ${if stdenv.hostPlatform.parsed.cpu.significantByte.name == "littleEndian" then
127 cp -r $NIX_BUILD_TOP/${pkgsBuildBuild.glibc}/lib/locale $out/lib
131 test -f $out/etc/ld.so.cache && rm $out/etc/ld.so.cache
133 if test -n "$linuxHeaders"; then
134 # Include the Linux kernel headers in Glibc, except the `scsi'
135 # subdirectory, which Glibc provides itself.
136 (cd $dev/include && \
137 ln -sv $(ls -d $linuxHeaders/include/* | grep -v scsi\$) .)
140 # Fix for NIXOS-54 (ldd not working on x86_64). Make a symlink
142 if test -n "$is64bit"; then
146 # Get rid of more unnecessary stuff.
147 rm -rf $out/var $bin/bin/sln
149 # Backwards-compatibility to fix e.g.
150 # "configure: error: Pthreads are required to build libgomp" during `gcc`-build
151 # because it's not actually needed anymore to link against `pthreads` since
152 # it's now part of `libc.so.6` itself, but the gcc build breaks if
154 ln -sf $out/lib/libpthread.so.0 $out/lib/libpthread.so
155 ln -sf $out/lib/librt.so.1 $out/lib/librt.so
156 ln -sf $out/lib/libdl.so.2 $out/lib/libdl.so
157 test -f $out/lib/libutil.so.1 && ln -sf $out/lib/libutil.so.1 $out/lib/libutil.so
158 touch $out/lib/libpthread.a
160 # Put libraries for static linking in a separate output. Note
161 # that libc_nonshared.a and libpthread_nonshared.a are required
162 # for dynamically-linked applications.
164 mv $out/lib/*.a $static/lib
165 mv $static/lib/lib*_nonshared.a $out/lib
166 # If libutil.so.1 is missing, libutil.a is required.
167 test -f $out/lib/libutil.so.1 || mv $static/lib/libutil.a $out/lib
168 # Some of *.a files are linker scripts where moving broke the paths.
169 sed "/^GROUP/s|$out/lib/lib|$static/lib/lib|g" \
172 # Work around a Nix bug: hard links across outputs cause a build failure.
173 cp $bin/bin/getconf $bin/bin/getconf_
174 mv $bin/bin/getconf_ $bin/bin/getconf
177 separateDebugInfo = true;
180 (previousAttrs.passthru or {})
181 // lib.optionalAttrs (libgcc != null) {
185 meta = (previousAttrs.meta or {}) // { description = "The GNU C Library"; };