multiple commands in cmake execute_process
<p>I have a project that I am using cmake to help build.  The suppose the project is in <code>/home/proj</code> and the build directory is in <code>/home/proj-build</code></p>

<p>The <code>/home/proj</code> 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. </p>

<p>In my <code>CMakeLists.txt</code> file I have the following stanza in the setup portion:</p>

<pre><code>set(CMAKE_BUILD_TYPE None)
message(${CMAKE_CURRENT_SOURCE_DIR})
execute_process(COMMAND cd ${CMAKE_CURRENT_SOURCE_DIR} &amp;&amp; exec git describe --abbrev=0 --always OUTPUT_VARIABLE GIT_VERSION)
message("${GIT_VERSION}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__GIT_VERSION=\\\"${GIT_VERSION}\\\"")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__GIT_VERSION=\\\"${GIT_VERSION}\\\"")
</code></pre>

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

<p>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 <code>cmake</code> to get the output from a compound command as I would normally expect in the shell?</p>

<p><strong>Things I've tried</strong></p>

<p>I've tried to surround the compound command with parentheses as suggested <a href="https://stackoverflow.com/questions/786376/how-do-i-run-a-program-with-a-different-working-directory-from-current-from-lin%22here%22">here</a> in order to run the command in a sub-shell, but the output does not seem to be returned to the parent shell. </p>

<p><a href="https://cmake.org/pipermail/cmake/2009-June/030158.html" rel="nofollow noreferrer">This link</a> suggests that <code>execute_process</code> can be run with several <code>COMMAND</code> invocations, but it seems the <code>stdout</code> from the first is piped to <code>stdin</code> of the next, so that won't produce the desired result.</p>

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