Git registries 使用示例

创建注册表

1. 创建一个空的github仓库 https://github.com/PhoebeHui/vcpkg-registry 用于示例。

2. Git clone  https://github.com/PhoebeHui/vcpkg-registry

3. 在 vcpkg-registry 目录下创建以下文件夹及文件。

vcpkg-registry/
|-- ports/
|-------- beicode/
|------------ portfile.cmake
|------------ vcpkg.json
|-- versions/
|--------- baseline.json
|--------- b-/
|------------beicode.json

 4. 为了方便,可以直接从官方库中复制ports文件夹,这里主要介绍versions目录中的文件创建步骤。

versions/baseline.json 文件,注意:baseline字段对应的值为库的当前的版本号。

{
  "default": {
    "beicode": { "baseline": "1.0.0", "port-version": 0 }
  }
}

versions/b-/beicode.json 文件:

{
  "versions": [
    {
      "version": "1.0.0",
      "git-tree": ""
    }
  ]
}

5. 以上文件创建好之后, 提交改动。

PS E:\vcpkg\vcpkgtest\registries\vcpkg-registry> git add .
PS E:\vcpkg\vcpkgtest\registries\vcpkg-registry> git commit -m "[beicode] new port"
[main 0e4f1fa] [beicode] new port
 4 files changed, 41 insertions(+)
 create mode 100644 ports/beicode/portfile.cmake
 create mode 100644 ports/beicode/vcpkg.json
 create mode 100644 versions/b-/beicode.json
 create mode 100644 versions/baseline.json

 6. 获取git-tree字段值,这其实与versionning 特性中介绍的获取git-tree的原理是一样的。

PS E:\vcpkg\vcpkgtest\registries\vcpkg-registry> git rev-parse HEAD:ports/beicode
7fb5482270b093d40ab8ac31db89da4f880f01ba

 7. 复制以上哈希值,打开 "vcpkg-registry\versions\b-\beicode.json"文件,填写到git-tree字段中,然后保存。并提交改动。

PS E:\vcpkg\vcpkgtest\registries\vcpkg-registry> git add versions
PS E:\vcpkg\vcpkgtest\registries\vcpkg-registry> git commit --amend --no-edit
[main 94a566b] [beicode] new port
 Date: Sun Aug 15 23:23:18 2021 -0700
 4 files changed, 41 insertions(+)
 create mode 100644 ports/beicode/portfile.cmake
 create mode 100644 ports/beicode/vcpkg.json
 create mode 100644 versions/b-/beicode.json
 create mode 100644 versions/baseline.json

8. 获取当前最新的commit Id 作为baseline字段的值。

PS E:\vcpkg\vcpkgtest\registries\vcpkg-registry> git rev-parse HEAD
94a566bc8ea2706dafe1457cdd0682e09fd00416

 

使用注册表(manifest模式)

1. 在您的项目中使用beicode库,可以在您的项目根目录创建vcpkg-configuration.json 与vcpkg.json 文件,或者 在vcpkg的根目录下创建,为了快速测试,将这两个文件放置在vcpkg根目录下。

创建vcpkg-configuration.json 文件,注意baseline字段的哈希值就是在以上第8步获取的最新的commit id. repository字段的值设置本地git仓库的目录,我们测试之后再提交。

vcpkg-configuration.json 文件:

{
   "registries": [
    {
      "kind": "git",
      "baseline": "94a566bc8ea2706dafe1457cdd0682e09fd00416",
      "repository": "E:/vcpkg/vcpkgtest/registries/vcpkg-registry",
      "packages": [ "beicode"]
    }
  ]
}

vcpkg.json 文件:

{
  "name": "test",
  "version": "0",
  "dependencies": [
     "beicode",
     "fmt"
   ]
}

 2. 安装库,进行本地测试。

PS E:\vcpkg\clean\vcpkg> ./vcpkg install --triplet=x64-windows
Fetching baseline information from E:/vcpkg/vcpkgtest/registries/vcpkg-registry...
Fetching registry information from E:/vcpkg/vcpkgtest/registries/vcpkg-registry...
Detecting compiler hash for triplet x64-windows...
The following packages will be built and installed:
    beicode[core]:x64-windows -> 1.0.0 -- C:\Users\phoebe\AppData\Local\vcpkg\registries\git-trees\7fb5482270b093d40ab8ac31db89da4f880f01ba
    fmt[core]:x64-windows -> 7.1.3#5
  * vcpkg-cmake[core]:x64-windows -> 2021-07-30
  * vcpkg-cmake-config[core]:x64-windows -> 2021-05-22#1
Additional packages (*) will be modified to complete this operation. ...

 

可以看到vcpkg开始安装beicode与fmt,注意示例仓库并没有提供fmt,它其实来源于默认的注册表。即vcpkg本身提供的库,如果把beicode 从vcpkg-configuration.json 中移除,那么它就会报错,原因是vcpkg没有提供beicode库,如果您想使用自定义的fmt,那么需要把fmt添加到 registries 的 packages 字段中。

 3. 所有测试通过之后,可以把以上的改动提交到git仓库 https://github.com/PhoebeHui/vcpkg-registry

 4. 修改 vcpkg-configuration.json 文件中的repository 字段值为 https://github.com/PhoebeHui/vcpkg-registry

{
   "registries": [
    {
      "kind": "git",
      "baseline": "94a566bc8ea2706dafe1457cdd0682e09fd00416",
      "repository": "https://github.com/PhoebeHui/vcpkg-registry",
      "packages": [ "beicode"]
    }
  ]
}

 5. 使用beicode 与fmt库,可以看到它会从 https://github.com/PhoebeHui/vcpkg-registry 获取并安装所需要的库。

PS E:\vcpkg\clean\vcpkg> ./vcpkg install --triplet=x64-windows
Fetching registry information from https://github.com/PhoebeHui/vcpkg-registry...
Detecting compiler hash for triplet x64-windows...
The following packages will be rebuilt:
    beicode[core]:x64-windows -> 1.0.0 -- C:\Users\phoebe\AppData\Local\vcpkg\registries\git-trees\7fb5482270b093d40ab8ac31db89da4f880f01ba

The following packages will be built and installed:
    fmt[core]:x64-windows -> 7.1.3#5 -- C:\Users\phoebe\AppData\Local\vcpkg\registries\git-trees\52a5c56d85771a278330e955b703f4db86cfe86d
  * vcpkg-cmake[core]:x64-windows -> 2021-07-30
  * vcpkg-cmake-config[core]:x64-windows -> 2021-05-22#1

Additional packages (*) will be modified to complete this operation.
Starting package 1/5: beicode:x64-windows
...

 

使用注册表(经典模式)

使用经典模式可以方便测试,无需创建vcpkg.json文件。

1. 将vcpkg-configuration.json 放到 vcpkg 根目录下。

{
   "registries": [
    {
      "kind": "git",
      "baseline": "94a566bc8ea2706dafe1457cdd0682e09fd00416",
      "repository": "https://github.com/PhoebeHui/vcpkg-registry",
      "packages": [ "beicode"]
    }
  ]
}

 2. 安装 beicode库。

PS E:\vcpkg\clean\vcpkg> ./vcpkg install beicode
Computing installation plan...
The following packages will be built and installed:
    beicode[core]:x86-windows -> 1.0.0 -- C:\Users\phoebe\AppData\Local\vcpkg\registries\git-trees\7fb5482270b093d40ab8ac31db89da4f880f01ba

Detecting compiler hash for triplet x86-windows...
Using cached binary package: C:\Users\phoebe\AppData\Local\vcpkg\archives\e5\e528832ac67db7037907d1e01c73f528c9a701fefc9b9d5260246881d376953d.zip
Starting package 1/1: beicode:x86-windows
...

 

 参考:

https://github.com/northwindtraders/vcpkg-registry
https://devblogs.microsoft.com/cppblog/how-to-start-using-registries-with-vcpkg
https://devblogs.microsoft.com/cppblog/registries-bring-your-own-libraries-to-vcpkg

posted @ 2021-08-20 18:55  vcpkg_C++包管理器  阅读(586)  评论(0编辑  收藏  举报