[CMake] RPATH/RUNPATH

Michael Hertling mhertling at online.de
Thu Aug 18 20:02:30 EDT 2011


On 08/17/2011 10:03 PM, Knox, Kent wrote:
> Yes, that's right.  I'm currently using cmake with RPATH, but from what I've been reading the RUNPATH header is now the more preferred approach.  If the user does not set LD_LIBRARY_PATH, the RUNPATH header should take effect, but if the user DOES wish to overload your libraries, they can go ahead and set LD_LIBRARY_PATH themselves assuming that they know what they are doing.  
> 
> This would help in our testing, as my RPATH settings have screwed up a few of my testers as they thought they were shuffling libraries around, but in fact my RPATH silently made their efforts for naught.
> 
> Thank you for the suggestion on modifying the linker flags myself; that sounds feasible.  I had also thought of running the 'chrpath' <http://linux.die.net/man/1/chrpath> utility as a post-processing step on my executables to manually change RPATH's into RUNPATH's, but I'm not quite sure how to write this in CMake yet.  Wanted to check first if CMake had undocumented or planned settings first.
> 
> Kent

In order to enable ELF's DT_RUNPATH tag, you just need to set the
linker's --enable-new-dtags option, e.g. as in the following
exemplary project, provided you use GNU binutils' ld:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(RUNPATH C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
SET(CMAKE_INSTALL_RPATH $ENV{HOME}/lib)
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "int main(void){return 0;}\n")
ADD_EXECUTABLE(main main.c)
INSTALL(TARGETS main RUNTIME DESTINATION bin)
SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--enable-new-dtags")

In this way, the DT_RPATH tag in the final binary is duplicated for
DT_RUNPATH, and as the former is ignored when the latter is present,
it should suit your needs. Furthermore, CMake's rpath machinery can
operate as usual and the built-in ELF editor addresses the DT_RPATH
and DT_RUNPATH tags, so there is no necessity to have CMake handle
the DT_RUNPATH explicitly, in particular as ld does not provide an
immediate option to set this tag, AFAIK. Also, there's no need to
bother with chrpath/patchelf in a pre- or post-install manner.

'hope that helps.

Regards,

Michael

> -----Original Message-----
> From: David Cole [mailto:david.cole at kitware.com] 
> Sent: Wednesday, August 17, 2011 2:13 PM
> To: Knox, Kent
> Cc: cmake at cmake.org
> Subject: Re: [CMake] RPATH/RUNPATH
> 
> There is presently no explicit RUNPATH support in CMake. And no immediate plans to add any support. In fact, your question is the first time I've even noticed that RPATH and RUNPATH are two distinct things...
> 
> Searching yielded these explanatory blog posts:
>   http://fwarmerdam.blogspot.com/2010/12/rpath-runpath-and-ldlibrarypath.html
>   http://www.physics.drexel.edu/~wking/unfolding-disasters/posts/rpath/
> 
> If you would like to encode a RUNPATH into a library or executable that you are building with CMake, you can try adjusting the compiler and linker flags as needed.
> 
> I take it you want to use RUNPATH so that there is still a path hint encoded into the built products, but that you are then able to override with LD_LIBRARY_PATH in a testing environment...?
> 
> 
> On Wed, Aug 17, 2011 at 3:01 PM, Knox, Kent <Kent.Knox at amd.com> wrote:
>> Any comments on CMake support or planned support for RUNPATH?
>>
>> -----Original Message-----
>> From: David Cole [mailto:david.cole at kitware.com]
>> Sent: Wednesday, August 17, 2011 1:57 PM
>> To: Knox, Kent
>> Cc: cmake at cmake.org
>> Subject: Re: [CMake] RPATH/RUNPATH
>>
>> You can use CMake's RPATH related properties to put a path containing "$ORIGIN" into the final installed executable, and it will work as long as the underlying OS library loader supports ORIGIN. From what I understand, ORIGIN support is not uniformly available in all loaders...
>>
>> Does that answer your question?
>>
>>
>> On Wed, Aug 17, 2011 at 2:19 PM, Knox, Kent <Kent.Knox at amd.com> wrote:
>>> Yes, I have read this link (before my list inquiry) and it does not answer my questions.  At least, I could not derive anything from it relating to RUNPATH or $ORIGIN support.  Thanks for the answer though.
>>>
>>> -----Original Message-----
>>> From: David Cole [mailto:david.cole at kitware.com]
>>> Sent: Wednesday, August 17, 2011 11:37 AM
>>> To: Knox, Kent
>>> Cc: cmake at cmake.org
>>> Subject: Re: [CMake] RPATH/RUNPATH
>>>
>>> Perhaps try googling "cmake rpath" ... ?
>>>
>>>  http://www.cmake.org/Wiki/CMake_RPATH_handling
>>>
>>>
>>> On Wed, Aug 17, 2011 at 12:03 PM, Knox, Kent <Kent.Knox at amd.com> wrote:
>>>> Is there any support or planned support in CMake for RUNPATH?  On a related note, is it possible for CMake to set the RPATH/RUNPATH using $ORIGIN on 'make install'?  This way the install directory can be self-referential and moved as a unit around the filesystem?
>>>>
>>>> Kent


More information about the CMake mailing list