Patching files with GNU Patch has been a common task for decades. Due to technical issues with potential patch library implementation, CMake does not include a native patch function. We use CMake to detect if “patch” is available, which it virtually always is on Unix-like systems. For Windows, we use MSYS2 or WSL.
CMake Patch file repository example uses standard GNU Patch files across operating systems. We put the patched file into the binary directory to avoid ambiguity. The file patching occurs only if the source file changes using canonical CMake approach.
With slight modification, this patching can occur on files obtained via CMake FetchContent, header files, or other resources.