summaryrefslogtreecommitdiff
path: root/cmake/GitVersionGen.cmake
blob: 916572ff50a60e43d50b5e2fff7645ce3327e5f0 (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
macro(git_version_gen)

  include(FindGit)
  if (NOT GIT_FOUND)
    message(FATAL_ERROR "This is not a git repository")
  endif ()

  find_program(TAIL "tail")
  mark_as_advanced(TAIL)
  if (${TAIL} STREQUAL "")
    message(FATAL_ERROR "Cannot find the tail executable")
  endif ()

  execute_process(
    COMMAND ${GIT_EXECUTABLE} tag -l -n0
    COMMAND ${TAIL} -n 1
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    OUTPUT_VARIABLE _git_tag
    RESULT_VARIABLE _git_result
    OUTPUT_STRIP_TRAILING_WHITESPACE)
  if (NOT ${_git_result} EQUAL 0)
    message(FATAL_ERROR "Cannot fetch repository tag")
  endif ()
  message(STATUS "Repository tag is: ${_git_tag}")

  string(REGEX REPLACE
    "[^0-9]*([0-9]+)\\.[0-9]+.*" "\\1"
    _version_major "${_git_tag}")
  string(REGEX REPLACE
    "[^0-9]*[0-9]+\\.([0-9]+).*" "\\1"
    _version_minor "${_git_tag}")

  set(PACKAGE_VERSION_MAJOR "${_version_major}")
  set(PACKAGE_VERSION_MINOR "${_version_minor}")

endmacro(git_version_gen)