1

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
Community
  • 1
  • 1
jww
  • 97,681
  • 90
  • 411
  • 885

3 Answers3

3
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

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

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.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • I might be wrong, but for older versions that do know CMP0042, CMake emits a warning or even an error. – usr1234567 Jun 17 '16 at 12:38
  • Do you mean "for older versions that **don't know** CMP0042 ..."? Otherwise I don't see a problem with my answer. – Tsyvarev Jun 17 '16 at 13:00
  • Hmm, I believe that policies describe **every incompatibility** with older CMake versions. So if project has been successfully built on pre 2.8.12, and warning/error is generated for it on new version of CMake, than this warning/error can be disabled using policy mechanism. – Tsyvarev Jun 17 '16 at 14:16
  • 1
    Sure. My point is, if the CMake version is older then CMP0042, it will fail. Checking for the version might be the better option, depending on other version constraints. – usr1234567 Jun 18 '16 at 22:10
0

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.

  • `RPATH` can cause a security audit failure. it will always generate a finding. On OS X,we want to see `install_names`. On Linux, we don't want to see an RPATH. – jww Jun 20 '16 at 14:46
  • @jww CMake CMP0042 is only applicable to OS X builds, that's why I'm suggesting you to fix RPATH only for OS X builds. The above snippet woldn't touch anything on Linux. – Alexander Saprykin Jun 20 '16 at 14:58
  • `RPATH` will still generate a finding. Don't use rpath's. The questions asks in the context of both `set(CMAKE_MACOSX_RPATH 0)` and `set(CMAKE_MACOSX_RPATH 0)`. But our choice was already made - `set(CMAKE_MACOSX_RPATH 0)`. We did not mention it to avoid the protracted debates. – jww Jun 20 '16 at 14:59
  • @jww Have you tried to set `CMAKE_SKIP_RPATH` variable to completely disable `RPATH` management by CMake? – Alexander Saprykin Jun 20 '16 at 15:15