# exec\_program() could be useful.

**URL:** <https://discourse.cmake.org/t/exec-program-could-be-useful/9190>\
**Category:** Code\
**Created:** [October 12, 2023, 7:21pm UTC](https://discourse.cmake.org/t/exec-program-could-be-useful/9190 "2023-10-12T19:21:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnny96](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/8e7dd6/32.png) [@johnny96](https://discourse.cmake.org/u/johnny96)\
**Post date:** [October 12, 2023, 7:21pm UTC](https://discourse.cmake.org/t/exec-program-could-be-useful/9190/1 "2023-10-12T19:21:49Z")

</div>

I am a new comer to cmake. so I am not aware of the reasons and the discussions as to why execute\_program() was deprecated. But I think execute\_program() is useful in some circumstances.

Recently I hit a problem when using execute\_processs() to run “git submodule update --init --recursive”. The error is “fatal: unable to access ‘[GitHub - libbpf/libbpf: Automated upstream mirror for libbpf stand-alone build.](https://github.com/libbpf/libbpf.git/)’: Could not resolve host: [github.com](http://github.com)”. In fact there is not networking problem on my build machine. This can be verified that I can manually do “git clone” the libbpf to my build machine. So I guess the issue is caused by the environment of the child process created by execute\_process(). I wonder whether the community can provide some idea to resolve this.

In this case, I think execute\_program() is useful to at least help to rule out if the problem is related to the child process or not.

---

<div class="post-metadata">

**Author:** ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)\
**Post date:** [October 12, 2023, 7:32pm UTC](https://discourse.cmake.org/t/exec-program-could-be-useful/9190/2 "2023-10-12T19:32:27Z")

</div>

`execute_process()` has been around for a very long time, and can do everything `exec_program()` can do and more, and better. `exec_program()` has been deprecated since CMake 3.0, and should have been deprecated by policy at the same time, but was not due to an oversight.

The recent drive to remove it came about from a bunch of internal refactoring we did to migrate all of CMake’s process spawning to libuv. `exec_program()` was not able to be easily converted, and we didn’t want to sink a large amount of time into refactoring something that’s deprecated anyway. If `exec_program()` had been deprecated by policy back in 3.0, it would have been easy to justify removing it entirely now.

Has running `exec_program()` actually helped you debug what’s wrong with your program? If you believe it’s due to an environment issue, I would suggest running `execute_process(COMMAND ${CMAKE_COMMAND} -E environment)` to see what the environment variables look like.

---

<div class="post-metadata">

**Author:** ![johnny96](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/8e7dd6/32.png) [@johnny96](https://discourse.cmake.org/u/johnny96)\
**Post date:** [October 12, 2023, 8:50pm UTC](https://discourse.cmake.org/t/exec-program-could-be-useful/9190/3 "2023-10-12T20:50:24Z")

</div>

Thanks Kyle!

I gave the suggested command a try.

execute\_process(COMMAND git submodule update --init --recursive WORKING\_DIRECTORY ${CMAKE\_CURRENT\_SOURCE\_DIR} COMMAND\_ECHO STDOUT -E environment)"

then I saw this error  
| CMake Error at CMakeLists.txt:47 (execute\_process):  
| execute\_process given unknown argument “-E”.

BTW, is there any option that can let execute\_process() run the cmd in the context of the cmake process, rather than its child process ?

---

<div class="post-metadata">

**Author:** ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)\
**Post date:** [October 12, 2023, 8:53pm UTC](https://discourse.cmake.org/t/exec-program-could-be-useful/9190/4 "2023-10-12T20:53:54Z")

</div>

You’ve combined the two commands and omitted the `${CMAKE_COMMAND}` argument. Please add my suggested line, exactly as I have typed it, as an additional line before your original `execute_process()` call:

```cmake
# New command:
execute_process(COMMAND ${CMAKE_COMMAND} -E environment)
# Original command:
execute_process(COMMAND git submodule update --init --recursive WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMMAND_ECHO STDOUT)

```

> [@johnny96](#):
>
> BTW, is there any option that can let execute\_process() run the cmd in the context of the cmake process, rather than its child process ?

Any command that you run is inherently going to run in the context of a child process. It’s not possible to run `git`, or any other command, in the same address space and process as CMake.

---

<div class="post-metadata">

**Author:** ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)\
**Post date:** [October 12, 2023, 8:56pm UTC](https://discourse.cmake.org/t/exec-program-could-be-useful/9190/5 "2023-10-12T20:56:57Z")

</div>

Also, FWIW, I would not suggest having CMake initialize/update the Git submodules. Setting up the source tree should be separate from running CMake. Your users/developers may want to get the submodule from their own source rather than the ones you’ve provided.

---

<div class="post-metadata">

**Author:** ![johnny96](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/8e7dd6/32.png) [@johnny96](https://discourse.cmake.org/u/johnny96)\
**Post date:** [October 12, 2023, 10:09pm UTC](https://discourse.cmake.org/t/exec-program-could-be-useful/9190/6 "2023-10-12T22:09:01Z")

</div>

Yes, this works and thanks!

In order to test if the network was reachable or not, I added this line to CMakeLists.txt execute\_process(COMMAND /bin/ping 192.30.255.113 OUTPUT\_FILE ${CMAKE\_CURRENT\_SOURCE\_DIR}/ping.output)",

then I got this error “ping: socket: Operation not permitted”. However I don’t have this permission problem if I run the ping command in the same window where I ran cmake.

So I guess in the cmake context, the user ID and group ID might be changed. Not sure how both were changed.

---

<div class="post-metadata">

**Author:** ![johnny96](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/8e7dd6/32.png) [@johnny96](https://discourse.cmake.org/u/johnny96)\
**Post date:** [October 13, 2023, 12:36am UTC](https://discourse.cmake.org/t/exec-program-could-be-useful/9190/7 "2023-10-13T00:36:48Z")

</div>

That’s a good suggestion. Thanks Kyle!

---

<div class="post-metadata">

**Author:** ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)\
**Post date:** [October 13, 2023, 1:12pm UTC](https://discourse.cmake.org/t/exec-program-could-be-useful/9190/8 "2023-10-13T13:12:22Z")

</div>

> [@johnny96](#):
>
> then I got this error “ping: socket: Operation not permitted”. However I don’t have this permission problem if I run the ping command in the same window where I ran cmake.

That is very strange indeed. I have no explanation. My suggestion would be to try running `whoami`, both in the terminal and from CMake, to find clues about what your UID and GID are.
