在多人开发环境中,我们仅希望一个人管理项目需要的所有第三方库,并使用服务器部署和分发vcpkg中已编译的库,此时可以使用vcpkg的Binary source特性。
VCPKG 默认开启 Binarycaching 特性,默认情况下,依赖库的缓存文件存放在以下第一个路径下;如果没有设置第一个环境变量,则将放在第二个路径下;同样的,如果没有设置 第一个 和 第二个 环境变量,将会存放在第三个路径下。
- Windows
- %VCPKG_DEFAULT_BINARY_CACHE%
- %LOCALAPPDATA%\vcpkg\archives
- %APPDATA%\vcpkg\archives
- 非 Windows
- $VCPKG_DEFAULT_BINARY_CACHE
- $XDG_CACHE_HOME/vcpkg/archives
- $HOME/.cache/vcpkg/archives
一、服务器主机:
1. 首先设置环境变量 VCPKG_DEFAULT_BINARY_CACHE 至公共目录:
PS F:\vcpkg> $env:VCPKG_DEFAULT_BINARY_CACHE=F:/Manifest-Binary/cache
2. 使用vcpkg的install命令安装需要的依赖库:
PS F:\vcpkg> ./vcpkg install sqlite3:x64-windows
3. 在公共目录 "F:\Manifest-Binary\cache" 下查看vcpkg缓存:
PS F:Manifest-Binary\cache> dir Directory: G:\binarycaching Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 10/10/2022 5:24 PM 4f PS F:\Manifest-Binary\cache>cd 4f PS F:\Manifest-Binary\cache\4f>dir Directory: F:\Manifest-Binary\cache\4f Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 10/10/2022 5:23 PM 3329642 4f95bcae2db85409ba2e8c189788b17c6049ba1d07e51502081c10e159536083.zip
二、开发端主机:
此示例中开发端主机的CMake工程test2依赖于sqlite3。
1. 在工程文件夹中创建manifest文件并写入依赖内容
文件结构如下 (根目录为 F:/Manifest-Binary):
| --test/
| ----build
| ----CMakeLists.txt
| ----main.cpp
| ----vcpkg.json
相应文件内容为:
vcpkg.json
{ "name": "test2", "version-string": "0.0.1", "dependencies": [ "sqlite3" ] }
main.cpp
#include <sqlite3.h> #include <stdio.h> int main() { printf("%s\n", sqlite3_libversion()); return 0; }
CMakeLists.txt
cmake_minimum_required(VERSION 3.18) project(versionstest2 CXX) add_executable(main main.cpp) find_package(unofficial-sqlite3 CONFIG REQUIRED) target_link_libraries(main PRIVATE unofficial::sqlite3::sqlite3)
2. Clone vcpkg
PS F:\> git clone https://github.com/microsoft/vcpkg.git PS F:\> cd vcpkg PS F:\vcpkg> .\bootstrap-vcpkg.bat PS F:\vcpkg> .\vcpkg.exe integrate install Applied user-wide integration for this vcpkg root. CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=F:/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. 配置CMake工程,将Binarycaching参数 "--binarysource=clear;--binarysource=files,F:\\Manifest-Binary\cache,read" 添加到cmake命令行中运行以下命令:
"C:\Program Files\CMake\bin\cmake.exe" .. -G "Visual Studio 16 2019" -A x64 -DVCPKG_TARGET_TRIPLET=x64-windows -DCMAKE_BUILD_TYPE=Debug -DCMAKE_TOOLCHAIN_FILE=F:/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_INSTALL_OPTIONS="--binarysource=clear;--binarysource=files,F:\\Manifest-Binary\cache,read"
输出:
-- 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 1 package(s) from F:\\Manifest-Binary\cache in 120.4 ms. Use --debug to see more details. Installing 1/3 vcpkg-cmake-config:x64-windows... Building vcpkg-cmake-config[core]:x64-windows... -- Installing: F:/vcpkg/packages/vcpkg-cmake-config_x64-windows/share/vcpkg-cmake-config/vcpkg_cmake_config_fixup.cmake -- Installing: F:/vcpkg/packages/vcpkg-cmake-config_x64-windows/share/vcpkg-cmake-config/vcpkg-port-config.cmake -- Installing: F:/vcpkg/packages/vcpkg-cmake-config_x64-windows/share/vcpkg-cmake-config/copyright -- Performing post-build validation -- Performing post-build validation done Elapsed time to handle vcpkg-cmake-config:x64-windows: 72.18 ms Installing 2/3 vcpkg-cmake:x64-windows... Building vcpkg-cmake[core]:x64-windows... -- Installing: F:/vcpkg/packages/vcpkg-cmake_x64-windows/share/vcpkg-cmake/vcpkg_cmake_configure.cmake -- Installing: F:/vcpkg/packages/vcpkg-cmake_x64-windows/share/vcpkg-cmake/vcpkg_cmake_build.cmake -- Installing: F:/vcpkg/packages/vcpkg-cmake_x64-windows/share/vcpkg-cmake/vcpkg_cmake_install.cmake -- Installing: F:/vcpkg/packages/vcpkg-cmake_x64-windows/share/vcpkg-cmake/vcpkg-port-config.cmake -- Installing: F:/vcpkg/packages/vcpkg-cmake_x64-windows/share/vcpkg-cmake/copyright -- Performing post-build validation -- Performing post-build validation done Elapsed time to handle vcpkg-cmake:x64-windows: 116 ms Installing 3/3 sqlite3:x64-windows... Elapsed time to handle sqlite3:x64-windows: 52.98 ms Total elapsed time: 2.706 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.19042. -- 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 -- Configuring done -- Generating done -- Build files have been written to: F:/Manifest-Binary/test2/build
从上面的构建日志中我们可以看到,开发端主机成功使用了共享路径 "F:\Manifest-Binary\cache" 下已经构建好的sqlite3缓存文件,大大节省了时间。
4. 构建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 F:/Manifest-Binary/test2/CMakeLists.txt main.cpp main.vcxproj -> F:\Manifest-Binary\test2\build\Debug\main.exe Building Custom Rule F:/Manifest-Binary/test2/CMakeLists.txt
5. 运行CMake工程
.\Debug\main.exe
输出:
3.39.2
注意:如果想通过Visual Studio在Manifest模式下使用Binarycaching,唯一需要注意的是CMakeSettings.json文件的设置:
{ "configurations": [ { "name": "x64-Debug", "generator": "Visual Studio 16 2019 Win64", "configurationType": "Debug", "inheritEnvironments": [ "msvc_x64_x64" ], "buildRoot": "${projectDir}\\out\\build\\${name}", "installRoot": "${projectDir}\\out\\install\\${name}", "cmakeCommandArgs": "", "buildCommandArgs": "", "ctestCommandArgs": "", "variables": [ { "name": "CMAKE_TOOLCHAIN_FILE", "value": "F:/Manifest-Binary/vcpkg/scripts/buildsystems/vcpkg.cmake" }, { "name": "VCPKG_INSTALL_OPTIONS", "value": "--binarysource=clear;--binarysource=\"files,F:/Manifest-Binary/cache,read\"" } ] } ] }
Msbuild工程使用Manifest和Binarycaching,参考 https://www.cnblogs.com/vcpkg/p/15019899.html 。
标签:cmake,Binarycaching,--,示例,x64,Manifest,windows,vcpkg From: https://www.cnblogs.com/vcpkg/p/16791097.html