nixpkgs/pkgs/development/compilers/llvm/common/mlir/default.nix
Acture 7e8258d3af llvm/mlir: fix MLIRConfig.cmake to support external tablegen overrides
MLIRConfig.cmake unconditionally overwrites MLIR_TABLEGEN_EXE and does
not create an imported target, breaking standalone builds that provide
their own mlir-tblgen binary (e.g. Nix sandboxed builds).

The patch adds guards to respect caller-set MLIR_TABLEGEN_EXE,
MLIR_PDLL_TABLEGEN_EXE, and MLIR_SRC_SHARDER_TABLEGEN_EXE values, and
auto-creates an imported mlir-tblgen target for downstream consumers.
This replaces the previous dummy-target workaround in flang's
CMakeLists.txt.

Upstream issue: https://github.com/llvm/llvm-project/issues/150986

Co-authored-by: Alexander Bantyev <balsoft@balsoft.ru>
2026-05-04 18:06:37 +08:00

110 lines
3.6 KiB
Nix

{
lib,
stdenv,
llvm_meta,
release_version,
buildLlvmPackages,
monorepoSrc,
runCommand,
cmake,
ninja,
libxml2,
libllvm,
version,
devExtraCmakeFlags ? [ ],
getVersionFile,
}:
stdenv.mkDerivation (finalAttrs: {
pname = "mlir";
inherit version;
doCheck =
(
!stdenv.hostPlatform.isx86_32 # TODO: why
)
&& (!stdenv.hostPlatform.isMusl);
# Blank llvm dir just so relative path works
src = runCommand "${finalAttrs.pname}-src-${version}" { inherit (monorepoSrc) passthru; } ''
mkdir -p "$out"
cp -r ${monorepoSrc}/cmake "$out"
cp -r ${monorepoSrc}/mlir "$out"
cp -r ${monorepoSrc}/third-party "$out/third-party"
mkdir -p "$out/llvm"
'';
sourceRoot = "${finalAttrs.src.name}/mlir";
patches = [
./gnu-install-dirs.patch
# MLIRConfig.cmake unconditionally overwrites MLIR_TABLEGEN_EXE, breaking standalone
# builds that provide their own pre-built mlir-tblgen (e.g. in Nix sandboxed builds).
# The patch adds guards to respect caller-set values and auto-creates an imported
# mlir-tblgen target for downstream consumers. This replaces the previous dummy target
# workaround in flang's CMakeLists.txt.
# Upstream issue: https://github.com/llvm/llvm-project/issues/150986
./mlir-tablegen-imported-target.patch
]
++ lib.optional (lib.versionOlder release_version "20") [
# Fix build with gcc15
# https://github.com/llvm/llvm-project/commit/41eb186fbb024898bacc2577fa3b88db0510ba1f
# https://github.com/llvm/llvm-project/commit/101109fc5460d5bb9bb597c6ec77f998093a6687
(getVersionFile "mlir/mlir-add-include-cstdint.patch")
];
nativeBuildInputs = [
cmake
ninja
];
buildInputs = [
libllvm
libxml2
];
cmakeFlags = [
(lib.cmakeBool "LLVM_BUILD_TOOLS" true)
# Install headers as well
(lib.cmakeBool "LLVM_INSTALL_TOOLCHAIN_ONLY" false)
(lib.cmakeFeature "MLIR_TOOLS_INSTALL_DIR" "${placeholder "out"}/bin/")
(lib.cmakeBool "LLVM_ENABLE_IDE" false)
(lib.cmakeFeature "MLIR_INSTALL_PACKAGE_DIR" "${placeholder "dev"}/lib/cmake/mlir")
(lib.cmakeFeature "MLIR_INSTALL_CMAKE_DIR" "${placeholder "dev"}/lib/cmake/mlir")
(lib.cmakeBool "LLVM_BUILD_TESTS" finalAttrs.finalPackage.doCheck)
(lib.cmakeBool "LLVM_ENABLE_FFI" true)
(lib.cmakeFeature "LLVM_HOST_TRIPLE" stdenv.hostPlatform.config)
(lib.cmakeFeature "LLVM_DEFAULT_TARGET_TRIPLE" stdenv.hostPlatform.config)
(lib.cmakeBool "LLVM_ENABLE_DUMP" true)
(lib.cmakeFeature "LLVM_TABLEGEN_EXE" "${buildLlvmPackages.tblgen}/bin/llvm-tblgen")
(lib.cmakeFeature "MLIR_TABLEGEN_EXE" "${buildLlvmPackages.tblgen}/bin/mlir-tblgen")
(lib.cmakeBool "LLVM_BUILD_LLVM_DYLIB" (!stdenv.hostPlatform.isStatic))
]
++ lib.optionals stdenv.hostPlatform.isStatic [
# Disables building of shared libs, -fPIC is still injected by cc-wrapper
(lib.cmakeBool "LLVM_ENABLE_PIC" false)
(lib.cmakeBool "LLVM_BUILD_STATIC" true)
(lib.cmakeBool "LLVM_LINK_LLVM_DYLIB" false)
]
++ devExtraCmakeFlags;
outputs = [
"out"
"dev"
];
requiredSystemFeatures = [ "big-parallel" ];
meta = llvm_meta // {
homepage = "https://mlir.llvm.org/";
description = "Multi-Level IR Compiler Framework";
longDescription = ''
The MLIR project is a novel approach to building reusable and extensible
compiler infrastructure. MLIR aims to address software fragmentation,
improve compilation for heterogeneous hardware, significantly reduce
the cost of building domain specific compilers, and aid in connecting
existing compilers together.
'';
};
})