Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mlibc: init at 5.0.0-unstable-2025-01-05 #371092

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/systems/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ let
else if final.isWasi then "wasilibc"
else if final.isWasm && !final.isWasi then null
else if final.isRedox then "relibc"
else if final.isMlibc then "mlibc"
else if final.isMusl then "musl"
else if final.isUClibc then "uclibc"
else if final.isAndroid then "bionic"
Expand Down Expand Up @@ -187,7 +188,7 @@ let
# omit any `.so` build products such as libgcc_s.so. When that happens,
# it causes hard-to-troubleshoot build failures.
hasSharedLibraries = with final;
(isAndroid || isGnu || isMusl # Linux (allows multiple libcs)
(isAndroid || isGnu || isMusl || isMlibc # Linux (allows multiple libcs)
|| isDarwin || isSunOS || isOpenBSD || isFreeBSD || isNetBSD # BSDs
|| isCygwin || isMinGW || isWindows # Windows
|| isWasm # WASM
Expand Down
21 changes: 17 additions & 4 deletions lib/systems/examples.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
let
platforms = import ./platforms.nix { inherit lib; };

riscv = bits: {
config = "riscv${bits}-unknown-linux-gnu";
riscv = bits: abi: {
config = "riscv${bits}-unknown-linux-${abi}";
};
in

Expand Down Expand Up @@ -82,6 +82,10 @@ rec {
useLLVM = true;
};

aarch64-linux-mlibc = {
config = "aarch64-unknown-linux-mlibc";
};

pogoplug4 = {
config = "armv5tel-unknown-linux-gnueabi";
} // platforms.pogoplug4;
Expand Down Expand Up @@ -118,11 +122,16 @@ rec {
gnu64_simplekernel = gnu64 // platforms.pc_simplekernel; # see test/cross/default.nix
gnu32 = { config = "i686-unknown-linux-gnu"; };

x86_64-linux-mlibc = { config = "x86_64-unknown-linux-mlibc"; };
i686-linux-mlibc = { config = "i686-unknown-linux-mlibc"; };

musl64 = { config = "x86_64-unknown-linux-musl"; };
musl32 = { config = "i686-unknown-linux-musl"; };

riscv64 = riscv "64";
riscv32 = riscv "32";
riscv64 = riscv "64" "gnu";
riscv32 = riscv "32" "gnu";

riscv64-linux-mlibc = riscv "64" "mlibc";

riscv64-embedded = {
config = "riscv64-none-elf";
Expand Down Expand Up @@ -181,6 +190,10 @@ rec {
config = "m68k-unknown-linux-gnu";
};

m68k-linux-mlibc = {
config = "m68k-unknown-linux-mlibc";
};

s390 = {
config = "s390-unknown-linux-gnu";
};
Expand Down
1 change: 1 addition & 0 deletions lib/systems/inspect.nix
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ rec {

isAndroid = [ { abi = abis.android; } { abi = abis.androideabi; } ];
isGnu = with abis; map (a: { abi = a; }) [ gnuabi64 gnuabin32 gnu gnueabi gnueabihf gnuabielfv1 gnuabielfv2 ];
isMlibc = { abi = abis.mlibc; };
isMusl = with abis; map (a: { abi = a; }) [ musl musleabi musleabihf muslabin32 muslabi64 ];
isUClibc = with abis; map (a: { abi = a; }) [ uclibc uclibceabi uclibceabihf ];

Expand Down
2 changes: 2 additions & 0 deletions lib/systems/parse.nix
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ rec {
gnuabielfv2 = { abi = "elfv2"; };
gnuabielfv1 = { abi = "elfv1"; };

mlibc = {};

musleabi = { float = "soft"; };
musleabihf = { float = "hard"; };
musl = {};
Expand Down
1 change: 1 addition & 0 deletions pkgs/build-support/bintools-wrapper/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ let
# shell glob that ought to match it.
dynamicLinker =
/**/ if sharedLibraryLoader == null then ""
else if targetPlatform.libc == "mlibc" then "${sharedLibraryLoader}/lib/ld.so"
else if targetPlatform.libc == "musl" then "${sharedLibraryLoader}/lib/ld-musl-*"
else if targetPlatform.libc == "uclibc" then "${sharedLibraryLoader}/lib/ld*-uClibc.so.1"
else if (targetPlatform.libc == "bionic" && targetPlatform.is32bit) then "/system/bin/linker"
Expand Down
116 changes: 116 additions & 0 deletions pkgs/by-name/ml/mlibc/package.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
lib,
buildPackages,
stdenvNoLibc,
fetchFromGitHub,
ninja,
pkg-config,
linuxHeaders,
}:

let
freestnd-c-hdrs = fetchFromGitHub {
owner = "osdev0";
repo = "freestnd-c-hdrs";
rev = "910d752825a08421432ed71329ac140dd608e33a";
sha256 = "sha256-aLnbiST4WjTnkjEUus+ijevbB2ml4jUOXQUnMp95iAE=";
};
freestnd-cxx-hdrs = fetchFromGitHub {
owner = "osdev0";
repo = "freestnd-cxx-hdrs";
rev = "2b2345a2fc8edced19983130a1f17164fd2cbdfe";
sha256 = "sha256-BBn6mglVtfEtPldb1f2BiWfMeemMHzrY1c+38XOMRRE=";
};
frigg = fetchFromGitHub {
owner = "managarm";
repo = "frigg";
rev = "307e9361acb49e7d9e5e7f62a1b48de66974d8f4";
sha256 = "sha256-X5ZcXYe7H3Qrec/LD1mmHJ13ZKRutPjoPjg6j/MMFKM=";
};
in
stdenvNoLibc.mkDerivation (finalAttrs: {
pname = "mlibc";
version = "5.0.0-unstable-2025-01-05";

src = fetchFromGitHub {
owner = "managarm";
repo = "mlibc";
rev = "c556f8c17ba19e060f099433e33d35add17d9c7d";
sha256 = "sha256-wQ0dXsFofVYHalGUjPCQoN48uCd/psv0tPcsEdKTrN8=";
};

depsBuildBuild = [
buildPackages.stdenv.cc
];

nativeBuildInputs = [
(buildPackages.meson.override { python3 = buildPackages.__splicedPackages.python3Minimal; })
ninja
pkg-config
];

# patchelf adds entries it shouldn't to ld.so
dontPatchELF = true;

mesonBuildType = "release";
mesonFlags = [
"-Ddefault_library=both"
"-Dlinux_kernel_headers=${linuxHeaders}/include"
"-Dbuild_tests=false"
];

preConfigure = ''
# Vendor-in required subprojects.
# freestnd-*-hdrs are not required when building with a linux-mlibc
# compiler, but currently, we need the freestanding headers to bootstrap.
# Alternatively, we could install headers only and bootstrap a linux-mlibc
# compiler with those headers, then compile with the linux-mlibc compiler.
ln -sf ${freestnd-c-hdrs} $(pwd)/subprojects/freestnd-c-hdrs
ln -sf ${freestnd-cxx-hdrs} $(pwd)/subprojects/freestnd-cxx-hdrs
ln -sf ${frigg} $(pwd)/subprojects/frigg
'';

postInstall = ''
# This mlibc-gcc uses a specs file to wrap the host's one. It's a hack,
# we don't use it.
rm $out/bin/mlibc-gcc $out/lib/mlibc-gcc.specs

# some mlibc headers depend on linux headers
# scsi/* and linux/libc-compat.h are provided by mlibc
ln -s $(ls -d ${linuxHeaders}/include/* | grep -Pv "scsi$|linux$") $dev/include
ln -s $(ls -d ${linuxHeaders}/include/linux/* | grep -v "libc-compat.h$") $dev/include/linux
'';

postFixup = ''
# The dynamic loader should never have a RPATH entry, let's remove it.
# Normally NIX_DONT_SET_RPATH would be used but it breaks meson's sanity
# checks, so we're doing it manually.
if [ -f $out/lib/ld.so ]; then
patchelf --remove-rpath $out/lib/ld.so
fi
'';

outputs = [
"out"
"dev"
];

meta = with lib; {
description = "Portable C standard library";
platforms = [
# These are the only platforms supported by both mlibc and nixpkgs
"aarch64-linux"
"i686-linux"
"m68k-linux"
"riscv64-linux"
"x86_64-linux"
];
license = [
licenses.bsd0 # cshim
licenses.mit # mlibc, cxxshim, frigg, parts of musl
];
maintainers = [
maintainers.sanana
];
};
})
8 changes: 7 additions & 1 deletion pkgs/development/compilers/gcc/common/configure-flags.nix
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ let
"--disable-libatomic" # requires libc
"--disable-decimal-float" # requires libc
"--disable-libmpx" # requires libc
"--disable-hosted-libstdcxx"
"--disable-libstdcxx-backtrace"
"--disable-linux-futex"
"--disable-libvtv"
"--disable-libitm"
] ++ lib.optionals crossMingw [
"--with-headers=${lib.getDev libcCross}/include"
"--with-gcc"
Expand All @@ -93,7 +98,7 @@ let
else if targetPlatform.isWindows then (threadsCross.model or "win32")
else "single"}"
"--enable-nls"
] ++ lib.optionals (targetPlatform.libc == "uclibc" || targetPlatform.libc == "musl") [
] ++ lib.optionals (lib.elem targetPlatform.libc [ "mlibc" "musl" "uclibc" ]) [
# libsanitizer requires netrom/netrom.h which is not
# available in uclibc.
"--disable-libsanitizer"
Expand Down Expand Up @@ -217,6 +222,7 @@ let
# On Illumos/Solaris GNU as is preferred
"--with-gnu-as" "--without-gnu-ld"
]
++ lib.optional (targetPlatform.libc == "mlibc") "--disable-libsanitizer"
++ lib.optional (targetPlatform.libc == "musl")
# musl at least, disable: https://git.buildroot.net/buildroot/commit/?id=873d4019f7fb00f6a80592224236b3ba7d657865
"--disable-libmpx"
Expand Down
4 changes: 3 additions & 1 deletion pkgs/development/compilers/gcc/common/dependencies.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
which,
gettext,
gnused,
autoconf269,
patchelf,
gmp,
mpfr,
Expand Down Expand Up @@ -49,7 +50,8 @@ in
++ optionals langRust [ cargo ]
# The builder relies on GNU sed (for instance, Darwin's `sed' fails with
# "-i may not be used with stdin"), and `stdenvNative' doesn't provide it.
++ optionals buildPlatform.isDarwin [ gnused ];
++ optionals buildPlatform.isDarwin [ gnused ]
++ optionals (withoutTargetLibc || targetPlatform.isMlibc) [ autoconf269 ];

# For building runtime libs
# same for all gcc's
Expand Down
10 changes: 10 additions & 0 deletions pkgs/development/compilers/gcc/common/pre-configure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ lib.optionalString (hostPlatform.isSunOS && hostPlatform.is64bit) ''
export STRIP='strip -x'
''


+ lib.optionalString (withoutTargetLibc || targetPlatform.isMlibc) ''
for i in */configure.ac; do
pushd "$(dirname "$i")"
echo "Running autoreconf in $PWD"
autoconf -f
popd
done
''

# HACK: if host and target config are the same, but the platforms are
# actually different we need to convince the configure script that it
# is in fact building a cross compiler although it doesn't believe it.
Expand Down
2 changes: 2 additions & 0 deletions pkgs/development/compilers/gcc/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
, texinfo ? null
, perl ? null # optional, for texi2pod (then pod2man)
, gmp, mpfr, libmpc, gettext, which, patchelf, binutils
, autoconf269
, isl ? null # optional, for the Graphite optimization framework.
, zlib ? null
, libucontext ? null
Expand Down Expand Up @@ -103,6 +104,7 @@ let
# inherit generated with 'nix eval --json --impure --expr "with import ./. {}; lib.attrNames (lib.functionArgs gcc${majorVersion}.cc.override)" | jq '.[]' --raw-output'
inherit
apple-sdk
autoconf269
binutils
buildPackages
cargo
Expand Down
12 changes: 12 additions & 0 deletions pkgs/development/compilers/gcc/patches/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ in
})
]

## mlibc
++ optionals (stdenv.targetPlatform.isMlibc && atLeast14) [
(fetchpatch {
url = "https://github.com/managarm/gcc/commit/092cb31af8ed12dbecc02327444f030a7c40c344.diff";
hash = "sha256-Ix95v0UxC73tfW2UliWiRgS0wC1wVUo5waM38qFlRFM=";
})
(fetchpatch {
url = "https://github.com/managarm/gcc/commit/b5cdfff8afc4a668e65541eec5b512d080116a43.diff";
hash = "sha256-0xbw/vRbUvsIIhXM8iyAxm2FWXDnYXblAJ1gFEgeOPg=";
})
]

## Darwin

# Fixes detection of Darwin on x86_64-darwin. Otherwise, GCC uses a deployment target of 10.5, which crashes ld64.
Expand Down
4 changes: 2 additions & 2 deletions pkgs/development/interpreters/python/cpython/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ let

passthru = let
# When we override the interpreter we also need to override the spliced versions of the interpreter
# bluez is excluded manually to break an infinite recursion.
inputs' = lib.filterAttrs (n: v: n != "bluez" && n != "passthruFun" && ! lib.isDerivation v) inputs;
# bluez and openssl are excluded manually to break infinite recursion errors.
inputs' = lib.filterAttrs (n: v: n != "bluez" && n != "openssl" && n != "passthruFun" && ! lib.isDerivation v) inputs;
override = attr: let python = attr.override (inputs' // { self = python; }); in python;
in passthruFun rec {
inherit self sourceVersion packageOverrides;
Expand Down
2 changes: 1 addition & 1 deletion pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6040,7 +6040,6 @@ with pkgs;
isl = if !stdenv.hostPlatform.isDarwin then isl_0_20 else null;

withoutTargetLibc = true;
langCC = false;
libcCross = libcCross1;
targetPackages.stdenv.cc.bintools = binutilsNoLibc;
enableShared =
Expand Down Expand Up @@ -9123,6 +9122,7 @@ with pkgs;
else if name == "newlib" && stdenv.targetPlatform.isOr1k then targetPackages.or1k-newlib or or1k-newlib
else if name == "newlib" then targetPackages.newlib or newlib
else if name == "newlib-nano" then targetPackages.newlib-nano or newlib-nano
else if name == "mlibc" then targetPackages.mlibc or mlibc
else if name == "musl" then targetPackages.muslCross or muslCross
else if name == "msvcrt" then targetPackages.windows.mingw_w64 or windows.mingw_w64
else if name == "ucrt" then targetPackages.windows.mingw_w64 or windows.mingw_w64
Expand Down
2 changes: 1 addition & 1 deletion pkgs/top-level/stage.nix
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ let
crossSystem = {
isStatic = true;
config = lib.systems.parse.tripleFromSystem (
if stdenv.hostPlatform.isLinux
if stdenv.hostPlatform.isLinux && !stdenv.hostPlatform.isMlibc
then makeMuslParsedPlatform stdenv.hostPlatform.parsed
else stdenv.hostPlatform.parsed
);
Expand Down
Loading