diff --git a/cmake/MacOSX/codesign.cmake b/cmake/MacOSX/codesign.cmake new file mode 100644 index 00000000..bbf4d1ce --- /dev/null +++ b/cmake/MacOSX/codesign.cmake @@ -0,0 +1,91 @@ +# Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin +# SPDX-License-Identifier: BSD-3-Clause + +## Code signing for the install rules of the macOS package (cmake/package_mac_productbuild.cmake, +## src/openms_gui/add_mac_bundle.cmake). Not used at configure time: those install rules include +## this file when CPack installs a component. +## +## openms_gui_codesign( IDENTITY [IDENTIFIER ] [TIMESTAMP] [OPTIONS ...]) +## Signs (a file or a bundle) with the hardened runtime. TIMESTAMP requests a secure +## timestamp from Apple's timestamp service, over the network; a single request of a nightly's +## many can fail. A failed codesign is therefore tried again (3 attempts, 10 s apart), and the +## last failure stops the installation with the output of codesign, which CPack otherwise does +## not show (it hides the STATUS messages of install rules). +function(openms_gui_codesign _path) + cmake_parse_arguments(PARSE_ARGV 1 _cs "TIMESTAMP" "IDENTITY;IDENTIFIER" "OPTIONS") + set(_cmd codesign --force --options runtime ${_cs_OPTIONS}) + if(_cs_TIMESTAMP) + list(APPEND _cmd --timestamp) + endif() + if(_cs_IDENTIFIER) + list(APPEND _cmd -i "${_cs_IDENTIFIER}") + endif() + list(APPEND _cmd --sign "${_cs_IDENTITY}" "${_path}") + set(_attempts 3) + foreach(_attempt RANGE 1 ${_attempts}) + execute_process(COMMAND ${_cmd} RESULT_VARIABLE _result OUTPUT_VARIABLE _out ERROR_VARIABLE _out) + if(_result EQUAL 0) + return() + endif() + string(STRIP "${_out}" _out) + if(_attempt LESS _attempts) + message(WARNING "codesign failed for ${_path} (attempt ${_attempt} of ${_attempts}, exit code ${_result}), trying again: ${_out}") + execute_process(COMMAND sleep 10) + endif() + endforeach() + message(FATAL_ERROR "codesign failed for ${_path} (exit code ${_result}): ${_out}") +endfunction() + +## openms_gui_codesign_dir( IDENTITY IDENTIFIER_PREFIX [TIMESTAMP]) +## Signs what holds, with the identifiers .: +## - each framework as a bundle: its versions (Versions/A), which seals the binary and, as +## resources, everything else in it (Info.plist, Headers/). One request per framework, not one +## per header, and the signature macOS expects of a framework; +## - every other file on its own (libraries, plugins), except symbolic links. +function(openms_gui_codesign_dir _dir) + cmake_parse_arguments(PARSE_ARGV 1 _cs "TIMESTAMP" "IDENTITY;IDENTIFIER_PREFIX" "") + set(_timestamp "") + if(_cs_TIMESTAMP) + set(_timestamp TIMESTAMP) + endif() + ## Not into linked folders (Versions/Current): the install script sets no policies. + cmake_policy(PUSH) + cmake_policy(SET CMP0009 NEW) + file(GLOB_RECURSE _entries LIST_DIRECTORIES true "${_dir}/*") + cmake_policy(POP) + set(_frameworks "") + set(_files "") + foreach(_entry IN LISTS _entries) + if(IS_SYMLINK "${_entry}") + continue() + endif() + file(RELATIVE_PATH _relative "${_dir}" "${_entry}") + if(_relative MATCHES "^(.*/)?[^/]+[.]framework$") + list(APPEND _frameworks "${_entry}") + elseif(NOT IS_DIRECTORY "${_entry}" AND NOT _relative MATCHES "[.]framework/") + list(APPEND _files "${_entry}") + endif() + endforeach() + + foreach(_file IN LISTS _files) + get_filename_component(_name "${_file}" NAME) + openms_gui_codesign("${_file}" IDENTITY "${_cs_IDENTITY}" IDENTIFIER "${_cs_IDENTIFIER_PREFIX}.${_name}" ${_timestamp}) + endforeach() + + foreach(_framework IN LISTS _frameworks) + get_filename_component(_name "${_framework}" NAME_WE) + file(GLOB _versions LIST_DIRECTORIES true "${_framework}/Versions/*") + set(_signed FALSE) + foreach(_version IN LISTS _versions) + ## Versions/Current is a link to one of them + if(IS_SYMLINK "${_version}" OR NOT IS_DIRECTORY "${_version}") + continue() + endif() + openms_gui_codesign("${_version}" IDENTITY "${_cs_IDENTITY}" IDENTIFIER "${_cs_IDENTIFIER_PREFIX}.${_name}" ${_timestamp}) + set(_signed TRUE) + endforeach() + if(NOT _signed) + message(FATAL_ERROR "${_framework} has no Versions/ folder to sign") + endif() + endforeach() +endfunction() diff --git a/cmake/package_mac_productbuild.cmake b/cmake/package_mac_productbuild.cmake index edd4e2c4..f11408a5 100644 --- a/cmake/package_mac_productbuild.cmake +++ b/cmake/package_mac_productbuild.cmake @@ -102,24 +102,16 @@ set(_openms_fix "${OPENMS_HOST_DIRECTORY}/cmake/MacOSX/fix_dependencies.rb") if("${CPACK_BUNDLE_APPLE_CERT_APP}" STREQUAL "-") set(_openms_codesign_timestamp "") else() - set(_openms_codesign_timestamp "--timestamp") + set(_openms_codesign_timestamp "TIMESTAMP") endif() -## codesign every file under (no symbolic links) with the identifier . +## codesign what holds, with the identifiers .: the frameworks as bundles, +## every other file on its own (openms_gui_codesign_dir in cmake/MacOSX/codesign.cmake) macro(_openms_gui_sign_files _dir _id_prefix _component) install(CODE " - file(GLOB_RECURSE _sign_files LIST_DIRECTORIES false \"\${CMAKE_INSTALL_PREFIX}/${_dir}/*\") - foreach(_sign_file IN LISTS _sign_files) - if(IS_SYMLINK \"\${_sign_file}\") - continue() - endif() - get_filename_component(_sign_name \"\${_sign_file}\" NAME) - execute_process(COMMAND codesign --force --options runtime ${_openms_codesign_timestamp} -i ${_id_prefix}.\${_sign_name} --sign \"${CPACK_BUNDLE_APPLE_CERT_APP}\" \"\${_sign_file}\" - RESULT_VARIABLE _sign_result OUTPUT_VARIABLE _sign_out ERROR_VARIABLE _sign_out) - message(STATUS \"\${_sign_out}\") - if(NOT _sign_result EQUAL 0) - message(FATAL_ERROR \"codesign failed for \${_sign_file} (\${_sign_result})\") - endif() - endforeach()" + include(\"${OPENMS_HOST_DIRECTORY}/cmake/MacOSX/codesign.cmake\") + openms_gui_codesign_dir(\"\${CMAKE_INSTALL_PREFIX}/${_dir}\" + IDENTITY \"${CPACK_BUNDLE_APPLE_CERT_APP}\" + IDENTIFIER_PREFIX ${_id_prefix} ${_openms_codesign_timestamp})" COMPONENT ${_component}) endmacro() ## fix_dependencies.rb ; fails the installation when the script fails (a function: diff --git a/src/openms_gui/add_mac_bundle.cmake b/src/openms_gui/add_mac_bundle.cmake index 06215bb3..7eb09329 100644 --- a/src/openms_gui/add_mac_bundle.cmake +++ b/src/openms_gui/add_mac_bundle.cmake @@ -88,17 +88,15 @@ endif()" COMPONENT ${OPENMS_GUI_APPLICATIONS_COMPONENT}) ## The identity is CPACK_BUNDLE_APPLE_CERT_APP (the top-level CMakeLists.txt; '-' signs ad hoc). ## It has to be unique and present in an unlocked keychain of the search list. A secure ## timestamp is only requested for a real identity (ad hoc signatures cannot have one). + ## A failed codesign is tried again and reported with its output (cmake/MacOSX/codesign.cmake). if("${CPACK_BUNDLE_APPLE_CERT_APP}" STREQUAL "-") set(_codesign_timestamp "") else() - set(_codesign_timestamp "--timestamp") + set(_codesign_timestamp "TIMESTAMP") endif() install(CODE " -execute_process(COMMAND codesign --deep --force --options runtime ${_codesign_timestamp} --sign \"${CPACK_BUNDLE_APPLE_CERT_APP}\" -i de.openms.${_name} \${CMAKE_INSTALL_PREFIX}/${_name}.app RESULT_VARIABLE sign_result OUTPUT_VARIABLE sign_out ERROR_VARIABLE sign_out) -message(STATUS \"\${sign_out}\") -if(NOT sign_result EQUAL 0) - message(FATAL_ERROR \"codesign failed for ${_name}.app (\${sign_result})\") -endif() +include(\"${OPENMS_HOST_DIRECTORY}/cmake/MacOSX/codesign.cmake\") +openms_gui_codesign(\"\${CMAKE_INSTALL_PREFIX}/${_name}.app\" IDENTITY \"${CPACK_BUNDLE_APPLE_CERT_APP}\" IDENTIFIER de.openms.${_name} ${_codesign_timestamp} OPTIONS --deep) execute_process(COMMAND codesign -dv \${CMAKE_INSTALL_PREFIX}/${_name}.app OUTPUT_VARIABLE sign_check_out ERROR_VARIABLE sign_check_out) message(STATUS \"\${sign_check_out}\")" COMPONENT ${OPENMS_GUI_APPLICATIONS_COMPONENT}) unset(_codesign_timestamp)