Executing bash commands from a CMake file

I'm having trouble understanding CMake. I want to execute the following bash command during the build process:

date +"%F %T" > timestamp

Selects the current date and writes it to a file. However, I cannot reproduce this simple action using CMake commands.

Here are some things I've tried:

execute_process(COMMAND "date +'%F %T' > timestamp")

add_custom_command(OUTPUT timestamp COMMAND date +"%F %T")

file(WRITE timestamp date +"%F %T")

Nothing works. I almost wonder if they are even executed at all.

I have very limited knowledge of how CMake and its syntax, so I probably do very poorly. I hope someone can point me in the right direction. Thanks!

+5
source share
2 answers

I think my main problem was the lack of quotes around my team's arguments. Also, thanks to @Mark Setchell, I realized that I should use OUTPUT_VARIABLE instead of OUTPUT

Anyway, here is the answer I came to:

 execute_process ( COMMAND bash -c "date +'%F %T'" OUTPUT_VARIABLE outVar ) 

This is where the bash output to the outVar variable is outVar

 file(WRITE "datestamp" "${outVar}") 

And this writes the contents of outVar to a file called "datestamp".

+6
source

Note. Using bash -c will also prepare a new line to the end of the variable, which will make make complain depending on how you use it.

build.make: *** missing delimiter. Stop.

this should solve the above

 execute_process(COMMAND which grpc_cpp_plugin OUTPUT_VARIABLE GRPC_CPP_PLUGIN) string(STRIP ${GRPC_CPP_PLUGIN} GRPC_CPP_PLUGIN) message(STATUS "MY_VAR=${GRPC_CPP_PLUGIN}") 
0
source

Source: https://habr.com/ru/post/1241756/


All Articles