Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing bash commands from a CMake file

Tags:

bash

cmake

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!

like image 974
user3308321 Avatar asked Jan 28 '16 21:01

user3308321


People also ask

How to run command from CMake?

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.

What is Cmakelist?

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.

What is command CMake?

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.


2 Answers

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".

like image 64
user3308321 Avatar answered Oct 03 '22 06:10

user3308321


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}")
like image 23
Alex Punnen Avatar answered Oct 03 '22 06:10

Alex Punnen