Matthew Cole
Matthew Cole

Reputation: 597

CMake build with LLVM Project as a subdirectory

TLDR

I want to build the LLVM project monorepo as a subdirectory to a larger CMake project. But the usual paradigm of add_subdirectory(llvm-project/llvm) doesn't give the desired results. How can I add LLVM to my CMake project so that building the project also builds LLVM?

Background

I have project tree that looks like this (some files and subdirectories omitted for brevity) :

.
├── 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

As you can see, I have four subdirectories, all of which are CMake projects in their own right, and all of which build individually with no problems. Both 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 to use Mylib for instrumentation as part of my research, but it builds on its own. I would like to unify all four of these subprojects as one CMake-driven build.

The content of the top-level CMakeLists.txt is:

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)

Problem

When I try to generate the build using the following command:

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

I get an error from CMake once it recurses into the LLVM subproject (shortened for brevity):

-- 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 contains:

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}")

I checked the two files at the end of the output, but neither had messages relevant to the problem.

Questions

Potentially Related

Upvotes: 2

Views: 1628

Answers (1)

Matthew Cole
Matthew Cole

Reputation: 597

What does the error from CMake really mean?

@Tsyvarev correctly identified that the problem was in fact that llvm-project/llvm/include/llvm/module.modulemap.build didn't exist. This was because I downloaded an archive of the project through Github, which appears to have excluded .build files by default. When cloning or forking the project, the missing files are present.

The problem is reproducible, but I don't know why Github behaves the way it does. That is beyond the scope of the question.

How can I fix this so that my top level project can build all four subprojects?

Using add_subdirectory(llvm-project/llvm) command in the top-level CMakeLists.txt file is sufficient, but you must also specify LLVM's options at generate time, of course.

Upvotes: 1

Related Questions