2

I'm trying a fairly simple experiment using cpack. The aim is to create an RPM installer (using cpack) with a trivial executable which uses an external 3rd party shared library (called libSomeSharedLib.so).

The structure I'd like the RPM to install is

opt
|_cmakeFindPackageTest
  |_bin
    |_cmakeFindPackageTest (an executable)
  |_lib
    |_libSomeSharedLib.so (the shared library)

I want the executable's RPATH to be /opt/cmakeFindPackageTest/lib (to ensure it uses the installed shared lib).

The full CMakeLists.txt is pasted at the bottom, but note the following properties are SET

 15 SET(CMAKE_INSTALL_PREFIX "/opt/cmakeFindPackageTest")
 16 SET( INSTALL_DIR_BIN ${CMAKE_INSTALL_PREFIX}/bin )
 17 SET( INSTALL_DIR_LIB ${CMAKE_INSTALL_PREFIX}/lib )
 18 
 19 SET(CMAKE_INSTALL_RPATH "\${INSTALL_DIR_LIB}")
 20 SET(CMAKE_SKIP_BUILD_RPATH TRUE)
 21 SET(CMAKE_SKIP_INSTALL_RPATH FALSE)

To my understanding, lines 17,19,20 should cause cpack to set the executable's RPATH to /opt/cmakeFindPackageTest/lib

HOWEVER...

when I build the project (from clean) and run cpack to generate an RPM I see this in the output

bash-4.2$ cpack -V -G RPM
CPack: Enable Verbose
... irrelevant looking output ommitted ...
CPack Verbose: Set runtime path of "/local/bfarnham/workspace/OPC-UA/CMake_examples/cmakeFindPackageTest/build/_CPack_Packages/Linux/RPM/cmakeFindPackageTest-0.0.0-Linux/opt/cmakeFindPackageTest/bin/cmakeFindPackageTest" to ""

RPATH gets set to empty! Eh? Checked with readelf - sure enough

bash-4.2$ readelf -d ./_CPack_Packages/Linux/RPM/cmakeFindPackageTest-0.0.0-Linux/opt/cmakeFindPackageTest/bin/cmakeFindPackageTest | grep --context=1 -i RPATH
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000000f (RPATH)              Library rpath: []
 0x000000000000000c (INIT)               0x400848

So, finally, the question: how do I get cpack to set the RPATH of this executable in the RPM ?

================ full CMakeLists.txt ================

  1 cmake_minimum_required( VERSION 3.0 )
  2 project( cmakeFindPackageTest CXX )
  3 set (CMAKE_CXX_STANDARD 11)
  4 
  5 list( INSERT CMAKE_MODULE_PATH 0 ${CMAKE_SOURCE_DIR}/cmake )
  6 message( STATUS "CMAKE_MODULE_PATH [${CMAKE_MODULE_PATH}]" )
  7 find_package( SomeSharedLib 1.0 REQUIRED MODULE )
  8 message( STATUS "SomeSharedLib_INCLUDE_DIR [${SomeSharedLib_INCLUDE_DIR}]  SomeSharedLib_LIBRARY [${SomeSharedLib_LIBRARY}]" )
  9 
 10 add_executable( cmakeFindPackageTest src/main.cpp )
 11 target_link_libraries( cmakeFindPackageTest SomeSharedLib::SomeSharedLib )
 12 
 13 # =============================== INSTALL DETAILS BELOW THIS POINT ==========================
 14 
 15 SET(CMAKE_INSTALL_PREFIX "/opt/cmakeFindPackageTest")
 16 SET( INSTALL_DIR_BIN ${CMAKE_INSTALL_PREFIX}/bin )
 17 SET( INSTALL_DIR_LIB ${CMAKE_INSTALL_PREFIX}/lib )
 18 
 19 SET(CMAKE_INSTALL_RPATH "\${INSTALL_DIR_LIB}")
 20 SET(CMAKE_SKIP_BUILD_RPATH TRUE)
 21 SET(CMAKE_SKIP_INSTALL_RPATH FALSE)
 22 SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE)
 23 
 24 SET(CPACK_PACKAGE_NAME cmakeFindPackageTest )
 25 SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "test")
 26 SET(CPACK_PACKAGE_VENDOR "TEST_VENDOR")
 27 SET(CPACK_PACKAGE_RELOCATABLE FALSE)
 28 SET(CPACK_PACKAGE_VERSION_MAJOR "0")
 29 SET(CPACK_PACKAGE_VERSION_MINOR "0")
 30 SET(CPACK_PACKAGE_VERSION_PATCH "0")
 31 
 32 install( TARGETS 
 33   cmakeFindPackageTest
 34   RUNTIME DESTINATION ${INSTALL_DIR_BIN} )
 35 
 36 install( FILES
 37   ${SomeSharedLib_LIBRARY}
 38   DESTINATION ${INSTALL_DIR_LIB} )
 39   
 40 include( CPack )
BenF
  • 71
  • 7
  • What is the purpose of the backslash character when setting `CMAKE_INSTALL_RPATH`? Have you tried removing the backslash? – Kevin Jan 08 '20 at 16:25
  • '\' was most recent experiment (I was wondering if it the problem was something to do with when variables were dereferenced to obtain their values). – BenF Jan 08 '20 at 16:34
  • 1
    Just tried with the '\' removed in the CMakeLists.txt:19 - same result (empty RPATH) – BenF Jan 08 '20 at 16:35
  • Also, just FYI, even if I hardcode the value of `CMAKE_INSTALL_PREFIX` `SET(CMAKE_INSTALL_RPATH "/opt/cmakeFindPackageTest/lib")` I *still* get empty RPATH - just looks like it is being ignored – BenF Jan 08 '20 at 16:54

2 Answers2

3

I fixed this by explicitly setting the INSTALL_RPATH property value for the executable target. So, the addition of the set_target_properties line after the call to install

install( TARGETS 
  cmakeFindPackageTest
  RUNTIME DESTINATION ${INSTALL_DIR_BIN} )
set_target_properties( cmakeFindPackageTest PROPERTIES INSTALL_RPATH "/opt/cmakeFindPackageTest/lib" )

From the cmake/cpack documentation it seemed like setting the CMAKE_INSTALL_RPATH variable should have worked. The CMAKE_INSTALL_RPATH docs read

The rpath to use for installed targets.

A semicolon-separated list specifying the rpath to use in installed targets (for platforms that support it). This is used to initialize the target property INSTALL_RPATH for all targets.

BenF
  • 71
  • 7
2

This is not an answer; rather an addendum to the fix described above...

With the fix above, cpack generates an RPM with an executable with the correct RPATH. However, the RPM was 'broken': installation fails - despite the RPM containing the required shared library, it appears cpackRPM somehow did not give it the corresponding attributes in the generated RPM. Install fails like this

bash-4.2$ sudo yum install cmakeFindPackageTest-0.0.0-Linux.rpm 
Loaded plugins: changelog, fastestmirror, kernel-module, langpacks, protectbase, tsflags, versionlock
Examining cmakeFindPackageTest-0.0.0-Linux.rpm: cmakefindpackagetest-0.0.0-1.x86_64
Marking cmakeFindPackageTest-0.0.0-Linux.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package cmakefindpackagetest.x86_64 0:0.0.0-1 will be installed
--> Processing Dependency: libSomeSharedLib.so()(64bit) for package: cmakefindpackagetest-0.0.0-1.x86_64
Loading mirror speeds from cached hostfile
211 packages excluded due to repository protections
--> Finished Dependency Resolution
Beginning Kernel Module Plugin
Finished Kernel Module Plugin
Error: Package: cmakefindpackagetest-0.0.0-1.x86_64 (/cmakeFindPackageTest-0.0.0-Linux)
           Requires: libSomeSharedLib.so()(64bit)
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest
bash-4.2$ 

This is resolved by instructing cpack to install the 3rd party library not as a FILE but as as PROGRAM. The key block of the CMakeLists.txt is

 22 install( PROGRAMS
 23   ${SomeSharedLib_LIBRARY}
 24   DESTINATION ${INSTALL_DIR_LIB} )

The full CMakeLists.txt file is pasted below. This creates an RPM with executable RPATH set to the installation site lib directory, and the shared lib correctly identified as a shared library during RPM installation (i.e. resolves the 'Requires: libSomeSharedLib.so' failure)

  1 cmake_minimum_required( VERSION 3.0 )
  2 project( cmakeFindPackageTest CXX )
  3 set (CMAKE_CXX_STANDARD 11)
  4 
  5 list( INSERT CMAKE_MODULE_PATH 0 ${CMAKE_SOURCE_DIR}/cmake )
  6 message( STATUS "CMAKE_MODULE_PATH [${CMAKE_MODULE_PATH}]" )
  7 find_package( SomeSharedLib 1.0 REQUIRED MODULE )
  8 message( STATUS "SomeSharedLib_INCLUDE_DIR [${SomeSharedLib_INCLUDE_DIR}]  SomeSharedLib_LIBRARY [${SomeSharedLib_LIBRARY}]" )
  9 
 10 add_executable( cmakeFindPackageTest src/main.cpp )
 11 target_link_libraries( cmakeFindPackageTest SomeSharedLib::SomeSharedLib )
 12 
 13 SET(CMAKE_INSTALL_PREFIX "/opt/cmakeFindPackageTest" )
 14 SET(INSTALL_DIR_BIN ${CMAKE_INSTALL_PREFIX}/bin)
 15 SET(INSTALL_DIR_LIB ${CMAKE_INSTALL_PREFIX}/lib)
 16 
 17 install( TARGETS 
 18   cmakeFindPackageTest
 19   RUNTIME DESTINATION ${INSTALL_DIR_BIN} )
 20 set_target_properties( cmakeFindPackageTest PROPERTIES INSTALL_RPATH "${INSTALL_DIR_LIB}" )
 21 
 22 install( PROGRAMS
 23   ${SomeSharedLib_LIBRARY}
 24   DESTINATION ${INSTALL_DIR_LIB} )
 25 
 26 SET(CPACK_PACKAGE_NAME cmakeFindPackageTest )
 27 SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "test")
 28 SET(CPACK_PACKAGE_VENDOR "TEST_VENDOR")
 29 SET(CPACK_PACKAGE_RELOCATABLE FALSE)
 30 SET(CPACK_PACKAGE_VERSION_MAJOR "0")
 31 SET(CPACK_PACKAGE_VERSION_MINOR "0")
 32 SET(CPACK_PACKAGE_VERSION_PATCH "0")
 33 
 34 include( CPack )
 35 #include (${PROJECT_SOURCE_DIR}/CMakeEpilogue.cmake)
BenF
  • 71
  • 7