# Execute patch from Cmake

**URL:** https://discourse.cmake.org/t/execute-patch-from-cmake/7000
**Category:** Usage
**Tags:** os:windows
**Created:** [December 1, 2022, 2:17pm UTC](https://discourse.cmake.org/t/execute-patch-from-cmake/7000 "2022-12-01T14:17:56Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Joost\_v\_Doorn](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/2acd7d/32.png) [@Joost\_v\_Doorn](https://discourse.cmake.org/u/Joost_v_Doorn)
#### Post date: [December 1, 2022, 2:17pm UTC](https://discourse.cmake.org/t/execute-patch-from-cmake/7000/1 "2022-12-01T14:17:56Z")

</div>

I run CMake under Windows to download a zipfile with source code and unzip it.  
After the unzip I want to execute patches over the directory with unzipped source code. I am taking the zipfile with source code from the Linux end of my project. Under Linux gnu patch is installed, which is used to perform the directory patch. Under Windows gnu patch is not installed and I like to keep it that way.  
I am not able to figure out how to execute the patches over the directory using CMake. Is it possible to provide some explanation with an example?

Thank you.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [December 1, 2022, 2:43pm UTC](https://discourse.cmake.org/t/execute-patch-from-cmake/7000/2 "2022-12-01T14:43:19Z")

</div>

`cmake -E patch` does not exist, but does [have a tracking issue](https://gitlab.kitware.com/cmake/cmake/-/issues/16854). In the meantime, the usual replacement is `git apply` after making sure you’re not in a git directory (`git --git-dir= apply` should do it).

---

<div class="post-metadata">

### Author: ![Joost\_v\_Doorn](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/2acd7d/32.png) [@Joost\_v\_Doorn](https://discourse.cmake.org/u/Joost_v_Doorn)
#### Post date: [December 1, 2022, 4:49pm UTC](https://discourse.cmake.org/t/execute-patch-from-cmake/7000/3 "2022-12-01T16:49:47Z")

</div>

Sorry, but I am new to this Cmake part. Only used it for simple compile and link things. Do you have an example on how to execute ‘git apply’ from a Cmake file?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [December 1, 2022, 4:57pm UTC](https://discourse.cmake.org/t/execute-patch-from-cmake/7000/4 "2022-12-01T16:57:12Z")

</div>

If you want to run it like a script, `execute_process` is what you want. If you want it to run at build time, `add_custom_command` is what to use.

---

<div class="post-metadata">

### Author: ![bertulli](https://discourse.cmake.org/user_avatar/discourse.cmake.org/bertulli/32/4630_2.png) [@bertulli](https://discourse.cmake.org/u/bertulli)
#### Post date: [August 1, 2025, 3:21pm UTC](https://discourse.cmake.org/t/execute-patch-from-cmake/7000/5 "2025-08-01T15:21:45Z")

</div>

i found a contribution that might help:  
[Example: GNU Patch from CMake - Code - CMake Discourse](https://discourse.cmake.org/t/example-gnu-patch-from-cmake/4492)
