cmake setting rpath allowing the target to run from the build tree doesn't work when also setting BUILD_RPATH property
cmake adds directories to the BUILD_RPATH
for any libraries in the build tree, so that targets can be run from the build tree and find the libraries also in the build tree.
However, it does this by appending these directories to any that already appear in the target's BUILD_RPATH
property. If any directory already in the BUILD_RPATH
happens to have library that is also in the build tree, then the library that is not in the build tree will be used in preference (appearing first the rpath list) to the one in the build tree. Thus breaking the ability to run binaries from the build tree.
It really should prepend the build directories, so that the build libraries are used in preference to any that are already present (perhaps from a previous install of the package being built).
There appears to be no possible way to add a directory to the BUILD_RPATH
after cmake adds the appropriate build tree directories. Any directories specified manually will always precede the automatic build tree directories.
Even manually finding the build tree directories and placing them at the beginning of BUILD_RPATH
is quite a challenge, as generator expressions like $<TARGET_LINKER_FILE_DIR:mylibtgt>
are not evaluated in BUILD_RPATH
. There doesn't appear to be a equivalent of $<TARGET_LINKER_FILE_DIR>
that can be evaluated at configure time.