90 lines
2.8 KiB
CMake
Raw Normal View History

2013-11-03 12:27:13 +02:00
2023-07-02 19:51:09 +02:00
cmake_minimum_required(VERSION 3.5)
2013-11-03 12:27:13 +02:00
project(SystemIncludeDirectories)
add_library(systemlib systemlib.cpp)
target_include_directories(systemlib PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/systemlib")
2021-11-20 13:41:27 +01:00
function (apply_error_flags target)
if (MSVC)
target_compile_options(${target} PRIVATE /we4101)
else ()
target_compile_options(${target} PRIVATE -Werror=unused-variable)
endif ()
endfunction ()
2013-11-03 12:27:13 +02:00
add_library(upstream upstream.cpp)
target_link_libraries(upstream LINK_PUBLIC systemlib)
2021-11-20 13:41:27 +01:00
apply_error_flags(upstream)
2013-11-03 12:27:13 +02:00
target_include_directories(upstream SYSTEM PUBLIC
$<TARGET_PROPERTY:systemlib,INTERFACE_INCLUDE_DIRECTORIES>
)
2015-04-27 22:25:09 +02:00
add_library(config_specific INTERFACE)
2022-08-04 22:12:04 +02:00
get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(isMultiConfig)
2017-04-14 19:02:05 +02:00
# CMAKE_BUILD_TYPE does not work here for multi-config generators
target_include_directories(config_specific SYSTEM INTERFACE
"${CMAKE_CURRENT_SOURCE_DIR}/config_specific"
)
else()
set(testConfig ${CMAKE_BUILD_TYPE})
target_include_directories(config_specific SYSTEM INTERFACE
"$<$<CONFIG:${testConfig}>:${CMAKE_CURRENT_SOURCE_DIR}/config_specific>"
)
endif()
2015-04-27 22:25:09 +02:00
2013-11-03 12:27:13 +02:00
add_library(consumer consumer.cpp)
2015-04-27 22:25:09 +02:00
target_link_libraries(consumer upstream config_specific)
2021-11-20 13:41:27 +01:00
apply_error_flags(consumer)
2014-08-03 19:52:23 +02:00
add_library(iface IMPORTED INTERFACE)
2018-04-23 21:13:27 +02:00
set_property(TARGET iface PROPERTY INTERFACE_INCLUDE_DIRECTORIES
"$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_CURRENT_SOURCE_DIR}/systemlib_header_only>"
)
2014-08-03 19:52:23 +02:00
add_library(imported_consumer imported_consumer.cpp)
target_link_libraries(imported_consumer iface)
2021-11-20 13:41:27 +01:00
apply_error_flags(imported_consumer)
2014-08-03 19:52:23 +02:00
add_library(imported_consumer2 imported_consumer.cpp)
target_link_libraries(imported_consumer2 imported_consumer)
2021-11-20 13:41:27 +01:00
apply_error_flags(imported_consumer2)
2014-08-03 19:52:23 +02:00
2015-11-17 17:22:37 +01:00
# add a target which has a relative system include
add_library(somelib imported_consumer.cpp)
target_include_directories(somelib SYSTEM PUBLIC "systemlib_header_only")
2021-11-20 13:41:27 +01:00
apply_error_flags(somelib)
2015-11-17 17:22:37 +01:00
# add a target which consumes a relative system include
add_library(otherlib upstream.cpp)
target_link_libraries(otherlib PUBLIC somelib)
2021-11-20 13:41:27 +01:00
apply_error_flags(otherlib)
2015-11-17 17:22:37 +01:00
2014-08-03 19:52:23 +02:00
macro(do_try_compile error_option)
set(TC_ARGS
IFACE_TRY_COMPILE_${error_option}
"${CMAKE_CURRENT_BINARY_DIR}/try_compile_iface" "${CMAKE_CURRENT_SOURCE_DIR}/imported_consumer.cpp"
LINK_LIBRARIES iface
)
if (${error_option} STREQUAL WITH_ERROR)
2021-11-20 13:41:27 +01:00
if (MSVC)
list(APPEND TC_ARGS COMPILE_DEFINITIONS /we4101)
else ()
list(APPEND TC_ARGS COMPILE_DEFINITIONS -Werror=unused-variable)
endif ()
2014-08-03 19:52:23 +02:00
endif()
try_compile(${TC_ARGS})
endmacro()
do_try_compile(NO_ERROR)
if (NOT IFACE_TRY_COMPILE_NO_ERROR)
message(SEND_ERROR "try_compile failed with imported target.")
endif()
do_try_compile(WITH_ERROR)
if (NOT IFACE_TRY_COMPILE_WITH_ERROR)
message(SEND_ERROR "try_compile failed with imported target with error option.")
endif()