在2.8.12之前的target_include_directories?

时间:2016-06-18 18:31:18

标签: cmake versioning

我们的CMakeList.txttarget_include_directories蠕变。它打破了我们的下层客户端,如Ubuntu LTS,CentOS和Solaris。

我知道我可以用以下内容保护它(感谢ZW),但我需要Cmake版本2.8.11及更早版本(???)。

cmake_minimum_required(VERSION 2.8.5 FATAL_ERROR)
...

if (NOT CMAKE_VERSION VERSION_LESS 2.8.12)
  target_include_directories(...)
else()
  ???
endif()

我们的目录结构非常简单。有一个目录中包含头文件,同一目录中包含源文件。这是设计使用户在调试器下没有麻烦。

在给定我们的配置下可以使用以下内容(@steveire answer applies to us时我不清楚):

if (NOT CMAKE_VERSION VERSION_LESS 2.8.12)
  target_include_directories(...)
else()
  include_directories("${ROOT_SOURCE_DIR}")
endif()

如果没有,应该使用什么来代替2.8.11及更早版本的target_include_directories

1 个答案:

答案 0 :(得分:5)

<强>问题

target_include_directories()相比,include_directories()命令具有扩展功能。所以我同意@Tsyvarev,它主要取决于你如何使用target_include_directories()

<强>解决方案

如果您使用target_include_directories(my_target PRIVATE ...),则等效值为set_property(TARGET my_target APPEND PROPERTY INCLUDE_DIRECTORIES ...)。但相比之下,include_directories()会为当前CMakeLists.txt及其所有add_subdirectory()兄弟中的所有目标设置这些目录。

如果您使用target_include_directories(my_target INTERFACE/PUBLIC ...),则在版本2.8.11之前的CMake中没有此类功能。您可以模拟自传播包含目录的行为,但这要复杂得多。

<强>建议

如果您仍想支持旧版本的CMake,请不要使用新命令。因为这不仅仅是target_include_directories()(请参阅CMake Version Compatibility Matrix/Commands)。

<强>参考

向后兼容target_include_directories()

以下代码显示了较旧的CMake版本必须执行哪些模拟target_include_directories()

function(target_include_directories _target)
    set_property(GLOBAL APPEND PROPERTY GLOBAL_TARGETS "${_target}")
    set(_mode "PRIVATE")
    foreach(_arg ${ARGN})
        if (_arg MATCHES "SYSTEM|BEFORE")
            message(FATAL_ERROR "target_include_directories: SYSTEM or BEFORE not supported")
        endif()
        if (_arg MATCHES "INTERFACE|PUBLIC|PRIVATE")
            set(_mode "${_arg}")
        else()
            get_filename_component(_inc_dir "${_arg}" ABSOLUTE)
            if (_mode MATCHES "PUBLIC|PRIVATE")
                set_property(TARGET ${_target} APPEND PROPERTY INCLUDE_DIRECTORIES "${_inc_dir}")
            endif()            
            if (_mode MATCHES "INTERFACE|PUBLIC")
                set_property(TARGET ${_target} APPEND PROPERTY MY_INTERFACE_INCLUDE_DIRECTORIES "${_inc_dir}")
            endif()            
        endif()
    endforeach()
endfunction(target_include_directories)

function(target_link_libraries _target)
    set_property(GLOBAL APPEND PROPERTY GLOBAL_TARGETS "${_target}")
    set(_mode "PUBLIC")
    foreach(_arg ${ARGN})
        if (_arg MATCHES "INTERFACE|PUBLIC|PRIVATE|LINK_PRIVATE|LINK_PUBLIC|LINK_INTERFACE_LIBRARIES")
            set(_mode "${_arg}")
        else()
            if (NOT _arg MATCHES "debug|optimized|general")
                set_property(TARGET ${_target} APPEND PROPERTY MY_LINK_LIBARIES "${_arg}")
            endif()
        endif()
    endforeach()
    _target_link_libraries(${_target} ${ARGN})
endfunction(target_link_libraries)

function(my_update_depending_inc_dirs _targets _target _dep_target)
    get_property(_libs TARGET ${_dep_target} PROPERTY MY_LINK_LIBARIES)
    if (NOT _libs)
        return()
    endif()
    foreach(_lib ${_libs})
        list(FIND _targets "${_lib}" _idx)
        if (NOT _idx EQUAL -1)
            get_property(_inc_dirs TARGET ${_lib} PROPERTY MY_INTERFACE_INCLUDE_DIRECTORIES)
            set_property(TARGET ${_target} APPEND PROPERTY INCLUDE_DIRECTORIES "${_inc_dirs}")
            # to prevent cyclic dependencies getting us into an endless loop
            # remove the target we already processed from the list
            list(REMOVE_AT _targets ${_idx})
            my_update_depending_inc_dirs("${_targets}" "${_target}" "${_lib}")
        endif()
    endforeach()
endfunction(my_update_depending_inc_dirs)

function(my_update_inc_dirs)
    get_property(_targets GLOBAL PROPERTY GLOBAL_TARGETS)
    list(REMOVE_DUPLICATES _targets)
    foreach(_target ${_targets})
        my_update_depending_inc_dirs("${_targets}" "${_target}" "${_target}")
    endforeach()
endfunction(my_update_inc_dirs)

使用以下代码进行测试(注意:最后需要my_update_inc_dirs()调用):

<强>的CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

project(TargetIncludeDirectories)

...

add_library(PLib plib/source/plib.cpp)
target_include_directories(PLib PRIVATE plib/source PUBLIC plib/include)

add_library(Lib lib/source/lib.cpp)
target_include_directories(Lib PRIVATE lib/source PUBLIC lib/include)
target_link_libraries(Lib PRIVATE PLib)

add_executable(App main.cpp)
target_include_directories(App PRIVATE . PUBLIC include)
target_link_libraries(App PRIVATE Lib)

my_update_inc_dirs()