Fix rpath handling and deployment of macOS helper binaries

CPack by default invokes the 'make install' target to install
all project files into a staging area for further packaging.

The order of installation follows the order of install() commands.

One of the first install() commands is the one that installs the
KeePassXC.app bundle and all the contents inside of it,
which includes POST_BUILD copied binaries like keepassxc-cli
and keepassxc-proxy.

Subsequent install(TARGETS) commands would then override the
keepassxc-cli and keepassxc-proxy binaries inside the staging area
with the ones which didn't have macdeployqt run on them (the ones from
src/cli and src/proxy).
Launching the binaries would then fail because of missing rpath
adjustments.

The libkeepassxc-autotype-cocoa.so library was working fine because
there is no install(TARGETS) command for it in a WITH_APP_BUNDLE build,
so the POST_BUILD copy with the adjusted rpaths was preserved.

To fix the issue and make the handling consistent, macdeployqt is no
longer run at POST_BUILD time, but instead at 'make install' time,
after each binary is installed by install(TARGETS).

libkeepassxc-autotype-cocoa.so also has its install command run
unconditionally now.

The build dir binaries that are POST_BUILD copied into
src/KeePassXC.app continue to run because they use the build dir
rpaths that CMake embeds by default. They don't macdeployqt run for
them anymore, which slightly speeds up the build time.

Fixes: #7475
This commit is contained in:
alcroito 2022-03-01 18:24:07 +01:00 committed by Jonathan White
parent 046e50882b
commit abfebea4f2
5 changed files with 50 additions and 10 deletions

View File

@ -33,6 +33,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
# Support Visual Studio Code # Support Visual Studio Code
include(CMakeToolsHelpers OPTIONAL) include(CMakeToolsHelpers OPTIONAL)
include(FeatureSummary) include(FeatureSummary)
include(KPXCHelpers)
include(CheckCCompilerFlag) include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag) include(CheckCXXCompilerFlag)

33
cmake/KPXCHelpers.cmake Normal file
View File

@ -0,0 +1,33 @@
function(kpxc_run_macdeployqt_on_installed_helper_binary app_name installed_binary_relative_path)
# Running macdeployqt on a POST_BUILD copied binary is not useful for CPack, because
# the copied binary will be overriden by the corresponding install(TARGETS) command of the same
# binary.
# Thus, we run macdeployqt using install(CODE) on the already installed binary.
# The precondition is that install(TARGETS) has to be called before this function is called.
set(escaped_prefix "\${CMAKE_INSTALL_PREFIX}")
set(app_bundle_name "${app_name}.app")
set(app_bundle_path "${escaped_prefix}/${app_bundle_name}")
set(installed_binary_path "${escaped_prefix}/${installed_binary_relative_path}")
if(CMAKE_VERSION VERSION_GREATER "3.14")
set(command_echo "COMMAND_ECHO STDOUT")
else()
set(command_echo "")
endif()
install(CODE
"
execute_process(
COMMAND
${MACDEPLOYQT_EXE}
${app_bundle_path}
-executable=${installed_binary_path} -no-plugins 2> /dev/null
${command_echo}
RESULT_VARIABLE exit_code
)
if(NOT exit_code EQUAL 0)
message(FATAL_ERROR
\"Running macdeployqt on ${installed_binary_path} failed with exit code \${exit_code}.\")
endif()
")
endfunction()

View File

@ -4,15 +4,16 @@ add_library(keepassxc-autotype-cocoa MODULE ${autotype_mac_SOURCES})
set_target_properties(keepassxc-autotype-cocoa PROPERTIES LINK_FLAGS "-framework Foundation -framework AppKit -framework Carbon") set_target_properties(keepassxc-autotype-cocoa PROPERTIES LINK_FLAGS "-framework Foundation -framework AppKit -framework Carbon")
target_link_libraries(keepassxc-autotype-cocoa ${PROGNAME} Qt5::Core Qt5::Widgets) target_link_libraries(keepassxc-autotype-cocoa ${PROGNAME} Qt5::Core Qt5::Widgets)
install(TARGETS keepassxc-autotype-cocoa
LIBRARY DESTINATION ${PLUGIN_INSTALL_DIR} COMPONENT Runtime)
if(WITH_APP_BUNDLE) if(WITH_APP_BUNDLE)
add_custom_command(TARGET keepassxc-autotype-cocoa add_custom_command(TARGET keepassxc-autotype-cocoa
POST_BUILD POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/libkeepassxc-autotype-cocoa.so ${PLUGIN_INSTALL_DIR}/libkeepassxc-autotype-cocoa.so COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/libkeepassxc-autotype-cocoa.so ${PLUGIN_INSTALL_DIR}/libkeepassxc-autotype-cocoa.so
COMMAND ${MACDEPLOYQT_EXE} ${PROGNAME}.app -executable=${PLUGIN_INSTALL_DIR}/libkeepassxc-autotype-cocoa.so -no-plugins 2> /dev/null
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/src WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/src
COMMENT "Deploying autotype plugin") COMMENT "Copying autotype plugin into app bundle")
else()
install(TARGETS keepassxc-autotype-cocoa kpxc_run_macdeployqt_on_installed_helper_binary(
BUNDLE DESTINATION . COMPONENT Runtime "${PROGNAME}" "${PLUGIN_INSTALL_DIR}/libkeepassxc-autotype-cocoa.so")
LIBRARY DESTINATION ${PLUGIN_INSTALL_DIR} COMPONENT Runtime)
endif() endif()

View File

@ -71,10 +71,13 @@ endif()
if(APPLE AND WITH_APP_BUNDLE) if(APPLE AND WITH_APP_BUNDLE)
set(CLI_APP_DIR "${CMAKE_BINARY_DIR}/src/${CLI_INSTALL_DIR}") set(CLI_APP_DIR "${CMAKE_BINARY_DIR}/src/${CLI_INSTALL_DIR}")
add_custom_command(TARGET keepassxc-cli add_custom_command(TARGET keepassxc-cli
POST_BUILD POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/keepassxc-cli ${CLI_APP_DIR}/keepassxc-cli COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/keepassxc-cli ${CLI_APP_DIR}/keepassxc-cli
COMMAND ${MACDEPLOYQT_EXE} ${PROGNAME}.app -executable=${CLI_APP_DIR}/keepassxc-cli -no-plugins 2> /dev/null
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/src WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/src
COMMENT "Deploying keepassxc-cli") COMMENT "Copying keepassxc-cli into app bundle")
kpxc_run_macdeployqt_on_installed_helper_binary(
"${PROGNAME}" "${CLI_INSTALL_DIR}/keepassxc-cli")
endif() endif()

View File

@ -34,9 +34,11 @@ if(WITH_XC_BROWSER)
add_custom_command(TARGET keepassxc-proxy add_custom_command(TARGET keepassxc-proxy
POST_BUILD POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/keepassxc-proxy ${PROXY_APP_DIR}/keepassxc-proxy COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/keepassxc-proxy ${PROXY_APP_DIR}/keepassxc-proxy
COMMAND ${MACDEPLOYQT_EXE} ${PROGNAME}.app -executable=${PROXY_APP_DIR}/keepassxc-proxy -no-plugins 2> /dev/null
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/src WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/src
COMMENT "Deploying keepassxc-proxy") COMMENT "Copying keepassxc-proxy into app bundle")
kpxc_run_macdeployqt_on_installed_helper_binary(
"${PROGNAME}" "${PROXY_INSTALL_DIR}/keepassxc-proxy")
endif() endif()
if(WIN32) if(WIN32)