Reputation: 951
I am having trouble understanding CMake. What I want to do is execute the following bash command during the build process:
date +"%F %T" > timestamp
This fetches the current date and writes it to a file. However, I cannot seem to reproduce this simple action using CMake commands.
Here are a few things that 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")
Neither seem to work. I almost wonder if they are even being executed at all.
I have a very limited knowledge of how CMake and its syntax, so I am probably doing things very wrong. I am hoping someone could point me in the right direction. Thanks!
Upvotes: 39
Views: 62562
Reputation: 6254
Note -Using bash -c will also prep-end a new line to end of the variable which will cause make to complain depending on how you are using it
build.make: *** missing separator. 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}")
Upvotes: 14
Reputation: 951
I think my main issue was the lack of quotes around my command arguments. Also, thanks to @Mark Setchell I realized I should be using OUTPUT_VARIABLE
in lieu of OUTPUT
At any rate, here is the answer I arrived at:
execute_process (
COMMAND bash -c "date +'%F %T'"
OUTPUT_VARIABLE outVar
)
This stores the output of the bash command into the variable outVar
file(WRITE "datestamp" "${outVar}")
And this writes the contents of outVar
to a file called "datestamp".
Upvotes: 41