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