fix(cpn): tie macOS bundle to its own SDL copies, and verify it (#7653)
Co-authored-by: Peter Feerick <5500713+pfeerick@users.noreply.github.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Peter Feerick
Claude Opus 5
parent
2025ac7274
commit
c562f486d0
@@ -570,26 +570,35 @@ elseif(APPLE)
|
||||
# Run macdeployqt
|
||||
install(SCRIPT ${qt_deploy_companion} COMPONENT Runtime)
|
||||
|
||||
option(COMPANION_VERIFY_BUNDLE
|
||||
"Fail the install if the macOS bundle references libraries outside itself" ON)
|
||||
set(COMPANION_VERIFY_BUNDLE_ALLOW "" CACHE STRING
|
||||
"Regex of absolute library paths the bundle verification may ignore")
|
||||
|
||||
# Bundle SDL2 (copy the real dylib; IMPORTED_RUNTIME_ARTIFACTS keeps
|
||||
# Homebrew symlinks and breaks the app inside a .dmg). Homebrew's
|
||||
# sdl2-compat also loads SDL3 from @loader_path/libSDL3.dylib at runtime.
|
||||
set(_bundle_expects_sdl3 FALSE)
|
||||
set(_bundled_sdl2 FALSE)
|
||||
if(TARGET SDL2::SDL2)
|
||||
get_target_property(_sdl2_location SDL2::SDL2 IMPORTED_LOCATION)
|
||||
if(NOT _sdl2_location)
|
||||
get_target_property(_sdl2_location SDL2::SDL2 IMPORTED_LOCATION_RELEASE)
|
||||
endif()
|
||||
if(_sdl2_location AND _sdl2_location MATCHES "\\.framework(/|$)")
|
||||
# Genuine upstream SDL2.framework (CI's setup-sdl2.sh, or a manual framework
|
||||
# install): no SDL3 dlopen shim to worry about, and IMPORTED_RUNTIME_ARTIFACTS
|
||||
# copies frameworks correctly — it's only the Homebrew dylib case below whose
|
||||
# symlinks it mangles. This is what CI has shipped all along.
|
||||
# Genuine upstream SDL2.framework (a manual framework install): no SDL3 dlopen
|
||||
# shim to worry about, and IMPORTED_RUNTIME_ARTIFACTS copies frameworks
|
||||
# correctly — it's only the dylib case below whose symlinks it mangles.
|
||||
# CI no longer takes this path; setup-sdl.sh builds sdl2-compat + SDL3 instead.
|
||||
message(STATUS "Bundling SDL2 framework: ${_sdl2_location}")
|
||||
install(IMPORTED_RUNTIME_ARTIFACTS SDL2::SDL2
|
||||
FRAMEWORK DESTINATION "${companion_app_dir}/Contents/Frameworks"
|
||||
LIBRARY DESTINATION "${companion_app_dir}/Contents/Frameworks"
|
||||
COMPONENT Runtime)
|
||||
set(_bundled_sdl2 TRUE)
|
||||
elseif(_sdl2_location)
|
||||
message(STATUS "Bundling SDL2 library: ${_sdl2_location}")
|
||||
set(_bundled_sdl2 TRUE)
|
||||
get_filename_component(_sdl2_real "${_sdl2_location}" REALPATH)
|
||||
get_filename_component(_sdl2_name "${_sdl2_location}" NAME)
|
||||
install(FILES "${_sdl2_real}"
|
||||
@@ -610,6 +619,10 @@ elseif(APPLE)
|
||||
endif()
|
||||
if(NOT _sdl3_real)
|
||||
set(_sdl3_hints "")
|
||||
# SDL3 usually lives right next to SDL2 (setup-sdl.sh installs both to one prefix) -
|
||||
# check there before falling back to Homebrew heuristics.
|
||||
get_filename_component(_sdl2_libdir "${_sdl2_location}" DIRECTORY)
|
||||
list(APPEND _sdl3_hints "${_sdl2_libdir}")
|
||||
if(DEFINED ENV{HOMEBREW_PREFIX})
|
||||
list(APPEND _sdl3_hints
|
||||
"$ENV{HOMEBREW_PREFIX}/opt/sdl3/lib"
|
||||
@@ -645,8 +658,12 @@ elseif(APPLE)
|
||||
DESTINATION "${companion_app_dir}/Contents/Frameworks"
|
||||
RENAME "libSDL3.dylib"
|
||||
COMPONENT Runtime)
|
||||
set(_bundle_expects_sdl3 TRUE)
|
||||
else()
|
||||
message(WARNING "SDL3 not found; packaged Companion may crash if linked against sdl2-compat")
|
||||
message(FATAL_ERROR
|
||||
"SDL3 not found, but the bundled SDL2 (${_sdl2_location}) is sdl2-compat, which "
|
||||
"dlopens libSDL3.dylib at runtime - Companion would not start without it. Install "
|
||||
"SDL3, or point CMAKE_PREFIX_PATH/HOMEBREW_PREFIX at a prefix that has it.")
|
||||
endif()
|
||||
|
||||
install(CODE "
|
||||
@@ -673,6 +690,21 @@ elseif(APPLE)
|
||||
else()
|
||||
message(WARNING "SDL2::SDL2 has no IMPORTED_LOCATION; SDL2 will not be bundled")
|
||||
endif()
|
||||
|
||||
# Point the executable at the SDL copies bundled above. qt_deploy_runtime_dependencies
|
||||
# ran before they existed, and install_name_tool -id only fixes each dylib's own id, so
|
||||
# nothing else guarantees this. Runs for both the framework and dylib branches, and must
|
||||
# stay ahead of the re-sign below so the signature covers the rewritten load commands.
|
||||
# Skipped when nothing was bundled - rewriting to Frameworks would then point the
|
||||
# executable at files that are not there.
|
||||
if(_bundled_sdl2)
|
||||
configure_file(
|
||||
"${COMPANION_TARGETS_DIR}/bundle_sdl_fixup.cmake.in"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/bundle_sdl_fixup.cmake"
|
||||
@ONLY
|
||||
)
|
||||
install(SCRIPT "${CMAKE_CURRENT_BINARY_DIR}/bundle_sdl_fixup.cmake" COMPONENT Runtime)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Re-sign bundle after all contents are installed
|
||||
@@ -685,6 +717,17 @@ elseif(APPLE)
|
||||
)
|
||||
" COMPONENT Runtime)
|
||||
|
||||
# Last gate: a bundle that still points at /opt/homebrew or a build-machine Qt prefix
|
||||
# starts on the machine that built it and nowhere else. Fail here rather than ship it.
|
||||
if(COMPANION_VERIFY_BUNDLE)
|
||||
configure_file(
|
||||
"${COMPANION_TARGETS_DIR}/bundle_verify.cmake.in"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/bundle_verify.cmake"
|
||||
@ONLY
|
||||
)
|
||||
install(SCRIPT "${CMAKE_CURRENT_BINARY_DIR}/bundle_verify.cmake" COMPONENT Runtime)
|
||||
endif()
|
||||
|
||||
else()
|
||||
# Detect architecture for linuxdeploy AppImage
|
||||
if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64")
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
# Rewrite absolute SDL load commands in the Companion executable to the bundled copies.
|
||||
#
|
||||
# Configured from companion/targets/mac/bundle_sdl_fixup.cmake.in - do not edit the copy in
|
||||
# the build tree.
|
||||
#
|
||||
# Companion used to run fixup_bundle, which walked every Mach-O in the bundle and rewrote all
|
||||
# of their load commands in one pass. That was replaced by qt_deploy_runtime_dependencies,
|
||||
# which runs *before* the SDL install(FILES) steps, and by an install_name_tool -id call that
|
||||
# only fixes each dylib's own id. Nothing in that chain guarantees the executable actually
|
||||
# references the SDL copies sitting in Contents/Frameworks, so make it explicit here.
|
||||
|
||||
set(_app "${CMAKE_INSTALL_PREFIX}/@companion_app_dir@")
|
||||
set(_bin "${_app}/Contents/MacOS/@COMPANION_OSX_APP_BUNDLE_NAME@")
|
||||
|
||||
if(NOT EXISTS "${_bin}")
|
||||
message(FATAL_ERROR "Companion executable not found for SDL fixup: ${_bin}")
|
||||
endif()
|
||||
|
||||
execute_process(
|
||||
COMMAND otool -L "${_bin}"
|
||||
OUTPUT_VARIABLE _otool_out
|
||||
ERROR_VARIABLE _otool_err
|
||||
RESULT_VARIABLE _otool_res
|
||||
)
|
||||
if(NOT _otool_res EQUAL 0)
|
||||
message(FATAL_ERROR "otool -L failed for ${_bin}: ${_otool_err}")
|
||||
endif()
|
||||
|
||||
string(REPLACE "\n" ";" _lines "${_otool_out}")
|
||||
foreach(_line IN LISTS _lines)
|
||||
# dependency lines are tab-indented: "\t<path> (compatibility version ..., current ...)"
|
||||
string(REGEX MATCH "^\t([^ ]+)" _matched "${_line}")
|
||||
if(NOT _matched)
|
||||
continue()
|
||||
endif()
|
||||
set(_ref "${CMAKE_MATCH_1}")
|
||||
|
||||
# @rpath / @executable_path / @loader_path already resolve inside the bundle
|
||||
if(NOT _ref MATCHES "^/")
|
||||
continue()
|
||||
endif()
|
||||
if(NOT _ref MATCHES "SDL2|SDL3")
|
||||
continue()
|
||||
endif()
|
||||
|
||||
# Keep the framework tail (SDL2.framework/Versions/A/SDL2) so the rewritten reference
|
||||
# matches the layout install(IMPORTED_RUNTIME_ARTIFACTS) produced; plain dylibs land
|
||||
# directly in Frameworks under their own name.
|
||||
if(_ref MATCHES "/(SDL[23]\\.framework/.*)$")
|
||||
set(_tail "${CMAKE_MATCH_1}")
|
||||
else()
|
||||
get_filename_component(_tail "${_ref}" NAME)
|
||||
endif()
|
||||
|
||||
set(_new "@executable_path/../Frameworks/${_tail}")
|
||||
message(STATUS "Rewriting SDL reference: ${_ref} -> ${_new}")
|
||||
execute_process(
|
||||
COMMAND install_name_tool -change "${_ref}" "${_new}" "${_bin}"
|
||||
ERROR_VARIABLE _change_err
|
||||
RESULT_VARIABLE _change_res
|
||||
)
|
||||
if(NOT _change_res EQUAL 0)
|
||||
message(FATAL_ERROR "install_name_tool -change failed for ${_ref}: ${_change_err}")
|
||||
endif()
|
||||
endforeach()
|
||||
@@ -0,0 +1,92 @@
|
||||
# Verify the installed Companion .app carries no references to libraries outside itself.
|
||||
#
|
||||
# Configured from companion/targets/mac/bundle_verify.cmake.in - do not edit the copy in the
|
||||
# build tree.
|
||||
#
|
||||
# A .dmg that links against /opt/homebrew, /usr/local/Cellar or a build-machine Qt prefix
|
||||
# starts fine on the machine that built it and fails everywhere else, which is exactly how the
|
||||
# sdl2-compat breakage reached users. Catch it at install time instead.
|
||||
#
|
||||
# Disable with -DCOMPANION_VERIFY_BUNDLE=OFF; widen with -DCOMPANION_VERIFY_BUNDLE_ALLOW=<regex>.
|
||||
|
||||
set(_app "${CMAKE_INSTALL_PREFIX}/@companion_app_dir@")
|
||||
set(_allow "@COMPANION_VERIFY_BUNDLE_ALLOW@")
|
||||
set(_expect_sdl3 "@_bundle_expects_sdl3@")
|
||||
|
||||
if(NOT IS_DIRECTORY "${_app}")
|
||||
message(FATAL_ERROR "Bundle not found for verification: ${_app}")
|
||||
endif()
|
||||
|
||||
# sdl2-compat dlopens "@loader_path/libSDL3.dylib" - invisible to otool, so check by hand
|
||||
if(_expect_sdl3 AND NOT EXISTS "${_app}/Contents/Frameworks/libSDL3.dylib")
|
||||
message(FATAL_ERROR
|
||||
"Contents/Frameworks/libSDL3.dylib is missing. Companion was linked against "
|
||||
"sdl2-compat, which loads SDL3 at runtime, so the bundle will not start.")
|
||||
endif()
|
||||
|
||||
file(GLOB_RECURSE _candidates "${_app}/Contents/*")
|
||||
|
||||
set(_offenders "")
|
||||
set(_checked 0)
|
||||
foreach(_file IN LISTS _candidates)
|
||||
if(IS_SYMLINK "${_file}" OR IS_DIRECTORY "${_file}")
|
||||
continue()
|
||||
endif()
|
||||
|
||||
execute_process(
|
||||
COMMAND otool -L "${_file}"
|
||||
OUTPUT_VARIABLE _otool_out
|
||||
ERROR_VARIABLE _otool_err
|
||||
RESULT_VARIABLE _otool_res
|
||||
)
|
||||
# non-Mach-O files (plists, icons, translations) simply fail the scan - skip them
|
||||
if(NOT _otool_res EQUAL 0 OR _otool_out MATCHES "is not an object file")
|
||||
continue()
|
||||
endif()
|
||||
math(EXPR _checked "${_checked} + 1")
|
||||
|
||||
string(REPLACE "\n" ";" _lines "${_otool_out}")
|
||||
foreach(_line IN LISTS _lines)
|
||||
string(REGEX MATCH "^\t([^ ]+)" _matched "${_line}")
|
||||
if(NOT _matched)
|
||||
continue()
|
||||
endif()
|
||||
set(_ref "${CMAKE_MATCH_1}")
|
||||
|
||||
if(NOT _ref MATCHES "^/")
|
||||
continue() # @rpath / @executable_path / @loader_path resolve inside the bundle
|
||||
endif()
|
||||
if(_ref MATCHES "^/usr/lib/" OR _ref MATCHES "^/System/")
|
||||
continue() # shipped with macOS
|
||||
endif()
|
||||
if(_allow AND _ref MATCHES "${_allow}")
|
||||
continue()
|
||||
endif()
|
||||
|
||||
file(RELATIVE_PATH _rel "${_app}" "${_file}")
|
||||
list(APPEND _offenders "${_rel}: ${_ref}")
|
||||
endforeach()
|
||||
endforeach()
|
||||
|
||||
if(_offenders)
|
||||
list(REMOVE_DUPLICATES _offenders)
|
||||
string(REPLACE ";" "\n " _report "${_offenders}")
|
||||
message(FATAL_ERROR
|
||||
"Bundle references libraries outside itself - this .dmg would not start on another "
|
||||
"machine:\n ${_report}\n"
|
||||
"Re-run with -DCOMPANION_VERIFY_BUNDLE=OFF to package anyway.")
|
||||
endif()
|
||||
|
||||
message(STATUS "Bundle verification: ${_checked} Mach-O files, no external references")
|
||||
|
||||
# The bundle seal is a separate concern from the per-Mach-O signatures: dyld does not consult
|
||||
# CodeResources when loading, so a stale seal does not stop the app launching. It would matter
|
||||
# for Gatekeeper/notarization, so report it - but do not fail a build over it.
|
||||
execute_process(
|
||||
COMMAND codesign --verify --deep --strict --verbose=2 "${_app}"
|
||||
ERROR_VARIABLE _codesign_err
|
||||
RESULT_VARIABLE _codesign_res
|
||||
)
|
||||
if(NOT _codesign_res EQUAL 0)
|
||||
message(WARNING "Bundle signature is not valid (not fatal): ${_codesign_err}")
|
||||
endif()
|
||||
Reference in New Issue
Block a user