@@ -30,15 +30,14 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
3030
3131# Set RPATH for Unix systems to find shared libraries in executable directory
3232if (UNIX )
33- # Use $ORIGIN on Linux, @executable_path on macOS
3433 if (APPLE )
35- set (CMAKE_BUILD_RPATH "@executable_path " )
36- set (CMAKE_INSTALL_RPATH "@executable_path " )
34+ set (CMAKE_BUILD_RPATH "@loader_path " )
35+ set (CMAKE_INSTALL_RPATH "@loader_path " )
3736 else ()
3837 set (CMAKE_BUILD_RPATH "$ORIGIN" )
3938 set (CMAKE_INSTALL_RPATH "$ORIGIN" )
39+ set (CMAKE_BUILD_RPATH_USE_ORIGIN TRUE )
4040 endif ()
41- set (CMAKE_BUILD_RPATH_USE_ORIGIN TRUE )
4241endif ()
4342
4443if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME )
@@ -510,6 +509,67 @@ if(WIN32)
510509 endif ()
511510endif ()
512511
512+ # -------------------- macOS install_name fixes (SDK bundle) --------------------
513+ if (APPLE )
514+ install (CODE [[
515+ # Respect DESTDIR if used
516+ set(_prefix "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}")
517+ # If CMAKE_INSTALL_LIBDIR is empty for any reason, default to "lib"
518+ if(NOT DEFINED CMAKE_INSTALL_LIBDIR OR CMAKE_INSTALL_LIBDIR STREQUAL "")
519+ set(CMAKE_INSTALL_LIBDIR "lib")
520+ endif()
521+ set(_libdir "${_prefix}/${CMAKE_INSTALL_LIBDIR}")
522+ set(_livekit "${_libdir}/liblivekit.dylib")
523+ set(_ffi "${_libdir}/liblivekit_ffi.dylib")
524+
525+ message(STATUS "macOS fixup: prefix=${_prefix}")
526+ message(STATUS "macOS fixup: libdir=${_libdir}")
527+ message(STATUS "macOS fixup: patching ${_ffi}")
528+ message(STATUS "macOS fixup: patching ${_livekit}")
529+
530+ if(NOT EXISTS "${_ffi}")
531+ message(FATAL_ERROR "Expected ${_ffi} to exist at install time")
532+ endif()
533+ if(NOT EXISTS "${_livekit}")
534+ message(FATAL_ERROR "Expected ${_livekit} to exist at install time")
535+ endif()
536+
537+ # Make the dylib's "id" relocatable
538+ execute_process(
539+ COMMAND /usr/bin/install_name_tool -id "@rpath/liblivekit_ffi.dylib" "${_ffi}"
540+ RESULT_VARIABLE _rv
541+ )
542+ if(NOT _rv EQUAL 0)
543+ message(FATAL_ERROR "install_name_tool -id failed for ${_ffi}")
544+ endif()
545+
546+ # Make liblivekit depend on @rpath/liblivekit_ffi.dylib (replace any absolute build path)
547+ execute_process(
548+ COMMAND /usr/bin/install_name_tool
549+ -change "${RUST_ROOT}/target/release/deps/liblivekit_ffi.dylib" "@rpath/liblivekit_ffi.dylib"
550+ -change "${RUST_ROOT}/target/debug/deps/liblivekit_ffi.dylib" "@rpath/liblivekit_ffi.dylib"
551+ "${_livekit}"
552+ RESULT_VARIABLE _rv
553+ )
554+ # Note: if the old path isn't present, install_name_tool returns non-zero.
555+ # That can happen depending on how the dylib was built. Don't hard-fail here.
556+ if(NOT _rv EQUAL 0)
557+ message(STATUS "macOS fixup: -change returned ${_rv} (old path may not exist); continuing")
558+ endif()
559+
560+ # Ensure @rpath can resolve when loaded from SDK's lib/ directory
561+ execute_process(
562+ COMMAND /usr/bin/install_name_tool -add_rpath "@loader_path" "${_livekit}"
563+ RESULT_VARIABLE _rv
564+ )
565+ # -add_rpath fails if already present; don't fail the install.
566+ if(NOT _rv EQUAL 0)
567+ message(STATUS "macOS fixup: -add_rpath returned ${_rv} (may already exist); continuing")
568+ endif()
569+ ]] )
570+ endif ()
571+
572+
513573# Install public headers
514574install (DIRECTORY "${CMAKE_SOURCE_DIR} /include/"
515575 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR} "
0 commit comments