This is an archive of the discontinued LLVM Phabricator instance.

[llvm][cmake] Fix add_subdirectory build in multi-config
ClosedPublic

Authored by sebastian-ne on Dec 8 2022, 4:58 AM.

Details

Summary

Using CMAKE_CFG_INTDIR in paths that are used in configure_file,
resulted in a folder that is literally called '${CONFIGURATION}'
for the multi-config ninja build.

I think this is a regression from a while ago. Fix this by replacing
CMAKE_CFG_INTDIR with '.'. We can only create one of the
LLVMConfig.cmake files as the consuming CMake project can only import a
single file. This creates LLVMConfig.cmake and others in the place where
they were previously and where they are for a single-config build.

Diff Detail

Event Timeline

sebastian-ne created this revision.Dec 8 2022, 4:58 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 8 2022, 4:58 AM
sebastian-ne requested review of this revision.Dec 8 2022, 4:58 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 8 2022, 4:58 AM

I still get the ${CONFIGURATION} directory even with this patch, running:

cmake -G"Ninja Multi-Config" -DLLVM_ENABLE_PROJECTS="clang;lld;clang-tools-extra" -DLLVM_TARGETS_TO_BUILD="X86;AMDGPU" ../llvm

I still get the ${CONFIGURATION} directory even with this patch

This is caused by set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_TOOLS_BINARY_DIR} ) in llvm/CMakeLists.txt:L963.
If I understand it correctly, set_output_directory() sets the individual output directories for targets to sensible directories and the generic CMAKE_RUNTIME_OUTPUT_DIRECTORY isn’t used.

I see this patch as a band-aid. Long-term, we want to get rid of CMAKE_CFG_INTDIR and use generator expressions.

jsilvanus accepted this revision.EditedDec 19 2022, 12:31 PM

Ok, fine with me.

For completeness, the patch avoids the creation of mentioned dir under some but not all circumstances?

This revision is now accepted and ready to land.Dec 19 2022, 12:31 PM

For completeness, the patch avoids the creation of mentioned dir under some but not all circumstances?

Yes, I didn’t see a ${CONFIGURATION} dir in a build where llvm was added with add_subdirectory.
More importantly, LLVMConfig.cmake and others are not created inside that directory.

nhat-nguyen added a subscriber: nhat-nguyen.EditedJan 11 2023, 9:43 AM

@sebastian-ne I'm currently experiencing an mlir build issue after this change with the following error:

CMake Error at D:/a/_work/1/b/llvm/lib/cmake/mlir/MLIRConfig.cmake:6 (find_package):
  Could not find a package configuration file provided by "LLVM" (requested
  version 16.0.0) with any of the following names:

    LLVMConfig.cmake
    llvm-config.cmake

  Add the installation prefix of "LLVM" to CMAKE_PREFIX_PATH or set
  "LLVM_DIR" to a directory containing one of the above files.  If "LLVM"
  provides a separate development package or SDK, be sure it has been
  installed.
Call Stack (most recent call first):
  MLIR.cmake:13 (find_package)
  CMakeLists.txt:84 (include)

I believe this is due to llvm_cmake_builddir not being set correctly for mlir when CMAKE_CFG_INTDIR is $(Configuration). Here's my attempt at fixing the issue -- I'm not particularly well-versed in cmake, so I would very much appreciate if you could take a quick look:

From a601cd27cfdeea2d5a7e37091fe73728c78c9ee3 Mon Sep 17 00:00:00 2001
From: Nhat Nguyen <honguye@microsoft.com>
Date: Tue, 10 Jan 2023 14:07:00 -0500
Subject: [PATCH] Attempted fix

---
 llvm-project/mlir/cmake/modules/CMakeLists.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm-project/mlir/cmake/modules/CMakeLists.txt b/llvm-project/mlir/cmake/modules/CMakeLists.txt
index 3f15c2d5cd0b..9d4d51c7a6e1 100644
--- a/llvm-project/mlir/cmake/modules/CMakeLists.txt
+++ b/llvm-project/mlir/cmake/modules/CMakeLists.txt
@@ -15,7 +15,8 @@ set(mlir_cmake_builddir "${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/mlir
 set(LLVM_INSTALL_PACKAGE_DIR "${CMAKE_INSTALL_PACKAGEDIR}/llvm" CACHE STRING
   "Path for CMake subdirectory for LLVM (defaults to '${CMAKE_INSTALL_PACKAGEDIR}/llvm')")
 # CMAKE_INSTALL_PACKAGEDIR might be absolute, so don't reuse below.
-set(llvm_cmake_builddir "${LLVM_LIBRARY_DIR}/cmake/llvm")
+string(REPLACE "${CMAKE_CFG_INTDIR}" "." llvm_cmake_builddir  "${LLVM_LIBRARY_DIR}")
+set(llvm_cmake_builddir "${llvm_cmake_builddir}/cmake/llvm")
 
 get_property(MLIR_EXPORTS GLOBAL PROPERTY MLIR_EXPORTS)
 export(TARGETS ${MLIR_EXPORTS} FILE ${mlir_cmake_builddir}/MLIRTargets.cmake)
-- 
2.34.0.windows.1

Thank you!

That looks good to me. I guess the same change applies to the duplicated code in flang, polly, lld and clang.