marital_weeping
marital_weeping

Reputation: 866

Modifying or injecting headers after fetching a git repo using FetchContent

Let's say one's importing a cmake project from a github repository via the FetchContent route. In order for the repo, in this case hiberlite, to play nice with my project, I need to inject/patch/replace one of its headers, say hiberdefs.h located in ${hiberlite_SOURCE_DIR}/include with my own.

How does one do that such that my entire project only sees the modified header? Also, any libraries built by the repo must use the modified header.

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)

Upvotes: 6

Views: 2806

Answers (1)

marital_weeping
marital_weeping

Reputation: 866

Thanks to @Tsyvarev 's comment above here's what one needs to do.

  1. Obtain the patch file
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. Modify your CMakeLists.txt by adding PATCH_COMMAND to your FetchContent_Declare.
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)

Upvotes: 10

Related Questions