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