9

In my CMake project I build a bunch of libraries that are loaded at runtime as plugins. I therefore need to set various RPATHs so that these libraries can be found by the dynamic loading mechanism. I also need the whole thing to be relocatable, because it is part of a Python extension module and so gets copied around by various tools during the build process.

It seems that the proper way to do this is to set the RPATH in a relative fashion by using the special $ORIGIN variable on Linux, and @loader_path on OSX. Ok cool, that's fine. But looking through

https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/RPATH-handling

it seems like I have to do this manually? CMake does not have any built-in variable like ${ORIGIN} or something that expands to the correct thing depending on the OS? (and maybe it even varies depending on the linker or something? I have no idea). It just seems like this is the sort of thing that CMake should figure out for me, so I want to check that I'm not missing something before I try to implement the switching myself...

BTW I don't care about Windows, so it doesn't matter if there is some solution that doesn't work for that :).

Ben Farmer
  • 2,387
  • 1
  • 25
  • 44
  • `...that expands to the correct thing depending on the OS` - As far as I understand, there is simply **no cross-platform notion** about "ORIGIN": Linux has a single "variable" for this purpose, OSX has several "variables" with similar semantic, and Windows has none. It will be a little sence in having CMake variable, which behaves differently on different platforms. BTW, you may create your own `ORIGIN` variable (with value depending on the OS), and use it in your code. – Tsyvarev Nov 22 '18 at 10:04
  • 1
    Well ok maybe a variable is too simplistic, but CMake deals with a lot of things that are different between platforms. There could be an option somewhere like `CMAKE_USE_RELATIVE_RPATHS`, or some such. But I guess there is no such option? – Ben Farmer Nov 22 '18 at 10:06
  • There seems to be this option: `MACOSX_RPATH`, https://cmake.org/cmake/help/latest/prop_tgt/MACOSX_RPATH.html, which claims to set some RPATH to @rpath on Mac. Maybe I need this and some equivalent for Linux? – Ben Farmer Nov 22 '18 at 10:13
  • You seem to answer your question by yourself :) "Maybe I need this and some equivalent for Linux?" - Yes, this looks as a perfect way. – Tsyvarev Nov 22 '18 at 10:15
  • Haha ok but I cannot find the Linux equivalent so far – Ben Farmer Nov 22 '18 at 10:16
  • Possibly I am just supposed to set `INSTALL_RPATH` to `$ORIGIN` manually, and then the above variable overrides it on Mac? The behaviour is not clear to me. – Ben Farmer Nov 22 '18 at 10:19

0 Answers0