swig generated code not finding libraries on macOS 12. rpath issue?

Hi –

My project contains swig files that create libraries that need to link to external libraries. Some of the external libraries are obtained via pkgconfig, and pkg_check_modules places their (nonstandard) full paths in a variable like EXT_LINK_LIBRARIES.

On macOS 11.6, this works to build and link the swig modules:

swig_add_library(
   ${swigfile} 
   TYPE MODULE
   LANGUAGE PYTHON 
   SOURCES ${swigfile}.i)
target_link_libraries(
   ${swigfile}
   PUBLIC
   ${EXT_LINK_LIBRARIES})

On macOS 12.5, however, even though the full path to the libraries was given in EXT_LINK_LIBRARIES, when the program tries to import the python module it fails, because it’s only looking for the swig-generated .so file in standard locations (<prefix>/lib, /usr/local/lib, etc).

On both systems running otool -L on the dylib file shows that it’s linking to @rpath/lib[myswiggedlib].dylib. I know that something changed with rpath handling in macOS 12, but I haven’t found instructions on how to fix it in this context.

Why is the full path provided to target_link_libraries being ignored? Is there an rpath-related fix that I’m missing?

(I’m using cmake 3.23.3 on both systems, with macOS 12.5.1 and Xcode 13.4.1 on an M1, and macOS 11.6.8 and Xcode 13.2 on an Intel Mac.)

Thanks.
– Steve

macOS rpath is silly in that @rpath/ requires using LC_RPATH load commands and LC_RPATH commands only work on @rpath/-using library lookups. If the library wants rpath entries, you’ll have to add the relevant paths to the CMAKE_*_RPATH variables yourself.

I’ve wanted “rpath usage requirements” to deal with this, but have never gotten around to actually handling this.

Ok, thanks.
– Steve