Out-of-tree CMake dependencies

Hi all,

In include-what-you-use, we recently had this bug report:

And it's symptomatic for a whole class of problems where we're
duplicating knowledge from the LLVM/Clang CMake systems to allow both
in-tree and out-of-tree IWYU development.

(in our terminology, in-tree == cloning the include-what-you-use repo
into a full LLVM and Clang checkout/clone under
llvm/tools/clang/tools/include-what-you-use and out-of-tree == cloning
include-what-you-use anywhere, and giving CMake information about
where to find LLVM/Clang headers and libraries)

Looking around my system, I see a lot of CMake modules in
/usr/lib/llvm-4.0 and /usr/share/llvm-4.0, but it's not clear to me
how to use them. Also, it would be nice to allow a "next-to-tree"
configuration instead of in-tree, where I can point IWYU at a trunk
checkout of LLVM and Clang and use its CMake modules.

Is there a correct way to accomplish this?

- Kim

This might help you get started (this is for out of tree pointing to my development build, but you could also point to the installed version if you have one).

if you only have/need LLVM:

export LLVM_DIR=/Users/dhinton/projects/llvm_project/build/Debug/lib/cmake/llvm

if you also have/need clang:

export CLANG_DIR=/Users/dhinton/projects/llvm_project/build/Debug/lib/cmake/clang

While just setting LLVM_DIR will make find_package work, you need to

add it to CMAKE_MODULE_PATH if you want to include AddLLVM, e.g., if

you want to use add_llvm_loadable_module.

find_package(Clang REQUIRED CONFIG)



Hi Don,

Thanks, that looks like it might work! I'll post back with results
when I have them.

- Kim

You might also want to look at clang’s CMakeLists.txt file. I can be build either in or out of tree (llvm tree), and handles a lot of this via llvm-config.


Hi all,

I now have a draft version that seems to be working:

I needed to add `include (HandleLLVMOptions)` to get CFLAGS/CXXFLAGS
in place, otherwise I was able to use your example almost verbatim.

Both the build and FreeBSD packaging seem to put the LLVM and Clang
CMake modules in $PREFIX/lib/cmake, but the apt packages for Ubuntu
seem to scatter them:

  $ find /usr -name "ClangConfig.cmake" -o -name "LLVMConfig.cmake" 2> /dev/null

The corresponding layout on FreeBSD is:

  $ find /usr/local -name "ClangConfig.cmake" -o -name
"LLVMConfig.cmake" 2> /dev/null

This makes the UX for IWYU's CMake painful, as we have to specify both
CMake search paths:

    $ cmake -DIWYU_LLVM_CMAKE_DIR=/usr/lib/llvm-4.0/cmake

Could this be improved in the packaging, or is there a benefit to
keeping them separate? Am I missing some trick to make system packages
easier to pick up?

- Kim


Maybe a little too much detail below. TL;DR; why are Clang CMake
modules for installed separately from LLVM CMake modules in the Debian

- Kim

+sylvestre -- you appear to be working on the Debian packaging of LLVM/Clang

Hey again,

I dug into the Debian packaging a bit more today, and it looks like
there have been changes to accommodate for the install location of
LLVMConfig.cmake, but not ClangConfig.cmake.

I think this might be a bug, but I don't feel confident enough/have a
clear enough problem report to file a package bug.

Could someone confirm/refute that it would make sense to install
LLVMConfig.cmake in /usr/lib/llvm-x.y/cmake/llvm and ClangConfig.cmake
in /usr/lib/llvm-x.y/cmake/clang?

- Kim