CMAKE and generator expressions in imported libs
Not sure where the problem is (sage or CMake). In short, I am seeing a cmake error when using the $<COMPILER_LANGUAGE>
generator expression in a target_include_directories()
statement of a target that gets exported, and the using project includes the generated export module. Perhaps you can help me, or point me in the right direction?
Peeling the onion, it turns out that when generating an export project (via install(EXPORT)
), cmake converts all library targets into imported targets (in the generated cmake export module file). If one of the original targets uses $<COMPILER_LANGUAGE>
in target_include_directories()
, the problem arises when using the installed export in a consuming project.
The root-cause is that for imported libraries (add_library( ... IMPORTED ...)
), the $<COMPILER_LANGUAGE>
generator expression cannot be used in the INTERFACE_INCLUDE_DIRECTORIES
property. If one tries, and the project is added to another target using target_link_libraries()
, cmake fails with
CMake Error:
Error evaluating generator expression:
$<COMPILE_LANGUAGE:CXX>
$<COMPILE_LANGUAGE:...> may only be used to specify include directories
compile definitions, compile options and to evaluate components of the
file(GENERATE) command.
Not sure why that is, and it wouldn’t necessarily be a problem for me, if the import lib wouldn’t be the result of the install(EXPORT)
.
Btw., it works with $<COMPILER_LANGUAGE>
present in just the INTERFACE_COMPILE_OPTIONS
. Just not in INTERFACE_INCLUDE_DIRECTORIES
.
Repro of the final error (a.c, b.cpp, inccxx_dir/ present and empty):
cmake_minimum_required(VERSION 3.5.1)
add_library(lib
STATIC IMPORTED # fails
# INTERFACE # succeeds
)
set_target_properties(lib PROPERTIES
INTERFACE_COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:CXX>:-nostdinc++>"
INTERFACE_INCLUDE_DIRECTORIES "$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_SOURCE_DIR}/inccxx_dir>"
)
add_library(src STATIC a.c b.cpp)
target_link_libraries(src lib)