使用FetchContent获取git repo后修改或注入头



假设通过FetchContent路由从github存储库导入cmake项目。为了进行回购,在本例中为hiberlite,为了更好地处理我的项目,我需要注入/修补/替换它的一个标头,比如位于${hiberlite_SOURCE_DIR}/include中的hiberdefs.h

如何做到这一点,使我的整个项目只看到修改后的标题?此外,由repo构建的任何库都必须使用修改后的头。

CMakeLists.txt

cmake_minimum_required(VERSION 3.24)
project(hiberlite_patch)
set(CMAKE_CXX_STANDARD 20)
include(FetchContent)
FetchContent_Declare(hiberlite
GIT_REPOSITORY https://github.com/paulftw/hiberlite
GIT_TAG master
)
FetchContent_MakeAvailable(hiberlite)
include_directories(${hiberlite_SOURCE_DIR}/include)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} hiberlite)

感谢@Tsyvarev在上面的评论,以下是需要做的。

  1. 获取patch文件
git clone https://github.com/paulftw/hiberlite
cd hiberlite
# edit the repo as you see fit
git diff > hiberlite.patch
mv hiberlite.patch /path_to_your_project/patches/
  1. 通过将PATCH_COMMAND添加到FetchContent_Declare来修改CMakeLists.txt
cmake_minimum_required(VERSION 3.24)
project(hiberlite_patch)
set(CMAKE_CXX_STANDARD 20)
include(FetchContent)
set(hiberlite_patch git apply ${CMAKE_CURRENT_SOURCE_DIR}/patches/hiberlite.patch)
FetchContent_Declare(hiberlite
GIT_REPOSITORY https://github.com/paulftw/hiberlite
GIT_TAG master
PATCH_COMMAND ${hiberlite_patch}
UPDATE_DISCONNECTED 1
)
FetchContent_MakeAvailable(hiberlite)
include_directories(${hiberlite_SOURCE_DIR}/include)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} hiberlite)

相关内容

  • 没有找到相关文章

最新更新