# Post-deploy fixup for CopyQ macOS bundle. # macdeployqt does not handle transitive dependencies of third-party libraries. # This script walks the bundle, finds unresolved @rpath deps, copies them from # known library directories, and rewrites install names. # # Generated from fixup_bundle.cmake.in by configure_file(@ONLY). set(_bundle "@BUNDLE@") set(_libpaths "@COPYQ_DEPLOY_LIBPATHS@") set(_fwk_dir "${_bundle}/Contents/Frameworks") # Collect all Mach-O binaries (dylibs + framework executables) under directories. function(collect_bundle_binaries out_var) set(_result "") foreach(_dir ${ARGN}) file(GLOB_RECURSE _dylibs "${_dir}/*.dylib") list(APPEND _result ${_dylibs}) file(GLOB_RECURSE _fwk_files "${_dir}/*.framework/Versions/*") foreach(_f ${_fwk_files}) get_filename_component(_name "${_f}" NAME) if(NOT IS_DIRECTORY "${_f}" AND NOT IS_SYMLINK "${_f}" AND NOT _name MATCHES "\\.") list(APPEND _result "${_f}") endif() endforeach() endforeach() set(${out_var} ${_result} PARENT_SCOPE) endfunction() # Return all @rpath-relative references from a binary via otool -L. function(get_rpath_deps bin out_var) execute_process( COMMAND otool -L "${bin}" OUTPUT_VARIABLE _output OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET ) set(_refs "") string(REPLACE "\n" ";" _lines "${_output}") foreach(_line ${_lines}) if(_line MATCHES "^[ \t]+@rpath/([^ ]+)") list(APPEND _refs "${CMAKE_MATCH_1}") endif() endforeach() set(${out_var} ${_refs} PARENT_SCOPE) endfunction() # --- Phase 1: Copy missing transitive dependencies into the bundle. --- message(STATUS "Fixup bundle: searching for missing deps in: ${_libpaths}") foreach(_iter RANGE 1 5) collect_bundle_binaries(_bins "${_bundle}/Contents/Frameworks" "${_bundle}/Contents/PlugIns") set(_found_new FALSE) foreach(_bin ${_bins}) get_rpath_deps("${_bin}" _refs) foreach(_rel ${_refs}) if(EXISTS "${_fwk_dir}/${_rel}") continue() endif() foreach(_sd ${_libpaths}) if(NOT EXISTS "${_sd}/${_rel}") continue() endif() if(_rel MATCHES "^([^/]+\\.framework)/") set(_fname "${CMAKE_MATCH_1}") if(NOT EXISTS "${_fwk_dir}/${_fname}") message(STATUS " Bundling framework: ${_fname}") file(COPY "${_sd}/${_fname}" DESTINATION "${_fwk_dir}") set(_found_new TRUE) endif() else() get_filename_component(_dest "${_fwk_dir}/${_rel}" DIRECTORY) file(MAKE_DIRECTORY "${_dest}") message(STATUS " Bundling library: ${_rel}") file(COPY "${_sd}/${_rel}" DESTINATION "${_dest}") set(_found_new TRUE) endif() break() endforeach() endforeach() endforeach() if(NOT _found_new) message(STATUS " No new deps found in iteration ${_iter}, done.") break() endif() endforeach() # --- Phase 2: Rewrite @rpath references to @executable_path/../Frameworks/. --- collect_bundle_binaries(_all_bins "${_bundle}/Contents/Frameworks" "${_bundle}/Contents/PlugIns") foreach(_bin ${_all_bins}) get_rpath_deps("${_bin}" _refs) foreach(_rel ${_refs}) if(EXISTS "${_fwk_dir}/${_rel}") execute_process( COMMAND install_name_tool -change "@rpath/${_rel}" "@executable_path/../Frameworks/${_rel}" "${_bin}" ERROR_QUIET ) endif() endforeach() endforeach() message(STATUS "Bundle fixup complete.")