aboutsummaryrefslogtreecommitdiff
path: root/cmake/GetGitVersion.cmake
blob: e017fa30517ec42a8f5d2af299e85767007338e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# - Returns a version string from Git tags
#
# This function inspects the annotated git tags for the project and returns a string
# into a CMake variable
#
#  get_git_version(<var>)
#
# - Example
#
# include(GetGitVersion)
# get_git_version(GIT_VERSION)
#
# Requires CMake 2.6+

if(__get_git_version)
  return()
endif()
set(__get_git_version INCLUDED)

function(get_git_version var)
  execute_process(COMMAND git describe --match "v[0-9]*.[0-9]*.[0-9]*" --abbrev=8
      RESULT_VARIABLE status
      OUTPUT_VARIABLE GIT_VERSION
      ERROR_QUIET)
  if(${status})
      set(GIT_VERSION "v0.0.0")
  else()
      string(STRIP ${GIT_VERSION} GIT_VERSION)
      string(REGEX REPLACE "-[0-9]+-g" "-" GIT_VERSION ${GIT_VERSION})
  endif()

  # Work out if the repository is dirty
  execute_process(COMMAND git update-index -q --refresh
      OUTPUT_QUIET
      ERROR_QUIET)
  execute_process(COMMAND git diff-index --name-only HEAD --
      OUTPUT_VARIABLE GIT_DIFF_INDEX
      ERROR_QUIET)
  string(COMPARE NOTEQUAL "${GIT_DIFF_INDEX}" "" GIT_DIRTY)
  if (${GIT_DIRTY})
      set(GIT_VERSION "${GIT_VERSION}-dirty")
  endif()
  message("-- git Version: ${GIT_VERSION}")
  set(${var} ${GIT_VERSION} PARENT_SCOPE)
endfunction()