Security code depending on CMake version

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

 MACOSX_RPATH is not specified for the following targets... 

The quoted question / answer uses the use of set(CMAKE_MACOSX_RPATH 0) or set(CMAKE_MACOSX_RPATH 1) . Our problem is that this feature is too new , and it violates existing Cmake installations, such as those found on Ubuntu LTS or CentOS. We need to protect its use for Cmake> = 2,8.12, but the post does not discuss how to do this.

The search yields no useful results: how to protect the cmake directive . My inability to find relevant results is probably related to my inexperienced with Cmake (others had to support her).

How do we protect 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 say has nothing to do. From testing, we know that it does not reject target_include_directories , although target_include_directories does not meet the minimum requirements and causes failures for lower-level clients such as Ubuntu LTS.

I think that what I am looking for is closer to the C macro processor:

 #define 2_8_12 ... #if CMAKE_VERSION >= 2_8_12 set(CMAKE_MACOSX_RPATH 0) #endif 
+1
source share
3 answers
 if (CMAKE_VERSION VERSION_LESS 2.8.12) message("Not setting RPATH") else() set(CMAKE_MACOSX_RPATH 0) endif() 

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

+2
source

Alternatively, if you just want to overcome this warning, you can check if there is a policy that generates this warning:

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

This "if" form is described in the documentation .

+1
source

Why not process it correctly for CMake? There is a wiki page called Handling CMake RPATH . Accordingly, 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 version 2.8.x.

0
source

Source: https://habr.com/ru/post/1273568/


All Articles