LLVM Discussion Forums

Cmake configuration for usage of libclang in a project

I have a simple C++ project which uses libclang to parse C++ source code. Currently my project uses the FindLLVM.cmake module to locate a local installation of LLVM (via llvm-config) on the system, and use it to compile/link.

I’d like to instead use LLVM as a git submodule in my project. Is there a cmake-ish way to indicate that I want to build and use libclang from the submodule in my repo, instead of from the system?

I was able to make this work by doing the following:

In the llvm submodule in my repo (named “llvm”), I do this:

mkdir build
cd build
cmake -GNinja -DLLVM_ENABLE_PROJECTS="clang" -DCMAKE_BUILD_TYPE=Release ../llvm
ninja libclang

Then the CMakeLists.txt file in my repo looks like this (at least the LLVM-specific portion):

find_package(LLVM REQUIRED CONFIG)

set(CLANG_INCLUDE_DIRS "llvm/clang/include")
set(CLANG_LIBS clang)

Then I can use the following CMake variables in various places to get my project to build:

  • LLVM_INCLUDE_DIRS
  • LLVM_LIBRARY_DIRS
  • LLVM_LD_FLAGS_STRING
  • CLANG_INCLUDE_DIRS
  • CLANG_LIBS

I’d like to find a way to avoid building libclang manually, and just have everything driven from the top-level CMakeLists.txt project in my repo, but I’m not sure how to make that happen.