Manifest使用示例5 - assetsource使用缓存文件

背景

如果开发环境为内网,不能连接外网时,可以使用vcpkg的asset source特性来下载并编译库。即使原始源更改或消失也能确保继续运行。

该特性的工作流程是:

  1. 尝试从镜像中读取
  2. (如果第 1 步失败)从原始 url 读取
  3. (如果第 2 步成功)写回镜像

有效的源字符串 ( <source>)

形式  描述
clear 删除该命令前所有缓存的来源
x-azurl <url>[,<sas>[,<rw>]] 添加默认的基于文件的位置
x-block-origin  在镜像没有可用文件的情况下禁用原始url


以下步骤是利用镜像下载的资产缓存安装库sqlite3的示例. 

使用示例

1.本示例使用sqlite3作为示例库,请确保您在断开外网前已提前下载指定版本的资源包sqlite-amalgamation-3390200.zip到固定目录下,注意文件名需要改成资源包的哈希值.

例如:将C:\cache\sqlite-amalgamation-3390200.zip 改为 C:\cache\a8fb7903cdc985d17b421035d6affe16795382085d7eb70428bdbbb4abc7ea6674aa251d4e532b531733c195e8867bfbd3c5556824c76cf321f8bc617bad6a32

2. 示例的文件结构:

C:/
| --manifest_test/
| --vcpkg/
| ----build/
| ----CMakeLists.txt
| ----main.cpp
| ----vcpkg.json

 vcpkg.json

{
    "name": "test",
    "version-string": "0.1.0",
    "dependencies": [
        "sqlite3"
    ]
}

 

CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
project(test)
find_package(unofficial
-sqlite3 CONFIG REQUIRED) add_executable(main main.cpp) target_link_libraries(main PRIVATE unofficial::sqlite3::sqlite3)

 

main.cpp

//调用sqlite3_libversion()来打印sqlite3的版本
#include <sqlite3.h>
#include <stdio.h>
int main()
{
    printf("%s\n", sqlite3_libversion());
    return 0;
}

 

3. 配置cmake工程:

注意:通过添加路径前缀file:///将文件系统(例如网络驱动器)用作资产缓存。本实例中缓存文件放在C:/cache路径下, 所以使用--x-asset-sources="x-azurl,file:///C:/cache,readwrite" 来访问镜像文件。
在Cmake工程中需要使用-DVCPKG_INSTALL_OPTIONS来添加额外选项。

使用PowerShell运行以下命令:

'..\..\Program Files\CMake\bin\cmake.exe' "C:\manifest_test\" -G "Visual Studio 17 2022" -A x64 -DVCPKG_TARGET_TRIPLET=x64-windows -DVCPKG_BUILD_TYPE=debug -DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_INSTALL_OPTIONS="--x-asset-sources="x-azurl,file:///C:/cache,readwrite""

 

输出:

C:\manifest_test>"C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" "C:\manifest_test\CMakeLists.txt" -G "Visual Studio 17 2022" -A x64 -DVCPKG_TARGET_TRIPLET=x64-windows -DVCPKG_BUILD_TYPE=debug -DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_INSTALL_OPTIONS="--x-asset-sources="x-azurl,file:///C:/cache,readwrite""
-- Running vcpkg install
Detecting compiler hash for triplet x64-windows...
The following packages will be built and installed:
sqlite3[core]:x64-windows -> 3.39.2
* vcpkg-cmake[core]:x64-windows -> 2022-08-18
* vcpkg-cmake-config[core]:x64-windows -> 2022-02-06#1
Additional packages (*) will be modified to complete this operation.
Restored 0 package(s) from C:\Users\user\AppData\Local\vcpkg\archives in 159.8 us. Use --debug to see more details.
Installing 1/3 vcpkg-cmake-config:x64-windows...
Building vcpkg-cmake-config[core]:x64-windows...
-- Installing: C:/vcpkg/packages/vcpkg-cmake-config_x64-windows/share/vcpkg-cmake-config/vcpkg_cmake_config_fixup.cmake
-- Installing: C:/vcpkg/packages/vcpkg-cmake-config_x64-windows/share/vcpkg-cmake-config/vcpkg-port-config.cmake
-- Installing: C:/vcpkg/packages/vcpkg-cmake-config_x64-windows/share/vcpkg-cmake-config/copyright
-- Performing post-build validation
-- Performing post-build validation done
Stored binary cache: "C:\Users\user\AppData\Local\vcpkg\archives\6e\6e7dade925da6c3ba9411866eb3736590e431e6e8e2aaf7bb3f1f94072bebc01.zip"
Elapsed time to handle vcpkg-cmake-config:x64-windows: 121.8 ms
Installing 2/3 vcpkg-cmake:x64-windows...
Building vcpkg-cmake[core]:x64-windows...
-- Installing: C:/vcpkg/packages/vcpkg-cmake_x64-windows/share/vcpkg-cmake/vcpkg_cmake_configure.cmake
-- Installing: C:/vcpkg/packages/vcpkg-cmake_x64-windows/share/vcpkg-cmake/vcpkg_cmake_build.cmake
-- Installing: C:/vcpkg/packages/vcpkg-cmake_x64-windows/share/vcpkg-cmake/vcpkg_cmake_install.cmake
-- Installing: C:/vcpkg/packages/vcpkg-cmake_x64-windows/share/vcpkg-cmake/vcpkg-port-config.cmake
-- Installing: C:/vcpkg/packages/vcpkg-cmake_x64-windows/share/vcpkg-cmake/copyright
-- Performing post-build validation
-- Performing post-build validation done
Stored binary cache: "C:\Users\user\AppData\Local\vcpkg\archives\b3\b3edeab4f7391fa86e4b3862becca6c1e8286e23f5100f3954cc9799598a061c.zip"
Elapsed time to handle vcpkg-cmake:x64-windows: 131.6 ms
Installing 3/3 sqlite3:x64-windows...
Building sqlite3[core]:x64-windows...
-- Downloading https://sqlite.org/2022/sqlite-amalgamation-3390200.zip -> sqlite-amalgamation-3390200.zip...
-- Cleaning sources at C:/vcpkg/buildtrees/sqlite3/src/3390200-90a72c9855.clean. Use --editable to skip cleaning for the packages you specify.
-- Extracting source C:/vcpkg/downloads/sqlite-amalgamation-3390200.zip
-- Applying patch fix-arm-uwp.patch
-- Using source at C:/vcpkg/buildtrees/sqlite3/src/3390200-90a72c9855.clean
-- Found external ninja('1.11.0').
-- Configuring x64-windows
-- Building x64-windows-dbg
-- Building x64-windows-rel
-- Fixing pkgconfig file: C:/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 C:/vcpkg/downloads/tools/msys2/9a1ec3f33446b195
-- Fixing pkgconfig file: C:/vcpkg/packages/sqlite3_x64-windows/debug/lib/pkgconfig/sqlite3.pc
-- Performing post-build validation
-- Performing post-build validation done
Stored binary cache: "C:\Users\user\AppData\Local\vcpkg\archives\c6\c6a5f4fb89b1605da46158a3681e40c35a257da912b30335245e7d17e8fbcfa0.zip"
Elapsed time to handle sqlite3:x64-windows: 11.61 s

Total elapsed time: 14.53 s
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)

-- Running vcpkg install - done
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19044.
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/user/Desktop/manifest_test

  

注意:请注意输出中不会显示缓存来源信息。

 4. 使用以下命令生成可执行文件:

"C:\Program Files\CMake\bin\cmake.exe" --build .

 

输出:

MSBuild version 17.3.1+2badb37d1 for .NET Framework
Building Custom Rule C:/manifest_test/CMakeLists.txt
main.cpp
main.vcxproj -> C:\manifest_test\build\Debug\main.exe
Building Custom Rule C:/manifest_test/CMakeLists.txt

 5. 运行main.exe.

输出:

3.39.2

 

注意:在镜像没有可用文件的情况下将会从原始url下载, 你可以添加额外参数x-block-origin来禁用此设置。
再次安装将会出现以下报错(在设置的缓存路径下未找到缓存文件包):

Error: Failed to download from mirror set:
file:///C:/cache/a8fb7903cdc985d17b421035d6affe16795382085d7eb70428bdbbb4abc7ea6674aa251d4e532b531733c195e8867bfbd3c5556824c76cf321f8bc617bad6a32?*** SECRET ***: curl: (37) Couldn't open file C:/cache/a8fb7903cdc985d17b421035d6affe16795382085d7eb70428bdbbb4abc7ea6674aa251d4e532b531733c195e8867bfbd3c5556824c76cf321f8bc617bad6a32

  

posted @ 2022-10-18 17:25  vcpkg_C++包管理器  阅读(211)  评论(0编辑  收藏  举报