1{ stdenv, fetchurl, buildPackages, lib, fetchpatch, texinfo
2, # "newlib-nano" is what the official ARM embedded toolchain calls this build
3 # configuration that prioritizes low space usage. We include it as a preset
4 # for embedded projects striving for a similar configuration.
5 nanoizeNewlib ? false
6}:
7
8stdenv.mkDerivation (finalAttrs: {
9 pname = "newlib";
10 version = "4.3.0.20230120";
11
12 src = fetchurl {
13 url = "ftp://sourceware.org/pub/newlib/newlib-${finalAttrs.version}.tar.gz";
14 sha256 = "sha256-g6Yqma9Z4465sMWO0JLuJNcA//Q6IsA+QzlVET7zUVA=";
15 };
16
17 patches = lib.optionals nanoizeNewlib [
18 # https://bugs.gentoo.org/723756
19 (fetchpatch {
20 name = "newlib-3.3.0-no-nano-cxx.patch";
21 url = "https://gitweb.gentoo.org/repo/gentoo.git/plain/sys-libs/newlib/files/newlib-3.3.0-no-nano-cxx.patch?id=9ee5a1cd6f8da6d084b93b3dbd2e8022a147cfbf";
22 sha256 = "sha256-S3mf7vwrzSMWZIGE+d61UDH+/SK/ao1hTPee1sElgco=";
23 })
24 ];
25
26 depsBuildBuild = [
27 buildPackages.stdenv.cc
28 texinfo # for makeinfo
29 ];
30
31 # newlib expects CC to build for build platform, not host platform
32 preConfigure = ''
33 export CC=cc
34 '';
35
36 configurePlatforms = [ "build" "target" ];
37 # 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
38 # sort alphabetically
39 configureFlags = [
40 "--host=${stdenv.buildPlatform.config}"
41 ] ++ (if !nanoizeNewlib then [
42 "--disable-newlib-supplied-syscalls"
43 "--disable-nls"
44 "--enable-newlib-io-c99-formats"
45 "--enable-newlib-io-long-long"
46 "--enable-newlib-reent-check-verify"
47 "--enable-newlib-register-fini"
48 "--enable-newlib-retargetable-locking"
49 ] else [
50 "--disable-newlib-fseek-optimization"
51 "--disable-newlib-fvwrite-in-streamio"
52 "--disable-newlib-supplied-syscalls"
53 "--disable-newlib-unbuf-stream-opt"
54 "--disable-newlib-wide-orient"
55 "--disable-nls"
56 "--enable-lite-exit"
57 "--enable-newlib-global-atexit"
58 "--enable-newlib-nano-formatted-io"
59 "--enable-newlib-nano-malloc"
60 "--enable-newlib-reent-check-verify"
61 "--enable-newlib-reent-small"
62 "--enable-newlib-retargetable-locking"
63 ]);
64
65 dontDisableStatic = true;
66
67 # 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
68 postInstall = lib.optionalString nanoizeNewlib ''
69 mkdir -p $out${finalAttrs.passthru.incdir}/newlib-nano
70 cp $out${finalAttrs.passthru.incdir}/newlib.h $out${finalAttrs.passthru.incdir}/newlib-nano/
71
72 (
73 cd $out${finalAttrs.passthru.libdir}
74
75 for f in librdimon.a libc.a libg.a; do
76 cp "$f" "''${f%%\.a}_nano.a"
77 done
78 )
79 '';
80
81 passthru = {
82 incdir = "/${stdenv.targetPlatform.config}/include";
83 libdir = "/${stdenv.targetPlatform.config}/lib";
84 };
85
86 meta = with lib; {
87 description = "a C library intended for use on embedded systems";
88 homepage = "https://sourceware.org/newlib/";
89 # arch has "bsd" while gentoo has "NEWLIB LIBGLOSS GPL-2" while COPYING has "gpl2"
90 # there are 5 copying files in total
91 # COPYING
92 # COPYING.LIB
93 # COPYING.LIBGLOSS
94 # COPYING.NEWLIB
95 # COPYING3
96 license = licenses.gpl2Plus;
97 };
98})