Search code examples
cmakerpath

What is the $ORIGIN token in CMake?


The cmake documentation page about BUILD_RPATH_USE_ORIGIN mentions the existance of a $ORIGIN token, but does not explain what it is or how it can be used. I also can't seem to find any resources specific to this $ORIGIN token online. Can someone give me an explanation?


Solution

  • It doesn't really have anything in particular to do with CMake- more with library loaders. Some platforms treat the token "$ORIGIN" with a special meaning.

    For example, quoting from the man page for ld.so's section named "Dynamic string tokens":

    $ORIGIN (or equivalently ${ORIGIN}) This expands to the directory containing the program or shared object. Thus, an application located in somedir/app could be compiled with

    gcc -Wl,-rpath,'$ORIGIN/../lib'
    

    so that it finds an associated shared object in somedir/lib no matter where somedir is located in the directory hierarchy. This facilitates the creation of "turn-key" applications that do not need to be installed into special directories, but can instead be unpacked into any directory and still find their own shared objects.

    It mentions that token having meaning in contexts like the --library-path argument, and the LD_LIBRARY_PATH, LD_PRELOAD, and LD_AUDIT environment variables.

    CMake's community wiki also has a little to say about it- mostly to the same effect:

    $ORIGIN: On Linux/Solaris, it's probably a very good idea to specify any RPATH setting one requires to look up the location of a package's private libraries via a relative expression, to not lose the capability to provide a fully relocatable package. This is what $ORIGIN is for. In CMAKE_INSTALL_RPATH lines, it should have its dollar sign escaped with a backslash to have it end up with proper syntax in the final executable. See also the CMake and $ORIGIN discussion.

    And then goes on to talk about mechanisms for doing the same on other platforms.

    If you want some other usage tips, see Craig Scott's talk "Deep CMake for Library Authors" at ~t=49:30, which also talks about other mechanisms on other platforms.

    Platforms that support $ORIGIN include (but aren't necessarily limited to) Linux and Solaris. I found a GNU libtool mailing list question about other platforms in 2005, but no reply, unfortunately.