flang-rt: add runtime package

Add a standalone derivation for LLVM's flang-rt runtime libraries
(libFortranRuntime, libFortranDecimal). flang-rt is a runtimes-style
package: building it requires running a working flang binary at *build*
time, but the produced libraries are linked into derivations targeting
the *host*.

The runtime is therefore wired up using `buildFlang`, sourced from
`buildLlvmPackages.flang-unwrapped` so that the build-platform flang
(spliced via the LLVM package set) compiles the runtime, while the
resulting libraries are still produced for the host platform. This
mirrors the existing `buildLlvmTools.tblgen` / `buildLlvmPackages.tblgen`
pattern used elsewhere in the LLVM package set.

`unwrapped` rather than the wrapped flang is used because flang-rt's
CMake glue invokes the compiler directly and does not need (or want)
Nix's cc-wrapper resource-dir and rpath plumbing during the runtime
build.

flang-rt only consumes LLVM's CMake helpers and the build-time flang;
it does not link against MLIR or libclang, so neither MLIR_DIR nor
CLANG_DIR is passed and the unused empty `dev` output is omitted.

Co-authored-by: stove <stove@rivosinc.com>
Co-authored-by: acture <acture@gmail.com>
This commit is contained in:
Acture 2026-03-30 17:03:25 +08:00
commit 324f837bf1

View file

@ -0,0 +1,81 @@
{
lib,
stdenv,
llvm_meta,
monorepoSrc,
runCommand,
cmake,
libllvm,
ninja,
python3,
buildFlang,
version,
}:
let
minDarwinVersion = "10.12";
effectiveDarwinVersion =
if stdenv.isDarwin && lib.versionOlder stdenv.hostPlatform.darwinMinVersion minDarwinVersion then
minDarwinVersion
else
stdenv.hostPlatform.darwinMinVersion;
in
stdenv.mkDerivation (finalAttrs: {
pname = "flang-rt";
inherit version;
src =
runCommand "${finalAttrs.pname}-src-${version}"
{
inherit (monorepoSrc) passthru;
}
''
mkdir -p "$out"
cp -r ${monorepoSrc}/cmake "$out"
mkdir -p "$out/llvm"
cp -r ${monorepoSrc}/llvm/cmake "$out/llvm"
cp -r ${monorepoSrc}/llvm/utils "$out/llvm"
cp -r ${monorepoSrc}/third-party "$out"
cp -r ${monorepoSrc}/${finalAttrs.pname} "$out"
cp -r ${monorepoSrc}/flang "$out"
cp -r ${monorepoSrc}/runtimes "$out"
'';
sourceRoot = "${finalAttrs.src.name}/runtimes";
outputs = [ "out" ];
nativeBuildInputs = [
buildFlang
cmake
ninja
python3
];
buildInputs = [
libllvm
];
env = lib.optionalAttrs stdenv.isDarwin {
MACOSX_DEPLOYMENT_TARGET = effectiveDarwinVersion;
NIX_CFLAGS_COMPILE = "-mmacosx-version-min=${effectiveDarwinVersion}";
};
cmakeFlags = [
(lib.cmakeFeature "LLVM_DEFAULT_TARGET_TRIPLE" stdenv.hostPlatform.config)
(lib.cmakeFeature "CMAKE_Fortran_COMPILER" "${buildFlang}/bin/flang")
(lib.cmakeBool "CMAKE_Fortran_COMPILER_WORKS" true)
(lib.cmakeBool "CMAKE_Fortran_COMPILER_SUPPORTS_F90" true)
(lib.cmakeFeature "LLVM_DIR" "${libllvm.dev}/lib/cmake/llvm")
(lib.cmakeFeature "LLVM_ENABLE_RUNTIMES" "flang-rt")
]
++ lib.optionals stdenv.isDarwin [
(lib.cmakeFeature "CMAKE_OSX_DEPLOYMENT_TARGET" effectiveDarwinVersion)
];
meta = llvm_meta // {
homepage = "https://flang.llvm.org";
description = "LLVM Fortran Runtime";
};
})