Skip to content

Commit

Permalink
opt: reinvent winlibs with vcpkg export (#1669)
Browse files Browse the repository at this point in the history
  • Loading branch information
shenlebantongying authored Jul 11, 2024
1 parent 34bc4ca commit 3f6cfb8
Show file tree
Hide file tree
Showing 175 changed files with 57 additions and 37,161 deletions.
1 change: 1 addition & 0 deletions .github/workflows/PR-check-cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ jobs:
New-Item -Path './build_dir' -ItemType Directory
cmake -S . -B "./build_dir" `
-G Ninja `
-DWITH_VCPKG_BREAKPAD=ON `
-DCMAKE_C_COMPILER="cl.exe" -DCMAKE_CXX_COMPILER="cl.exe" `
-DWITH_FFMPEG_PLAYER=OFF
cmake --build "./build_dir"
1 change: 0 additions & 1 deletion .github/workflows/release-windows-vcpkg-cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ jobs:
-DCMAKE_C_COMPILER="cl.exe" -DCMAKE_CXX_COMPILER="cl.exe" `
-DCMAKE_BUILD_TYPE=RelWithDebInfo `
-DWITH_FFMPEG_PLAYER=OFF `
-DUSE_VCPKG=ON `
-DCMAKE_TOOLCHAIN_FILE="$env:VCPKG_INSTALLATION_ROOT\scripts\buildsystems\vcpkg.cmake" `
-DWITH_VCPKG_BREAKPAD=ON
cmake --build "./build_dir"
Expand Down
75 changes: 39 additions & 36 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ option(WITH_TTS "enable QTexttoSpeech support" ON)
option(USE_SYSTEM_FMT "use system fmt instead of bundled one" OFF)
option(USE_SYSTEM_TOML "use system toml++ instead of bundled one" OFF)

# vcpkg build - only tested on Windows, does not support FFMPEG
option(USE_VCPKG "uses VCPKG for providing dependencies" OFF)
option(WITH_VCPKG_BREAKPAD "build with Breakpad support for VCPKG build only" OFF)

## Change binary & resources folder to parallel install with original GD.
Expand All @@ -22,9 +20,29 @@ option(USE_ALTERNATIVE_NAME "Force the name goldendict-ng " OFF)

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}") # to put staff in the ./cmake folder

if(WITH_VCPKG_BREAKPAD)
list(APPEND VCPKG_MANIFEST_FEATURES "breakpad")
endif()

# vcpkg handling code, must be placed before project()
if (WIN32)
if (DEFINED CMAKE_TOOLCHAIN_FILE)
message(STATUS "Using toolchain file: ${CMAKE_TOOLCHAIN_FILE}")
else ()
message(STATUS "CMAKE_TOOLCHAIN_FILE is not set. Try fetching cached vcpkg.")
include(FetchContent)
FetchContent_Declare(
vcpkg-export
URL https://github.com/xiaoyifang/goldendict-ng/releases/download/vcpkg_240711_3d72d8c930e1b6a1b2432b262c61af7d3287dcd0/goldendict-ng-vcpkg-export.tar.zst
URL_HASH SHA512=CB7AB20F03CE1BB1D46FE5DF25C9B9F4E365B72CE0A817B3C9E39BD0C85DC571F992363A1A5E8DD1E31C44FF88C51E9A88A537688C6070B4E58FF8FA2585EAB7
)
FetchContent_MakeAvailable(vcpkg-export)
set(VCPKG_MANIFEST_MODE OFF CACHE BOOL "disable existing manifest mode caused by the existrance of vcpkg.json" FORCE)
set(CMAKE_TOOLCHAIN_FILE "${CMAKE_BINARY_DIR}/_deps/vcpkg-export-src/scripts/buildsystems/vcpkg.cmake")
endif ()
endif ()


if (WITH_VCPKG_BREAKPAD)
list(APPEND VCPKG_MANIFEST_FEATURES "breakpad")
endif ()

include(FeatureSummary)

Expand Down Expand Up @@ -203,10 +221,8 @@ endif ()

#### libraries linking && includes for Win or Unix

if (USE_VCPKG)
if (WIN32)
include(Deps_Vcpkg)
elseif (WIN32)
include(Deps_Win)
else ()
include(Deps_Unix)
endif ()
Expand Down Expand Up @@ -313,36 +329,23 @@ if (WIN32)
LIBRARY_OUTPUT_DIRECTORY "${GD_WIN_OUTPUT_DIR}"
)

if (NOT USE_VCPKG)
add_custom_target(windeploy
COMMENT "Deploy everything to the output dir"
DEPENDS ${GOLDENDICT} # build this target will check if Goldendict.exe is already built
COMMAND ${WINDEPLOYQT_EXECUTABLE} --no-quick-import "${GD_WIN_OUTPUT_DIR}/${GOLDENDICT}.exe" --plugindir "${GD_WIN_OUTPUT_DIR}/plugins"
COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_SOURCE_DIR}/opencc" "${GD_WIN_OUTPUT_DIR}/opencc"
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_SOURCE_DIR}/LICENSE.txt" "${GD_WIN_OUTPUT_DIR}/LICENSE.txt"
COMMAND ${CMAKE_COMMAND} -E rm -f "${GD_WIN_OUTPUT_DIR}/goldendict.exe.manifest" "${GD_WIN_OUTPUT_DIR}/eb.dll.manifest"
WORKING_DIRECTORY ${GD_WIN_OUTPUT_DIR}
)

else () # VCPKG deploy
set(CMAKE_INSTALL_PREFIX "${GD_WIN_OUTPUT_DIR}" CACHE PATH "If you see this message, don't change this unless you want look into CMake build script. If you are an expert, yes, this is wrong. Help welcomed." FORCE)
set(CMAKE_INSTALL_PREFIX "${GD_WIN_OUTPUT_DIR}" CACHE PATH "If you see this message, don't change this unless you want look into CMake build script. If you are an expert, yes, this is wrong. Help welcomed." FORCE)

qt_generate_deploy_script(
TARGET ${GOLDENDICT}
OUTPUT_SCRIPT deploy_script
CONTENT "qt_deploy_runtime_dependencies(
EXECUTABLE \"${CMAKE_INSTALL_PREFIX}/goldendict.exe\"
BIN_DIR .
LIB_DIR .
)"
)
qt_generate_deploy_script(
TARGET ${GOLDENDICT}
OUTPUT_SCRIPT deploy_script
CONTENT "qt_deploy_runtime_dependencies(
EXECUTABLE \"${CMAKE_INSTALL_PREFIX}/goldendict.exe\"
BIN_DIR .
LIB_DIR .
)"
)

install(SCRIPT ${deploy_script})
install(DIRECTORY "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/share/opencc" DESTINATION .)
# TODO: do we really need to carry a copy of openSSL?
install(FILES "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/bin/libssl-3-x64.dll" DESTINATION .)
install(FILES "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/bin/libcrypto-3-x64.dll" DESTINATION .)
endif ()
install(SCRIPT ${deploy_script})
install(DIRECTORY "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/share/opencc" DESTINATION .)
# TODO: do we really need to carry a copy of openSSL?
install(FILES "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/bin/libssl-3-x64.dll" DESTINATION .)
install(FILES "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/bin/libcrypto-3-x64.dll" DESTINATION .)

# trick CPack to make the output folder as NSIS installer
install(DIRECTORY "${GD_WIN_OUTPUT_DIR}/"
Expand Down
50 changes: 0 additions & 50 deletions cmake/Deps_Win.cmake

This file was deleted.

52 changes: 17 additions & 35 deletions website/docs/howto/build_from_source.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
## Dependencies

* C++17 compiler
* C++17 compiler (For windows it must be MSVC)
* Latest QT6
* Various libraries for Linux & macOS, see below
* On Windows all the libraries are included in the repository

For debian/ubuntu, those packages are needed

Expand All @@ -18,7 +16,6 @@ qt6-webengine-dev x11proto-record-dev zlib1g-dev

In other words, those libraries

* Qt6 (with webengine)
* ffmpeg
* libzim
* xapian
Expand All @@ -35,68 +32,54 @@ And a few compression libraries:
* lzo2
* zlib

## CMake Build
## Build

Basically, you need those commands:

```shell
cd goldendict-ng && mkdir build_dir
# config step
cmake -S . -B build_dir \
--install-prefix=/usr/local/ \
-DCMAKE_BUILD_TYPE=Release
cmake -S . -B build_dir -G Ninja
# actual build
cmake --build build_dir --parallel 7
cmake --build build_dir

cmake --install ./build_dir/
```

The `release-*` and `PR-check-*` workflow files under `.github` in the source code has actual build & commands for reference.

### Feature flags

Append `-D{flag_names}=ON/OFF` to cmake's config step
Append `-D{flag_names}=ON/OFF` to cmake's config step.

Available flags can be found on the top of `CMakeLists.txt`

### Windows

Install Qt6(msvc) through the standard installer and pass Qt's path to CMake
Install Qt6 (MSVC) through [Qt Online Installer](https://doc.qt.io/qt-6/get-and-install-qt.html) and add Qt's path to CMake

```
-DCMAKE_PREFIX_PATH=F:\Qt\6.4.1\msvc2019_64
```
The built artifacts will end up in `build_dir/goldendict`

#### Using pre-built winlibs
#### Make the `.exe` runable

Use `windeploy` target to copy necessary runtime files.
Call `cmake --install {the cmake output folder}` will copy all necessary dependencies to correct locations.

```
cmake --build . --target windeploy
```
TODO: (untested) you can also `${Qt's install path}\Qt\6.5.2\msvc2019_64\bin` and vcpkg's bin paths to your PATH environment variable

Or you can also manually run `windeployqt.exe {your_build_dir}/goldendict.exe` which will copy the qt related things to `build_dir`.
Note that using `-G Ninja` in CMake is assumed to be used. TODO: MSBuild has minor bugs for being "Multi-Config".

#### Using Vcpkg
#### Vcpkg

The dependencies can be built via Vcpkg instead of using the pre-built ones.
vcpkg is the primary method to build GoldenDict-ng's dependencies.

Vcpkg CMake build utilize the "manifest mode", all you need to do is basically
set `CMAKE_TOOLCHAIN_FILE` as described [here](https://learn.microsoft.com/en-us/vcpkg/consume/manifest-mode?tabs=cmake%2Cbuild-MSBuild#2---integrate-vcpkg-with-your-build-system).
There are a few ways to use it.

Add this to cmake command:
```sh
-DUSE_VCPKG=ON
```
First, just do nothing. Without any additional CMake config options, a pre-built cached version of vcpkg will be automatically obtained and setup.

Most `.dll` built by vcpkg will be automatically copied, but the Qt ones won't.
Second, install vcpkg on your local machine, then set `CMAKE_TOOLCHAIN_FILE` as described [here](https://learn.microsoft.com/vcpkg/consume/manifest-mode?tabs=cmake%2Cbuild-MSBuild#2---integrate-vcpkg-with-your-build-system), which says append `-DCMAKE_TOOLCHAIN_FILE={Your vcpkg install location}/scripts/buildsystems/vcpkg.cmake` to CMake's config step. Note that this cost long time to build

You can

* run `cmake --install .` (recommended)
* manually run windeployqt
* add `${Qt's install path}\Qt\6.5.2\msvc2019_64\bin` to your PATH environment variable

Note that `-G Ninja` in CMake is assumed to be used. MSBuild has minor bugs for being "Multi-Config".

### macOS

Expand All @@ -105,4 +88,3 @@ If you build in an IDE, then the created `GoldenDict.app` will be runnable from
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/`.

4 changes: 0 additions & 4 deletions winlibs/.clang-format

This file was deleted.

Loading

0 comments on commit 3f6cfb8

Please sign in to comment.