feat: utilize CMake for macOS bundle creation

This commit is contained in:
shenleban tongying 2024-05-03 16:14:27 -04:00
parent c062ab232d
commit a7eaef5469
4 changed files with 79 additions and 13 deletions

View file

@ -24,9 +24,8 @@ project(goldendict-ng
VERSION 24.02.16 VERSION 24.02.16
LANGUAGES CXX C) LANGUAGES CXX C)
if (NOT USE_ALTERNATIVE_NAME) set(GOLDENDICT "goldendict") # binary/executable name
set(GOLDENDICT "goldendict") # binary/executable name if (USE_ALTERNATIVE_NAME OR APPLE)
else ()
set(GOLDENDICT "goldendict-ng") set(GOLDENDICT "goldendict-ng")
endif () endif ()
@ -80,6 +79,9 @@ file(GLOB_RECURSE ALL_SOURCE_FILES CONFIGURE_DEPENDS src/*.cc src/*.hh src/*.c)
if (APPLE) if (APPLE)
file(GLOB_RECURSE MACOS_SOURCE_FILES CONFIGURE_DEPENDS src/macos/*.mm) file(GLOB_RECURSE MACOS_SOURCE_FILES CONFIGURE_DEPENDS src/macos/*.mm)
set(MACOS_APP_ICON ${CMAKE_SOURCE_DIR}/icons/macicon.icns)
set_source_files_properties(${MACOS_APP_ICON} PROPERTIES MACOSX_PACKAGE_LOCATION "Resources")
endif () endif ()
if (WIN32) if (WIN32)
@ -102,6 +104,7 @@ target_sources(${GOLDENDICT} PRIVATE
src/stylesheets/css.qrc src/stylesheets/css.qrc
${ALL_SOURCE_FILES} ${ALL_SOURCE_FILES}
${MACOS_SOURCE_FILES} ${MACOS_SOURCE_FILES}
${MACOS_APP_ICON}
${QSINGLEAPP_SOURCE_FILES} ${QSINGLEAPP_SOURCE_FILES}
${WINDOWS_ICON_RC} ${WINDOWS_ICON_RC}
) )
@ -198,12 +201,51 @@ qt_add_translations(${GOLDENDICT} TS_FILES ${TRANS_FILES}
#### installation or assemble redistribution #### installation or assemble redistribution
if (APPLE) if (APPLE)
set(PLIST_FILE "${CMAKE_BINARY_DIR}/info_generated.plist")
configure_file("${CMAKE_SOURCE_DIR}/redist/mac_info_plist_template_cmake.plist" "${PLIST_FILE}" @ONLY)
set_target_properties(${GOLDENDICT} PROPERTIES set_target_properties(${GOLDENDICT} PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE MACOSX_BUNDLE TRUE
MACOSX_BUNDLE_INFO_PLIST "${PLIST_FILE}"
) )
set(Assembling_Dir "${CMAKE_BINARY_DIR}/redist")
set(Redistributable_APP "${Assembling_Dir}/${GOLDENDICT}.app")
qt_generate_deploy_script(
TARGET ${GOLDENDICT}
OUTPUT_SCRIPT deploy_script
CONTENT "qt_deploy_runtime_dependencies(
EXECUTABLE \"${Redistributable_APP}\"
GENERATE_QT_CONF
NO_APP_STORE_COMPLIANCE
)"
)
install(TARGETS ${GOLDENDICT} BUNDLE DESTINATION "${Assembling_Dir}")
install(FILES ${qm_files} DESTINATION "${Redistributable_APP}/Contents/MacOS/locale")
install(DIRECTORY "${CMAKE_SOURCE_DIR}/opencc" DESTINATION "${Redistributable_APP}/Contents/MacOS")
install(SCRIPT ${deploy_script})
install(CODE "execute_process(COMMAND codesign --force --deep -s - ${Redistributable_APP})")
find_program(CREATE-DMG "create-dmg")
if (CREATE-DMG)
install(CODE "
execute_process(COMMAND ${CREATE-DMG} \
--skip-jenkins \
--format \"ULMO\"
--volname ${CMAKE_PROJECT_NAME}-${CMAKE_PROJECT_VERSION}-${CMAKE_SYSTEM_PROCESSOR} \
--volicon ${CMAKE_SOURCE_DIR}/icons/macicon.icns \
--icon \"goldendict-ng.app\" 100 100
--app-drop-link 300 100 \
\"${CMAKE_PROJECT_NAME}-${CMAKE_PROJECT_VERSION}-${CMAKE_SYSTEM_PROCESSOR}.dmg\" \
\"${Assembling_Dir}\")"
)
else ()
message(WARNING "create-dmg not found. No .dmg will be created")
endif ()
endif () endif ()
if (LINUX OR BSD) if (LINUX OR BSD)

View file

@ -1,8 +1,12 @@
#### Include Paths #### Various workarounds
if (APPLE) if (APPLE)
# old & new homebrew's include paths # old & new homebrew's include paths
target_include_directories(${GOLDENDICT} PRIVATE /usr/local/include /opt/homebrew/include) target_include_directories(${GOLDENDICT} PRIVATE /usr/local/include /opt/homebrew/include)
# libzim depends on ICU, but the ICU from homebrew is "key-only", we need to manually prioritize it
# See `brew info icu4c` if this no longer works
set(ENV{PKG_CONFIG_PATH} "$ENV{PKG_CONFIG_PATH}:/usr/local/opt/icu4c/lib/pkgconfig:/opt/homebrew/opt/icu4c/lib/pkgconfig")
endif () endif ()
target_include_directories(${GOLDENDICT} PRIVATE target_include_directories(${GOLDENDICT} PRIVATE

View file

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
<dict>
<key>CFBundleExecutable</key>
<string>@GOLDENDICT@</string>
<key>CFBundleIconFile</key>
<string>macicon.icns</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>@CMAKE_PROJECT_VERSION@</string>
<key>CFBundleIdentifier</key>
<string>org.xiaoyifang</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
</dict>
</plist>

View file

@ -37,6 +37,8 @@ And a few compression libraries:
## CMake Build ## CMake Build
Basically, you need those commands:
```shell ```shell
cd goldendict-ng && mkdir build_dir cd goldendict-ng && mkdir build_dir
# config step # config step
@ -80,14 +82,14 @@ Use`windeployqt.exe {your_build_dir}/goldendict.exe` which will copy the qt rela
### macOS ### macOS
Similar to Linux build, but need `macdeployqt ./goldendict.app` to copy necessary dependencies to the app bundle. If you build in an IDE, then the created `goldendict-ng.app` will be runnable from the IDE which set up necessary magics for you.
To make the `.app` runnable elsewhere, you can run `cmake --install build_dir/` which will invoke macdeployqt, ad-hoc code signing and various other things. The produced app will end up in `build_dir/redist/goldendict-ng.app`
To create `.dmg` installer, you have to have [create-dmg](https://github.com/create-dmg/create-dmg) installed on your machine, then also `cmake --install build_dir/`.
## Qmake ## Qmake
```shell
git clone https://github.com/xiaoyifang/goldendict-ng.git
```
### Build Steps ### Build Steps
**Notice**: All additional configs for `qmake` that must be combined in one of pass config options to `qmake`: **Notice**: All additional configs for `qmake` that must be combined in one of pass config options to `qmake`: