Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: Xcode based app bundle and code signing #1693

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ option(IMHEX_USE_DEFAULT_BUILD_SETTINGS "Use default build settings"
option(IMHEX_STRICT_WARNINGS "Enable most available warnings and treat them as errors" ON )
option(IMHEX_STATIC_LINK_PLUGINS "Statically link all plugins into the main executable" OFF)
option(IMHEX_GENERATE_PACKAGE "Specify if a native package should be built. (Windows and MacOS only)" OFF)
option(IMHEX_GENERATE_PACKAGE_XCODE "Specify if a native package should be built with Xcode. (Experimental)" OFF)
option(IMHEX_ENABLE_SANDBOXING_XCODE "Enable sandboxing, hardened runtime and adhoc signing on macOS when using Xcode" OFF)
option(IMHEX_ENABLE_UNITY_BUILD "Enables building ImHex as a unity build." OFF)
option(IMHEX_GENERATE_PDBS "Enable generating PDB files in non-debug builds (Windows only)" OFF)
option(IMHEX_REPLACE_DWARF_WITH_PDB "Remove DWARF information from binaries when generating PDBS (Windows only)" OFF)
Expand Down
6 changes: 5 additions & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@

"cacheVariables": {
"CMAKE_C_COMPILER": "clang",

"CMAKE_CXX_COMPILER": "clang++",
"CMAKE_CXX_FLAGS": "-fexperimental-library -Wno-shorten-64-to-32 -Wno-deprecated-declarations",

"IMHEX_EXCLUDE_PLUGINS": "windows",

"IMHEX_OFFLINE_BUILD": "YES",
"IMHEX_GENERATE_PACKAGE_XCODE": "YES",

"IMHEX_IDE_HELPERS_OVERRIDE_XCODE_COMPILER": "ON"
}
}
Expand Down
61 changes: 60 additions & 1 deletion cmake/build_helpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,17 @@ macro(createPackage)
endif()

if (APPLE)
# Verify we are running with sane options
if (IMHEX_GENERATE_PACKAGE_XCODE)
if (IMHEX_GENERATE_PACKAGE)
message(FATAL_ERROR "IMHEX_GENERATE_PACKAGE_XCODE and IMHEX_GENERATE_PACKAGE are incompatible!")
endif()

if (NOT XCODE)
message(FATAL_ERROR "IMHEX_GENERATE_PACKAGE_XCODE can only be used with Xcode")
endif()
endif()

if (IMHEX_GENERATE_PACKAGE)
include(PostprocessBundle)

Expand Down Expand Up @@ -273,7 +284,52 @@ macro(createPackage)
endif()

install(CODE [[ message(STATUS "MacOS Bundle finalized. DO NOT TOUCH IT ANYMORE! ANY MODIFICATIONS WILL BREAK IT FROM NOW ON!") ]])
elseif(IMHEX_GENERATE_PACKAGE_XCODE)
# Main executable will be built as an application bundle
set_target_properties(main PROPERTIES
MACOSX_BUNDLE YES
MACOSX_BUNDLE_INFO_PLIST "${CMAKE_SOURCE_DIR}/resources/dist/macos/MacOSXBundleInfo.plist.in"
)

# Embed plugins
set_target_properties(main PROPERTIES
XCODE_EMBED_APP_EXTENSIONS "${PLUGINS}"
)

# Download and embed required resources
set(bundleResourcesBaseDir "${CMAKE_BINARY_DIR}/BundleResources")
downloadImHexPatternsFiles("${bundleResourcesBaseDir}")

set(bundleResources "")
list(APPEND bundleResources "${bundleResourcesBaseDir}/constants")
list(APPEND bundleResources "${bundleResourcesBaseDir}/encodings")
list(APPEND bundleResources "${bundleResourcesBaseDir}/includes")
list(APPEND bundleResources "${bundleResourcesBaseDir}/patterns")
list(APPEND bundleResources "${bundleResourcesBaseDir}/magic")
list(APPEND bundleResources "${bundleResourcesBaseDir}/nodes")

set_target_properties(main PROPERTIES
XCODE_EMBED_RESOURCES "${bundleResources}"
)

if (IMHEX_ENABLE_SANDBOXING_XCODE)
# Application sandbox requires a bundle ID, entitlements, and signing
set_target_properties(main PROPERTIES
XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "net.WerWolv.ImHex"

XCODE_ATTRIBUTE_ENABLE_APP_SANDBOX "YES"
XCODE_ATTRIBUTE_ENABLE_HARDENED_RUNTIME "YES"
XCODE_ATTRIBUTE_CODE_SIGN_ENTITLEMENTS "${CMAKE_SOURCE_DIR}/resources/dist/macos/main.entitlements"
)
else()
# Xcode will by default try to sign the final bundle, disable this
set_target_properties(main PROPERTIES
XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED "NO"
XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY ""
)
endif()
else()
# Download required resources to (potentially configuration dependent) main output dir
downloadImHexPatternsFiles("${IMHEX_MAIN_OUTPUT_DIRECTORY}")
endif()
else()
Expand Down Expand Up @@ -379,10 +435,13 @@ macro(configureCMake)
set(CMAKE_POLICY_DEFAULT_CMP0063 NEW)

set(CMAKE_WARN_DEPRECATED OFF CACHE BOOL "Disable deprecated warnings" FORCE)

# XCODE_EMBED_FRAMEWORKS only works for monolithic projects
set(CMAKE_XCODE_GENERATE_TOP_LEVEL_PROJECT_ONLY ON)
endmacro()

function(configureProject)
if (XCODE)
if (XCODE AND NOT IMHEX_GENERATE_PACKAGE_XCODE)
# Support Xcode's multi configuration paradigm by placing built artifacts into separate directories
set(IMHEX_MAIN_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Configs/$<CONFIG>" PARENT_SCOPE)
else()
Expand Down
5 changes: 5 additions & 0 deletions lib/libimhex/source/helpers/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ namespace hex::fs {

#elif defined(OS_MACOS)

paths.push_back(wolv::io::fs::getMainBundleResourcesDirectoryPath());
paths.push_back(wolv::io::fs::getApplicationSupportDirectoryPath());

#elif defined(OS_LINUX) || defined(OS_WEB)
Expand Down Expand Up @@ -395,6 +396,10 @@ namespace hex::fs {
break;
case ImHexPath::Plugins:
result = appendPath(getPluginPaths(), "plugins");

#if defined(OS_MACOS)
result.push_back(wolv::io::fs::getMainBundleBuiltInPlugInsDirectoryPath());
#endif
break;
case ImHexPath::Libraries:
result = appendPath(getPluginPaths(), "lib");
Expand Down
6 changes: 6 additions & 0 deletions main/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ add_executable(main ${APPLICATION_TYPE}
${IMHEX_ICON}
)

# NB: Source file level properties must be set in the same CMakeLists.txt
# file in which their target resides for them to take effect
set_source_files_properties(${IMHEX_ICON} PROPERTIES
MACOSX_PACKAGE_LOCATION "Resources"
)

target_include_directories(main PUBLIC include)
setupCompilerFlags(main)

Expand Down
92 changes: 92 additions & 0 deletions resources/dist/macos/MacOSXBundleInfo.plist.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- https://developer.apple.com/documentation/bundleresources/information_property_list -->

<!-- Core settings / Bundle configuration / Categorization -->

<key>CFBundlePackageType</key>
<string>APPL</string>

<key>LSApplicationCategoryType</key>
<string>public.app-category.developer-tools</string>

<!-- Core settings / Bundle configuration / Identification -->

<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>

<key>CFBundleName</key>
<string>ImHex</string>

<!-- Core settings / Bundle configuration / Naming -->

<key>CFBundleVersion</key>
<string>${IMHEX_VERSION}</string>

<!-- Core settings / Bundle configuration / Bundle version -->

<key>CFBundleVersion</key>
<string>${IMHEX_VERSION}</string>

<key>CFBundleShortVersionString</key>
<string>${IMHEX_VERSION}</string>

<key>NSHumanReadableCopyright</key>
<string>${IMHEX_COPYRIGHT}</string>

<!-- Core settings / User Interface / Icons -->

<key>CFBundleIconFile</key>
<string>AppIcon.icns</string>

<!-- Core settings / Localization -->

<key>CFBundleDevelopmentRegion</key>
<string>en</string>

<key>CFBundleLocalizations</key>
<array>
<string>de_DE</string>
<string>en_US</string>
<string>es_ES</string>
<string>hu_HU</string>
<string>it_IT</string>
<string>ja_JP</string>
<string>ko_KR</string>
<string>pt_BR</string>
<string>zh_CN</string>
<string>zh_TW</string>
</array>

<!-- Core settings / App execution / Launch -->

<key>CFBundleExecutable</key>
<string>imhex</string>

<!-- Servives / Data and storage / Documents -->

<key>CFBundleDocumentTypes</key>
<array>
<!-- ImHex can edit any file format -->
<dict>
<key>CFBundleTypeName</key>
<string>All Files</string>

<key>CFBundleTypeRole</key>
<string>Editor</string>

<key>NSDocumentClass</key>
<string>HexDocument</string>

<!-- NB: This key is not listed in the documentation -->
<key>CFBundleTypeExtensions</key>
<array>
<string>*</string>
</array>
</dict>
</array>

</dict>
</plist>
21 changes: 21 additions & 0 deletions resources/dist/macos/main.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- ImHex is subject to sandboxing, and will declare entitlements in this file -->
<key>com.apple.security.app-sandbox</key>
<true/>

<!-- ImHex may have read-write access to files the user has selected using an Open or Save dialog. -->
<key>com.apple.security.files.user-selected.read-write</key>
<true/>

<!-- ImHex may open outgoing network connections -->
<key>com.apple.security.network.client</key>
<true/>

<!-- ImHex runs in a hardened runtime, but allows loading unsigned plugins -->
<key>com.apple.security.cs.disable-library-validation</key>
<true/>
</dict>
</plist>
Loading