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