vcpkg添加自定义包安装

文章目录

    • 前言
    • 新建overlay-ports
    • 编写baseline.json
    • 编写openssl.json
    • 编写配置
      • 修改vcpkg.json
      • 修改portfile.cmake
    • 挂载安装
    • 后话

前言

vcpkg收集了很多C++的包,可总是会有没收录进去的,以openssl 3.0.0版本举例,这个版本vcpkg没有收录进去,本教程教你vcpkg自定义添加


新建overlay-ports

在vcpkg根目录下创建一个命名为overlayports的文件夹(其实可以随意命名,后面教程都是用这个来举例),在overlayports中新建两个文件夹,分别为ports, versions

# overlayports目录结构
├─ports
│  └─openssl
└─versions
    └─o-
    │ └─openssl.json
    └─baseline.json

编写baseline.json

overlayports/versions目录下新建baseline.json, 保存以下内容

{
  "default": {
	"openssl": {
      "baseline": "3.0.0",
      "port-version": 0
    }
  }
}

port-version 是版本控制来的,默认为0即可,比如3.0.0有两个小版本,就分别为0,1

vcpkg添加自定义包安装_第1张图片
vcpkg添加自定义包安装_第2张图片


编写openssl.json

因为openssl是o字母开头,所以在在overlayports/versions目录下新建文件夹o-, 在o-文件夹中新建openssl.json

{
  "versions": [
    {
      "git-tree": "3e5163bcf41573dd2bc14130db52e77599f4132d",
      "version": "3.0.0",
      "port-version": 0
    }
  ]
}

git-tree 字段需要是commit的hash,但是也可以随意填

git add ports/openssl
git commit -m "ports: add openssl"
git rev-parse HEAD:ports/openssl

这样就可以计算出来了


编写配置

这个我建议直接把原来portsopenssl整个文件夹拷贝过来

修改vcpkg.json

version 修改成3.0.0port-version键值改成0,其他不变就行。

{
  "name": "openssl",
  "version": "3.0.0",
  "port-version": 1,
  "description": "OpenSSL is an open source project that provides a robust, commercial-grade, and full-featured toolkit for the Transport Layer Security (TLS) and Secure Sockets Layer (SSL) protocols. It is also a general-purpose cryptography library.",
  "homepage": "https://www.openssl.org",
  "license": "Apache-2.0",
  "dependencies": [
    {
      "name": "vcpkg-cmake",
      "host": true
    },
    {
      "name": "vcpkg-cmake-config",
      "host": true
    },
    {
      "name": "vcpkg-cmake-get-vars",
      "host": true
    }
  ],
  "features": {
    "tools": {
      "description": "Install openssl executable and scripts",
      "supports": "!uwp"
    }
  }
}

修改portfile.cmake

这个改动有点大,设计到一些命令,例如github上的openssl是没有上传3.0.0代码的,官网才有,本身是用vcpkg_from_github拉源码,就要改成vcpkg_download_distfile., 原本打的patch是不可以用的, 需要额外使用vcpkg_extract_source_archive命令来打patch, SHA512需要下载到本地计算获得

(Get-FileHash .\openssl-3.0.0.tar.gz -Algorithm SHA512 | Format-List).Hash.ToLower()
  • 保存即可
if(EXISTS "${CURRENT_INSTALLED_DIR}/share/libressl/copyright"
    OR EXISTS "${CURRENT_INSTALLED_DIR}/share/boringssl/copyright")
    message(FATAL_ERROR "Can't build openssl if libressl/boringssl is installed. Please remove libressl/boringssl, and try install openssl again if you need it.")
endif()

if(VCPKG_TARGET_IS_EMSCRIPTEN)
    vcpkg_check_linkage(ONLY_STATIC_LIBRARY)
endif()

if (NOT "${VERSION}" MATCHES [[^([0-9]+)\.([0-9]+)\.([0-9]+)$]])
    message(FATAL_ERROR "Version regex did not match.")
endif()
set(OPENSSL_VERSION_MAJOR "${CMAKE_MATCH_1}")
set(OPENSSL_VERSION_MINOR "${CMAKE_MATCH_2}")
set(OPENSSL_VERSION_FIX "${CMAKE_MATCH_3}")
configure_file("${CMAKE_CURRENT_LIST_DIR}/vcpkg-cmake-wrapper.cmake.in" "${CURRENT_PACKAGES_DIR}/share/${PORT}/vcpkg-cmake-wrapper.cmake" @ONLY)

vcpkg_download_distfile(
    ARCHIVE
    URLS "https://www.openssl.org/source/old/3.0/openssl-${VERSION}.tar.gz"
    FILENAME "openssl-${OPENSSL_VERSION}.tar.gz"
    SHA512 1fdf769ef69631eb802671a9e0bb3a01883d58282f651986212983c2d7c9d3499dabe99a6dee0411e997e6fcfa6f682b121b7698596de0d6c78850c2200241b1
)

vcpkg_extract_source_archive(
        SOURCE_PATH
        ARCHIVE "${ARCHIVE}"
        PATCHES
            disable-apps.patch
            disable-install-docs.patch
            script-prefix.patch
            windows/install-layout.patch
            windows/install-pdbs.patch
            unix/android-cc.patch
            unix/move-openssldir.patch
            unix/no-empty-dirs.patch
            unix/no-static-libs-for-shared.patch
)

vcpkg_list(SET CONFIGURE_OPTIONS
    enable-static-engine
    enable-capieng
    no-ssl3
    no-weak-ssl-ciphers
    no-tests
)

if(VCPKG_LIBRARY_LINKAGE STREQUAL "dynamic")
    vcpkg_list(APPEND CONFIGURE_OPTIONS shared)
else()
    vcpkg_list(APPEND CONFIGURE_OPTIONS no-shared no-module)
endif()

if(NOT "tools" IN_LIST FEATURES)
    vcpkg_list(APPEND CONFIGURE_OPTIONS no-apps)
endif()

if(DEFINED OPENSSL_USE_NOPINSHARED)
    vcpkg_list(APPEND CONFIGURE_OPTIONS no-pinshared)
endif()

if(OPENSSL_NO_AUTOLOAD_CONFIG)
    vcpkg_list(APPEND CONFIGURE_OPTIONS no-autoload-config)
endif()

if(VCPKG_TARGET_IS_WINDOWS AND NOT VCPKG_TARGET_IS_MINGW)
    include("${CMAKE_CURRENT_LIST_DIR}/windows/portfile.cmake")
    include("${CMAKE_CURRENT_LIST_DIR}/install-pc-files.cmake")
else()
    include("${CMAKE_CURRENT_LIST_DIR}/unix/portfile.cmake")
endif()

file(INSTALL "${CMAKE_CURRENT_LIST_DIR}/usage" DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}")
file(INSTALL "${SOURCE_PATH}/LICENSE.txt" DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}" RENAME copyright)


挂载安装

执行vcpkg的安装命令时,需要输入挂载的路径

vcpkg install --overlay-ports=overlayports/ports/openssl openssl:x64-windows-static
  • Log
PS E:\vcpkg> vcpkg install --overlay-ports=overlayports/ports/openssl openssl:x64-windows-static
Computing installation plan...
The following packages will be built and installed:
    openssl[core]:x64-windows-static -> 3.0.0 -- E:\vcpkg\overlayports/ports/openssl
Detecting compiler hash for triplet x64-windows-static...
-- Automatically setting HTTP(S)_PROXY environment variables to "127.0.0.1:7890".
Restored 0 package(s) from C:\Users\NueXini\AppData\Local\vcpkg\archives in 114 us. Use --debug to see more details.
Installing 1/1 openssl:x64-windows-static...
Building openssl[core]:x64-windows-static...
-- Installing port from location: E:\vcpkg\overlayports/ports/openssl
-- Downloading https://www.openssl.org/source/old/3.0/openssl-3.0.0.tar.gz -> openssl-.tar.gz...
-- Extracting source E:/vcpkg/downloads/openssl-.tar.gz
-- Applying patch disable-apps.patch
-- Applying patch disable-install-docs.patch
-- Applying patch script-prefix.patch
-- Applying patch windows/install-layout.patch
-- Applying patch windows/install-pdbs.patch
-- Applying patch unix/android-cc.patch
-- Applying patch unix/move-openssldir.patch
-- Applying patch unix/no-empty-dirs.patch
-- Applying patch unix/no-static-libs-for-shared.patch
-- Using source at E:/vcpkg/buildtrees/openssl/src/openssl--79f2f1128b.clean
-- Found external ninja('1.11.0').
-- Getting CMake variables for x64-windows-static
-- Getting CMake variables for x64-windows-static
-- Prerunning x64-windows-static-dbg
-- Building x64-windows-static-dbg
-- Prerunning x64-windows-static-rel
-- Building x64-windows-static-rel
-- Fixing pkgconfig file: E:/vcpkg/packages/openssl_x64-windows-static/lib/pkgconfig/libcrypto.pc
-- Fixing pkgconfig file: E:/vcpkg/packages/openssl_x64-windows-static/lib/pkgconfig/libssl.pc
-- Fixing pkgconfig file: E:/vcpkg/packages/openssl_x64-windows-static/lib/pkgconfig/openssl.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 E:/vcpkg/downloads/tools/msys2/9a1ec3f33446b195
-- Fixing pkgconfig file: E:/vcpkg/packages/openssl_x64-windows-static/debug/lib/pkgconfig/libcrypto.pc
-- Fixing pkgconfig file: E:/vcpkg/packages/openssl_x64-windows-static/debug/lib/pkgconfig/libssl.pc
-- Fixing pkgconfig file: E:/vcpkg/packages/openssl_x64-windows-static/debug/lib/pkgconfig/openssl.pc
-- Installing: E:/vcpkg/packages/openssl_x64-windows-static/share/openssl/usage
-- Installing: E:/vcpkg/packages/openssl_x64-windows-static/share/openssl/copyright
-- Performing post-build validation
Stored binary cache: "C:\Users\NueXini\AppData\Local\vcpkg\archives\52\52725e8d8c97f33f16c3d5b775ac6942aa930813223f43e0faa0b77d8af5b265.zip"
Elapsed time to handle openssl:x64-windows-static: 2 min
Total install time: 2 min
The package openssl is compatible with built-in CMake targets:

    find_package(OpenSSL REQUIRED)
    target_link_libraries(main PRIVATE OpenSSL::SSL OpenSSL::Crypto)

这样就完成了openssl3.0.0的安装


后话

以上就是vcpkg添加自定义包安装的全部教程,如果还是看不懂的话可以参考下面文章:

  1. https://devblogs.microsoft.com/cppblog/registries-bring-your-own-libraries-to-vcpkg/

  2. https://zhuanlan.zhihu.com/p/354382196

  3. https://learn.microsoft.com/zh-cn/vcpkg/

enjoy ~

你可能感兴趣的:(vcpkg,vcpkg,c++,cmake,openssl,跨平台)