Skip to content

Instantly share code, notes, and snippets.

@ianhinder
Created June 18, 2020 13:44
Show Gist options
  • Save ianhinder/02c0a4e4f28e05729f6dbaf92bce4e08 to your computer and use it in GitHub Desktop.
Save ianhinder/02c0a4e4f28e05729f6dbaf92bce4e08 to your computer and use it in GitHub Desktop.
How to store and output git version information in a CMake code
===============================================================================================
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