Guard code depending on CMake version

橙三吉。 提交于 2019-12-11 12:49:04

问题


Our project has a CMakeList.txt, but its not our primary build system. On Mac OS X, using Cmake generates a warning (nothing new here):

MACOSX_RPATH is not specified for the following targets...

The cited question/answer states to use either set(CMAKE_MACOSX_RPATH 0) or set(CMAKE_MACOSX_RPATH 1). Our problem is, the feature is too new and it breaks existing Cmake installations, like those found on Ubuntu LTS's or CentOS. We need to guard its use for Cmake >= 2.8.12, but the blog post does not discuss how to do it.

Searching is not producing useful results: how to guard cmake directive. My inability to find relevant results is probably due to my inexperience with Cmake (others were supposed to maintain it).

How do we guard set(CMAKE_MACOSX_RPATH 0) or set(CMAKE_MACOSX_RPATH 1)?


Please note, I'm not looking for cmake_minimum_required(VERSION 2.8.5 FATAL_ERROR). The best I can tell it does not do a damn thing. From testing we know it does not reject target_include_directories even though target_include_directories does not meet minimum requirements and causes failures in down level clients like Ubuntu LTS's.

I think what I'm looking for is closer to a C preprocessor macro:

#define 2_8_12 ...
#if CMAKE_VERSION >= 2_8_12
set(CMAKE_MACOSX_RPATH 0)
#endif

回答1:


if (CMAKE_VERSION VERSION_LESS 2.8.12)
  message("Not setting RPATH")
else()
  set(CMAKE_MACOSX_RPATH 0)
endif()

Docs for 2.8.10 here: https://cmake.org/cmake/help/v2.8.10/cmake.html#variable:CMAKE_VERSION




回答2:


Alternatively, if you just want to overcome the warning, you may check whether policy, generating this warning, exists:

if(POLICY CMP0042)
    set(CMAKE_MACOSX_RPATH 0)
endif()

This "if" form is decribed in documentation.




回答3:


Why not to handle it properly for CMake? There is a wiki page called CMake RPATH handling. According to that you can add something like this:

string (TOLOWER ${CMAKE_SYSTEM_NAME} TARGET_OS)

if (TARGET_OS STREQUAL darwin)
    set (CMAKE_MACOSX_RPATH TRUE)
    set (CMAKE_SKIP_BUILD_RPATH FALSE)

    # Fix runtime paths on OS X 10.5 and less
    if (CMAKE_SYSTEM_VERSION VERSION_LESS "10.0.0")
        set (CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
    else()
        set (CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
    endif()

    set (CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
    set (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

    list (FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir)

    if ("${isSystemDir}" STREQUAL "-1")
        set (CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
    endif ("${isSystemDir}" STREQUAL "-1")
endif()

This snippet should fix CMP0042 with any 2.8.x version.



来源:https://stackoverflow.com/questions/37881058/guard-code-depending-on-cmake-version

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!