Manifest使用示例2 - 依赖多个vcpkg的历史版本库
以下示例将提供在自定义工程中使用 vcpkg 中 fmt、sqlite3、zlib 的固定版本。
Manifest模式 - CMake 工程
1. 示例根目录: E:/test_manifest,文件目录结构如下:
E:/
|--test_manifest/
| ----build/
| ----CMakeLists.txt
| ----main.cpp
| ----vcpkg.json
2. 需在 vcpkg 目录中打开 powershell 并执行 install 命令来获取 CMAKE_TOOLCHAIN_FILE
./vcpkg integrate install
输出:
Applied user-wide integration for this vcpkg root.
CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=E:/src/test/vcpkg/scripts/buildsystems/vcpkg.cmake"All MSBuild C++ projects can now #include any installed libraries. Linking will be handled automatically. Installing new libraries will make them instantly available.
3. 获取 builtin-baseline,builtin-baseline 指的是 vcpkg 仓库包含的 commit id,可以通过命令行来获取 vcpkg 所需的 commit id。
获取 vcpkg 最新的 commit id:
git rev-parse HEAD
获取某个库历史版本的 commit id:
git blame -l versions/<port-first-char>-/<portname>.json
假如我们需要 "8.1.0#0" 的 fmt,那就在输出列表中选择 "8.1.0#0" 对应的 commit id: "b87c730e96fc6e7c9f55bb1cf31f58a838073b68"。
E:/src/test/vcpkg>git blame -l versions/f-/fmt.json
2f6537fa2b8928d2329e827f862692112793435d port_versions/f-/fmt.json (Victor Romero 2021-01-14 16:08:36 -0800 1) {
2f6537fa2b8928d2329e827f862692112793435d port_versions/f-/fmt.json (Victor Romero 2021-01-14 16:08:36 -0800 2) "versions": [
a4f58019f65b1d810f7d668832e73a2ee59b599f versions/f-/fmt.json (chausner 2022-01-27 17:07:14 +0100 23) {
a4f58019f65b1d810f7d668832e73a2ee59b599f versions/f-/fmt.json (chausner 2022-01-27 17:07:14 +0100 24) "git-tree": "9748716da169977423d02b3c3f0de7b7f274e34e",
a4f58019f65b1d810f7d668832e73a2ee59b599f versions/f-/fmt.json (chausner 2022-01-27 17:07:14 +0100 25) "version": "8.1.1",
a4f58019f65b1d810f7d668832e73a2ee59b599f versions/f-/fmt.json (chausner 2022-01-27 17:07:14 +0100 26) "port-version": 0
a4f58019f65b1d810f7d668832e73a2ee59b599f versions/f-/fmt.json (chausner 2022-01-27 17:07:14 +0100 27) },
b87c730e96fc6e7c9f55bb1cf31f58a838073b68 versions/f-/fmt.json ( 2022-01-06 11:28:29 +0800 28) {
b87c730e96fc6e7c9f55bb1cf31f58a838073b68 versions/f-/fmt.json ( 2022-01-06 11:28:29 +0800 29) "git-tree": "3aa440d7c335eb584170ae5fdf4eea6a17c900ee",
b87c730e96fc6e7c9f55bb1cf31f58a838073b68 versions/f-/fmt.json ( 2022-01-06 11:28:29 +0800 30) "version": "8.1.0",
b87c730e96fc6e7c9f55bb1cf31f58a838073b68 versions/f-/fmt.json ( 2022-01-06 11:28:29 +0800 31) "port-version": 0
b87c730e96fc6e7c9f55bb1cf31f58a838073b68 versions/f-/fmt.json ( 2022-01-06 11:28:29 +0800 32) },
...
4. 创建文件 vcpkg.json/CMakeLists.txt/main.cpp,本示例使用fmt、sqlite3和zlib,实现打印各端口版本的功能。
vcpkg.json
{
"name": "test-manifest",
"version": "1.0.0",
"description": "Test manifest using the specified version. ",
"dependencies": [
"fmt",
{
"name": "sqlite3",
"version>=": "3.37.2#1"
},
{
"name": "zlib",
"version>=": "1.2.11#10"
}
],
"builtin-baseline": "b87c730e96fc6e7c9f55bb1cf31f58a838073b68",
"overrides": [
{
"name": "zlib", "version": "1.2.11#11"
}
]
}
注: builtin-baseline 为 34f072dfcb8b9356e6ed2c74947f87390ddb2706 的提交中,提交日期为 "2022-01-05" ,各依赖项的版本如下:
- fmt 的 version 是 "8.1.0#0";
- sqlite3 的 version 是 "3.37.0#1";
- zlib 的 version 是 "1.2.11#13"。
CMakeLists.txt
cmake_minimum_required(VERSION 3.18)
project(test-manifest CXX)
add_executable(main main.cpp)
find_package(fmt CONFIG REQUIRED)
find_package(ZLIB REQUIRED)
find_package(unofficial-sqlite3 CONFIG REQUIRED)
target_link_libraries(main PRIVATE fmt::fmt ZLIB::ZLIB unofficial::sqlite3::sqlite3)
main.cpp
#include <iostream>
#include <fmt/core.h>
#include <sqlite3.h>
#include <zlib.h>
int main()
{
fmt::print("Version of fmt: {}\n"
"Version of sqlite3: {}\n"
"Version of zlib: {}\n",
FMT_VERSION, sqlite3_libversion(), ZLIB_VERSION);
return 0;
}
5. 在 build 目录下打开 cmd 窗口,配置 CMake 工程
"C:\Program Files\CMake\bin\cmake.exe" "E:\test_manifest" -G "Visual Studio 16 2019" -A x64 -DVCPKG_TARGET_TRIPLET=x64-windows "-DCMAKE_TOOLCHAIN_FILE=E:/src/test/vcpkg/scripts/buildsystems/vcpkg.cmake"
输出:
-- Running vcpkg install
Detecting compiler hash for triplet x64-windows...
The following packages will be built and installed:
fmt[core]:x64-windows -> 8.1.0 -- E:\src\test\vcpkg\buildtrees\versioning_\versions\fmt\3aa440d7c335eb584170ae5fdf4eea6a17c900ee
sqlite3[core]:x64-windows -> 3.37.2#1 -- E:\src\test\vcpkg\buildtrees\versioning_\versions\sqlite3\f49ad405225c6326558adb79f2082814002a8cda
* vcpkg-cmake[core]:x64-windows -> 2021-12-20 -- E:\src\test\vcpkg\buildtrees\versioning_\versions\vcpkg-cmake\a35eb7c761372dc64526d59fa918a13c0dfbba1b
* vcpkg-cmake-config[core]:x64-windows -> 2021-12-28 -- E:\src\test\vcpkg\buildtrees\versioning_\versions\vcpkg-cmake-config\e33152002c946b93a0262931ba8bf54a2e6ab9ad
zlib[core]:x64-windows -> 1.2.11#11 -- E:\src\test\vcpkg\buildtrees\versioning_\versions\zlib\ad5b59cd3722af26f09921297562dfc76c83dbf8
Additional packages (*) will be modified to complete this operation.
Restored 3 package(s) from C:\Users\UserName\AppData\Local\vcpkg\archives in 233.4 ms. Use --debug to see more details.
Installing 1/5 vcpkg-cmake-config:x64-windows...
Elapsed time to handle vcpkg-cmake-config:x64-windows: 8.51 ms
Installing 2/5 vcpkg-cmake:x64-windows...
Elapsed time to handle vcpkg-cmake:x64-windows: 17.89 ms
Installing 3/5 fmt:x64-windows...
Elapsed time to handle fmt:x64-windows: 42.7 ms
Installing 4/5 sqlite3:x64-windows...
Building sqlite3[core]:x64-windows...
-- Installing port from location: E:\src\test\vcpkg\buildtrees\versioning_\versions\sqlite3\f49ad405225c6326558adb79f2082814002a8cda
-- Downloading https://sqlite.org/2021/sqlite-amalgamation-3370100.zip -> sqlite-amalgamation-3370100.zip...
-- Extracting source E:/src/test/vcpkg/downloads/sqlite-amalgamation-3370100.zip
-- Applying patch fix-arm-uwp.patch
-- Using source at E:/src/test/vcpkg/buildtrees/sqlite3/src/3370100-a1573e1602.clean
-- Found external ninja('1.10.2').
-- Configuring x64-windows
-- Building x64-windows-dbg
-- Building x64-windows-rel
-- Fixing pkgconfig file: E:/src/test/vcpkg/packages/sqlite3_x64-windows/lib/pkgconfig/sqlite3.pc
-- Using cached msys-mingw-w64-i686-pkg-config-0.29.2-3-any.pkg.tar.zst.
-- Using cached msys-mingw-w64-i686-libwinpthread-git-9.0.0.6373.5be8fcd83-1-any.pkg.tar.zst.
-- Using msys root at E:/src/test/vcpkg/downloads/tools/msys2/9a1ec3f33446b195
-- Fixing pkgconfig file: E:/src/test/vcpkg/packages/sqlite3_x64-windows/debug/lib/pkgconfig/sqlite3.pc
-- Performing post-build validation
-- Performing post-build validation done
Stored binary cache: "C:\Users\UserName\AppData\Local\vcpkg\archives\36\363b231a44c799fbfd8abf5101ae0384b3df91cbeed9003875bf90cce770d0ae.zip"
Elapsed time to handle sqlite3:x64-windows: 16.25 s
Installing 5/5 zlib:x64-windows...
Building zlib[core]:x64-windows...
-- Installing port from location: E:\src\test\vcpkg\buildtrees\versioning_\versions\zlib\ad5b59cd3722af26f09921297562dfc76c83dbf8
-- Using cached zlib1211.tar.gz.
-- Cleaning sources at E:/src/test/vcpkg/buildtrees/zlib/src/1.2.11-e9a093319e.clean. Use --editable to skip cleaning for the packages you specify.
-- Extracting source E:/src/test/vcpkg/downloads/zlib1211.tar.gz
-- Applying patch cmake_dont_build_more_than_needed.patch
-- Applying patch 0001-Prevent-invalid-inclusions-when-HAVE_-is-set-to-0.patch
-- Applying patch add_debug_postfix_on_mingw.patch
-- Applying patch 0002-android-build-mingw.patch
-- Using source at E:/src/test/vcpkg/buildtrees/zlib/src/1.2.11-e9a093319e.clean
-- Found external ninja('1.10.2').
-- Configuring x64-windows
-- Building x64-windows-dbg
-- Building x64-windows-rel
-- Installing: E:/src/test/vcpkg/packages/zlib_x64-windows/share/zlib/vcpkg-cmake-wrapper.cmake
-- Fixing pkgconfig file: E:/src/test/vcpkg/packages/zlib_x64-windows/lib/pkgconfig/zlib.pc
-- Using cached msys-mingw-w64-i686-pkg-config-0.29.2-3-any.pkg.tar.zst.
-- Using cached msys-mingw-w64-i686-libwinpthread-git-9.0.0.6373.5be8fcd83-1-any.pkg.tar.zst.
-- Using msys root at E:/src/test/vcpkg/downloads/tools/msys2/9a1ec3f33446b195
-- Fixing pkgconfig file: E:/src/test/vcpkg/packages/zlib_x64-windows/debug/lib/pkgconfig/zlib.pc
-- Installing: E:/src/test/vcpkg/packages/zlib_x64-windows/share/zlib/copyright
-- Performing post-build validation
-- Performing post-build validation done
Stored binary cache: "C:\Users\UserName\AppData\Local\vcpkg\archives\4a\4af3c4b2f08152e69bc8d8854fdcd3290083506702a7cbd84d92c33c9b7f67f2.zip"
Elapsed time to handle zlib:x64-windows: 8.092 s
Total elapsed time: 31.25 s
The package fmt provides CMake targets:
find_package(fmt CONFIG REQUIRED)
target_link_libraries(main PRIVATE fmt::fmt)
# Or use the header-only version
find_package(fmt CONFIG REQUIRED)
target_link_libraries(main PRIVATE fmt::fmt-header-only)
sqlite3 provides CMake targets:
# this is heuristically generated, and may not be correct
find_package(unofficial-sqlite3 CONFIG REQUIRED)
target_link_libraries(main PRIVATE unofficial::sqlite3::sqlite3)
The package zlib is compatible with built-in CMake targets:
find_package(ZLIB REQUIRED)
target_link_libraries(main PRIVATE ZLIB::ZLIB)
-- Running vcpkg install - done
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19044.
-- The CXX compiler identification is MSVC 19.29.30146.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found ZLIB: optimized;E:/test_manifest/build/vcpkg_installed/x64-windows/lib/zlib.lib;debug;E:/test_manifest/build/vcpkg_installed/x64-windows/debug/lib/zlibd.lib (found version "1.2.11")
-- Configuring done
-- Generating done
-- Build files have been written to: E:/test_manifest/build
请重点关注以下输出:
The following packages will be built and installed:
fmt[core]:x64-windows -> 8.1.0 -- E:\src\test\vcpkg\buildtrees\versioning_\versions\fmt\3aa440d7c335eb584170ae5fdf4eea6a17c900ee
sqlite3[core]:x64-windows -> 3.37.2#1 -- E:\src\test\vcpkg\buildtrees\versioning_\versions\sqlite3\f49ad405225c6326558adb79f2082814002a8cda
* vcpkg-cmake[core]:x64-windows -> 2021-12-20 -- E:\src\test\vcpkg\buildtrees\versioning_\versions\vcpkg-cmake\a35eb7c761372dc64526d59fa918a13c0dfbba1b
* vcpkg-cmake-config[core]:x64-windows -> 2021-12-28 -- E:\src\test\vcpkg\buildtrees\versioning_\versions\vcpkg-cmake-config\e33152002c946b93a0262931ba8bf54a2e6ab9ad
zlib[core]:x64-windows -> 1.2.11#11 -- E:\src\test\vcpkg\buildtrees\versioning_\versions\zlib\ad5b59cd3722af26f09921297562dfc76c83dbf8
输出分析:
- 对于 fmt 来说: 只有 builtin-baseline 起版本约束作用,所以是 "8.1.0";
- 对于 sqlit3 来说: 有 version>= 和 builtin-baseline 两个版本约束,它会比较这两个约束,从中选择最高的执行,所以是 "3.37.2#1";
- 对于 zlib 来说: 有 version>= 和 builtin-baseline 和 overrides 三个版本约束,它会选择 overrides 执行,因为 overrides 有强制覆盖其他版本的作用,所以是 "1.2.11#11"。
- 依赖项的安装位置:E:/test_manifest/build/vcpkg_installed/x64-windows/ 。
需要注意的是,如果有多个依赖项,我们同样要查看这个 commit id 对其他依赖项的影响,如果不为其他依赖做另外的版本约束,它们就会默认安装 builtin-baseline 指定的 commit 提交日期之前发布的最新版本。 例如: 我们为 fmt 选择了 "8.1.0#0",发布日期是 "2022-01-05",如果不为 sqlite3 和 zlib 做其他版本约束(version>= 或者 overrides ),它们就会安装 "2022-01-05" 之前的最后一个版本, 即 "2021-12-21" 发布的 "3.37.0#1" 和 "2021-10-11" 发布的 "1.2.11#13"。
E:/src/test/vcpkg>git blame -l versions/s-/sqlite3.json
2f6537fa2b8928d2329e827f862692112793435d port_versions/s-/sqlite3.json (Victor Romero 2021-01-14 16:08:36 -0800 1) {
2f6537fa2b8928d2329e827f862692112793435d port_versions/s-/sqlite3.json (Victor Romero 2021-01-14 16:08:36 -0800 2) "versions": [
7f3e168d8fe2c7df5cba99a62f4059468c650e37 versions/s-/sqlite3.json (Jack·Boos·Yu 2022-02-17 05:00:36 +0800 18) {
7f3e168d8fe2c7df5cba99a62f4059468c650e37 versions/s-/sqlite3.json (Jack·Boos·Yu 2022-02-17 05:00:36 +0800 19) "git-tree": "f49ad405225c6326558adb79f2082814002a8cda",
7f3e168d8fe2c7df5cba99a62f4059468c650e37 versions/s-/sqlite3.json (Jack·Boos·Yu 2022-02-17 05:00:36 +0800 20) "version": "3.37.2",
7f3e168d8fe2c7df5cba99a62f4059468c650e37 versions/s-/sqlite3.json (Jack·Boos·Yu 2022-02-17 05:00:36 +0800 21) "port-version": 1
7f3e168d8fe2c7df5cba99a62f4059468c650e37 versions/s-/sqlite3.json (Jack·Boos·Yu 2022-02-17 05:00:36 +0800 22) },
4f2d305da8a46dcaf0afbab5a59385a0441342e7 versions/s-/sqlite3.json (Frank 2022-02-02 06:50:42 +0800 23) {
4f2d305da8a46dcaf0afbab5a59385a0441342e7 versions/s-/sqlite3.json (Frank 2022-02-02 06:50:42 +0800 24) "git-tree": "ac17b6f385706852a3895200764c358697c5c584",
4f2d305da8a46dcaf0afbab5a59385a0441342e7 versions/s-/sqlite3.json (Frank 2022-02-02 06:50:42 +0800 25) "version": "3.37.1",
4f2d305da8a46dcaf0afbab5a59385a0441342e7 versions/s-/sqlite3.json (Frank 2022-02-02 06:50:42 +0800 26) "port-version": 0
4f2d305da8a46dcaf0afbab5a59385a0441342e7 versions/s-/sqlite3.json (Frank 2022-02-02 06:50:42 +0800 27) },
1cd11e2ce7cbcada0a696b7bb32fc011605796f5 versions/s-/sqlite3.json (Matthias Kuhn 2021-12-22 05:12:49 +0100 28) {
1cd11e2ce7cbcada0a696b7bb32fc011605796f5 versions/s-/sqlite3.json (Matthias Kuhn 2021-12-22 05:12:49 +0100 29) "git-tree": "abbe21d1a38c7eb9e8c87965235d92721001e997",
1cd11e2ce7cbcada0a696b7bb32fc011605796f5 versions/s-/sqlite3.json (Matthias Kuhn 2021-12-22 05:12:49 +0100 30) "version": "3.37.0",
1cd11e2ce7cbcada0a696b7bb32fc011605796f5 versions/s-/sqlite3.json (Matthias Kuhn 2021-12-22 05:12:49 +0100 31) "port-version": 1
1cd11e2ce7cbcada0a696b7bb32fc011605796f5 versions/s-/sqlite3.json (Matthias Kuhn 2021-12-22 05:12:49 +0100 32) },
...
E:/src/test/vcpkg>git blame -l versions/z-/zlib.json
2f6537fa2b8928d2329e827f862692112793435d port_versions/z-/zlib.json (Victor Romero 2021-01-14 16:08:36 -0800 1) {
2f6537fa2b8928d2329e827f862692112793435d port_versions/z-/zlib.json (Victor Romero 2021-01-14 16:08:36 -0800 2) "versions": [
659b6b5eeacb50ffe25856413960d231ce1a4b58 versions/z-/zlib.json (Thomas1664 2022-05-10 01:12:37 +0200 13) {
659b6b5eeacb50ffe25856413960d231ce1a4b58 versions/z-/zlib.json (Thomas1664 2022-05-10 01:12:37 +0200 14) "git-tree": "ecc4c064d4911faf12d8bf5fd6bcd5c556d89774",
659b6b5eeacb50ffe25856413960d231ce1a4b58 versions/z-/zlib.json (Thomas1664 2022-05-10 01:12:37 +0200 15) "version": "1.2.12",
659b6b5eeacb50ffe25856413960d231ce1a4b58 versions/z-/zlib.json (Thomas1664 2022-05-10 01:12:37 +0200 16) "port-version": 1
659b6b5eeacb50ffe25856413960d231ce1a4b58 versions/z-/zlib.json (Thomas1664 2022-05-10 01:12:37 +0200 17) },
9b064ff07b987c8f213fdbf4358122fd4fe38ef1 versions/z-/zlib.json (Magnus Edenhill 2022-03-31 01:53:20 +0200 18) {
9b064ff07b987c8f213fdbf4358122fd4fe38ef1 versions/z-/zlib.json (Magnus Edenhill 2022-03-31 01:53:20 +0200 19) "git-tree": "9c4edb1fabbd87dd236a200ba55eaf241cd8c8d0",
9b064ff07b987c8f213fdbf4358122fd4fe38ef1 versions/z-/zlib.json (Magnus Edenhill 2022-03-31 01:53:20 +0200 20) "version": "1.2.12",
9b064ff07b987c8f213fdbf4358122fd4fe38ef1 versions/z-/zlib.json (Magnus Edenhill 2022-03-31 01:53:20 +0200 21) "port-version": 0
9b064ff07b987c8f213fdbf4358122fd4fe38ef1 versions/z-/zlib.json (Magnus Edenhill 2022-03-31 01:53:20 +0200 22) },
822eb4b4ce82d76fa5bcfa8f5774f47db4e01e8b versions/z-/zlib.json (Kai Pastor 2021-10-11 19:21:07 +0200 23) {
822eb4b4ce82d76fa5bcfa8f5774f47db4e01e8b versions/z-/zlib.json (Kai Pastor 2021-10-11 19:21:07 +0200 24) "git-tree": "92cfe30c807d343c6359d272242f0765ad906740",
822eb4b4ce82d76fa5bcfa8f5774f47db4e01e8b versions/z-/zlib.json (Kai Pastor 2021-10-11 19:21:07 +0200 25) "version": "1.2.11",
822eb4b4ce82d76fa5bcfa8f5774f47db4e01e8b versions/z-/zlib.json (Kai Pastor 2021-10-11 19:21:07 +0200 26) "port-version": 13
822eb4b4ce82d76fa5bcfa8f5774f47db4e01e8b versions/z-/zlib.json (Kai Pastor 2021-10-11 19:21:07 +0200 27) },
...
注意:
- 如果不指定任何版本,那么会默认选择 vcpkg 的最新提交版本;
- 如果指定了 version>= 或者 overrides 中的任何一个(或者同时指定这两个),那么必须指定 builtin-baseline ,因为它们对 builtin-baseline 有依赖性;
- 可以单独使用 builtin-baseline ,它对其他的版本约束没有依赖性;
- version>= 主要用于表示需要使用的最低版本,只能在 dependencies[] 中使用;
- vcpkg 不会比较 version 类型不同的版本,例如: 同一个库的两个版本 "version-string: 8.1.0" 与 "version: 8.1.2" 类型不同,所以不能比较。
所有的约束组合有以下几种:
- 不指定任何约束(默认选择 vcpkg 的最新提交版本);
- builtin-baseline;
- version>= 和 builtin-baseline;
- overrides 和 builtin-baseline;
- version>= 和 builtin-baseline 和 overrides。
6. 构建 CMake 工程
"C:\Program Files\CMake\bin\cmake.exe" --build .
输出:
Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
Checking Build System
Building Custom Rule E:/test_manifest/CMakeLists.txt
main.cpp
main.vcxproj -> E:\test_manifest\build\Debug\main.exe
Building Custom Rule E:/test_manifest/CMakeLists.txt
7. 运行可执行文件
.\Debug\main.exe
输出:
Version of fmt: 80100
Version of sqlite3: 3.37.1
Version of zlib: 1.2.11
Manifest模式 - MSBuild 工程
在Manifest模式下使用 MSBuild 工程请参阅:https://www.cnblogs.com/vcpkg/p/15019899.html。