lol
1/* Build configuration used to build glibc, Info files, and locale
2 information.
3
4 Note that this derivation has multiple outputs and does not respect the
5 standard convention of putting the executables into the first output. The
6 first output is `lib` so that the libraries provided by this derivation
7 can be accessed directly, e.g.
8
9 "${pkgs.glibc}/lib/ld-linux-x86_64.so.2"
10
11 The executables are put into `bin` output and need to be referenced via
12 the `bin` attribute of the main package, e.g.
13
14 "${pkgs.glibc.bin}/bin/ldd".
15
16 The executables provided by glibc typically include `ldd`, `locale`, `iconv`
17 but the exact set depends on the library version and the configuration.
18*/
19
20# Note: this package is used for bootstrapping fetchurl, and thus
21# cannot use fetchpatch! All mutable patches (generated by GitHub or
22# cgit) that are needed here should be included directly in Nixpkgs as
23# files.
24
25{ stdenv, lib
26, buildPackages
27, fetchurl
28, linuxHeaders ? null
29, gd ? null, libpng ? null
30, libidn2
31, bison
32, python3Minimal
33}:
34
35{ pname
36, withLinuxHeaders ? false
37, profilingLibraries ? false
38, withGd ? false
39, withLibcrypt ? false
40, extraBuildInputs ? []
41, extraNativeBuildInputs ? []
42, ...
43} @ args:
44
45let
46 version = "2.38";
47 patchSuffix = "-27";
48 sha256 = "sha256-+4KZiZiyspllRnvBtp0VLpwwfSzzAcnq+0VVt3DvP9I=";
49in
50
51assert withLinuxHeaders -> linuxHeaders != null;
52assert withGd -> gd != null && libpng != null;
53
54stdenv.mkDerivation ({
55 version = version + patchSuffix;
56
57 enableParallelBuilding = true;
58
59 patches =
60 [
61 /* No tarballs for stable upstream branch, only https://sourceware.org/git/glibc.git and using git would complicate bootstrapping.
62 $ git fetch --all -p && git checkout origin/release/2.38/master && git describe
63 glibc-2.38-27-g750a45a783
64 $ git show --minimal --reverse glibc-2.38.. | gzip -9n --rsyncable - > 2.38-master.patch.gz
65
66 To compare the archive contents zdiff can be used.
67 $ zdiff -u 2.38-master.patch.gz ../nixpkgs/pkgs/development/libraries/glibc/2.38-master.patch.gz
68 */
69 ./2.38-master.patch.gz
70
71 /* Allow NixOS and Nix to handle the locale-archive. */
72 ./nix-locale-archive.patch
73
74 /* Don't use /etc/ld.so.cache, for non-NixOS systems. */
75 ./dont-use-system-ld-so-cache.patch
76
77 /* Don't use /etc/ld.so.preload, but /etc/ld-nix.so.preload. */
78 ./dont-use-system-ld-so-preload.patch
79
80 /* The command "getconf CS_PATH" returns the default search path
81 "/bin:/usr/bin", which is inappropriate on NixOS machines. This
82 patch extends the search path by "/run/current-system/sw/bin". */
83 ./fix_path_attribute_in_getconf.patch
84
85 ./fix-x64-abi.patch
86
87 /* https://github.com/NixOS/nixpkgs/pull/137601 */
88 ./nix-nss-open-files.patch
89
90 ./0001-Revert-Remove-all-usage-of-BASH-or-BASH-in-installed.patch
91
92 /* Patch derived from archlinux,
93 https://gitlab.archlinux.org/archlinux/packaging/packages/glibc/-/blob/e54d98e2d1aae4930ecad9404ef12234922d9dfd/reenable_DT_HASH.patch
94
95 See also https://github.com/ValveSoftware/Proton/issues/6051
96 & https://github.com/NixOS/nixpkgs/pull/188492#issuecomment-1233802991
97 */
98 ./reenable_DT_HASH.patch
99 ]
100 /* NVCC does not support ARM intrinsics. Since <math.h> is pulled in by almost
101 every HPC piece of software, without this patch CUDA compilation on ARM
102 is effectively broken. See
103 https://forums.developer.nvidia.com/t/nvcc-fails-to-build-with-arm-neon-instructions-cpp-vs-cu/248355/2.
104 */
105 ++ (
106 let
107 isAarch64 = stdenv.buildPlatform.isAarch64 || stdenv.hostPlatform.isAarch64;
108 isLinux = stdenv.buildPlatform.isLinux || stdenv.hostPlatform.isLinux;
109 in
110 lib.optional (isAarch64 && isLinux) ./0001-aarch64-math-vector.h-add-NVCC-include-guard.patch
111 )
112 ++ lib.optional stdenv.hostPlatform.isMusl ./fix-rpc-types-musl-conflicts.patch
113 ++ lib.optional stdenv.buildPlatform.isDarwin ./darwin-cross-build.patch;
114
115 postPatch =
116 ''
117 # Needed for glibc to build with the gnumake 3.82
118 # http://comments.gmane.org/gmane.linux.lfs.support/31227
119 sed -i 's/ot \$/ot:\n\ttouch $@\n$/' manual/Makefile
120
121 # nscd needs libgcc, and we don't want it dynamically linked
122 # because we don't want it to depend on bootstrap-tools libs.
123 echo "LDFLAGS-nscd += -static-libgcc" >> nscd/Makefile
124
125 # Ensure that `__nss_files_fopen` can still be wrapped by `libredirect`.
126 sed -i -e '/libc_hidden_def (__nss_files_fopen)/d' nss/nss_files_fopen.c
127 sed -i -e '/libc_hidden_proto (__nss_files_fopen)/d' include/nss_files.h
128 ''
129 # FIXME: find a solution for infinite recursion in cross builds.
130 # For now it's hopefully acceptable that IDN from libc doesn't reliably work.
131 + lib.optionalString (stdenv.hostPlatform == stdenv.buildPlatform) ''
132
133 # Ensure that libidn2 is found.
134 patch -p 1 <<EOF
135 --- a/inet/idna.c
136 +++ b/inet/idna.c
137 @@ -25,1 +25,1 @@
138 -#define LIBIDN2_SONAME "libidn2.so.0"
139 +#define LIBIDN2_SONAME "${lib.getLib libidn2}/lib/libidn2.so.0"
140 EOF
141 '';
142
143 configureFlags =
144 [ "-C"
145 "--enable-add-ons"
146 "--sysconfdir=/etc"
147 "--enable-stack-protector=strong"
148 "--enable-bind-now"
149 (lib.withFeatureAs withLinuxHeaders "headers" "${linuxHeaders}/include")
150 (lib.enableFeature profilingLibraries "profile")
151 "--enable-fortify-source"
152 ] ++ lib.optionals (stdenv.hostPlatform.isx86 || stdenv.hostPlatform.isAarch64) [
153 # This feature is currently supported on
154 # i386, x86_64 and x32 with binutils 2.29 or later,
155 # and on aarch64 with binutils 2.30 or later.
156 # https://sourceware.org/glibc/wiki/PortStatus
157 "--enable-static-pie"
158 ] ++ lib.optionals stdenv.hostPlatform.isx86 [
159 # Enable Intel Control-flow Enforcement Technology (CET) support
160 "--enable-cet"
161 ] ++ lib.optionals withLinuxHeaders [
162 "--enable-kernel=3.10.0" # RHEL 7 and derivatives, seems oldest still supported kernel
163 ] ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
164 (lib.flip lib.withFeature "fp"
165 (stdenv.hostPlatform.gcc.float or (stdenv.hostPlatform.parsed.abi.float or "hard") == "soft"))
166 "--with-__thread"
167 ] ++ lib.optionals (stdenv.hostPlatform == stdenv.buildPlatform && stdenv.hostPlatform.isAarch32) [
168 "--host=arm-linux-gnueabi"
169 "--build=arm-linux-gnueabi"
170
171 # To avoid linking with -lgcc_s (dynamic link)
172 # so the glibc does not depend on its compiler store path
173 "libc_cv_as_needed=no"
174 ]
175 ++ lib.optional withGd "--with-gd"
176 ++ lib.optional withLibcrypt "--enable-crypt";
177
178 makeFlags = (args.makeFlags or []) ++ [
179 "OBJCOPY=${stdenv.cc.targetPrefix}objcopy"
180 ];
181
182 postInstall = (args.postInstall or "") + ''
183 moveToOutput bin/getent $getent
184 '';
185
186 installFlags = [ "sysconfdir=$(out)/etc" ];
187
188 # out as the first output is an exception exclusive to glibc
189
190 # getent is its own output, not kept in bin, since many things
191 # depend on getent but not on the locale generation tools in the bin
192 # output. This saves a couple of megabytes of closure size in many cases.
193 outputs = [ "out" "bin" "dev" "static" "getent" ];
194
195 strictDeps = true;
196 depsBuildBuild = [ buildPackages.stdenv.cc ];
197 nativeBuildInputs = [ bison python3Minimal ] ++ extraNativeBuildInputs;
198 buildInputs = [ linuxHeaders ] ++ lib.optionals withGd [ gd libpng ] ++ extraBuildInputs;
199
200 env = {
201 linuxHeaders = lib.optionalString withLinuxHeaders linuxHeaders;
202 inherit (stdenv) is64bit;
203 # Needed to install share/zoneinfo/zone.tab. Set to impure /bin/sh to
204 # prevent a retained dependency on the bootstrap tools in the stdenv-linux
205 # bootstrap.
206 BASH_SHELL = "/bin/sh";
207 };
208
209 # Used by libgcc, elf-header, and others to determine ABI
210 passthru = { inherit version; minorRelease = version; };
211}
212
213// (removeAttrs args [ "withLinuxHeaders" "withGd" "postInstall" "makeFlags" ]) //
214
215{
216 src = fetchurl {
217 url = "mirror://gnu/glibc/glibc-${version}.tar.xz";
218 inherit sha256;
219 };
220
221 # Remove absolute paths from `configure' & co.; build out-of-tree.
222 preConfigure = ''
223 export PWD_P=$(type -tP pwd)
224 for i in configure io/ftwtest-sh; do
225 # Can't use substituteInPlace here because replace hasn't been
226 # built yet in the bootstrap.
227 sed -i "$i" -e "s^/bin/pwd^$PWD_P^g"
228 done
229
230 mkdir ../build
231 cd ../build
232
233 configureScript="`pwd`/../$sourceRoot/configure"
234
235 ${lib.optionalString (stdenv.cc.libc != null)
236 ''makeFlags="$makeFlags BUILD_LDFLAGS=-Wl,-rpath,${stdenv.cc.libc}/lib OBJDUMP=${stdenv.cc.bintools.bintools}/bin/objdump"''
237 }
238
239
240 '' + lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) ''
241 sed -i s/-lgcc_eh//g "../$sourceRoot/Makeconfig"
242
243 cat > config.cache << "EOF"
244 libc_cv_forced_unwind=yes
245 libc_cv_c_cleanup=yes
246 libc_cv_gnu89_inline=yes
247 EOF
248
249 # ./configure has logic like
250 #
251 # AR=`$CC -print-prog-name=ar`
252 #
253 # This searches various directories in the gcc and its wrapper. In nixpkgs,
254 # this returns the bare string "ar", which is build ar. This can result as
255 # a build failure with the following message:
256 #
257 # libc_pic.a: error adding symbols: archive has no index; run ranlib to add one
258 #
259 # (Observed cross compiling from aarch64-linux -> armv7l-linux).
260 #
261 # Nixpkgs passes a correct value for AR and friends, so to use the correct
262 # set of tools, we only need to delete this special handling.
263 sed -i \
264 -e '/^AR=/d' \
265 -e '/^AS=/d' \
266 -e '/^LD=/d' \
267 -e '/^OBJCOPY=/d' \
268 -e '/^OBJDUMP=/d' \
269 $configureScript
270 '';
271
272 preBuild = lib.optionalString withGd "unset NIX_DONT_SET_RPATH";
273
274 doCheck = false; # fails
275
276 meta = with lib; {
277 homepage = "https://www.gnu.org/software/libc/";
278 description = "The GNU C Library";
279
280 longDescription =
281 '' Any Unix-like operating system needs a C library: the library which
282 defines the "system calls" and other basic facilities such as
283 open, malloc, printf, exit...
284
285 The GNU C library is used as the C library in the GNU system and
286 most systems with the Linux kernel.
287 '';
288
289 license = licenses.lgpl2Plus;
290
291 maintainers = with maintainers; [ eelco ma27 connorbaker ];
292 platforms = platforms.linux;
293 } // (args.meta or {});
294})