| 1 |
|
| 2 |
cmake_minimum_required(VERSION 3.10) |
| 3 |
|
| 4 |
project(SystemIncludeDirectories) |
| 5 |
|
| 6 |
add_library(systemlib systemlib.cpp) |
| 7 |
target_include_directories(systemlib PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/systemlib") |
| 8 |
|
| 9 |
function (apply_error_flags target) |
| 10 |
if (MSVC) |
| 11 |
target_compile_options(${target} PRIVATE /we4101) |
| 12 |
else () |
| 13 |
target_compile_options(${target} PRIVATE -Werror=unused-variable) |
| 14 |
endif () |
| 15 |
endfunction () |
| 16 |
|
| 17 |
add_library(upstream upstream.cpp) |
| 18 |
target_link_libraries(upstream LINK_PUBLIC systemlib) |
| 19 |
apply_error_flags(upstream) |
| 20 |
|
| 21 |
target_include_directories(upstream SYSTEM PUBLIC |
| 22 |
$<TARGET_PROPERTY:systemlib,INTERFACE_INCLUDE_DIRECTORIES> |
| 23 |
) |
| 24 |
|
| 25 |
add_library(config_specific INTERFACE) |
| 26 |
get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) |
| 27 |
if(isMultiConfig) |
| 28 |
# CMAKE_BUILD_TYPE does not work here for multi-config generators |
| 29 |
target_include_directories(config_specific SYSTEM INTERFACE |
| 30 |
"${CMAKE_CURRENT_SOURCE_DIR}/config_specific" |
| 31 |
) |
| 32 |
else() |
| 33 |
set(testConfig ${CMAKE_BUILD_TYPE}) |
| 34 |
target_include_directories(config_specific SYSTEM INTERFACE |
| 35 |
"$<$<CONFIG:${testConfig}>:${CMAKE_CURRENT_SOURCE_DIR}/config_specific>" |
| 36 |
) |
| 37 |
endif() |
| 38 |
|
| 39 |
add_library(consumer consumer.cpp) |
| 40 |
target_link_libraries(consumer upstream config_specific) |
| 41 |
apply_error_flags(consumer) |
| 42 |
|
| 43 |
add_library(iface IMPORTED INTERFACE) |
| 44 |
set_property(TARGET iface PROPERTY INTERFACE_INCLUDE_DIRECTORIES |
| 45 |
"$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_CURRENT_SOURCE_DIR}/systemlib_header_only>" |
| 46 |
) |
| 47 |
|
| 48 |
add_library(imported_consumer imported_consumer.cpp) |
| 49 |
target_link_libraries(imported_consumer iface) |
| 50 |
apply_error_flags(imported_consumer) |
| 51 |
|
| 52 |
add_library(imported_consumer2 imported_consumer.cpp) |
| 53 |
target_link_libraries(imported_consumer2 imported_consumer) |
| 54 |
apply_error_flags(imported_consumer2) |
| 55 |
|
| 56 |
# add a target which has a relative system include |
| 57 |
add_library(somelib imported_consumer.cpp) |
| 58 |
target_include_directories(somelib SYSTEM PUBLIC "systemlib_header_only") |
| 59 |
apply_error_flags(somelib) |
| 60 |
|
| 61 |
# add a target which consumes a relative system include |
| 62 |
add_library(otherlib upstream.cpp) |
| 63 |
target_link_libraries(otherlib PUBLIC somelib) |
| 64 |
apply_error_flags(otherlib) |
| 65 |
|
| 66 |
macro(do_try_compile error_option) |
| 67 |
set(TC_ARGS |
| 68 |
IFACE_TRY_COMPILE_${error_option} |
| 69 |
"${CMAKE_CURRENT_BINARY_DIR}/try_compile_iface" "${CMAKE_CURRENT_SOURCE_DIR}/imported_consumer.cpp" |
| 70 |
LINK_LIBRARIES iface |
| 71 |
) |
| 72 |
if (${error_option} STREQUAL WITH_ERROR) |
| 73 |
if (MSVC) |
| 74 |
list(APPEND TC_ARGS COMPILE_DEFINITIONS /we4101) |
| 75 |
else () |
| 76 |
list(APPEND TC_ARGS COMPILE_DEFINITIONS -Werror=unused-variable) |
| 77 |
endif () |
| 78 |
endif() |
| 79 |
try_compile(${TC_ARGS}) |
| 80 |
endmacro() |
| 81 |
|
| 82 |
do_try_compile(NO_ERROR) |
| 83 |
if (NOT IFACE_TRY_COMPILE_NO_ERROR) |
| 84 |
message(SEND_ERROR "try_compile failed with imported target.") |
| 85 |
endif() |
| 86 |
do_try_compile(WITH_ERROR) |
| 87 |
if (NOT IFACE_TRY_COMPILE_WITH_ERROR) |
| 88 |
message(SEND_ERROR "try_compile failed with imported target with error option.") |
| 89 |
endif() |