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