|
| 1 | +# cmake/bundle_ffmpeg_macos.cmake |
| 2 | +# Phase 23 follow-up — bundle the vendored LGPL ffmpeg dylibs into a JUCE |
| 3 | +# plugin bundle's Contents/Frameworks/ and rewrite the main binary's rpath so |
| 4 | +# dyld finds them via @loader_path/../Frameworks instead of an absolute build- |
| 5 | +# tree path baked in by `target_link_options(... LINKER:-rpath,...)`. |
| 6 | +# |
| 7 | +# Run as a POST_BUILD step: |
| 8 | +# cmake -DSRC_DIR=<ffmpeg-lib-dir> |
| 9 | +# -DBUNDLE=<bundle-dir> # e.g. .../JamWide.app or .../JamWide.vst3 |
| 10 | +# -DBINARY=<bundle>/Contents/MacOS/<name> |
| 11 | +# -P cmake/bundle_ffmpeg_macos.cmake |
| 12 | +# |
| 13 | +# Background (root cause of beta.20.8 crash-on-launch): |
| 14 | +# `otool -L JamWide.app/Contents/MacOS/JamWide` showed each ffmpeg dylib |
| 15 | +# referenced as `@rpath/libavcodec.61.19.101.dylib`, but the only LC_RPATH |
| 16 | +# on the binary was the GitHub Actions runner's absolute build path |
| 17 | +# (`/Users/runner/work/JamWide/JamWide/build/ffmpeg-universal/lib`). That |
| 18 | +# path doesn't exist on user machines → dyld aborts with |
| 19 | +# "Library not loaded: @rpath/libavcodec.61.19.101.dylib". Distribution |
| 20 | +# binaries need a bundle-relative rpath PLUS the dylibs physically present |
| 21 | +# inside Contents/Frameworks/. This script does both. |
| 22 | + |
| 23 | +if(NOT SRC_DIR OR NOT BUNDLE OR NOT BINARY) |
| 24 | + message(FATAL_ERROR |
| 25 | + "bundle_ffmpeg_macos.cmake requires -DSRC_DIR -DBUNDLE -DBINARY") |
| 26 | +endif() |
| 27 | + |
| 28 | +if(NOT EXISTS "${SRC_DIR}") |
| 29 | + message(FATAL_ERROR "SRC_DIR does not exist: ${SRC_DIR}") |
| 30 | +endif() |
| 31 | +if(NOT EXISTS "${BINARY}") |
| 32 | + message(FATAL_ERROR "BINARY does not exist: ${BINARY}") |
| 33 | +endif() |
| 34 | + |
| 35 | +set(_dst "${BUNDLE}/Contents/Frameworks") |
| 36 | +file(MAKE_DIRECTORY "${_dst}") |
| 37 | + |
| 38 | +# Copy every *.dylib in SRC_DIR into Frameworks/, preserving symlinks. |
| 39 | +# CMake's `-E copy` follows symlinks (dereferences them) so we handle |
| 40 | +# symlinks separately via create_symlink. The short-name symlinks (e.g. |
| 41 | +# libavcodec.61.dylib → libavcodec.61.19.101.dylib) MUST survive because |
| 42 | +# libavformat's LC_LOAD_DYLIB references @rpath/libavcodec.61.dylib (the |
| 43 | +# short alias), not the versioned canonical name. |
| 44 | +file(GLOB _items "${SRC_DIR}/*.dylib") |
| 45 | +foreach(_item ${_items}) |
| 46 | + get_filename_component(_name "${_item}" NAME) |
| 47 | + set(_d "${_dst}/${_name}") |
| 48 | + if(IS_SYMLINK "${_item}") |
| 49 | + file(READ_SYMLINK "${_item}" _link_target) |
| 50 | + file(REMOVE "${_d}") |
| 51 | + execute_process( |
| 52 | + COMMAND ${CMAKE_COMMAND} -E create_symlink "${_link_target}" "${_d}" |
| 53 | + RESULT_VARIABLE _rc |
| 54 | + ) |
| 55 | + if(NOT _rc EQUAL 0) |
| 56 | + message(FATAL_ERROR |
| 57 | + "Failed to create symlink ${_d} -> ${_link_target}") |
| 58 | + endif() |
| 59 | + else() |
| 60 | + # configure_file with COPYONLY does timestamp-based skip, so |
| 61 | + # incremental rebuilds don't re-copy ~50MB of dylibs. |
| 62 | + configure_file("${_item}" "${_d}" COPYONLY) |
| 63 | + endif() |
| 64 | +endforeach() |
| 65 | + |
| 66 | +# Rewrite rpaths on the main binary. |
| 67 | +# 1. Strip any LC_RPATH whose path looks like a build-tree absolute path — |
| 68 | +# either the SRC_DIR itself or anything matching /build/ffmpeg-universal/. |
| 69 | +# install_name_tool -delete_rpath requires an exact match; we discover |
| 70 | +# what's currently there via `otool -l` and only delete what exists. |
| 71 | +# 2. Add @loader_path/../Frameworks if missing. |
| 72 | +execute_process( |
| 73 | + COMMAND otool -l "${BINARY}" |
| 74 | + OUTPUT_VARIABLE _otool_out |
| 75 | + RESULT_VARIABLE _otool_rc |
| 76 | +) |
| 77 | +if(NOT _otool_rc EQUAL 0) |
| 78 | + message(FATAL_ERROR "otool -l failed on ${BINARY}") |
| 79 | +endif() |
| 80 | + |
| 81 | +# Parse LC_RPATH paths. Each load command appears as: |
| 82 | +# cmd LC_RPATH |
| 83 | +# cmdsize N |
| 84 | +# path /actual/path (offset 12) |
| 85 | +string(REGEX MATCHALL "LC_RPATH[^\n]*\n[^\n]*\n[^\n]*path [^ ]+ " _rpath_blocks "${_otool_out}") |
| 86 | +set(_existing_rpaths "") |
| 87 | +foreach(_block ${_rpath_blocks}) |
| 88 | + if(_block MATCHES "path ([^ ]+) ") |
| 89 | + list(APPEND _existing_rpaths "${CMAKE_MATCH_1}") |
| 90 | + endif() |
| 91 | +endforeach() |
| 92 | + |
| 93 | +set(_target_rpath "@loader_path/../Frameworks") |
| 94 | + |
| 95 | +foreach(_rp ${_existing_rpaths}) |
| 96 | + # Drop ANY rpath that's an absolute filesystem path under a build dir |
| 97 | + # (matches both local dev builds and CI runner builds). We keep |
| 98 | + # @loader_path/... or @executable_path/... rpaths untouched. |
| 99 | + if(_rp MATCHES "^/") |
| 100 | + execute_process( |
| 101 | + COMMAND install_name_tool -delete_rpath "${_rp}" "${BINARY}" |
| 102 | + RESULT_VARIABLE _rc |
| 103 | + ERROR_VARIABLE _err |
| 104 | + OUTPUT_QUIET |
| 105 | + ) |
| 106 | + if(NOT _rc EQUAL 0) |
| 107 | + message(WARNING |
| 108 | + "install_name_tool -delete_rpath '${_rp}' failed: ${_err}") |
| 109 | + else() |
| 110 | + message(STATUS " stripped build-tree rpath: ${_rp}") |
| 111 | + endif() |
| 112 | + endif() |
| 113 | +endforeach() |
| 114 | + |
| 115 | +# Add the bundle-relative rpath if it isn't already there. |
| 116 | +list(FIND _existing_rpaths "${_target_rpath}" _has_target_rpath) |
| 117 | +if(_has_target_rpath EQUAL -1) |
| 118 | + execute_process( |
| 119 | + COMMAND install_name_tool -add_rpath "${_target_rpath}" "${BINARY}" |
| 120 | + RESULT_VARIABLE _rc |
| 121 | + ERROR_VARIABLE _err |
| 122 | + ) |
| 123 | + if(NOT _rc EQUAL 0) |
| 124 | + message(FATAL_ERROR |
| 125 | + "install_name_tool -add_rpath '${_target_rpath}' failed: ${_err}") |
| 126 | + endif() |
| 127 | + message(STATUS " added bundle rpath: ${_target_rpath}") |
| 128 | +endif() |
0 commit comments