• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# - Returns a version string from Git tags
2#
3# This function inspects the annotated git tags for the project and returns a string
4# into a CMake variable
5#
6#  get_git_version(<var>)
7#
8# - Example
9#
10# include(GetGitVersion)
11# get_git_version(GIT_VERSION)
12#
13# Requires CMake 2.8.11+
14find_package(Git)
15
16if(__get_git_version)
17  return()
18endif()
19set(__get_git_version INCLUDED)
20
21function(get_git_version var)
22  if(GIT_EXECUTABLE)
23      execute_process(COMMAND ${GIT_EXECUTABLE} describe --match "v[0-9]*.[0-9]*.[0-9]*" --abbrev=8
24          RESULT_VARIABLE status
25          OUTPUT_VARIABLE GIT_VERSION
26          ERROR_QUIET)
27      if(${status})
28          set(GIT_VERSION "v0.0.0")
29      else()
30          string(STRIP ${GIT_VERSION} GIT_VERSION)
31          string(REGEX REPLACE "-[0-9]+-g" "-" GIT_VERSION ${GIT_VERSION})
32      endif()
33
34      # Work out if the repository is dirty
35      execute_process(COMMAND ${GIT_EXECUTABLE} update-index -q --refresh
36          OUTPUT_QUIET
37          ERROR_QUIET)
38      execute_process(COMMAND ${GIT_EXECUTABLE} diff-index --name-only HEAD --
39          OUTPUT_VARIABLE GIT_DIFF_INDEX
40          ERROR_QUIET)
41      string(COMPARE NOTEQUAL "${GIT_DIFF_INDEX}" "" GIT_DIRTY)
42      if (${GIT_DIRTY})
43          set(GIT_VERSION "${GIT_VERSION}-dirty")
44      endif()
45  else()
46      set(GIT_VERSION "v0.0.0")
47  endif()
48
49  message("-- git Version: ${GIT_VERSION}")
50  set(${var} ${GIT_VERSION} PARENT_SCOPE)
51endfunction()
52