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