Manifest使用示例6 - 安装并使用git私有仓库

有的开发者会借助github创建自己的私有库,那么如何利用vcpkg 使用git上的私有库呢? 请参考以下示例。

 使用示例

1. 准备一个私有仓库Cheney-W/test, 且为这个私有库生成一个token. (github在2020年8月13日更新以后,停止了对https用户名及密码的支持,所以本示例使用token进行自动认证。)

2. 准备一个Overlay ports,这个port用来在vcpkg中解释这个私有仓库, 库名mytest,使用CMake编译源码包,文件结构如下。

  |---mytest/ (此文件夹可随意放置)

      |---vcpkg.json

      |---portfile.cmake

 vcpkg.json

{
   "name": "mytest",
   "version": "1.0.4",
   "description": "Test manifest mode",
   "dependencies": [
    {
      "name": "vcpkg-cmake",
      "host": true
    },
    {
      "name": "vcpkg-cmake-config",
      "host": true
    }
  ]
}

 portfile.cmake

#从环境变量获取私有库的token 
set(MY_TOKEN_VAR "*******************")
if(DEFINED ENV{MY_TOKEN_VAR})
    set(MY_TOKEN_VAR "$ENV{MY_TOKEN_VAR}@")
endif()

#下载私有库
vcpkg_from_git(
    OUT_SOURCE_PATH SOURCE_PATH
    URL "https://Cheney-W:${MY_TOKEN_VER}@github.com/Cheney-W/test"
    REF 2253a2a3cbe7e1a1047ad6cb5579059a8f70b099 #v1.0.4
    HEAD_REF main
 )

#使用CMake配置工程文件
vcpkg_cmake_configure(
    SOURCE_PATH "${SOURCE_PATH}"
)

#使用CMake安装源文件
vcpkg_cmake_install()

#修复库的config文件的路径
vcpkg_cmake_config_fixup()

#删除debug目录下的头文件,vcpkg默认直接调用release目录下的头文件
file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include")

#安装license文件
file(INSTALL "${SOURCE_PATH}/LICENSE" DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}" RENAME copyright)

 

3. 本示例所需的全部的文件结构: (manifest-test即为自己的工程文件夹)

   根目录 C:\ 

|---manifest-test

        |---build/

        |---vcpkg.json

        |---CMakeLists.txt

        |---test.cpp

 vcpkg.json

{
  "name": "test",
  "version-string": "0.0.1",
  "dependencies": [
    "mytest"
  ]
}

 CMakeLists.txt

cmake_minimum_required (VERSION 3.8)
 
project(test)
#Add source to this project's executable.
add_executable (test "test.cpp")

find_package(mytest CONFIG REQUIRED)
target_link_libraries(test PRIVATE mytest)

 test.cpp

#include <iostream>
#include <Mytest.h>

using namespace std;

int main()
{
    cout << addition(10,5) << endl;
    cout << subtraction(10,5) << endl;
    cout << multiplication(10,5) << endl;
    cout << division(10,5) << endl;
    return 0;
}

 

4. 在build文件夹中使用cmd运行以下命令:

Set VCPKG_KEEP_ENV_VARS=MY_TOKEN_VAR
Set MY_TOKEN_VAR=abc123 (这里的值实际上是私有库的token)

cmake.exe "C:\manifest-test" -G "Visual Studio 17 2022" -A x64  -DCMAKE_CONFIGURATION_TYPES:STRING="Debug" -DCMAKE_TOOLCHAIN_FILE:STRING="C:\vcpkg\scripts\buildsystems\vcpkg.cmake" -DVCPKG_OVERLAY_PORTS="C:\manifest-test\mytest"

输出:

-- Running vcpkg install
A suitable version of cmake was not found (required v3.24.0). Downloading portable cmake v3.24.0...
Downloading cmake...
https://github.com/Kitware/CMake/releases/download/v3.24.0/cmake-3.24.0-windows-i386.zip -> C:\vcpkg\downloads\cmake-3.24.0-windows-i386.zip
Extracting cmake...
Detecting compiler hash for triplet x64-windows...
A suitable version of powershell-core was not found (required v7.2.5). Downloading portable powershell-core v7.2.5...
Downloading powershell-core... https://github.com/PowerShell/PowerShell/releases/download/v7.2.5/PowerShell-7.2.5-win-x86.zip -> C:\vcpkg\downloads\PowerShell-7.2.5-win-x86.zip
Extracting powershell-core...
The following packages will be built and installed:
    mytest[core]:x64-windows -> 1.0.4 -- C:\manifest-test\mytest
  * vcpkg-cmake[core]:x64-windows -> 2022-07-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\xxx\AppData\Local\vcpkg\archives in 2.428 ms. 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
A suitable version of 7zip was not found (required v21.7.0). Downloading portable 7zip v21.7.0...
Downloading 7zip...
  https://www.7-zip.org/a/7z2107-extra.7z -> C:\vcpkg\downloads\7z2107-extra.7z
Extracting 7zip...
Stored binary cache: "C:\Users\xxxx\AppData\Local\vcpkg\archives\07\07b134143814967b68c15670722cb68a20ab286426d04e6ec879fa82aa8fb672.zip"
Elapsed time to handle vcpkg-cmake-config:x64-windows: 4.424 s
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\xxxx\AppData\Local\vcpkg\archives\28\28d639c03633f392fd9e42d95b946c3ad098db80eca5f48e61e69e599dac5548.zip"
Elapsed time to handle vcpkg-cmake:x64-windows: 236.2 ms
Installing 3/3 mytest:x64-windows...
Building mytest[core]:x64-windows...
-- Installing port from location: C:\manifest-test\mytest
-- Fetching https://Cheney-W:xxxxxxxxxxxxxxx@github.com/Cheney-W/test 2253a2a3cbe7e1a1047ad6cb5579059a8f70b099...
-- Extracting source C:/vcpkg/downloads/mytest-2253a2a3cbe7e1a1047ad6cb5579059a8f70b099.tar.gz
-- Using source at C:/vcpkg/buildtrees/mytest/src/9a8f70b099-3bd3e4a0d2.clean
-- Found external ninja('1.10.2').
-- Configuring x64-windows
-- Building x64-windows-dbg
-- Building x64-windows-rel
-- Installing: C:/vcpkg/packages/mytest_x64-windows/share/mytest/copyright
-- Performing post-build validation
-- Performing post-build validation done
Stored binary cache: "C:\Users\xxxx\AppData\Local\vcpkg\archives\11\11b32c50932b61b2ad082d5e65fdb5adde62339071bf4274d30d46c38ac91324.zip"

Elapsed time to handle mytest:x64-windows: 11.12 s
Total elapsed time: 2.438 min

mytest provides CMake targets:
    # this is heuristically generated, and may not be correct
    find_package(mytest CONFIG REQUIRED)
    target_link_libraries(main PRIVATE mytest)

-- Running vcpkg install - done
-- Selecting Windows SDK version 10.0.22000.0 to target Windows 10.0.22621.
-- The C compiler identification is MSVC 19.33.31424.0
-- The CXX compiler identification is MSVC 19.33.31424.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/MSVC/14.33.31424/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/MSVC/14.33.31424/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: C:/manifest-test

 

5. 构建CMake工程

cmake.exe --build .

输出:

Microsoft (R) Build Engine version 17.3.0-preview-22226-03+b6fc583d4 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
  Checking Build System
  Building Custom Rule C:/manifest-test/CMakeLists.txt
  test.cpp
  test.vcxproj -> C:\manifest-test\build\Debug\test.exe
  Building Custom Rule C:/manifest-test/CMakeLists.txt

 

6. 运行程序test.exe,显示:

15
5
50
2

 

注意:

本文档示例port的安装文件目录为: E:\Manifest\vcpkg_installed\x64-windows

在MSBuild工程中使用该库,需要在项目——属性——vcpkg 中将User Vcpkg 设为true,将--overlay-ports="<overlay_abs_prefix>\\mytest"添加进Additional Options中。

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