【问题标题】:CMake build with LLVM Project as a subdirectory使用 LLVM 项目作为子目录的 CMake 构建
【发布时间】:2019-07-31 20:18:34
【问题描述】:

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 项目,并且所有这些子目录都是单独构建的,没有任何问题。 CUnitHayai 均未修改。 Mylib 是我研究的产物,使用 CUnit 和 Hayai 作为 git 子模块/依赖项。作为我研究的一部分,我还修改了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 的错误的真正含义是什么?为什么 module.modulemap.build 不存在,或者更准确地说,为什么只构建 LLVM 时它存在?
  • 如何解决此问题,以便我的顶级项目可以构建所有四个子项目?

可能相关

  • 这个StackOverflow question 使用许多相同的关键字提出了类似的问题,但考虑到了不同的任务:她/他希望从现有 LLVM 构建链接到库,而不是构建 LLVM 本身。
  • 同样,这个StackOverflow question 和接受的答案对这个LLVM documentation page 的引用是关于将LLVM 库从树中嵌入到一个独立的项目中。这不是我想要的,因为我需要构建整个修改后的 LLVM 编译器工具链。
  • 这个llvm-dev mailing list thread 听起来正是我正在做的,但建议的答案不起作用,如上所述。

【问题讨论】:

  • "CMake 的错误到底是什么意思?" - 消息File ./llvm-project/llvm/include/llvm/module.modulemap.build does not exist 显然意味着给定文件不存在或CMake 由于某种原因无法访问它。您是否检查过此文件在您的机器上是否存在? (它应该在您向我们展示的llvm-project/llvm/include/llvm/CMakeLists.txt 脚本附近)。
  • 我认为 LLVM 本身就是一个 CMake 项目,所以我不知道它是否准备好成为一个简单的“子目录”。最重要的是,LLVM 有很多 CMake 机器,这可能会使解决这个问题变得非常困难。您是否考虑在 LLVM CMake 中添加您的项目?您知道您可以使用 CMake External Projects 隔离其他 CMake 项目吗?
  • 只是一个疯狂的猜测:您可以尝试将其添加为add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/llvm-project/llvm)。这是我从我的项目中看到的唯一区别,它工作得很好。另外,您使用哪个版本/提交的 LLVM?
  • @AlexDenisov 我试过了,但“module.modulemap.build 不存在”错误仍然存​​在。不过感谢您的建议!
  • 所以文件./llvm-project/llvm/include/llvm/module.modulemap.build实际上不存在。这本身就很奇怪,因为文件 exists in the repo.请注意,configure_file 不会在 in-source builds 的情况下被调用(当 CMAKE_SOURCE_DIR 等于 CMAKE_BINARY_DIR 时)。 “它在指定路径生成后存在,并且CMake已准备好构建。” - 你确定你说的是 module.modulemap.build 文件,而不是 module.modulemap 一个,它也存在于 repo 中?

标签: cmake llvm


【解决方案1】:

来自 CMake 的错误究竟意味着什么?

@Tsyvarev 正确识别出问题实际上是 llvm-project/llvm/include/llvm/module.modulemap.build 不存在。这是因为我通过 Github 下载了项目的存档,默认情况下似乎排除了 .build 文件。克隆或派生项目时,缺少的文件存在。

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多