Created
June 18, 2020 13:44
-
-
Save ianhinder/02c0a4e4f28e05729f6dbaf92bce4e08 to your computer and use it in GitHub Desktop.
How to store and output git version information in a CMake code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=============================================================================================== | |
CMakeLists.txt: | |
add_custom_target(git_version.h | |
COMMAND | |
${CMAKE_CURRENT_SOURCE_DIR}/git_version.sh ${CMAKE_CURRENT_BINARY_DIR}/git_version.h) | |
add_dependencies(<exe> git_version.h) | |
=============================================================================================== | |
git-version.sh: | |
#!/bin/bash | |
outfile=$1 | |
tmpfile=${outfile}.tmp | |
version=$(git describe --dirty) | |
cat >$tmpfile <<EOF | |
#pragma once | |
#define GIT_VERSION "$version" | |
EOF | |
if [ -r $outfile ]; then | |
if diff -q $outfile $tmpfile >/dev/null; then | |
# No change to version | |
exit 0 | |
fi | |
fi | |
mv $tmpfile $outfile | |
=============================================================================================== | |
main.cc: | |
#include "git_version.h" | |
printf("<code name> version: %s\n", GIT_VERSION); | |
=============================================================================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment