| 1 |
# Post-deploy fixup for CopyQ macOS bundle. |
| 2 |
# macdeployqt does not handle transitive dependencies of third-party libraries. |
| 3 |
# This script walks the bundle, finds unresolved @rpath deps, copies them from |
| 4 |
# known library directories, and rewrites install names. |
| 5 |
# |
| 6 |
# Generated from fixup_bundle.cmake.in by configure_file(@ONLY). |
| 7 |
|
| 8 |
set(_bundle "@BUNDLE@") |
| 9 |
set(_libpaths "@COPYQ_DEPLOY_LIBPATHS@") |
| 10 |
set(_fwk_dir "${_bundle}/Contents/Frameworks") |
| 11 |
|
| 12 |
# Collect all Mach-O binaries (dylibs + framework executables) under directories. |
| 13 |
function(collect_bundle_binaries out_var) |
| 14 |
set(_result "") |
| 15 |
foreach(_dir ${ARGN}) |
| 16 |
file(GLOB_RECURSE _dylibs "${_dir}/*.dylib") |
| 17 |
list(APPEND _result ${_dylibs}) |
| 18 |
file(GLOB_RECURSE _fwk_files "${_dir}/*.framework/Versions/*") |
| 19 |
foreach(_f ${_fwk_files}) |
| 20 |
get_filename_component(_name "${_f}" NAME) |
| 21 |
if(NOT IS_DIRECTORY "${_f}" AND NOT IS_SYMLINK "${_f}" AND NOT _name MATCHES "\\.") |
| 22 |
list(APPEND _result "${_f}") |
| 23 |
endif() |
| 24 |
endforeach() |
| 25 |
endforeach() |
| 26 |
set(${out_var} ${_result} PARENT_SCOPE) |
| 27 |
endfunction() |
| 28 |
|
| 29 |
# Return all @rpath-relative references from a binary via otool -L. |
| 30 |
function(get_rpath_deps bin out_var) |
| 31 |
execute_process( |
| 32 |
COMMAND otool -L "${bin}" |
| 33 |
OUTPUT_VARIABLE _output |
| 34 |
OUTPUT_STRIP_TRAILING_WHITESPACE |
| 35 |
ERROR_QUIET |
| 36 |
) |
| 37 |
set(_refs "") |
| 38 |
string(REPLACE "\n" ";" _lines "${_output}") |
| 39 |
foreach(_line ${_lines}) |
| 40 |
if(_line MATCHES "^[ \t]+@rpath/([^ ]+)") |
| 41 |
list(APPEND _refs "${CMAKE_MATCH_1}") |
| 42 |
endif() |
| 43 |
endforeach() |
| 44 |
set(${out_var} ${_refs} PARENT_SCOPE) |
| 45 |
endfunction() |
| 46 |
|
| 47 |
# --- Phase 1: Copy missing transitive dependencies into the bundle. --- |
| 48 |
message(STATUS "Fixup bundle: searching for missing deps in: ${_libpaths}") |
| 49 |
foreach(_iter RANGE 1 5) |
| 50 |
collect_bundle_binaries(_bins "${_bundle}/Contents/Frameworks" "${_bundle}/Contents/PlugIns") |
| 51 |
|
| 52 |
set(_found_new FALSE) |
| 53 |
foreach(_bin ${_bins}) |
| 54 |
get_rpath_deps("${_bin}" _refs) |
| 55 |
foreach(_rel ${_refs}) |
| 56 |
if(EXISTS "${_fwk_dir}/${_rel}") |
| 57 |
continue() |
| 58 |
endif() |
| 59 |
foreach(_sd ${_libpaths}) |
| 60 |
if(NOT EXISTS "${_sd}/${_rel}") |
| 61 |
continue() |
| 62 |
endif() |
| 63 |
if(_rel MATCHES "^([^/]+\\.framework)/") |
| 64 |
set(_fname "${CMAKE_MATCH_1}") |
| 65 |
if(NOT EXISTS "${_fwk_dir}/${_fname}") |
| 66 |
message(STATUS " Bundling framework: ${_fname}") |
| 67 |
file(COPY "${_sd}/${_fname}" DESTINATION "${_fwk_dir}") |
| 68 |
set(_found_new TRUE) |
| 69 |
endif() |
| 70 |
else() |
| 71 |
get_filename_component(_dest "${_fwk_dir}/${_rel}" DIRECTORY) |
| 72 |
file(MAKE_DIRECTORY "${_dest}") |
| 73 |
message(STATUS " Bundling library: ${_rel}") |
| 74 |
file(COPY "${_sd}/${_rel}" DESTINATION "${_dest}") |
| 75 |
set(_found_new TRUE) |
| 76 |
endif() |
| 77 |
break() |
| 78 |
endforeach() |
| 79 |
endforeach() |
| 80 |
endforeach() |
| 81 |
|
| 82 |
if(NOT _found_new) |
| 83 |
message(STATUS " No new deps found in iteration ${_iter}, done.") |
| 84 |
break() |
| 85 |
endif() |
| 86 |
endforeach() |
| 87 |
|
| 88 |
# --- Phase 2: Rewrite @rpath references to @executable_path/../Frameworks/. --- |
| 89 |
collect_bundle_binaries(_all_bins "${_bundle}/Contents/Frameworks" "${_bundle}/Contents/PlugIns") |
| 90 |
foreach(_bin ${_all_bins}) |
| 91 |
get_rpath_deps("${_bin}" _refs) |
| 92 |
foreach(_rel ${_refs}) |
| 93 |
if(EXISTS "${_fwk_dir}/${_rel}") |
| 94 |
execute_process( |
| 95 |
COMMAND install_name_tool -change |
| 96 |
"@rpath/${_rel}" "@executable_path/../Frameworks/${_rel}" "${_bin}" |
| 97 |
ERROR_QUIET |
| 98 |
) |
| 99 |
endif() |
| 100 |
endforeach() |
| 101 |
endforeach() |
| 102 |
|
| 103 |
message(STATUS "Bundle fixup complete.") |