Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Patch files with CMake
cmake_minimum_required(VERSION 3.19)
project(demo_patch LANGUAGES C)
include(mylib_patch.cmake)
if(mylib_patched)
return()
endif()
set(orig_file ${PROJECT_SOURCE_DIR}/my.c)
set(patch_file ${PROJECT_SOURCE_DIR}/my.patch)
if(WIN32)
find_program(WSL NAMES wsl REQUIRED)
execute_process(COMMAND ${WSL} wslpath ${orig_file}
TIMEOUT 5
OUTPUT_VARIABLE orig_file_path
COMMAND_ERROR_IS_FATAL ANY
OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(COMMAND ${WSL} wslpath ${patch_file}
TIMEOUT 5
OUTPUT_VARIABLE patch_file_path
COMMAND_ERROR_IS_FATAL ANY
OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(COMMAND ${WSL} patch ${orig_file_path} ${patch_file_path}
TIMEOUT 10
COMMAND_ERROR_IS_FATAL ANY
)
else()
find_program(PATCH NAMES patch REQUIRED)
execute_process(COMMAND ${PATCH} ${orig_file} ${patch_file}
TIMEOUT 10
COMMAND_ERROR_IS_FATAL ANY
)
endif()
set(mylib_patched true CACHE BOOL "source is patched")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment