目录
参考文档
运行环境
2022年12月
MBP M1
macOS Big Sur 11.4
cmake 3.21.3
vcpkg 2022-12-14-7ae0d8
创建注册表
- vcpkg提供了注册表服务,允许我不把代码发布到vcpkg官方平台,而是发布到私有的注册表中,成为一个私有的vcpkg端口
vcpkg x-init-registry <path>
$ git init my-ports
$ vcpkg x-init-registry my-ports
$ tree my-ports
my-ports
├── ports # 端口目录
└── versions # 版本管理目录
└── baseline.json # 最新版本记录文件
- 由于vcpkg的
x-add-version
命令不够健全,在创建注册表的时候需要把根目录中的脚本拷贝到注册表中
$ cp -fr $VCPKG_ROOT/scripts my-ports/scripts
$ tree my-ports
my-ports
├── ports
├── scripts # vcpkg 脚本
│ └── ...
└── versions
└── baseline.json
项目目录
- 对比上篇文章,新增了如下文件
./LICENSE
./README.md
./vcport/portfile.cmake
./vcport/vcpkg.json
.
├── CMakeLists.txt
├── LICENSE
├── README.md
├── cmake
│ └── tkportConfig.cmake.in
├── demo
│ ├── CMakeLists.txt
│ └── demo.cpp
├── lib
│ ├── CMakeLists.txt
│ ├── include
│ │ └── tkport
│ │ └── tkport.hpp
│ └── src
│ └── tkport
│ └── tkport.cpp
└── vcport
├── portfile.cmake
└── vcpkg.json
声明文件
LICENSE
和README.md
自己按需添加
打包项目文件并上传
- 当端口使用者通过vcpkg安装对应端口时,需要从互联网下载端口文件
- 在发布端口前,需要生成囊括所有项目构建和安装所需文件的压缩包,并将其上传到互联网
$ zip tkport-0.1.0.zip lib/**/* cmake/**/* CMakeLists.txt LICENSE
上传步骤按需自选,此处假设托管地址和hash码为
https://github.com/toki/vcpkg/dist/tkport-0.1.0.zip
1234567890234567890
准备端口文件
- vcpkg中的软件包以port(以下称为“端口”)的形式存在,目录为
$VCPKG_ROOT/ports/
./vcport
目录下的文件,用于添加到vcpkg端口列表中,为vcpkg提供检索、下载、构建、安装时的必要信息- 端口文件可以通过vcpkg命令生成,示例代码执行后的生成目录为
$VCPKG_ROOT/ports/tkport
$ vcpkg help create tkport: vcpkg create tkport https://github.com/toki/vcpkg/dist/tkport-0.1.0.zip "tkport-0.1.0.zip" Options: ...
本文中使用的是我根据案例工程配置好的端口文件
// ./vcport/vcpkg.json
{
"name": "tkport",
"version": "0.1.0",
"description": "The tkport is a port of vcpkg",
"homepage": "https://github.com/toki/tkport",
"dependencies": [
{
"name": "vcpkg-cmake",
"host": true
},
{
"name": "vcpkg-cmake-config",
"host": true
}
]
}
# ./vcport/portfile.cmake
vcpkg_download_distfile(ARCHIVE
URLS "https://github.com/toki/vcpkg/dist/tkport-0.1.0.zip"
FILENAME "tkport-0.1.0.zip"
SHA512 1234567890234567890
)
vcpkg_extract_source_archive_ex(
OUT_SOURCE_PATH SOURCE_PATH
ARCHIVE "${ARCHIVE}"
NO_REMOVE_ONE_LEVEL
)
vcpkg_cmake_configure(
SOURCE_PATH "${SOURCE_PATH}"
)
vcpkg_cmake_install()
vcpkg_cmake_config_fixup(CONFIG_PATH lib/cmake/tkport)
file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include")
vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE")
添加端口文件
- 将项目中
./vcport
中的文件拷贝到my-ports/ports/tkport
中- 此处如果想使用
vcpkg create <port> <url> [archivename]
命令生成端口文件到注册表中,需要额外指定vcpkg的根目录为注册表路径
--vcpkg-root=<path>
my-ports
├── ports
│ └── tkport
│ ├── portfile.cmake # 端口下载、构建、安装配置文件
│ └── vcpkg.json # 端口信息、依赖项配置文件
├── scripts
│ └── ...
└── versions
└── baseline.json
版本管理
- 添加好端口文件后,需要先提交,然后再通过命令生成对应的版本管理文件
vcpkg x-add-version <port>
- 由于我们的端口添加在注册表中,所以需要额外指定vcpkg的根目录为注册表路径
--vcpkg-root=<path>
$ git add .
$ git commit -m "[tkport] Add 0.1.0"
$ vcpkg x-add-version tkport --vcpkg-root=my-ports
my-ports
├── ports
│ └── tkport
│ ├── portfile.cmake
│ └── vcpkg.json
├── scripts
│ └── ...
└── versions
├── baseline.json
└── t- # t 开头端口版本管理文件目录
└── tkport.json # tkport端口版本管理文件
$ git add .
$ git commit -m "[tkport] Add 0.1.0" --amend
检验
- 找到上篇文章中用来检验的项目,尝试通过vcpkg集成tkport
$ vcpkg integrate install
Applied user-wide integration for this vcpkg root.
CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
$ vcpkg install tkport --overlay-ports=my-ports/tkport
Computing installation plan...
The following packages will be built and installed:
tkport[core]:arm64-osx -> 0.9.11 -- my-ports/tkport
Detecting compiler hash for triplet arm64-osx...
Restored 1 package(s) from ~/.cache/vcpkg/archives in 8.41 ms. Use --debug to see more details.
Installing 1/1 tkport:arm64-osx...
Elapsed time to handle tkport:arm64-osx: 3.307 ms
Total install time: 3.11 s
tkport provides CMake targets:
# this is heuristically generated, and may not be correct
find_package(tkport CONFIG REQUIRED)
target_link_libraries(main PRIVATE tkport::tkport)
- 根据提示修改配置文件,并构建运行
# ./CMakeLists.txt
cmake_minimum_required(VERSION 3.9.1)
set(CMAKE_TOOLCHAIN_FILE $ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake)
project(main)
add_executable(${PROJECT_NAME} main.cpp)
set_target_properties(
${PROJECT_NAME}
PROPERTIES
LINKER_LANGUAGE CXX
CXX_STANDARD 17
)
find_package(tkport CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE tkport::tkport)
$ cmake -B build
$ cmake --build build
$ build/main
Hellp CMake!