2023-04-12 12:48:02 +00:00
{ stdenv , fetchurl , buildPackages , lib , fetchpatch , texinfo
2021-10-06 13:57:05 +00:00
, # "newlib-nano" is what the official ARM embedded toolchain calls this build
# configuration that prioritizes low space usage. We include it as a preset
# for embedded projects striving for a similar configuration.
nanoizeNewlib ? false
} :
2020-04-24 23:36:52 +00:00
2023-04-12 12:48:02 +00:00
stdenv . mkDerivation ( finalAttrs : {
2020-04-24 23:36:52 +00:00
pname = " n e w l i b " ;
2023-04-12 12:48:02 +00:00
version = " 4 . 3 . 0 . 2 0 2 3 0 1 2 0 " ;
2021-06-28 23:13:55 +00:00
2020-04-24 23:36:52 +00:00
src = fetchurl {
2023-04-12 12:48:02 +00:00
url = " f t p : / / s o u r c e w a r e . o r g / p u b / n e w l i b / n e w l i b - ${ finalAttrs . version } . t a r . g z " ;
sha256 = " s h a 2 5 6 - g 6 Y q m a 9 Z 4 4 6 5 s M W O 0 J L u J N c A / / Q 6 I s A + Q z l V E T 7 z U V A = " ;
2020-04-24 23:36:52 +00:00
} ;
2023-04-12 12:48:02 +00:00
patches = lib . optionals nanoizeNewlib [
# https://bugs.gentoo.org/723756
( fetchpatch {
name = " n e w l i b - 3 . 3 . 0 - n o - n a n o - c x x . p a t c h " ;
url = " h t t p s : / / g i t w e b . g e n t o o . o r g / r e p o / g e n t o o . g i t / p l a i n / s y s - l i b s / n e w l i b / f i l e s / n e w l i b - 3 . 3 . 0 - n o - n a n o - c x x . p a t c h ? i d = 9 e e 5 a 1 c d 6 f 8 d a 6 d 0 8 4 b 9 3 b 3 d b d 2 e 8 0 2 2 a 1 4 7 c f b f " ;
sha256 = " s h a 2 5 6 - S 3 m f 7 v w r z S M W Z I G E + d 6 1 U D H + / S K / a o 1 h T P e e 1 s E l g c o = " ;
} )
] ;
depsBuildBuild = [
buildPackages . stdenv . cc
texinfo # for makeinfo
] ;
2020-04-24 23:36:52 +00:00
# newlib expects CC to build for build platform, not host platform
preConfigure = ''
export CC = cc
2023-11-16 04:20:00 +00:00
'' +
# newlib tries to disable itself when building for Linux *except*
# when native-compiling. Unfortunately the check for "is cross
# compiling" was written when newlib was part of GCC and newlib
# was built along with GCC (therefore newlib was built to execute
# on the targetPlatform, not the hostPlatform). Unfortunately
# when newlib was extracted from GCC, this "is cross compiling"
# logic was not fixed. So we must disable it.
''
substituteInPlace configure - - replace ' noconfigdirs target-newlib target-libgloss' ' noconfigdirs'
2024-01-02 11:29:13 +00:00
substituteInPlace configure - - replace ' cross_only = " t a r g e t - l i b g l o s s t a r g e t - n e w l i b ' ' c r o s s _ o n l y = " '
2020-04-24 23:36:52 +00:00
'' ;
2023-11-16 04:20:00 +00:00
2020-04-24 23:36:52 +00:00
configurePlatforms = [ " b u i l d " " t a r g e t " ] ;
2023-04-12 12:48:02 +00:00
# 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
# sort alphabetically
2020-04-24 23:36:52 +00:00
configureFlags = [
2023-11-16 04:20:00 +00:00
" - - w i t h - n e w l i b "
# The newlib configury uses `host` to refer to the platform
# which is being used to compile newlib. Ugh. It does this
# because of its history: newlib used to be distributed with and
# built as part of gcc.
#
# To prevent nixpkgs from going insane, this package presents the
# "normal" view to the outside world: the binaries in $out will
# execute on `stdenv.hostPlatform`. We then fool newlib's build
# process into doing the right thing.
" - - h o s t = ${ stdenv . targetPlatform . config } "
2023-04-12 12:48:02 +00:00
] ++ ( if ! nanoizeNewlib then [
2020-04-24 23:36:52 +00:00
" - - d i s a b l e - n e w l i b - s u p p l i e d - s y s c a l l s "
" - - d i s a b l e - n l s "
2023-04-12 12:48:02 +00:00
" - - e n a b l e - n e w l i b - i o - c 9 9 - f o r m a t s "
2020-04-24 23:36:52 +00:00
" - - e n a b l e - n e w l i b - i o - l o n g - l o n g "
2023-04-12 12:48:02 +00:00
" - - e n a b l e - n e w l i b - r e e n t - c h e c k - v e r i f y "
2020-04-24 23:36:52 +00:00
" - - e n a b l e - n e w l i b - r e g i s t e r - f i n i "
2023-04-12 12:48:02 +00:00
" - - e n a b l e - n e w l i b - r e t a r g e t a b l e - l o c k i n g "
2021-10-06 13:57:05 +00:00
] else [
" - - d i s a b l e - n e w l i b - f s e e k - o p t i m i z a t i o n "
2023-04-12 12:48:02 +00:00
" - - d i s a b l e - n e w l i b - f v w r i t e - i n - s t r e a m i o "
" - - d i s a b l e - n e w l i b - s u p p l i e d - s y s c a l l s "
2021-10-06 13:57:05 +00:00
" - - d i s a b l e - n e w l i b - u n b u f - s t r e a m - o p t "
2023-04-12 12:48:02 +00:00
" - - d i s a b l e - n e w l i b - w i d e - o r i e n t "
" - - d i s a b l e - n l s "
2021-10-06 13:57:05 +00:00
" - - e n a b l e - l i t e - e x i t "
" - - e n a b l e - n e w l i b - g l o b a l - a t e x i t "
" - - e n a b l e - n e w l i b - n a n o - f o r m a t t e d - i o "
2023-04-12 12:48:02 +00:00
" - - e n a b l e - n e w l i b - n a n o - m a l l o c "
" - - e n a b l e - n e w l i b - r e e n t - c h e c k - v e r i f y "
" - - e n a b l e - n e w l i b - r e e n t - s m a l l "
" - - e n a b l e - n e w l i b - r e t a r g e t a b l e - l o c k i n g "
2021-10-06 13:57:05 +00:00
] ) ;
2020-04-24 23:36:52 +00:00
dontDisableStatic = true ;
2023-04-12 12:48:02 +00:00
# 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
postInstall = lib . optionalString nanoizeNewlib ''
mkdir - p $ out $ { finalAttrs . passthru . incdir } /newlib-nano
cp $ out $ { finalAttrs . passthru . incdir } /newlib.h $ out $ { finalAttrs . passthru . incdir } /newlib-nano /
(
cd $ out $ { finalAttrs . passthru . libdir }
for f in librdimon . a libc . a libg . a ; do
2023-07-15 17:15:38 +00:00
# Some libraries are only available for specific architectures.
# For example, librdimon.a is only available on ARM.
[ - f " $ f " ] && cp " $ f " " ' ' ${ f % % \ . a } _ n a n o . a "
2023-04-12 12:48:02 +00:00
done
)
2023-07-15 17:15:38 +00:00
'' + '' [ " $ ( f i n d $ o u t - t y p e f | w c - l ) " - gt 0 ] || ( echo ' $ out is empty' 1 > & 2 && exit 1 ) '' ;
2023-04-12 12:48:02 +00:00
2020-04-24 23:36:52 +00:00
passthru = {
incdir = " / ${ stdenv . targetPlatform . config } / i n c l u d e " ;
libdir = " / ${ stdenv . targetPlatform . config } / l i b " ;
} ;
2023-04-12 12:48:02 +00:00
meta = with lib ; {
2024-06-20 14:57:18 +00:00
description = " C l i b r a r y i n t e n d e d f o r u s e o n e m b e d d e d s y s t e m s " ;
2023-04-12 12:48:02 +00:00
homepage = " h t t p s : / / s o u r c e w a r e . o r g / n e w l i b / " ;
# arch has "bsd" while gentoo has "NEWLIB LIBGLOSS GPL-2" while COPYING has "gpl2"
# there are 5 copying files in total
# COPYING
# COPYING.LIB
# COPYING.LIBGLOSS
# COPYING.NEWLIB
# COPYING3
license = licenses . gpl2Plus ;
} ;
} )