macos: Wrong usage of MACOSX_RPATH (when using -DFAST_BUILD=OFF)
In your CMake based build, MACOSX_RPATH is wrongly used here:
https://github.com/ERGO-Code/HiGHS/blob/364c83a51e44ba6c27def9c8fc1a49b1daf5ad5c/highs/CMakeLists.txt#L46
so dylib is build with absolute path and don't use @rpath...
note: Spotted in https://github.com/google/or-tools/issues/4674#issuecomment-2963390936 note2: same issue here: https://github.com/scipopt/soplex/issues/49 and https://github.com/scipopt/scip/issues/154
Protocol
cmake -S. -Bbuild -DFAST_BUILD=OFF
cmake --build build --config Release -j8
# Check path inside the shared library
otool -L build/lib/libhighs.dylib
Observed
build/lib/libhighs.dylib:
**/Users/corentinl/work/HiGHS/build/lib/libhighs.1.11.dylib (compatibility version 1.11.0, current version 1.11.0)**
/usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.12)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1900.180.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1351.0.0)
Expected
**@rpath/libhighs.1.11.dylib (compatibility version 1.11.0, current version 1.11.0)**
Explanation
MACOS_RPATHis a boolean property which must be set toTRUEorFALSEIf TRUE, the default, cmake will use @rpath as directory portion (aka prefix) of the install_name (otool LC_ID_DYLIB) note: CMP0042 set it to TRUE by default- Here, you are storing a "path" (i.e. the
string${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}) into a variable which should be use to storeTRUEorFALSEand CMakeif(string)is always evaluated toFALSE(unless store a "TRUE" constant which is not the case here) leading to NOT using@rpathdirectory prefix... - Your projects already use
set(CMAKE_MACOSX_RPATH TRUE)in root CMakeLists.txt, which already default setMACOS_RPATHtoTRUE, so you should just stop playing withMACOSX_RPATHTarget property IMHO... - side note: To change the runpath (otool LC_RPATH) (e.g. to set it to
@loader_path) you may use theINSTALL_RPATHtarget property...
ref: https://cmake.org/cmake/help/latest/command/if.html#string https://cmake.org/cmake/help/latest/variable/CMAKE_MACOSX_RPATH.html https://cmake.org/cmake/help/latest/prop_tgt/MACOSX_RPATH.html https://cmake.org/cmake/help/latest/prop_tgt/INSTALL_RPATH.html
With the fix:
diff --git a/highs/CMakeLists.txt b/highs/CMakeLists.txt
index 50301433d..a8b509d5f 100644
--- a/highs/CMakeLists.txt
+++ b/highs/CMakeLists.txt
@@ -43,7 +43,8 @@ if(NOT FAST_BUILD)
set_target_properties(libhighs PROPERTIES
OUTPUT_NAME "highs"
PDB_NAME "libhighs"
- MACOSX_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
+ #MACOSX_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}"
+ )
if(ZLIB AND ZLIB_FOUND)
target_link_libraries(libhighs ZLIB::ZLIB)
Protocol
cmake --build build --config Release -j8
otool -L build/lib/libhighs.dylib
Observed
build/lib/libhighs.dylib:
@rpath/libhighs.1.11.dylib (compatibility version 1.11.0, current version 1.11.0)
/usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.12)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1900.180.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1351.0.0)
Thanks
Closed by https://github.com/ERGO-Code/HiGHS/pull/2412