How to redirect the output of a custom CMake command to a file?

I need to run a command with add_custom_command to capture its stdout. Shell redirection to file > does not work, and add_custom_command has no related functions. How can i do this?

+7
cmake
source share
1 answer

(Note that I changed my answer after studying the problem due to Florian's comment. More about editing below)

The execute_process documentation clearly states that it does not use an intermediate shell, so redirection operators do not work there.

But this is not the case for add_custom_command . The redirection should work there as expected. [EDIT] The reason this sometimes doesn’t work is because of some unsuccessful combination of the CMake generator, the VERBATIM and (space) a space between > and the file name.

It turned out that if you make sure that between > and the name of the file with which it works in most cases, there is a space even with VERBATIM :

 add_custom_command(OUTPUT ${some-file} COMMAND cmake --version > ${some-file} VERBATIM # optional ) 

A note on an alternative solution: I used to think that add_custom_command , just like execute_process does not use an intermediate shell, so I suggested calling a CMake script that contains the execute_process command, which launches a valid command, redirecting its output using the OUTPUT_FILE option.

If for some reason the solution above is still not suitable for you, try an alternative solution using ExecuteProcessWrapper.cmake

+5
source share

All Articles