saunafs: 4.6.0 -> 4.7.0 (#379649)
[NixPkgs.git] / pkgs / development / misc / newlib / default.nix
blobfe71113a6f7d179ee2a1cd17e6e4ac2d97caf7d3
2   stdenv,
3   fetchurl,
4   buildPackages,
5   lib,
6   fetchpatch,
7   texinfo,
8   # "newlib-nano" is what the official ARM embedded toolchain calls this build
9   # configuration that prioritizes low space usage. We include it as a preset
10   # for embedded projects striving for a similar configuration.
11   nanoizeNewlib ? false,
14 stdenv.mkDerivation (finalAttrs: {
15   pname = "newlib";
16   version = "4.5.0.20241231";
18   src = fetchurl {
19     url = "ftp://sourceware.org/pub/newlib/newlib-${finalAttrs.version}.tar.gz";
20     sha256 = "sha256-M/EmBeAFSWWZbCXBOCs+RjsK+ReZAB9buMBjDy7IyFI=";
21   };
23   patches = lib.optionals nanoizeNewlib [
24     # https://bugs.gentoo.org/723756
25     (fetchpatch {
26       name = "newlib-3.3.0-no-nano-cxx.patch";
27       url = "https://gitweb.gentoo.org/repo/gentoo.git/plain/sys-libs/newlib/files/newlib-3.3.0-no-nano-cxx.patch?id=9ee5a1cd6f8da6d084b93b3dbd2e8022a147cfbf";
28       sha256 = "sha256-S3mf7vwrzSMWZIGE+d61UDH+/SK/ao1hTPee1sElgco=";
29     })
30   ];
32   depsBuildBuild = [
33     buildPackages.stdenv.cc
34     texinfo # for makeinfo
35   ];
37   # newlib expects CC to build for build platform, not host platform
38   preConfigure =
39     ''
40       export CC=cc
41     ''
42     +
43       # newlib tries to disable itself when building for Linux *except*
44       # when native-compiling.  Unfortunately the check for "is cross
45       # compiling" was written when newlib was part of GCC and newlib
46       # was built along with GCC (therefore newlib was built to execute
47       # on the targetPlatform, not the hostPlatform).  Unfortunately
48       # when newlib was extracted from GCC, this "is cross compiling"
49       # logic was not fixed.  So we must disable it.
50       ''
51         substituteInPlace configure --replace 'noconfigdirs target-newlib target-libgloss' 'noconfigdirs'
52         substituteInPlace configure --replace 'cross_only="target-libgloss target-newlib' 'cross_only="'
53       '';
55   configurePlatforms = [
56     "build"
57     "target"
58   ];
59   # flags copied from https://community.arm.com/support-forums/f/compilers-and-libraries-forum/53310/gcc-arm-none-eabi-what-were-the-newlib-compilation-options
60   # sort alphabetically
61   configureFlags =
62     [
63       "--with-newlib"
65       # The newlib configury uses `host` to refer to the platform
66       # which is being used to compile newlib.  Ugh.  It does this
67       # because of its history: newlib used to be distributed with and
68       # built as part of gcc.
69       #
70       # To prevent nixpkgs from going insane, this package presents the
71       # "normal" view to the outside world: the binaries in $out will
72       # execute on `stdenv.hostPlatform`.  We then fool newlib's build
73       # process into doing the right thing.
74       "--host=${stdenv.targetPlatform.config}"
76     ]
77     ++ (
78       if !nanoizeNewlib then
79         [
80           "--disable-newlib-supplied-syscalls"
81           "--disable-nls"
82           "--enable-newlib-io-c99-formats"
83           "--enable-newlib-io-long-long"
84           "--enable-newlib-reent-check-verify"
85           "--enable-newlib-register-fini"
86           "--enable-newlib-retargetable-locking"
87         ]
88       else
89         [
90           "--disable-newlib-fseek-optimization"
91           "--disable-newlib-fvwrite-in-streamio"
92           "--disable-newlib-supplied-syscalls"
93           "--disable-newlib-unbuf-stream-opt"
94           "--disable-newlib-wide-orient"
95           "--disable-nls"
96           "--enable-lite-exit"
97           "--enable-newlib-global-atexit"
98           "--enable-newlib-nano-formatted-io"
99           "--enable-newlib-nano-malloc"
100           "--enable-newlib-reent-check-verify"
101           "--enable-newlib-reent-small"
102           "--enable-newlib-retargetable-locking"
103         ]
104     );
106   enableParallelBuilding = true;
107   dontDisableStatic = true;
109   # apply necessary nano changes from https://developer.arm.com/-/media/Files/downloads/gnu/12.2.rel1/manifest/copy_nano_libraries.sh?rev=4c50be6ccb9c4205a5262a3925317073&hash=1375A7B0A1CD0DB9B9EB0D2B574ADF66
110   postInstall =
111     lib.optionalString nanoizeNewlib ''
112       mkdir -p $out${finalAttrs.passthru.incdir}/newlib-nano
113       cp $out${finalAttrs.passthru.incdir}/newlib.h $out${finalAttrs.passthru.incdir}/newlib-nano/
115       (
116         cd $out${finalAttrs.passthru.libdir}
118         for f in librdimon.a libc.a libm.a libg.a libgloss.a; do
119           # Some libraries are only available for specific architectures.
120           # For example, librdimon.a is only available on ARM.
121           [ -f "$f" ] && cp "$f" "''${f%%\.a}_nano.a"
122         done
123       )
124     ''
125     + ''[ "$(find $out -type f | wc -l)" -gt 0 ] || (echo '$out is empty' 1>&2 && exit 1)'';
127   passthru = {
128     incdir = "/${stdenv.targetPlatform.config}/include";
129     libdir = "/${stdenv.targetPlatform.config}/lib";
130   };
132   meta = with lib; {
133     description = "C library intended for use on embedded systems";
134     homepage = "https://sourceware.org/newlib/";
135     # arch has "bsd" while gentoo has "NEWLIB LIBGLOSS GPL-2" while COPYING has "gpl2"
136     # there are 5 copying files in total
137     # COPYING
138     # COPYING.LIB
139     # COPYING.LIBGLOSS
140     # COPYING.NEWLIB
141     # COPYING3
142     license = licenses.gpl2Plus;
143   };