Skip to content

Commit

Permalink
perf: load vcpkg chainload toolchains and triplets
Browse files Browse the repository at this point in the history
Signed-off-by: msclock <msclock@qq.com>
  • Loading branch information
msclock committed Mar 12, 2024
1 parent a8017ee commit 5a0c44a
Show file tree
Hide file tree
Showing 30 changed files with 604 additions and 48 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,16 @@ jobs:
- {os: {name: ubuntu-latest, type: linux}, compiler: {name: gcc-11, type: gcc}}
- {os: {name: ubuntu-latest, type: linux}, compiler: {name: llvm, type: llvm}}
- {os: {name: ubuntu-latest, type: mingw-dynamic-linux}, compiler: {name: mingw, type: mingw}}
- {os: {name: ubuntu-latest, type: mingw-static-linux}, compiler: {name: mingw, type: mingw}}
- {os: {name: macos-latest, type: osx}, compiler: {name: applellvm, type: llvm}}
- {os: {name: macos-latest, type: osx}, compiler: {name: llvm, type: llvm}}
# setup-cpp doesn't support mingw on macOS yet
# - {os: {name: macos-latest, type: mingw-dynamic-darwin}, compiler: {name: mingw, type: mingw}}
# - {os: {name: macos-latest, type: mingw-static-darwin}, compiler: {name: mingw, type: mingw}}
- {os: {name: windows-latest, type: windows}, compiler: {name: msvc, type: msvc}}
- {os: {name: windows-latest, type: windows}, compiler: {name: llvm, type: llvm}}
- {os: {name: windows-latest, type: mingw-dynamic-windows}, compiler: {name: mingw, type: mingw-gcc}}
- {os: {name: windows-latest, type: mingw-dynamic-windows}, compiler: {name: mingw, type: mingw}}
- {os: {name: windows-latest, type: mingw-static-windows}, compiler: {name: mingw, type: mingw}}
arch:
- {name: x64, type: x64}
vcpkg:
Expand Down
5 changes: 4 additions & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
"include": [
"cmake/presets/x64-linux-llvm.json",
"cmake/presets/x64-linux-gcc.json",
"cmake/presets/x64-mingw-dynamic-windows-mingw-gcc.json",
"cmake/presets/x64-mingw-dynamic-windows-mingw.json",
"cmake/presets/x64-mingw-dynamic-darwin-mingw.json",
"cmake/presets/x64-mingw-dynamic-linux-mingw.json",
"cmake/presets/x64-mingw-static-windows-mingw.json",
"cmake/presets/x64-mingw-static-darwin-mingw.json",
"cmake/presets/x64-mingw-static-linux-mingw.json",
"cmake/presets/x64-osx-gcc.json",
"cmake/presets/x64-osx-llvm.json",
"cmake/presets/x64-windows-llvm.json",
Expand Down
20 changes: 13 additions & 7 deletions cmake/build_configuration.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Build Congfiguration

Based on vcpkg and CMake, we can seamlessly creat, build, and test our projects on different platforms and configurations. Here are some tips and tricks to make things easier.
Based on vcpkg and CMake, we can seamlessly create, build, and test our projects on different platforms and configurations. Here are some tips and tricks to make things easier.

## Triplet Presets

The triplet presets are used to specify the target platform and build configuration for the project.It consists of three parts: architecture, target-compiling-os/toolchain, and distribution.
The triplet presets are used to specify the target platform and build configuration for the project.It consists of three parts: Architecture, Target-Compiling-Os/Toolchain, Distribution and Compiler.

- Architecture: x86, x64, arm, arm64, ppc64le, s390x, wasm32, etc.
- Target-Compiling-OS/Toolchain: windows, linux, macos, freebsd, android, ios, mingw, etc.
- Distribution: dynamic, static, release, etc.
- Compiler(Optional): MSVC, GCC, Clang, MinGW
- Compiler: MSVC, GCC, Clang, MinGW.

More about triplets, see: <https://learn.microsoft.com/en-us/vcpkg/users/triplets>

Expand All @@ -28,12 +28,11 @@ ctest --preset=<preset> # test the desired test preset

## Building without presets(Not Recommended)

If you don't want to use presets, you can still build the project by specifying the target platform and build configuration. It's not recommended as it's more complex and error-prone because you need to specify the correct triplet and toolchain for each platform and configuration.

You can also build the project without using presets. It's not recommended as it's more complex and error-prone because you need to specify the correct triplet and toolchain for each platform and configuration.

But it is documented here for reference to reveal the underlying mechanism. Here are some necessary variables to configure the project:

- `CMAKE_TOOLCHAIN_FILE(Required)`: specify the path to the project's toolchain file(cmake/vcpkg/vcpkg.toolchain.cmake). It contains steps to set the correct triplet and initialize the vcpkg environment.
- `CMAKE_TOOLCHAIN_FILE(Required)`: specify the path to the project's toolchain file(cmake/vcpkg/vcpkg.toolchain.cmake). It contains steps to set the correct triplet and initialize the vcpkg environment. More importantly, it also loads triplet variables and chainload toolchain according to `VCPKG_TARGET_TRIPLET`.
- `CMAKE_BUILD_TYPE(Required)`: specify the build configuration, such as "Debug", "Release", "RelWithDebInfo", "MinSizeRel", etc.
- `CMAKE_GENERATOR(Required)`: specify the generator for the build system, such as "Ninja", "Unix Makefiles", "Visual Studio 16 2019", etc. And it's recommended to use Ninja for cross-platform builds.
- `VCPKG_ROOT(Optional)`: specify the path to the toolchain file for the target platform. Normally it's in the vcpkg installation directory. If not specified, cmake/vcpkg/vcpkg.toolchain.cmake will automatically create one for you.
Expand All @@ -46,7 +45,14 @@ Here's an example of building the project with the specified variables:

```bash
# Configure
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=cmake/vcpkg/vcpkg.toolchain.cmake -DCMAKE_BUILD_TYPE=Debug -G Ninja -DVCPKG_TARGET_TRIPLET=x64-linux -DVCPKG_HOST_TRIPLET=x64-linux -DVCPKG_OVERLAY_TRIPLETS=cmake/vcpkg/triplets -DVCPKG_OVERLAY_PORTS=cmake/vcpkg/ports
cmake -S . -B build \
-DCMAKE_TOOLCHAIN_FILE=cmake/vcpkg/vcpkg.toolchain.cmake \
-DCMAKE_BUILD_TYPE=Debug \
-G Ninja \
-DVCPKG_TARGET_TRIPLET=x64-linux \
-DVCPKG_HOST_TRIPLET=x64-linux \
-DVCPKG_OVERLAY_TRIPLETS=cmake/vcpkg/triplets \
-DVCPKG_OVERLAY_PORTS=cmake/vcpkg/ports
# Build
cmake --build build --config Debug
# Testing
Expand Down
4 changes: 2 additions & 2 deletions cmake/presets/compilers/llvm.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"name": "llvm-cl",
"hidden": true,
"cacheVariables": {
"CMAKE_C_COMPILER": "clang-cl",
"CMAKE_CXX_COMPILER": "clang-cl"
"CMAKE_C_COMPILER": "clang-cl.exe",
"CMAKE_CXX_COMPILER": "clang-cl.exe"
}
}
]
Expand Down
3 changes: 1 addition & 2 deletions cmake/presets/triplets/x64-mingw-dynamic.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
],
"cacheVariables": {
"VCPKG_TARGET_TRIPLET": "x64-mingw-dynamic",
"VCPKG_HOST_TRIPLET": "x64-mingw-dynamic",
"VCPKG_CHAINLOAD_TOOLCHAIN_FILE": "${sourceDir}/cmake/vcpkg/scripts/toolchains/mingw.cmake"
"VCPKG_HOST_TRIPLET": "x64-mingw-dynamic"
}
}
]
Expand Down
19 changes: 19 additions & 0 deletions cmake/presets/triplets/x64-mingw-static.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": 6,
"include": [
"../arch/x64.json"
],
"configurePresets": [
{
"name": "x64-mingw-static",
"hidden": true,
"inherits": [
"x64"
],
"cacheVariables": {
"VCPKG_TARGET_TRIPLET": "x64-mingw-static",
"VCPKG_HOST_TRIPLET": "x64-mingw-static"
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"configurePresets": [
{
"name": "x64-mingw-dynamic-windows-mingw-gcc",
"name": "x64-mingw-dynamic-windows-mingw",
"inherits": [
"base",
"windows",
Expand All @@ -22,33 +22,33 @@
],
"buildPresets": [
{
"name": "x64-mingw-dynamic-windows-mingw-gcc",
"name": "x64-mingw-dynamic-windows-mingw",
"inherits": "base",
"configurePreset": "x64-mingw-dynamic-windows-mingw-gcc"
"configurePreset": "x64-mingw-dynamic-windows-mingw"
}
],
"testPresets": [
{
"name": "x64-mingw-dynamic-windows-mingw-gcc",
"name": "x64-mingw-dynamic-windows-mingw",
"inherits": "base",
"configurePreset": "x64-mingw-dynamic-windows-mingw-gcc"
"configurePreset": "x64-mingw-dynamic-windows-mingw"
}
],
"workflowPresets": [
{
"name": "x64-mingw-dynamic-windows-mingw-gcc",
"name": "x64-mingw-dynamic-windows-mingw",
"steps": [
{
"type": "configure",
"name": "x64-mingw-dynamic-windows-mingw-gcc"
"name": "x64-mingw-dynamic-windows-mingw"
},
{
"type": "build",
"name": "x64-mingw-dynamic-windows-mingw-gcc"
"name": "x64-mingw-dynamic-windows-mingw"
},
{
"type": "test",
"name": "x64-mingw-dynamic-windows-mingw-gcc"
"name": "x64-mingw-dynamic-windows-mingw"
}
]
}
Expand Down
53 changes: 53 additions & 0 deletions cmake/presets/x64-mingw-static-darwin-mingw.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"version": 6,
"include": [
"base.json",
"os/darwin.json",
"compilers/mingw.json",
"triplets/x64-mingw-static.json"
],
"configurePresets": [
{
"name": "x64-mingw-static-darwin-mingw",
"inherits": [
"base",
"darwin",
"mingw",
"x64-mingw-static"
]
}
],
"buildPresets": [
{
"name": "x64-mingw-static-darwin-mingw",
"inherits": "base",
"configurePreset": "x64-mingw-static-darwin-mingw"
}
],
"testPresets": [
{
"name": "x64-mingw-static-darwin-mingw",
"inherits": "base",
"configurePreset": "x64-mingw-static-darwin-mingw"
}
],
"workflowPresets": [
{
"name": "x64-mingw-static-darwin-mingw",
"steps": [
{
"type": "configure",
"name": "x64-mingw-static-darwin-mingw"
},
{
"type": "build",
"name": "x64-mingw-static-darwin-mingw"
},
{
"type": "test",
"name": "x64-mingw-static-darwin-mingw"
}
]
}
]
}
56 changes: 56 additions & 0 deletions cmake/presets/x64-mingw-static-linux-mingw.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"version": 6,
"include": [
"base.json",
"os/linux.json",
"compilers/mingw.json",
"triplets/x64-mingw-static.json"
],
"configurePresets": [
{
"name": "x64-mingw-static-linux-mingw",
"inherits": [
"base",
"linux",
"mingw",
"x64-mingw-static"
],
"cacheVariables": {
"USE_SANITIZER": "FALSE"
}
}
],
"buildPresets": [
{
"name": "x64-mingw-static-linux-mingw",
"inherits": "base",
"configurePreset": "x64-mingw-static-linux-mingw"
}
],
"testPresets": [
{
"name": "x64-mingw-static-linux-mingw",
"inherits": "base",
"configurePreset": "x64-mingw-static-linux-mingw"
}
],
"workflowPresets": [
{
"name": "x64-mingw-static-linux-mingw",
"steps": [
{
"type": "configure",
"name": "x64-mingw-static-linux-mingw"
},
{
"type": "build",
"name": "x64-mingw-static-linux-mingw"
},
{
"type": "test",
"name": "x64-mingw-static-linux-mingw"
}
]
}
]
}
56 changes: 56 additions & 0 deletions cmake/presets/x64-mingw-static-windows-mingw.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"version": 6,
"include": [
"base.json",
"os/windows.json",
"compilers/mingw.json",
"triplets/x64-mingw-static.json"
],
"configurePresets": [
{
"name": "x64-mingw-static-windows-mingw-gcc",
"inherits": [
"base",
"windows",
"mingw",
"x64-mingw-static"
],
"cacheVariables": {
"USE_SANITIZER": "FALSE"
}
}
],
"buildPresets": [
{
"name": "x64-mingw-static-windows-mingw-gcc",
"inherits": "base",
"configurePreset": "x64-mingw-static-windows-mingw-gcc"
}
],
"testPresets": [
{
"name": "x64-mingw-static-windows-mingw-gcc",
"inherits": "base",
"configurePreset": "x64-mingw-static-windows-mingw-gcc"
}
],
"workflowPresets": [
{
"name": "x64-mingw-static-windows-mingw-gcc",
"steps": [
{
"type": "configure",
"name": "x64-mingw-static-windows-mingw-gcc"
},
{
"type": "build",
"name": "x64-mingw-static-windows-mingw-gcc"
},
{
"type": "test",
"name": "x64-mingw-static-windows-mingw-gcc"
}
]
}
]
}
2 changes: 2 additions & 0 deletions cmake/vcpkg/bootstrap/vcpkg-config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ set(__vcpkg_bootstrap_list_dir "${CMAKE_CURRENT_LIST_DIR}")

include(${__vcpkg_bootstrap_list_dir}/vcpkg_skip_install_on_reconfigure.cmake)
include(${__vcpkg_bootstrap_list_dir}/vcpkg_bootstrap.cmake)
include(${__vcpkg_bootstrap_list_dir}/vcpkg_chainload_toolchain.cmake)
include(${__vcpkg_bootstrap_list_dir}/vcpkg_load_triplet.cmake)
include(${__vcpkg_bootstrap_list_dir}/vcpkg_configure.cmake)
41 changes: 41 additions & 0 deletions cmake/vcpkg/bootstrap/vcpkg_chainload_toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#[[
SPDX-License-Identifier: MIT
SPDX-FileCopyrightText: Copyright 2024 msclock
]]

# configure VCPKG_CHAINLOAD_TOOLCHAIN_FILE based on VCPKG_TARGET_TRIPLET and
# VCPKG_TARGET_ARCHITECTURE
function(_vcpkg_chainload_toolchain)
if(DEFINED VCPKG_CHAINLOAD_TOOLCHAIN_FILE)
return()
endif()

if(NOT DEFINED VCPKG_TARGET_TRIPLET OR NOT DEFINED VCPKG_TARGET_ARCHITECTURE)
message(
FATAL_ERROR
"VCPKG_TARGET_TRIPLET and VCPKG_TARGET_ARCHITECTURE must be set before calling _vcpkg_chainload_toolchain()"
)
endif()

string(LENGTH "${VCPKG_TARGET_ARCHITECTURE}-" _prefix_len)
string(SUBSTRING ${VCPKG_TARGET_TRIPLET} ${_prefix_len} -1 _stripped_string)
string(REPLACE "-" ";" _triplet_parts "${_stripped_string}")
list(GET _triplet_parts 0 _chainload_toolchain_name)

set(_toolchain "scripts/toolchains/${_chainload_toolchain_name}.cmake")

if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/${_toolchain}")
set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE
"${CMAKE_CURRENT_LIST_DIR}/${_toolchain}"
CACHE INTERNAL "vcpkg chainload")
elseif(EXISTS "${_VCPKG_ROOT}/${_toolchain}")
set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE
"${_VCPKG_ROOT}/${_toolchain}"
CACHE INTERNAL "vcpkg chainload")
else()
message(
WARNING
"Could not find toolchain file for ${_chainload_toolchain_name}, skipping chainload"
)
endif()
endfunction()
Loading

0 comments on commit 5a0c44a

Please sign in to comment.