From 2340ab05186702f6b184c168d743d45dff2a9c23 Mon Sep 17 00:00:00 2001 From: Mary Date: Tue, 29 Dec 2020 22:50:11 +0100 Subject: [PATCH] cmake: Handle code sign and packaging for macOS bundle --- CMakeLists.txt | 26 +++++++++++++++++++++----- cmake/modules/PostprocessBundle.cmake | 11 ++++++++++- cmake/modules/apple.cmake | 3 +++ 3 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 cmake/modules/apple.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 483d2d449..90fcf72f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,8 @@ project(imhex VERSION 1.5.0) set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules") +option (CREATE_PACKAGE "Create a package with CPack" OFF) + if (APPLE) option (CREATE_BUNDLE "Create a bundle on macOS" OFF) endif() @@ -21,7 +23,7 @@ pkg_search_module(CRYPTO REQUIRED libcrypto) pkg_search_module(CAPSTONE REQUIRED capstone) find_package(OpenGL REQUIRED) find_package(nlohmann_json REQUIRED) -find_package(Python COMPONENTS Interpreter Development) +find_package(Python COMPONENTS Development) add_subdirectory(external/llvm/Demangle) add_subdirectory(plugins/libimhex) @@ -99,8 +101,10 @@ elseif (APPLE) set_source_files_properties(${imhex_icon} PROPERTIES MACOSX_PACKAGE_LOCATION Resources) set(MACOSX_BUNDLE_ICON_FILE "AppIcon.icns") set(MACOSX_BUNDLE_INFO_STRING "WerWolv") + set(MACOSX_BUNDLE_BUNDLE_NAME "ImHex") + set(MACOSX_BUNDLE_BUNDLE_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") set(MACOSX_BUNDLE_GUI_IDENTIFIER "WerWolv.ImHex") - set(MACOSX_BUNDLE_LONG_VERSION_STRING ${PROJECT_VERSION}) + set(MACOSX_BUNDLE_LONG_VERSION_STRING "${PROJECT_VERSION}-${GIT_COMMIT_HASH}") set(MACOSX_BUNDLE_SHORT_VERSION_STRING "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}") set(MACOSX_BUNDLE_COPYRIGHT "Copyright © 2020 WerWolv and Thog. All rights reserved." ) if ("${CMAKE_GENERATOR}" STREQUAL "Xcode") @@ -158,15 +162,27 @@ elseif (UNIX) endif() if (CREATE_BUNDLE) - # Update library references to make the bundle portable include(PostprocessBundle) - postprocess_bundle(imhex) # Fix rpath add_custom_command(TARGET imhex POST_BUILD COMMAND ${CMAKE_INSTALL_NAME_TOOL} -add_rpath "@executable_path/../Frameworks/" $) # FIXME: Remove this once we move/integrate the plugins directory. - add_custom_target(build-time-make-directory ALL COMMAND ${CMAKE_COMMAND} -E make_directory "${bundle_path}/Contents/MacOS/plugins") + add_custom_target(build-time-make-plugins-directory ALL COMMAND ${CMAKE_COMMAND} -E make_directory "${bundle_path}/Contents/MacOS/plugins") + + # Update library references to make the bundle portable + postprocess_bundle(imhex) + + # Enforce DragNDrop packaging. + set(CPACK_GENERATOR "DragNDrop") + + install(TARGETS imhex BUNDLE DESTINATION .) else() install(TARGETS imhex RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) endif() + + +if (CREATE_PACKAGE) + include(apple) + include(CPack) +endif() \ No newline at end of file diff --git a/cmake/modules/PostprocessBundle.cmake b/cmake/modules/PostprocessBundle.cmake index 1d79d1cb3..743a4b6b4 100644 --- a/cmake/modules/PostprocessBundle.cmake +++ b/cmake/modules/PostprocessBundle.cmake @@ -15,7 +15,7 @@ if(CMAKE_GENERATOR) set(_POSTPROCESS_BUNDLE_MODULE_LOCATION "${CMAKE_CURRENT_LIST_FILE}") function(postprocess_bundle target) add_custom_command(TARGET ${target} POST_BUILD - COMMAND ${CMAKE_COMMAND} -DBUNDLE_PATH="$/../.." + COMMAND ${CMAKE_COMMAND} -DBUNDLE_PATH="$/../.." -DCODE_SIGN_CERTIFICATE_ID="${CODE_SIGN_CERTIFICATE_ID}" -P "${_POSTPROCESS_BUNDLE_MODULE_LOCATION}" ) endfunction() @@ -48,3 +48,12 @@ endfunction() include(BundleUtilities) set(BU_CHMOD_BUNDLE_ITEMS ON) fixup_bundle("${BUNDLE_PATH}" "${extra_libs}" "${extra_dirs}" IGNORE_ITEM "Python") + +if (CODE_SIGN_CERTIFICATE_ID) + # Hack around Apple Silicon signing bugs by copying the real app, signing it and moving it back. + # IMPORTANT: DON'T USE ${CMAKE_COMMAND} -E copy_directory HERE (this follow symbolic links). + execute_process(COMMAND cp -R "${BUNDLE_PATH}" "${BUNDLE_PATH}.temp") + execute_process(COMMAND codesign --deep --force --sign "${CODE_SIGN_CERTIFICATE_ID}" "${BUNDLE_PATH}.temp") + execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory "${BUNDLE_PATH}") + execute_process(COMMAND ${CMAKE_COMMAND} -E rename "${BUNDLE_PATH}.temp" "${BUNDLE_PATH}") +endif() diff --git a/cmake/modules/apple.cmake b/cmake/modules/apple.cmake new file mode 100644 index 000000000..85c80a368 --- /dev/null +++ b/cmake/modules/apple.cmake @@ -0,0 +1,3 @@ +set (CPACK_BUNDLE_NAME "ImHex") +set (CPACK_BUNDLE_ICON "${CMAKE_SOURCE_DIR}/res/mac/AppIcon.icns" ) +set (CPACK_BUNDLE_PLIST "${CMAKE_BINARY_DIR}/ImHex.app/Contents/Info.plist")