Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake's execute_process and arbitrary shell scripts

CMake's execute_process command seems to only let you, well, execute a process - not an arbitrary line you could feed a command shell. The thing is, I want to use pipes, file descriptor redirection, etc. - and that does not seem to be possible. The alternative would be very painful for me (I think)...

What should I do?

PS - CMake 2.8 and 3.x answer(s) are interesting.

like image 725
einpoklum Avatar asked Feb 28 '16 23:02

einpoklum


2 Answers

You can execute any shell script, using your shell's support for taking in a script within a string argument.

Example:

execute_process(
    COMMAND bash "-c" "echo -n hello | sed 's/hello/world/;'" 
    OUTPUT_VARIABLE FOO
)

will result in FOO containing world.

Of course, you would need to escape quotes and backslashes with care. Also remember that running bash would only work on platforms which have bash - i.e. it won't work on Windows.

like image 87
einpoklum Avatar answered Oct 31 '22 04:10

einpoklum


  1. execute_process command seems to only let you, well, execute a process - not an arbitrary line you could feed a command shell.

Yes, exactly this is written in documentation for that command:

All arguments are passed VERBATIM to the child process. No intermediate shell is used, so shell operators such as > are treated as normal arguments.

  1. I want to use pipes

Different COMMAND within same execute_process invocation are actually piped:

Runs the given sequence of one or more commands with the standard output of each process piped to the standard input of the next.

  1. file descriptor redirection, etc. - and that does not seem to be possible.

For complex things just prepare separate shell script and run it using execute_process. You can pass variables from CMake to this script using its parameters, or with prelimiary configure_file.

like image 36
Tsyvarev Avatar answered Oct 31 '22 04:10

Tsyvarev