CMakeToolsVersionFromGit

This module attempts to determine a project version from a Git tag. It searches for the most recent tag that can be reached from the current commit. This includes annotated and lightweight tags.

Result Variables

The module defines these variables:

CMAKE_TOOLS_GIT_TAG

The project version derived from the most recent Git tag. The module removes any prefix and suffix from the tag. For example, if the tag is v1.5.3, the value of this variable is 1.5.2.

CMAKE_TOOLS_GIT_TAG_MAJOR

An integer for the major component of the project version. The module assumes this is in the tag.

CMAKE_TOOLS_GIT_TAG_MINOR

An integer for the minor component of the project version. If this componenet is not in the Git tag, this variable is set to an empty string.

CMAKE_TOOLS_GIT_TAG_PATCH

An integer for the patch component of the project version. If this componenet is not in the Git tag, this variable is set to an empty string.

CMAKE_TOOLS_GIT_DISTANCE

An integer with the number of commits between the current commit and the tag. If the tag points to the current commit, this variable is set to 0.

Using This Module

All you have to do is include this module. Then use the appropriate variables where you would define a project version.

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake-tools")
include(CMakeToolsVersionFromGit)

# Using the tag as the project version:
project(MyProject VERSION ${CMAKE_TOOLS_GIT_TAG})

# Using the distance as the project version's tweak component:
project(MyProject VERSION ${CMAKE_TOOLS_GIT_TAG}.${CMAKE_TOOLS_GIT_DISTANCE})

This module locates Git via find_package(Git); see the FindGit documentaiton for details. If this module does not find Git, it issues a warning, leaves the above variables undefined, and allows CMake to continue.