From abfebea4f222df1d0cbd7d4dc22d7b6b3416a1d6 Mon Sep 17 00:00:00 2001 From: alcroito Date: Tue, 1 Mar 2022 18:24:07 +0100 Subject: [PATCH] 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 --- CMakeLists.txt | 1 + cmake/KPXCHelpers.cmake | 33 +++++++++++++++++++++++++++++++++ src/autotype/mac/CMakeLists.txt | 13 +++++++------ src/cli/CMakeLists.txt | 7 +++++-- src/proxy/CMakeLists.txt | 6 ++++-- 5 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 cmake/KPXCHelpers.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index def89ea89..68a346e36 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake) # Support Visual Studio Code include(CMakeToolsHelpers OPTIONAL) include(FeatureSummary) +include(KPXCHelpers) include(CheckCCompilerFlag) include(CheckCXXCompilerFlag) diff --git a/cmake/KPXCHelpers.cmake b/cmake/KPXCHelpers.cmake new file mode 100644 index 000000000..b7896401a --- /dev/null +++ b/cmake/KPXCHelpers.cmake @@ -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() diff --git a/src/autotype/mac/CMakeLists.txt b/src/autotype/mac/CMakeLists.txt index 883eeab73..1f77ac184 100644 --- a/src/autotype/mac/CMakeLists.txt +++ b/src/autotype/mac/CMakeLists.txt @@ -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") 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) add_custom_command(TARGET keepassxc-autotype-cocoa POST_BUILD 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 - COMMENT "Deploying autotype plugin") -else() - install(TARGETS keepassxc-autotype-cocoa - BUNDLE DESTINATION . COMPONENT Runtime - LIBRARY DESTINATION ${PLUGIN_INSTALL_DIR} COMPONENT Runtime) + COMMENT "Copying autotype plugin into app bundle") + + kpxc_run_macdeployqt_on_installed_helper_binary( + "${PROGNAME}" "${PLUGIN_INSTALL_DIR}/libkeepassxc-autotype-cocoa.so") endif() diff --git a/src/cli/CMakeLists.txt b/src/cli/CMakeLists.txt index f0d6cbd46..b25c6b06d 100644 --- a/src/cli/CMakeLists.txt +++ b/src/cli/CMakeLists.txt @@ -71,10 +71,13 @@ endif() if(APPLE AND WITH_APP_BUNDLE) set(CLI_APP_DIR "${CMAKE_BINARY_DIR}/src/${CLI_INSTALL_DIR}") + add_custom_command(TARGET keepassxc-cli POST_BUILD 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 - 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() diff --git a/src/proxy/CMakeLists.txt b/src/proxy/CMakeLists.txt index 589d7f7d8..066d65b81 100755 --- a/src/proxy/CMakeLists.txt +++ b/src/proxy/CMakeLists.txt @@ -34,9 +34,11 @@ if(WITH_XC_BROWSER) add_custom_command(TARGET keepassxc-proxy POST_BUILD 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 - 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() if(WIN32)