Embedding llvm as a git submodule in Project

Hi all.

I want to use llvm in my project and I want to make llvm a git submodule in my project.

http://llvm.org/docs/CMake.html#embedding-llvm-in-your-project

At this in the documentation it claims to describe how to embed llvm into a project. I tried it that way but it doesn't work, because there isn't any findLLVM.cmake in the llvm/cmake/modules directory anymore (i don't know if there was one in the past).

Can someone tell me how to do it now.

Timo Janssen

Hi Timo:

Please take a look at cmake’s find_package documentation for a full description on how it works, but in general, find_package(LLVM) will look for a file named LLVMConfig.cmake, not findLLVM.cmake.

However, unless that file is in the well known search path used by find_package, you’ll need to add the path to CMAKE_MODULE_PATH.

hth…
don

Hi,

I am sorry but I really am not good with cmake yet.

I made one mistake I used MODULE instead of CONFIG in find_package.

But even with CONFIG I doesn't work for me.

Here my CMakeLists.txt:

cmake_minimum_required(VERSION 3.5.1)
project(SimpleProject)

find_package(LLVM REQUIRED CONFIG PATHS "${CMAKE_CURRENT_SOURCE_DIR}/llvm/cmake$

message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")

include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})

add_executable(simple-tool main.cpp)

llvm_map_components_to_libnames(llvm_libs support)

target_link_libraries(simple-tool ${llvm_libs})

Here part of the cmake output in terminal:

CMake Error at CMakeLists.txt:4 (find_package):
   Could not find a package configuration file provided by "LLVM" with any of
   the following names:

     LLVMConfig.cmake
     llvm-config.cmake

   Add the installation prefix of "LLVM" to CMAKE_PREFIX_PATH or set
   "LLVM_DIR" to a directory containing one of the above files. If "LLVM"
   provides a separate development package or SDK, be sure it has been
   installed.

-- Configuring incomplete, errors occurred!
See also "/home/timoo/Schreibtisch/build/CMakeFiles/CMakeOutput.log"

I added the PATHS to find_package becouse the command must find the files.

In the llvm/cmake/modules dircotry are:

LLVM-Config.cmake (instead of lower case) and

LLVMConfig.cmake.in (.in added at end)

So what must I do?

Hi Timo:

You need to find LLVMConfig.cmake in the binary or install directory tree, not the source tree.

Although I don’t embed clang/llvm, my config might help you figure it – clang/llvm installed under (could have used build directory) ~/usr :

$ cd /Users/dhinton/projects/cover/build/ && rm -rf * && CC=~/usr/bin/clang CXX=~/usr/bin/clang++ LLVM_DIR=~/usr/lib/cmake/llvm cmake …/CVRFI/ -GNinja

$ cat …/CVRFI/CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

Make Debug the default, pass -DCMAKE_BUILD_TYPE=Release to change this.

if (NOT CMAKE_BUILD_TYPE)
message (“Setting CMAKE_BUILD_TYPE = Debug”)
set(CMAKE_BUILD_TYPE Debug)
endif()

if(APPLE)
set(CMAKE_MACOSX_RPATH ON)
set(CMAKE_MODULE_LINKER_FLAGS “${CMAKE_MODULE_LINKER_FLAGS} -undefined dynamic_lookup”)
set(CMAKE_SHARED_LINKER_FLAGS “${CMAKE_SHARED_LINKER_FLAGS} -undefined dynamic_lookup”)
endif()

set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -std=c++11”)

set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -Wall -Wextra -Wno-unused-parameter”)

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.

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} “$ENV{LLVM_DIR}”)
find_package(LLVM REQUIRED CONFIG)
include(AddLLVM)
add_definitions(${LLVM_DEFINITIONS})
include_directories(${LLVM_INCLUDE_DIRS})
link_directories(${LLVM_LIBRARY_DIRS})

Hi Don,

thanks, but that isn’t completely what I want.

THe way you explain it, I have to first build llvm an install and then seperately my project.

But that isn’t what I want. I want to build it together as one thing. In the Rust Compiler they have llvm as a submodule in src folder. I don’t know how they do it but they don’t use CMake so it won’t really help to look it up. And I want to only build those llvm libraries I will need for my project.

Sorry my example wasn’t helpful.

I did take a quick look at rust – though I didn’t download or try to build it – and they seem to allow you to use an installed or prebuilt version in addition to building it in-tree. However, even when they build it in-tree, they build the whole thing – see mk/llvm.mk.

Good luck…

I see. Thanks again.

THen I will look how I best do it.

But it would be good if it would be poosible if I could build llvm in-tree and only build those parts I really need. TO reduce build time.

But this way will probably be good enaugh. Many thanks.

Timo

Hi Timo,
You don't have to run through the entire install before it will work. You
just need to be able to locate an LLVMConfig.cmake, which is generated from
the configure step. After the initial cmake configure pass, you should
have a ./lib/cmake/llvm/LLVMConfig.cmake in the llvm build directory. If
you look at the CMake docs for find_package, you can see it's search
heuristics and that if you set the CMake variable LLVM_DIR to
/path/to/llvm-build-dir/lib/cmake/llvm/ then find_package should work.

- Chuck

Timo, if you want to have LLVM in-tree with your project, and your project builds with CMake, all you should need to do is call “add_subdirectory(llvm)” in the CMakeLists.txt file at the directory where the submodule gets checked out.

-Chris

That way it works. I Found it in the cmake docs yesterday and through a google search but I put the wrong path in my CMakeLists.txt.
Now everything works with find_package.

Thanks all.