VCPKG 特性 - Overlay triplets
背景
由于个人项目中的设置经常与vcpkg中默认的triplets中的设置不一致,在以前我们只能将自定义的triplet文件添加到VCPKG_ROOT/triplets中并使用install --triplet=CUSTOM_TRIPLET来满足个人的需求。
而现在使用overlay triplets就可以解决这类问题。
经典模式
vcpkg现在对于install、update、upgrade、export与depend-info命令提供了选项 --overlay-triplets,您可以将该选项添加这些命令之后来指定库覆盖安装的其他路径。
可使用如下命令:
vcpkg install port --overlay-triplets=<port_dir>
其中port_dir可以是相对路径,也可以是绝对路径。同时还可以通过多个--overlay-triplets选项来支持多个路径。
使用示例
下面以在linux上构建动态库为例:
一、经典模式中使用自定义triplets
1. 创建自定义triplet文件
假设在custom-triplets目录下创建一个x64-linux-dynamic.cmake的文件,其中custom-triplets是与vcpkg同级的目录。
root@linux003:/home/Nancy/code# mkdir custom-triplets
文件目录结构如下:
|vcpkg
|--triplets/x64-linux.cmake
|custom-triplets
|--x64-linux-dynamic.cmake
为了操作方便,直接复制已经存在的x64-linux.cmake文件中的内容到x64-linux-dynamic.cmake
root@linux003:/home/Nancy/code# cp vcpkg/triplets/x64-linux.cmake custom-triplets/x64-linux-dynamic.cmake
然后修改custom-triplets/x64-linux-dynamic.cmake文件
root@linux003:/home/Nancy/code# vim custom-triplets/x64-linux-dynamic.cmake
使其符合如下内容:
# root@linux003:/home/Nancy/code/custom-triplets/x64-linux-dynamic.cmake
set(VCPKG_TARGET_ARCHITECTURE x64)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_LIBRARY_LINKAGE dynamic) # This changed from static to dynamic
set(VCPKG_CMAKE_SYSTEM_NAME Linux)
2. 使用--overlay-triplets选项来构建动态库
root@linux003:/home/Nancy/code# ./vcpkg/vcpkg install sqlite3:x64-linux-dynamic --overlay-triplets=custom-triplets
运行命令,输出如下:
root@linux003:/home/Nancy/code# ./vcpkg/vcpkg install sqlite3:x64-linux-dynamic --overlay-triplets=custom-triplets
Computing installation plan...
The following packages will be built and installed:
sqlite3[core]:x64-linux-dynamic -> 3.35.5
Detecting compiler hash for triplet x64-linux-dynamic...
Could not locate cached archive: /root/.cache/vcpkg/archives/98/98cf1c246250381df7d8d596571db77e83aa3857.zip
Starting package 1/1: sqlite3:x64-linux-dynamic
Building package sqlite3[core]:x64-linux-dynamic...
-- [OVERLAY] Loading triplet configuration from: /home/Nancy/code/custom-triplets/x64-linux-dynamic.cmake
-- Downloading https://sqlite.org/2021/sqlite-amalgamation-3350500.zip -> sqlite-amalgamation-3350500.zip...
-- Extracting source /home/Nancy/code/vcpkg/downloads/sqlite-amalgamation-3350500.zip
-- Applying patch fix-arm-uwp.patch
-- Using source at /home/Nancy/code/vcpkg/buildtrees/sqlite3/src/3350500-adf155e1e1.clean
-- Configuring x64-linux-dynamic-dbg
-- Configuring x64-linux-dynamic-rel
-- Building x64-linux-dynamic-dbg
-- Building x64-linux-dynamic-rel
-- Performing post-build validation
-- Performing post-build validation done
Stored binary cache: /root/.cache/vcpkg/archives/98/98cf1c246250381df7d8d596571db77e83aa3857.zip
Building package sqlite3[core]:x64-linux-dynamic... done
Installing package sqlite3[core]:x64-linux-dynamic...
Installing package sqlite3[core]:x64-linux-dynamic... done
Elapsed time for package sqlite3:x64-linux-dynamic: 35.08 s
Total elapsed time: 35.42 s
The package sqlite3:x64-linux-dynamic provides CMake targets:
find_package(unofficial-sqlite3 CONFIG REQUIRED)
target_link_libraries(main PRIVATE unofficial::sqlite3::sqlite3)
注意: 若日志中出现如下信息表示使用的是自定义triplet。
-- [OVERLAY] Loading triplet configuration from: /home/Nancy/code/custom-triplets/x64-linux-dynamic.cmake
如果想remove已经安装的自定义triplet的库,需要在remove命令中传入自定义triplet所在的路径。例如:
./vcpkg/vcpkg remove sqlite3:x64-linux-static --overlay-triplets=custom-triplets
二、经典模式中使用覆盖默认的triplets
假如我们想在linux上实现像在windows上使用的triplet,来构建动态库(x64-windows)或者静态库(x64-windows-static),可以使用--overlay-triplets来覆盖默认的triplet。
x64-linux: 构建动态库
x64-linux-static:构建静态库
1. 创建overlay triplets
为了方便操作,可以将custom-triplets/x64-linux-dynamic.cmake重命名为custom-triplets/x64-linux.cmake,然后拷贝默认的x64-linux .cmake到custom-triplets文件夹下,同时将文件名称重命名为x64-linux-static.cmake。
文件目录结构如下:
|vcpkg
|--triplets/x64-linux.cmake
|custom-triplets
|--x64-linux-dynamic.cmake--> x64-linux.cmake
root@linux003:/home/Nancy/code# mv custom-triplets/x64-linux-dynamic.cmake custom-triplets/x64-linux.cmake
root@linux003:/home/Nancy/code# cp vcpkg/triplets/x64-linux.cmake custom-triplets/x64-linux-static.cmake
2. 使用--overlay-triplets选项来覆盖默认的triplets
root@linux003:/home/Nancy/code# ./vcpkg/vcpkg install sqlite3:x64-linux --overlay-triplets=custom-triplets
运行命令,输出如下:
root@linux003:/home/Nancy/code# ./vcpkg/vcpkg install sqlite3:x64-linux --overlay-triplets=custom-triplets
Computing installation plan...
The following packages will be built and installed:
sqlite3[core]:x64-linux -> 3.35.5
Detecting compiler hash for triplet x64-linux...
Could not locate cached archive: /root/.cache/vcpkg/archives/13/138eefbbfab9abc92b6efee341059012be477f92.zip
Starting package 1/1: sqlite3:x64-linux
Building package sqlite3[core]:x64-linux...
-- [OVERLAY] Loading triplet configuration from: /home/Nancy/code/custom-triplets/x64-linux.cmake
-- Using /home/Nancy/code/vcpkg/downloads/sqlite-amalgamation-3350500.zip
-- Cleaning sources at /home/Nancy/code/vcpkg/buildtrees/sqlite3/src/3350500-adf155e1e1.clean. Use --editable to skip cleaning for the packages you specify.
-- Extracting source /home/Nancy/code/vcpkg/downloads/sqlite-amalgamation-3350500.zip
-- Applying patch fix-arm-uwp.patch
-- Using source at /home/Nancy/code/vcpkg/buildtrees/sqlite3/src/3350500-adf155e1e1.clean
-- Configuring x64-linux-dbg
-- Configuring x64-linux-rel
-- Building x64-linux-dbg
-- Building x64-linux-rel
-- Performing post-build validation
-- Performing post-build validation done
Stored binary cache: /root/.cache/vcpkg/archives/13/138eefbbfab9abc92b6efee341059012be477f92.zip
Building package sqlite3[core]:x64-linux... done
Installing package sqlite3[core]:x64-linux...
Installing package sqlite3[core]:x64-linux... done
Elapsed time for package sqlite3:x64-linux: 33.66 s
Total elapsed time: 33.92 s
The package sqlite3:x64-linux provides CMake targets:
find_package(unofficial-sqlite3 CONFIG REQUIRED)
target_link_libraries(main PRIVATE unofficial::sqlite3::sqlite3)
注意: 若日志中出现如下信息表示默认triplet已经被自定义triplet所覆盖。
-- [OVERLAY] Loading triplet configuration from: /home/Nancy/code/custom-triplets/x64-linux.cmake
manifest模式
请参照Overlay Ports设置。
可以通过向cmake命令添加参数VCPKG_OVERLAY_TRIPLETS或者在msbuild项目选项Additional Options中传入--overlay-triplets=<path>实现在manifest中使用overlay triplets。
请参考Overlay ports中的manifest模式设置。
参考:https://github.com/microsoft/vcpkg/blob/master/docs/examples/overlay-triplets-linux-dynamic.md