# Redirecting input in ExternalProject\_Add PATCH\_COMMAND

**URL:** https://discourse.cmake.org/t/redirecting-input-in-externalproject-add-patch-command/2779
**Category:** Code
**Created:** [February 16, 2021, 10:55pm UTC](https://discourse.cmake.org/t/redirecting-input-in-externalproject-add-patch-command/2779 "2021-02-16T22:55:44Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![mwilbur](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mwilbur/32/1212_2.png) [@mwilbur](https://discourse.cmake.org/u/mwilbur)
#### Post date: [February 16, 2021, 10:55pm UTC](https://discourse.cmake.org/t/redirecting-input-in-externalproject-add-patch-command/2779/1 "2021-02-16T22:55:44Z")

</div>

In the latest code on master, if the PATCH\_COMMAND includes a redirection

```auto
ExternalProject_Add( PATCH_COMMAND patch -p1 < some_file )

```

the `<` gets interpreted as an input to the command. Poking around, it looks like the underlying `execute_process` command should be written to use `INPUT_FILE` but it doesn’t _look_ like there’s any machinery to do this. Is there an alternative way to do something like that? If not, is this a desired addtition to the `ExternalProject_Add` function. I’m not strong in CMake scripting but wouldn’t mind taking a crack at it.

---

<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: [February 16, 2021, 11:04pm UTC](https://discourse.cmake.org/t/redirecting-input-in-externalproject-add-patch-command/2779/2 "2021-02-16T23:04:01Z")

</div>

Cc: @craig.scott

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [February 16, 2021, 11:39pm UTC](https://discourse.cmake.org/t/redirecting-input-in-externalproject-add-patch-command/2779/3 "2021-02-16T23:39:26Z")

</div>

My recollection is that there is no guarantee that any particular shell is used to run the command, or even that a shell is used at all. Redirection may work in some cases but is not officially supported. If you need to do things like that, put it in a script and run that script as the command instead.

---

<div class="post-metadata">

### Author: ![mwilbur](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mwilbur/32/1212_2.png) [@mwilbur](https://discourse.cmake.org/u/mwilbur)
#### Post date: [February 16, 2021, 11:55pm UTC](https://discourse.cmake.org/t/redirecting-input-in-externalproject-add-patch-command/2779/4 "2021-02-16T23:55:14Z")

</div>

Fair enough. It seemed to me like the `INPUT_FILE` argument to `execute_process` was meant to handle just this sort of thing.

---

<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: [February 17, 2021, 12:13am UTC](https://discourse.cmake.org/t/redirecting-input-in-externalproject-add-patch-command/2779/5 "2021-02-17T00:13:36Z")

</div>

While true, it would mean CMake would need to parse out shell syntax like `>foo`, `2>foo`, and `>&foo` goings on. If you want shell features, ensure you’re using the shell you want explicitly.

---

<div class="post-metadata">

### Author: ![mwilbur](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mwilbur/32/1212_2.png) [@mwilbur](https://discourse.cmake.org/u/mwilbur)
#### Post date: [February 17, 2021, 12:23am UTC](https://discourse.cmake.org/t/redirecting-input-in-externalproject-add-patch-command/2779/6 "2021-02-17T00:23:56Z")

</div>

I wonder if perhaps I have not been clear. It’s not that I’d expect CMake to understand `<` and the like.  
Rather it’s that I’d like a way to pass down, via the `ExternalProject_Add( PATCH_COMMAND )`, something to feed to the `INPUT_FILE` argument of the `execute_process`, i.e. the final implementation would be something like

```auto
execute_process( COMMAND patch -p1 INPUT_FILE somefile)

```

as a result of

```auto
ExternalProject_Add( PATCH_COMMAND patch -p1 PATCH_INPUT somefile)

```

in order to get the effect of `patch -p1 < somefile`.

As far as I can tell, an extra argument(s) to the `ExternalProject_Add` would be needed, though.

Also, I’m only carrying on the argument so I can understand better. I have no trouble using a script instead.

---

<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: [February 17, 2021, 2:43pm UTC](https://discourse.cmake.org/t/redirecting-input-in-externalproject-add-patch-command/2779/7 "2021-02-17T14:43:48Z")

</div>

I think given the normal behavior of most `patch` commands, a `PATCH_INPUT_FILE` argument might make sense. @craig.scott? I know you’ve been refactoring in there lately, so I’m not sure how easy that’d be off-hand.

---

<div class="post-metadata">

### Author: ![mwilbur](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mwilbur/32/1212_2.png) [@mwilbur](https://discourse.cmake.org/u/mwilbur)
#### Post date: [February 17, 2021, 2:55pm UTC](https://discourse.cmake.org/t/redirecting-input-in-externalproject-add-patch-command/2779/8 "2021-02-17T14:55:23Z")

</div>

I’d love to be able to make a contribution if this is work you’d like done. I’d need some guidance, but I can work fairly independently. @craig.scott’s book has been a godsend for me.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [February 17, 2021, 4:40pm UTC](https://discourse.cmake.org/t/redirecting-input-in-externalproject-add-patch-command/2779/9 "2021-02-17T16:40:33Z")

</div>

Part of the complexity is that there can be more than just one command. After the `PATCH_COMMAND`, there could be further `COMMAND` blocks. Same for all the other custom command keywords like `DOWNLOAD_COMMAND`, `UPDATE_COMMAND` and so on. That means you would have to match up any `INPUT_FILE` with the command it was intended for. It would be even more complicated to have a keyword specific to just the `PATCH_COMMAND` due to the way the argument parsing is implemented, so this would most likely need to work for all custom command types, not just `PATCH_COMMAND`.

For the specific case in the original question, you don’t even have to use redirection. The `patch` command supports an `-i` or `--input` option for specifying the patch file (at least on macOS it does), so that allows you to specify an input file directly as part of the `patch` command.

While I’m not saying that being able to provide input redirection for commands wouldn’t be a useful enhancement, I’m wary of the complexity of implementing it and there is a well-established workaround of using a script as the command instead. The ExternalProject module is also one of the most complex of all the modules CMake provides. The way it parses arguments is… unusual, primarily because of the need to support keywords that can be repeated (the `COMMAND` keyword being the main culprit). It stores all keywords on targets, but only in some scenarios (FetchContent now uses it in a way that there are no targets to store them on). If you want to explore contributing this feature, perhaps take a look at the `Modules/ExternalProject.cmake` file as a first step. I can provide limited guidance, but my workload is already pretty high so you’d need to be fairly independent.

---

<div class="post-metadata">

### Author: ![mwilbur](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mwilbur/32/1212_2.png) [@mwilbur](https://discourse.cmake.org/u/mwilbur)
#### Post date: [February 17, 2021, 6:46pm UTC](https://discourse.cmake.org/t/redirecting-input-in-externalproject-add-patch-command/2779/10 "2021-02-17T18:46:17Z")

</div>

Well, I feel like an idiot now. I’ve used patch for years redirecting input from stdin. I even looked at the man page quickly, but missed that. Well, I certainly learned some more cmake, at least. Thanks for taking the time to respond @ben.boeckel and @craig.scott.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [March 6, 2021, 10:02pm UTC](https://discourse.cmake.org/t/redirecting-input-in-externalproject-add-patch-command/2779/11 "2021-03-06T22:02:08Z")

</div>

It has become apparent that this is a regression that needs to be fixed. Shell redirections previously worked for ExternalProject, even though it was not documented to be supported. The commands were previously embedded directly in the project files and therefore were being interpreted by the underlying shell that was used. With the 3.20.0 release candidates, the commands have been getting wrapped in a CMake script that used `execute_process()` to invoke them, but `execute_process()` does not support shell redirection. You can track the relevant issue here:

[https://gitlab.kitware.com/cmake/cmake/-/issues/21892](https://gitlab.kitware.com/cmake/cmake/-/issues/21892)
