Feature: Per target ability to specify directories as not eligible for `-rpath` entries
Description
Projects like the CUDA toolkit provide stub libraries that allow the linker to resolve all symbols but aren't valid to be used at runtime.
CMake by default in builds will add -rpath
entries for all libraries that are used ( and not in a system directory ).
There is no controls in CMake to specify extra directories to be excluded from -rpath
entries, as that information is currently decided
by compiler+system introspection.
Example
CMakeLists.txt:
cmake_minimum_required(VERSION 3.18)
project (rpath_entry CXX)
# This replicates a conda env where `libcuda.so` doesn't exist
# in the C or C++ implicit paths since the conda compilers
# `--print-sysroot` points to $CONDA_PREFIX/x86_64-conda-linux-gnu/sysroot
#
# If you wanted to replicate this with conda:
# - conda create --name test_rpath -y
# - conda activate test_rpath
# - conda install gxx gcc c-compiler cxx-compiler cuda-nvcc cuda-driver-dev --channel conda-forge -y
# set(CUDA_cuda_driver_LIBRARY "/usr/local/cuda/lib64/stubs/libcuda.so")
find_package(CUDAToolkit REQUIRED)
add_library(sample SHARED main.cpp)
target_link_libraries(sample PRIVATE CUDA::cuda_driver)
# Check the `RUNPATH` entries on sample to see the bad `/usr/local/cuda/lib64/stubs` version.
#
# For conda the compilers use `RPATH` entries and not `RUNPATH`. So the output looks like:
# 0x0000000000000001 (NEEDED) Shared library: [libcuda.so.1]
# 0x000000000000000e (SONAME) Library soname: [libsample.so]
# 0x000000000000000f (RPATH) Library rpath: [/home/rmaynard/miniconda3/envs/test_gcc/lib:/home/rmaynard/miniconda3/envs/test_gcc/lib/stubs]
#
# Which becomes the problem
main.cpp
#include <cuda.h>
int main() {
unsigned int flags = 0;
cuInit(flags);
return 0;
}
Proposal
Extend CMake targets with a new property called EXCLUDE_FROM_DEPENDENT_RPATH
which will be by default set to OFF
. That will allow packages like FindCUDAToolkit
to be updated to mark targets pointing to stub libraries as ON
. This preserves the current and very useful behavior but allows for the stubs edge case