以 LLVM 项目作为子目录的 CMake 构建

CMake build with LLVM Project as a subdirectory

TLDR

我想将 LLVM 项目 monorepo 构建为更大的 CMake 项目的子目录。但是 add_subdirectory(llvm-project/llvm) 的通常范例并没有给出预期的结果。如何将 LLVM 添加到我的 CMake 项目中,以便构建项目时也构建 LLVM?

背景

我的项目树看起来像这样(为简洁起见省略了一些文件和子目录):

.
├── build/
├── CMakeLists.txt
├── cunit/
│   ├── CUnit/
│   │   └── CMakeLists.txt
│   └── CMakeLists.txt
├── hayai/
│   └── CMakeLists.txt
├── install/
├── llvm-project/
│   ├── clang/
│   ├── clang-tools-extra/
│   ├── compiler-rt/
│   ├── debuginfo-tests/
│   ├── libcxx/
│   ├── libcxxabi/
│   ├── libunwind/
│   ├── lld/
│   ├── lldb/
│   ├── llvm/
│   │   └── CMakeLists.txt
│   ├── openmp/
│   └── polly/
└── mylib/
    └── CMakeLists.txt

如您所见,我有四个子目录,它们本身都是 CMake 项目,并且都可以单独构建,没有任何问题。作为我研究的一部分,CUnit and Hayai are unmodified. Mylib is the product of my research, and uses CUnit and Hayai as git submodules/dependencies. I have also modified the LLVM Project monorepo 都使用 Mylib 进行检测,但它是独立构建的。我想将所有这四个子项目统一为一个 CMake 驱动的构建。

顶层CMakeLists.txt的内容是:

cmake_minimum_required(VERSION 3.2)
project (myproject)
set(CMAKE_BUILD_TYPE Debug)

add_subdirectory(cunit/CUnit)
add_subdirectory(hayai)
add_subdirectory(mylib)
add_subdirectory(llvm-project/llvm)

问题

当我尝试使用以下命令生成构建时:

cd build
cmake .. -DCMAKE_INSTALL_PREFIX=../install -DCMAKE_BUILD_TYPE=Debug -DBUILD_HAYAI_SAMPLES=false -DBUILD_HAYAI_TESTS=false

当 CMake 递归到 LLVM 子项目(为简洁起见缩短)时,我从 CMake 收到错误消息:

-- LLVM host triple: x86_64-unknown-linux-gnu
-- LLVM default target triple: x86_64-unknown-linux-gnu
-- Building with -fPIC
-- Constructing LLVMBuild project information
-- Linker detection: GNU ld
CMake Error: File ./llvm-project/llvm/include/llvm/module.modulemap.build does not exist.
CMake Error at ./llvm-project/llvm/include/llvm/CMakeLists.txt:7 (configure_file):
  configure_file Problem configuring file


-- Targeting AArch64
-- Targeting AMDGPU
-- Targeting ARM
-- Targeting BPF
-- Targeting Hexagon
-- Targeting Lanai
-- Targeting Mips
-- Targeting MSP430
-- Targeting NVPTX
-- Targeting PowerPC
-- Targeting Sparc
-- Targeting SystemZ
-- Targeting X86
-- Targeting XCore
-- Configuring incomplete, errors occurred!
See also "./build/CMakeFiles/CMakeOutput.log".
See also "./build/CMakeFiles/CMakeError.log".

llvm-project/llvm/include/llvm/CMakeLists.txt 包含:

add_subdirectory(IR)
add_subdirectory(Support)

# If we're doing an out-of-tree build, copy a module map for generated
# header files into the build area.
if (NOT "${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_BINARY_DIR}")
  configure_file(module.modulemap.build module.modulemap COPYONLY)
endif (NOT "${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_BINARY_DIR}")

我检查了输出末尾的两个文件,但都没有与问题相关的消息。

问题

可能相关

CMake 错误的真正含义是什么?

@Tsyvarev 正确地确定问题实际上是 llvm-project/llvm/include/llvm/module.modulemap.build 不存在。这是因为我通过 Github 下载了该项目的存档,默认情况下似乎已排除 .build 文件。克隆或分叉项目时,存在丢失的文件。

问题是可重现的,但我不知道为什么 Github 会这样。这超出了问题的范围。

我该如何解决这个问题,以便我的顶级项目可以构建所有四个子项目?

在顶层 CMakeLists.txt 文件中使用 add_subdirectory(llvm-project/llvm) 命令就足够了,但当然你还必须 specify LLVM's options 在生成时。