教程:安装本地修改的依赖项

本教程将指导你使用自定义覆盖修改 vcpkg 端口。 建议先阅读打包库教程,然后再继续操作。

在本教程中,学习:

先决条件

1 - 创建覆盖端口

第一步是创建要修改的包的覆盖端口

创建用于保存覆盖端口的目录

可以在所选的任何文件系统位置创建覆盖端口目录。 在本教程的任何步骤中,将 $OVERLAY_LOCATION 替换为所选位置。

mkdir "$OVERLAY_LOCATION"
mkdir "$OVERLAY_LOCATION"

将端口的内容复制到覆盖端口目录中

在本教程中,你将修改打包教程中的 vcpkg-sample-library 端口以添加动态库支持。

Copy-Item -Path <path/to/vcpkg-sample-library> -Destination "$OVERLAY_LOCATION" -Recurse
xcopy <path/to/vcpkg-sample-library> "$OVERLAY_LOCATION" /E
cp -R <path/to/vcpkg-sample-library> "$OVERLAY_LOCATION"

2 - 获取端口的源代码

要获取要修改的端口的源代码,请运行以下命令:

vcpkg install "--overlay-ports=$OVERLAY_LOCATION" vcpkg-sample-library --editable

输出应包含类似如下的行:

-- Using source at path/to/vcpkg/buildtrees/vcpkg-sample-library/src/1.0.0-b54c55c215

这是端口源代码的位置。

3 - 创建临时的 Git 注册表

在源代码位置初始化临时 Git 存储库。 这是为了使用 Git 生成可在脚本中 portfile.cmake 应用的修补程序文件。 将 $SOURCE_PATH 替换为上一步中获取的位置。

cd "$SOURCE_PATH"
git init
git add .
git commit -m "Initial commit"

4 - 修改必要的文件

修改以下源代码文件以添加动态库支持到 vcpkg-sample-library

my_sample_lib.h

#pragma once

#include <string>

#if MYLIB_EXPORTS
__declspec(dllexport)
#endif
std::string greet(const std::string& name);

CMakeLists.txt

add_library() 调用中删除 STATIC

add_library(my_sample_lib my_sample_lib.cpp)

在构建为共享库时添加编译定义。

if (BUILD_SHARED_LIBS)
    target_compile_definitions(my_sample_lib PRIVATE MYLIB_EXPORTS)
endif()

5 - 生成修补程序文件

在源代码目录中,运行以下命令以生成修补程序文件。

git diff --output "$OVERLAY_LOCATION/vcpkg-sample-library/add-dynamic-lib-support.patch"

这会在覆盖端口中创建一个名为 add-dynamic-lib-support.patch 的文件,其内容类似于:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2b42f71..378aac7 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,26 +2,33 @@ cmake_minimum_required(VERSION 3.10)
 
 project(my_sample_lib)
 
+include(GNUInstallDirs)
+
 # Find the fmt library
 find_package(fmt CONFIG REQUIRED)
 
 # Add your library
-add_library(my_sample_lib STATIC my_sample_lib.cpp)
+add_library(my_sample_lib my_sample_lib.cpp)
+
+if (BUILD_SHARED_LIBS AND MSVC)
+    target_compile_definitions(my_sample_lib PRIVATE MYLIB_EXPORTS)
+endif()
 
 # Link your library to fmt
 target_link_libraries(my_sample_lib PRIVATE fmt::fmt)
 
+
 # Add include directories
 target_include_directories(my_sample_lib PUBLIC 
     $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>   # for headers when building
-    $<INSTALL_INTERFACE:include>  # for client in install mode
+    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>  # for client in install mode
 )
 # Install the library and its headers
 install(TARGETS my_sample_lib
         EXPORT my_sample_lib_targets
-        ARCHIVE DESTINATION lib
-        LIBRARY DESTINATION lib
-        RUNTIME DESTINATION bin)
+        ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
+        LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
+        RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
 
 install(FILES my_sample_lib.h DESTINATION include)
 
diff --git a/my_sample_lib.h b/my_sample_lib.h
index d6d70b8..0b62141 100644
--- a/my_sample_lib.h
+++ b/my_sample_lib.h
@@ -2,4 +2,7 @@
 
 #include <string>
 
+#if MYLIB_EXPORTS
+__declspec(dllexport)
+#endif
 std::string greet(const std::string& name);

6 - 修改 portfile.cmake 以应用修补程序

更改 portfile.cmake 以删除 ONLY_STATIC_LIBRARY 限制并将修补程序应用到源代码。

portfile.cmake

vcpkg_from_github(
    OUT_SOURCE_PATH SOURCE_PATH
    REPO MicrosoftDocs/vcpkg-docs
    REF 1.0.0 # This is for demonstration purposes as '1.0.2' of the sample library already has dynamic library support
    SHA512 3f206cc2fe61d9c97c82b30852e1e4e6df299d93f6159edd1e56c644fa03ccc4670f7681e356d0e3db898a74e099a1ec531821df5430a7b14d61c743c5aa8c30
    HEAD_REF cmake-sample-lib
    PATCHES
        "add-dynamic-lib-support.patch"
)

vcpkg_cmake_configure(
    SOURCE_PATH "${SOURCE_PATH}"
)

vcpkg_cmake_install()

vcpkg_cmake_config_fixup(PACKAGE_NAME "my_sample_lib")

file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include")

file(INSTALL "${CMAKE_CURRENT_LIST_DIR}/usage" DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}")
vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE")

7 - 安装覆盖端口

运行以下命令:

vcpkg install "--overlay-ports=$OVERLAY_LOCATION" vcpkg-sample-library

请确保删除 --editable 标志,以便修补程序文件可应用于源代码的原始副本。 你应会在输出中看到关于将修补程序文件应用到源代码的行:

-- Cleaning sources at D:/Work/vcpkg/buildtrees/vcpkg-sample-library/src/1.0.0-8f646312ed.clean. Use --editable to skip cleaning for the packages you specify.
-- Extracting source D:/Work/vcpkg/downloads/Microsoft-vcpkg-docs-1.0.0.tar.gz
-- Applying patch add-dynamic-lib-support.patch

后续步骤

大功告成! 你已使用修补程序文件安装了经本地修改的依赖项。

以下是接下来要尝试的一些任务: