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!
From the command line, cmake can be run as an interactive question and answer session or as a non-interactive program. To run in interactive mode, just pass the option “-i” to cmake. This will cause cmake to ask you to enter a value for each value in the cache file for the project.
CMake is a meta build system that uses scripts called CMakeLists to generate build files for a specific environment (for example, makefiles on Unix machines). When you create a new CMake project in CLion, a CMakeLists. txt file is automatically generated under the project root.
The cmake command creates many files at your current working directory (CWD, the directory you ran the command from), and among them is a file called Makefile , which has rules about which files to compile/build/copy/write/whatever and how to do it.
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".
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}")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With