villaa villaa - 1 year ago 230
Git Question

multiple commands in cmake execute_process

I have a project that I am using cmake to help build. The suppose the project is in

and the build directory is in

directory is a git repository with some tags, and I would like to incorporate the most recent tag into the code when it gets built so that the binary knows what version it is.

In my
file I have the following stanza in the setup portion:

execute_process(COMMAND cd ${CMAKE_CURRENT_SOURCE_DIR} && exec git describe --abbrev=0 --always OUTPUT_VARIABLE GIT_VERSION)

The problem is that the compound command in the
apparently returns an empty string for the version. I have to somehow run the
git describe
command from inside the repository directory, but it is distinct from the build directory where the code is getting made.

How can I get the make process to execute the appropriate git command in the appropriate directory, even when envoked from the build directory? Also, how can I get
to get the output from a compound command as I would normally expect in the shell?

Things I've tried

I've tried to surround the compound command with parentheses as suggested here in order to run the command in a sub-shell, but the output does not seem to be returned to the parent shell.

This link suggests that
can be run with several
invocations, but it seems the
from the first is piped to
of the next, so that won't produce the desired result.

I also tried explicitly envoking the shell using
sh -c
inside the
structure. I'm not sure I fully understood the results of this attempt but I could not get the
I would need in this situation to escape properly inside the

Answer Source

Command execute_process has WORKING_DIRECTORY option, which intention is to set directory where COMMAND will be invoked:

execute_process(COMMAND  git describe --abbrev=0 --always 

As for && operand, it doesn't work with execute_process, because shell isn't used for run the command. This is explicitely stated in documentation.

Normally, for achive sequential order of the commands, you need to call execute_process twice (or more). But such usage doesn't work with changing directory, that is why special option is introduced.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download