[clang] [mlir] [CMake] Don't reference unexported targets from dependency-only configs (PR #222630)

Eugene Epshteyn via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 10 05:37:50 PDT 2026


https://github.com/eugeneepshteyn created https://github.com/llvm/llvm-project/pull/222630

#221724 stopped exporting a project's targets file when that project is enabled only as another project's dependency, but the build-tree `ClangConfig.cmake` and `MLIRConfig.cmake` still include it unconditionally. `find_package()` therefore finds the config and fails inside it:

```
CMake Error at build/lib/cmake/clang/ClangConfig.cmake:19 (include):
  include could not find requested file:
    build/lib/cmake/clang/ClangTargets.cmake
Call Stack (most recent call first):
  CMakeLists.txt:90 (find_package)
```

This breaks any configuration that enables Flang without also enabling Clang or MLIR explicitly: `runtimes/CMakeLists.txt` calls `find_package(Clang ...)`, which is deliberately not `REQUIRED` and is expected to tolerate an unavailable package — but a *half-generated* package is a hard error rather than a "not found". Concretely, `-DLLVM_ENABLE_PROJECTS=flang -DLLVM_ENABLE_RUNTIMES=flang-rt` fails at `runtimes-configure`, which is the configuration premerge uses for flang-rt changes.

This patch guards the include the same way the export itself is guarded, so a dependency-only project generates a config without a dangling include. The install-tree path already handles this through `get_config_exports_includes()`; only the build-tree path was missed.

MLIR has the same defect and is fixed here too. It is currently latent only because `runtimes/CMakeLists.txt` calls `find_package(Clang)` and not `find_package(MLIR)`; a `find_package(MLIR)` against a dependency-only build tree fails identically at `MLIRConfig.cmake:33`.

**Verification** (configure-only, no build needed to reproduce):

Configuring with `-DLLVM_ENABLE_PROJECTS=flang -DLLVM_ENABLE_RUNTIMES=flang-rt` and then running `find_package` against the generated build tree, exactly as `runtimes/CMakeLists.txt` does:

| | `ClangConfig` | `ClangTargets` | config includes targets | `find_package(Clang)` | `find_package(MLIR)` |
|---|---|---|---|---|---|
| before | present | absent | yes | **error** | **error** |
| after | present | absent | no | ok | ok |

Control, with `-DLLVM_ENABLE_PROJECTS='clang;mlir;flang'` (both explicitly enabled): targets files are exported and the configs include them, as before — unchanged by this patch.

An alternative fix would be to emit `include(... OPTIONAL)`, matching the distribution path in `LLVMDistributionSupport.cmake`; I chose the explicit guard because it mirrors the two existing `LLVM_DEPENDENCY_ONLY_PROJECTS` checks in the same files. Happy to switch if you prefer the other form.


>From 27a3f3c279130f98ebd1a344a11909c6d7467a0c Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Thu, 10 Sep 2026 05:35:08 -0700
Subject: [PATCH] [CMake] Don't reference unexported targets from
 dependency-only configs

When a project is enabled only to satisfy another project's dependency, its
targets file is no longer exported, but the build-tree ClangConfig.cmake and
MLIRConfig.cmake still included it unconditionally. find_package() then found
the config and failed inside it:

  CMake Error at build/lib/cmake/clang/ClangConfig.cmake:19 (include):
    include could not find requested file:
      build/lib/cmake/clang/ClangTargets.cmake

This breaks configurations that enable Flang without also enabling Clang or
MLIR explicitly, because runtimes/CMakeLists.txt calls find_package(Clang),
which is not REQUIRED and is expected to tolerate an unavailable package.

Guard the include the same way the export itself is guarded, so a
dependency-only project generates a config without a dangling include.
---
 clang/cmake/modules/CMakeLists.txt | 6 +++++-
 mlir/cmake/modules/CMakeLists.txt  | 6 +++++-
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/clang/cmake/modules/CMakeLists.txt b/clang/cmake/modules/CMakeLists.txt
index 845250e4e0fd9..1dde1a0075fa9 100644
--- a/clang/cmake/modules/CMakeLists.txt
+++ b/clang/cmake/modules/CMakeLists.txt
@@ -30,7 +30,11 @@ endif()
 # Generate ClangConfig.cmake for the build tree.
 set(CLANG_CONFIG_CMAKE_DIR "${clang_cmake_builddir}")
 set(CLANG_CONFIG_LLVM_CMAKE_DIR "${llvm_cmake_builddir}")
-set(CLANG_CONFIG_INCLUDE_EXPORTS "include(\"${clang_cmake_builddir}/ClangTargets.cmake\")")
+if(NOT "clang" IN_LIST LLVM_DEPENDENCY_ONLY_PROJECTS)
+  set(CLANG_CONFIG_INCLUDE_EXPORTS "include(\"${clang_cmake_builddir}/ClangTargets.cmake\")")
+else()
+  set(CLANG_CONFIG_INCLUDE_EXPORTS "")
+endif()
 set(CLANG_CONFIG_INCLUDE_DIRS
   "${CLANG_SOURCE_DIR}/include"
   "${CLANG_BINARY_DIR}/include"
diff --git a/mlir/cmake/modules/CMakeLists.txt b/mlir/cmake/modules/CMakeLists.txt
index 7030666ed927c..cf8f430c827c6 100644
--- a/mlir/cmake/modules/CMakeLists.txt
+++ b/mlir/cmake/modules/CMakeLists.txt
@@ -52,7 +52,11 @@ get_property(MLIR_CAPI_LIBS GLOBAL PROPERTY MLIR_CAPI_LIBS)
 # Generate MlirConfig.cmake for the build tree.
 set(MLIR_CONFIG_CMAKE_DIR "${mlir_cmake_builddir}")
 set(MLIR_CONFIG_LLVM_CMAKE_DIR "${llvm_cmake_builddir}")
-set(MLIR_CONFIG_INCLUDE_EXPORTS "include(\"\${MLIR_CMAKE_DIR}/MLIRTargets.cmake\")")
+if(NOT "mlir" IN_LIST LLVM_DEPENDENCY_ONLY_PROJECTS)
+  set(MLIR_CONFIG_INCLUDE_EXPORTS "include(\"\${MLIR_CMAKE_DIR}/MLIRTargets.cmake\")")
+else()
+  set(MLIR_CONFIG_INCLUDE_EXPORTS "")
+endif()
 set(MLIR_CONFIG_INCLUDE_DIRS
   "${MLIR_SOURCE_DIR}/include"
   "${MLIR_BINARY_DIR}/include"



More information about the cfe-commits mailing list