samvv
samvv

Reputation: 2134

Best way to add include directories of root project to a subdirectory in CMake

I have a project with a CMakeLists.txt at the root, which includes a project in a subdirectory test/ using add_subdirectory with the flag EXCLUDE_FROM_ALL. The tests need all of the include directories of the parent project. What would be the most elegant way to do this?

CMakeLists.txt

CMAKE_MINIMUM_REQUIRED(VERSION 3.1)
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules")

PROJECT(autoffi CXX)

SET(CMAKE_CXX_STANDARD 11)

SET(AutoFFI_VERSION_MAJOR 0)
SET(AutoFFI_VERSION_MINOR 1)

SET(Boost_USE_STATIC_LIBS OFF)
FIND_PACKAGE(BOOST 1.50 COMPONENTS system filesystem REQUIRED) 

FIND_PACKAGE(LLVM 3.9 REQUIRED CONFIG)
ADD_DEFINITIONS(${LLVM_DEFINITIONS})

FIND_PACKAGE(Clang REQUIRED)

CONFIGURE_FILE(
  "${PROJECT_SOURCE_DIR}/include/env.h.in"
  #"${PROJECT_BINARY_DIR}/include/env.h"
  "${PROJECT_SOURCE_DIR}/include/env.h" # added to VCS
)

INCLUDE_DIRECTORIES(
  #"${PROJECT_BINARY_DIR}"
  "${PROJECT_SOURCE_DIR}/include"
  "${CLANG_INCLUDE_DIRS}"
  "${Boost_INCLUDE_DIRS}"
)

# The C++ interface
ADD_LIBRARY(autoffiCore src/Type.cpp)
# FIXME: boost package finder seems to be broken
TARGET_LINK_LIBRARIES(autoffiCore boost_filesystem boost_system)
ADD_LIBRARY(autoffiBin src/BinFormat.cpp)
TARGET_LINK_LIBRARIES(autoffiBin autoffiCore)
ADD_LIBRARY(autoffiClang src/ClangEngine.cpp)
llvm_map_components_to_libnames(llvm_libs all)
TARGET_LINK_LIBRARIES(autoffiClang autoffiCore ${llvm_libs} ${CLANG_LIBS})

# The C interface
ADD_LIBRARY(autoffi src/PrettyPrinter.cpp src/libautoffi.cpp)
TARGET_LINK_LIBRARIES(autoffi autoffiBin autoffiClang)

set_target_properties(autoffiCore autoffiBin autoffiClang autoffi PROPERTIES DEFINE_SYMBOL BUILDING_SHARED)

# CLI Tools

ADD_EXECUTABLE(afdump src/dump.cpp src/PrettyPrinter.cpp)
TARGET_LINK_LIBRARIES(afdump autoffiBin)

ADD_EXECUTABLE(afconvert src/convert.cpp)
TARGET_LINK_LIBRARIES(afconvert autoffiBin)

ADD_EXECUTABLE(afcompile src/tooling.cpp src/PrettyPrinter.cpp)
TARGET_LINK_LIBRARIES(afcompile autoffiClang ${CLANG_LIBS} autoffiBin)

# Testing

ENABLE_TESTING()
ADD_TEST(headerASTDump afcompile test/assets/basic.h)

test/CMakeLists.txt

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

project("AutoFFI Tests")

SET(CMAKE_CXX_STANDARD 11)

SET(GTEST_ROOT "${CMAKE_CURRENT_LIST_DIR}/gtest")
find_package(GTest REQUIRED)
INCLUDE_DIRECTORIES(${AUTOFFI_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS})

ADD_EXECUTABLE(autoffiTest libautoffi.cpp core.cpp formats.cpp clang.cpp)
TARGET_LINK_LIBRARIES(autoffiTest ${GTEST_BOTH_LIBRARIES})
SET_TARGET_PROPERTIES(autoffiTest PROPERTIES OUTPUT_NAME runtests)

Upvotes: 3

Views: 6341

Answers (2)

Antonio
Antonio

Reputation: 20256

You are keeping your main directory and your subdirectory test in completely separated projects. I suppose you are building test by invoking separately cmake and make a second time. No surprise that the include_directories of your main project as no impact on the test project.

You mention the command add_subdirectory, (that should be used to include test in from your main directory) but you are actually not using it...

Upvotes: 1

renefritze
renefritze

Reputation: 2223

If you're using include_directories() to add dirs, those should propagate down into targets in the test subdir. Beware though that relative paths are relative to the current source dir:

include_directories: Add include directories to the build. include_directories([AFTER|BEFORE] [SYSTEM] dir1 dir2 ...) Add the given directories to those the compiler uses to search for include files. Relative paths are interpreted as relative to the current source directory.

cmake docs

Upvotes: 1

Related Questions