
# CMakefile.txt: cmake/version.cmake
# Date: 2021
# Author: (AS) Alexander Sacharov <a.sacharov@gmx.de>
#         Based on an article by Matt Keeter
#
# Notes:

# At compile-time to embed the strings into code that reflects the current state of the Git repository:
#
#  - The short commit hash, with a trailing + if there are uncommitted changes.
#  - The tag (if any)
#  - The current branch

# This gets git info, inserts version and revision strings into a preformatted C code, then writes it to a local file if it has changed.
# The resulting file looks like this:
#
#        const char* GIT_REV="c634567+";
#        const char* GIT_TAG="v3.4.6";
#        const char* GIT_BRANCH="main";
#
# ########################################################################
# How to use
# ########################################################################
# 1) to add the following lines to main CMakeLists.txt:
#
#        # Add a custom command that produces version.cpp, plus
#        # a dummy output that's not actually produced, in order
#        # to force version.cmake to always be re-run before the build
#        ADD_CUSTOM_COMMAND(
#            OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/version.cpp
#                   ${CMAKE_CURRENT_BINARY_DIR}/_version.cpp
#            COMMAND ${CMAKE_COMMAND} -P
#                    ${CMAKE_CURRENT_SOURCE_DIR}/cmake/version.cmake)
#
# 2) add the generated version.cpp file
#
#    ${CMAKE_CURRENT_BINARY_DIR}/version.cpp
# to the list of files in my build.
#
# 3) To add a few small accessor functions in your code
#    // These variables are autogenerated and compiled
#    // into the library by the version.cmake script

#    extern const char* GIT_TAG;
#    extern const char* GIT_REV;
#    extern const char* GIT_BRANCH;
#
#    const char* myproject_git_version(void)
#    {
#       return GIT_TAG;
#    }
#
#    const char* myproject_git_revision(void)
#    {
#       return GIT_REV;
#    }
#
#    const char* myproject_git_branch(void)
#    {
#       return GIT_BRANCH;
#    }
#
# Enjoy!

execute_process(COMMAND git log --pretty=format:'%h' -n 1
        OUTPUT_VARIABLE GIT_REV
        ERROR_QUIET)

# Check whether we got any revision (which isn't
# always the case, e.g. when someone downloaded a zip
# file from Github instead of a checkout)
if ("${GIT_REV}" STREQUAL "")
    set(GIT_REV "N/A")
    set(GIT_DIFF "")
    set(GIT_TAG "N/A")
    set(GIT_BRANCH "N/A")
else()
    execute_process(
            COMMAND bash -c "git diff --quiet --exit-code || echo +"
            OUTPUT_VARIABLE GIT_DIFF)
    execute_process(
            COMMAND git describe --exact-match --tags
            OUTPUT_VARIABLE GIT_TAG ERROR_QUIET)
    execute_process(
            COMMAND git rev-parse --abbrev-ref HEAD
            OUTPUT_VARIABLE GIT_BRANCH)

    string(STRIP "${GIT_REV}" GIT_REV)
    string(SUBSTRING "${GIT_REV}" 1 7 GIT_REV)
    string(STRIP "${GIT_DIFF}" GIT_DIFF)
    string(STRIP "${GIT_TAG}" GIT_TAG)
    string(STRIP "${GIT_BRANCH}" GIT_BRANCH)
endif()

set(VERSION "const char* GIT_REV=\"${GIT_REV}${GIT_DIFF}\";
const char* GIT_TAG=\"${GIT_TAG}\";
const char* GIT_BRANCH=\"${GIT_BRANCH}\";")

if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/version.cpp)
    file(READ ${CMAKE_CURRENT_SOURCE_DIR}/version.cpp VERSION_)
else()
    set(VERSION_ "")
endif()

if (NOT "${VERSION}" STREQUAL "${VERSION_}")
    file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/version.cpp "${VERSION}")
endif()
