使用registry时,我们需要创建以下这几类文件:
- 库的描述及构建流程
- 库的版本信息
- 当前项目使用的manifest文件
- 库的注册表描述文件
以下示将实现两个库alpha和beta的registry使用方式:假设我们拥有两个库,名称各为alpha和beta,他们均有两次版本记录。
使用示例
一、我们需要在当前项目文件夹 project 中创建文件夹层级。
project/
|---port-registry/
|------ports/
|---------alpha/
|---------- 1.0/
|-----------2.0/
|---------beta/
|-----------1.0/
|-----------1.1/
|------versions/
|---------a-/
|---------b-/
层级关系显示alpha有两次版本记录分别是1.0和2.0, beta有两次记录分别是1.0和1.1。
二、 我们需要对每一个私有库均创建一组manifest文件及对应的portfile.cmake文件来描述每个版本信息及构建过程,并将它们放置在以库名称命名对应的文件夹中。
注意:当前使用manifest的命令行模式,对于集成至cmake或msvc工程同样适用。
alpha v1.0: 创建以下两个文件并放置在 project/port-registry/ports/alpha/1.0 中。
vcpkg.json
{ "name": "alpha", "version": "1.0" }
portfile.cmake
set(VCPKG_POLICY_EMPTY_PACKAGE enabled) #不执行任何操作
alpha v2.0: 创建以下两个文件并放置在 project/port-registry/ports/alpha/2.0 中。
vcpkg.json
{ "name": "alpha", "version": "2.0" }
portfile.cmake
set(VCPKG_POLICY_EMPTY_PACKAGE enabled) #不执行任何操作
beta v1.0: 创建以下两个文件并放置在 project/port-registry/ports/beta/1.0 中。
vcpkg.json
{ "name": "beta", "version": "1.0" }
portfile.cmake
set(VCPKG_POLICY_EMPTY_PACKAGE enabled) #不执行任何操作
对于beta v1.1: 创建以下文件并放置在 project/port-registry/ports/beta/1.1 中。
vcpkg.json
{ "name": "beta", "version": "1.1" }
portfile.cmake
set(VCPKG_POLICY_EMPTY_PACKAGE enabled) #不执行任何操作
然后,我们需要编写每个库的所有版本信息。
对于alpha: 创建以下文件并放置在 project/port-registry/versions/a- 中。
alpha.json
{ "versions": [ { "version": "2.0", "path": "$/ports/alpha/2.0" }, { "version": "1.0", "path": "$/ports/alpha/1.0" } ] }
此文件需记录每次需要的版本信息,以便指定版本时使用(使用versioning。
注意:
- `a-`为 alpha 的首字母及-结合,后续相同。
- $/ports 指以 project/port-registry 为基准的port文件夹。
对于beta: 将创建以下文件并放置在 project/port-registry/versions/b- 中。
beta.json
{ "versions": [ { "version": "1.1", "path": "$/ports/beta/1.1" }, { "version": "1.0", "path": "$/ports/beta/1.0" } ] }
然后,我们需要将这两个库的更新信息注册在根目录中。
创建以下文件并放置在 project/port-registry/versions 中。此文件中仅记录所有库的最新版本信息。
baseline.json
{ "2022-09-28": { "alpha": { "baseline": "2.0" }, "beta": { "baseline": "1.1" } }, "2022-09-27": { "alpha": { "baseline": "1.0" }, "beta": { "baseline": "1.0" } } }
最后,我们需要向当前工程创建manifest文件及registry信息。
并将以下两个文件放置在 project 中。
vcpkg.json
{ "name": "test", "version": "1.0", "dependencies": [ "alpha", "beta" ] }
vcpkg-configuration.json
{ "default-registry": { "kind": "filesystem", "path": "port-registry" } }
注意:registry的版本记录支持多种存储方式,参见 https://www.cnblogs.com/vcpkg/p/15167835.html 和 https://github.com/microsoft/vcpkg/blob/master/docs/users/registries.md。
至此,我们对于这两个库的文件添加完成。我们可以尝试使用vcpkg来安装这些库的指定版本:
vcpkg.exe install
输出:
Detecting compiler hash for triplet x86-windows... The following packages will be rebuilt: alpha[core]:x86-windows -> 2.0 -- F:\project\port-registry\ports/alpha/2.0 beta[core]:x86-windows -> 1.1 -- F:\project\port-registry\ports/beta/1.1 Removing 1/4 beta:x86-windows Elapsed time to handle beta:x86-windows: 8.001 ms Removing 2/4 alpha:x86-windows Elapsed time to handle alpha:x86-windows: 7.625 ms Restored 0 package(s) from G:\binarycaching in 48.94 ms. Use --debug to see more details. Installing 3/4 alpha:x86-windows... Building alpha[core]:x86-windows... -- Installing port from location: F:\project\port-registry\ports/alpha/2.0 -- Performing post-build validation -- Performing post-build validation done Stored binary cache: "G:\binarycaching\9f\9f5bbd02e7c3bb3e2187ecefaeac63aa2306d9b7a8c2af8c28b383aad5306671.zip" Elapsed time to handle alpha:x86-windows: 152.3 ms Installing 4/4 beta:x86-windows... Building beta[core]:x86-windows... -- Installing port from location: F:\project\port-registry\ports/beta/1.1 -- Performing post-build validation -- Performing post-build validation done Stored binary cache: "G:\binarycaching\0e\0ed5323c04e03f1b5b3468ab47e33c414c60ecce67f5c23b57ce74b1e4620ea9.zip" Elapsed time to handle beta:x86-windows: 111.7 ms Total elapsed time: 6.426 s
如果我们需要使用旧版本,则可以向 vcpkg-configuration.json中添加额外字段baseline使用指定的更新版本。
例如:
vcpkg-configuration.json
{ "default-registry": { "kind": "filesystem", "path": "port-registry", "baseline": "2022-09-27" } }
输出:
Detecting compiler hash for triplet x86-windows... The following packages will be built and installed: alpha[core]:x86-windows -> 1.0 -- F:\project\port-registry\ports/alpha/1.0 beta[core]:x86-windows -> 1.0 -- F:\project\port-registry\ports/beta/1.0 Restored 0 package(s) from G:\binarycaching in 41.57 ms. Use --debug to see more details. Installing 1/2 alpha:x86-windows... Building alpha[core]:x86-windows... -- Installing port from location: F:\project\port-registry\ports/alpha/1.0 -- Performing post-build validation -- Performing post-build validation done Stored binary cache: "G:\binarycaching\8e\8ea9fda595d886898445450dfb10c3e267d89835df46e6fc94e2c2da08613954.zip" Elapsed time to handle alpha:x86-windows: 357.6 ms Installing 2/2 beta:x86-windows... Building beta[core]:x86-windows... -- Installing port from location: F:\project\port-registry\ports/beta/1.0 -- Performing post-build validation -- Performing post-build validation done Stored binary cache: "G:\binarycaching\6c\6c655fd919df7885dad02a3e9b96203656652f664b8df228ba1d606e0a365e28.zip" Elapsed time to handle beta:x86-windows: 115.4 ms Total elapsed time: 6.324 s
在后续的库更新后,我们只需要在 project/port-registry/ports/<LIBRARY_NAME>/<VERSION_NUM> 中添加一组新的manifest文件及portfile.cmake文件,并向 project/port-registry/versions/<LIBRARY_NAME_PREFIX>-/<LIBRARY_NAME>.json 中添加一条新的记录,最后将当前版本信息更新到 project/port-registry/versions/baseline.json 中即可。
所有文件的层级如下:
project/
|---port-registry/
|------ports/
|---------alpha/
|---------- 1.0/
|------------- vcpkg.json
|------------- portfile.cmake
|-----------2.0/
|------------- vcpkg.json
|------------- portfile.cmake
|---------beta/
|-----------1.0/
|------------- vcpkg.json
|------------- portfile.cmake
|-----------1.1/
|------------- vcpkg.json
|------------- portfile.cmake
|------versions/
|---------a-/
|-----------alpha.json
|---------b-/
|-----------beta.json
|----------
|---------baseline.json
|---vcpkg.json
|---vcpkg-configuration.json