• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1set(VERSION_MAJOR 25)
2set(VERSION_MINOR 1)
3set(VERSION_PATCH 24)
4set(VERSION_COMMIT 0)
5
6if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
7  find_program(GIT git)
8  if(GIT)
9    execute_process(
10      COMMAND ${GIT} describe --tags
11      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
12      OUTPUT_VARIABLE GIT_DESCRIBE_DIRTY
13      OUTPUT_STRIP_TRAILING_WHITESPACE
14      RESULT_VARIABLE GIT_DESCRIBE_RESULT
15    )
16
17    if(GIT_DESCRIBE_RESULT EQUAL 0)
18      # Test if the most recent Git tag matches the pattern "v<major>.<minor>.<patch>*"
19      if(GIT_DESCRIBE_DIRTY MATCHES "^v[0-9]+\\.[0-9]+\\.[0-9]+.*")
20        string(REGEX REPLACE "^v([0-9]+)\\..*" "\\1" VERSION_MAJOR "${GIT_DESCRIBE_DIRTY}")
21        string(REGEX REPLACE "^v[0-9]+\\.([0-9]+).*" "\\1" VERSION_MINOR "${GIT_DESCRIBE_DIRTY}")
22        string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" VERSION_PATCH "${GIT_DESCRIBE_DIRTY}")
23        string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.[0-9]+\\-([0-9]+).*" "\\1" VERSION_COMMIT "${GIT_DESCRIBE_DIRTY}")
24        # If the tag points to the commit, then only the tag is shown in "git describe"
25        if(VERSION_COMMIT STREQUAL GIT_DESCRIBE_DIRTY)
26          set(VERSION_COMMIT 0)
27        endif()
28      else()
29        message(WARNING "\"${GIT_DESCRIBE_DIRTY}\" does not match pattern v<major>.<minor>.<patch>-<commit>")
30      endif()
31    else()
32      message(WARNING "git describe failed with exit code: ${GIT_DESCRIBE_RESULT}\nMake sure you cloned with tags or run 'git fetch --tags'.")
33    endif()
34  else()
35    message(WARNING "git is not found")
36  endif()
37endif()
38
39message(STATUS "Proceeding with version: ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}.${VERSION_COMMIT}")
40