# execute\_process and add\_custom\_target behave differently with respect to COMMAND

**URL:** https://discourse.cmake.org/t/execute-process-and-add-custom-target-behave-differently-with-respect-to-command/6196
**Category:** Usage
**Tags:** os:windows, gen:makefiles
**Created:** [August 1, 2022, 12:05pm UTC](https://discourse.cmake.org/t/execute-process-and-add-custom-target-behave-differently-with-respect-to-command/6196 "2022-08-01T12:05:12Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Maitrey](https://discourse.cmake.org/user_avatar/discourse.cmake.org/maitrey/32/2643_2.png) [@Maitrey](https://discourse.cmake.org/u/Maitrey)
#### Post date: [August 1, 2022, 12:05pm UTC](https://discourse.cmake.org/t/execute-process-and-add-custom-target-behave-differently-with-respect-to-command/6196/1 "2022-08-01T12:05:12Z")

</div>

Dear CMake Folks, I have a strange issue, I need to run matlab script and use the generated code for compilation.  
I would like to run matlab2018b(64 bit) in Windows without display and I am using MinGW as makefile generator.  
For me this works without the display but is not intended (I need generation before build)

```auto
add_custom_target(generate_sources ALL COMMAND ${Matlab_MAIN_PROGRAM} -nosplash -nodesktop -noFigureWindows -minimize -wait -log -r "matlab_script"

```

While this works with matlab display:

```auto
execute_process(
COMMAND "${Matlab_MAIN_PROGRAM}" -nosplash -nodesktop -noFigureWindows -minimize -wait -log -r "matlab_script"
ERROR_VARIABLE STDERR
RESULT_VARIABLE RES
TIMEOUT 600
INPUT_FILE NUL
COMMAND_ECHO STDOUT)
message("OUTPUT variable is: ${RES}")
if(NOT (RES EQUAL 0) OR STDERR MATCHES "\?\?\? Error")
message( FATAL_ERROR "execute_process returned ${RES}" )
endif()

```

I would like to use execute\_process as it is meant during config time.  
I suspect there is something in the way that I specify COMMAND in both cases.  
Could you please help?  
Thanks!

---

<div class="post-metadata">

### Author: ![bill.hoffman](https://discourse.cmake.org/user_avatar/discourse.cmake.org/bill.hoffman/32/13_2.png) [@bill.hoffman](https://discourse.cmake.org/u/bill.hoffman)
#### Post date: [August 2, 2022, 3:47pm UTC](https://discourse.cmake.org/t/execute-process-and-add-custom-target-behave-differently-with-respect-to-command/6196/2 "2022-08-02T15:47:49Z")

</div>

I am not exactly sure what the question is here. What are you trying to do at configure time and what are you trying to do at build time? What is not working?

---

<div class="post-metadata">

### Author: ![Maitrey](https://discourse.cmake.org/user_avatar/discourse.cmake.org/maitrey/32/2643_2.png) [@Maitrey](https://discourse.cmake.org/u/Maitrey)
#### Post date: [August 2, 2022, 7:28pm UTC](https://discourse.cmake.org/t/execute-process-and-add-custom-target-behave-differently-with-respect-to-command/6196/3 "2022-08-02T19:28:01Z")

</div>

HI Bill, Thanks for replying. Sorry if my earlier explanation was not clear enough. I would like to run a .m script at PRE BUILD step which I am using for generating code. I face the following issues:

- If I use execute\_process it is running twice , probably once at configure and once at build step. execute\_process is also not making MATLAB work in headless mode. It pops up the MATLAB window which prevents it from running in an automated CI environment like jenkins.
- If I use add\_custom\_target, it is triggered after BUILD step. It doesnot wait for MATLAB to finish.

---

<div class="post-metadata">

### Author: ![bill.hoffman](https://discourse.cmake.org/user_avatar/discourse.cmake.org/bill.hoffman/32/13_2.png) [@bill.hoffman](https://discourse.cmake.org/u/bill.hoffman)
#### Post date: [August 2, 2022, 8:30pm UTC](https://discourse.cmake.org/t/execute-process-and-add-custom-target-behave-differently-with-respect-to-command/6196/4 "2022-08-02T20:30:56Z")

</div>

What you want is a chain of custom commands/targets. This might help [https://cmake.org/cmake/help/latest/guide/tutorial/Adding%20a%20Custom%20Command%20and%20Generated%20File.html](https://cmake.org/cmake/help/latest/guide/tutorial/Adding%20a%20Custom%20Command%20and%20Generated%20File.html). Basically you want the outputs of your .m script to be inputs to some other target and make sure the chain of depends is setup so things are run in the right order. I don’t think you want to do any of this with execute\_process as that will only happen at configure time.

---

<div class="post-metadata">

### Author: ![Maitrey](https://discourse.cmake.org/user_avatar/discourse.cmake.org/maitrey/32/2643_2.png) [@Maitrey](https://discourse.cmake.org/u/Maitrey)
#### Post date: [August 3, 2022, 4:06am UTC](https://discourse.cmake.org/t/execute-process-and-add-custom-target-behave-differently-with-respect-to-command/6196/5 "2022-08-03T04:06:36Z")

</div>

Hi Bill, Thanks for your suggestion. Now it did work just the way I wanted. However why it did not work with execute\_process. In many places for example FindMatlab.cmake uses execute\_process as well as [cmake/MatlabTestsRedirect.cmake at master · RobotLocomotion/cmake · GitHub](https://github.com/RobotLocomotion/cmake/blob/master/MatlabTestsRedirect.cmake#L89) . Could you please explain why it works here and not in my use-case? Thanks!

---

<div class="post-metadata">

### Author: ![bill.hoffman](https://discourse.cmake.org/user_avatar/discourse.cmake.org/bill.hoffman/32/13_2.png) [@bill.hoffman](https://discourse.cmake.org/u/bill.hoffman)
#### Post date: [August 4, 2022, 9:44pm UTC](https://discourse.cmake.org/t/execute-process-and-add-custom-target-behave-differently-with-respect-to-command/6196/6 "2022-08-04T21:44:22Z")

</div>

In the link you sent that is for running Matlab tests at ctest time with a cmake -E script command. So it is not being done as part of the build at all.
