llvm in a subdirectory with cmake

Hi All,

The proposed patch makes possible to use LLVM’s CMake build infrastructure (most importantly add_llvm_library and add_llvm_executable macros) from the project that has llvm in a subdirectory.

For example:

demo/
llvm/
demo.cpp
CMakeLists.txt

where CMakeLists.txt is as follows:

----------------------------- Start of CMakeLists.txt ----------------------------------------

project(DEMO)
cmake_minimum_required(VERSION 2.6.1)

add_subdirectory("${DEMO_SOURCE_DIR}/llvm")

include_directories(
“${LLVM_SOURCE_DIR}/include”
“${LLVM_BINARY_DIR}/include”
)
set(LLVM_LINK_COMPONENTS core jit interpreter native)

add_llvm_executable(DEMO demo.cpp)

----------------------------- End of CMakeLists.txt ----------------------------------------

As you can see with this patch creating a project using CMake outside of the LLVM directory structure is as simple as inside.

To this end I made the following CMake variables stored in cache:

LLVM_COMMON_DEPENDS
LLVM_NATIVE_ARCH
llvm_libs
MSVC_LIB_DEPS_LLVM*

Best regards,
Victor

0001-Save-several-cmake-variables-to-cache-to-support-llv.patch (20.3 KB)

Victor Zverovich <victor.zverovich@googlemail.com> writes:

The proposed patch makes possible to use LLVM's CMake build infrastructure
(most importantly add_llvm_library and add_llvm_executable macros) from the
project that has llvm in a subdirectory.

For example:

demo/
    llvm/
    demo.cpp
    CMakeLists.txt

where CMakeLists.txt is as follows:

# ----------------------------- Start
of CMakeLists.txt ----------------------------------------

project(DEMO)
cmake_minimum_required(VERSION 2.6.1)

add_subdirectory("${DEMO_SOURCE_DIR}/llvm")

include_directories(
  "${LLVM_SOURCE_DIR}/include"
  "${LLVM_BINARY_DIR}/include"
)
set(LLVM_LINK_COMPONENTS core jit interpreter native)

add_llvm_executable(DEMO demo.cpp)

It is my intention to implement something similar to this on a cleaner
way. Using CACHE INTERNAL here and there is a recipe for maintenance
problems. My plan consists on configuring the client application with
-DLLVM=/path and then simply call functions for obtaining the list of
LLVM libraries for certain components, etc:

add_executable(foo foo.cpp)
llvm_libraries(MYVAR jit interpreter native)
add_target_dependencies(foo ${MYVAR})