summaryrefslogtreecommitdiffstats
path: root/src/Version.cmake
blob: 4dbd25a5bd14ce962653a707f95ab706d9eebadf (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
set(CastXML_VERSION "0.1")

if(EXISTS ${CastXML_SOURCE_DIR}/.git)
  find_package(Git)
  if(GIT_FOUND)
    macro(_git)
      execute_process(
        COMMAND ${GIT_EXECUTABLE} ${ARGN}
        WORKING_DIRECTORY ${CastXML_SOURCE_DIR}
        RESULT_VARIABLE _git_res
        OUTPUT_VARIABLE _git_out OUTPUT_STRIP_TRAILING_WHITESPACE
        ERROR_VARIABLE _git_err ERROR_STRIP_TRAILING_WHITESPACE
        )
    endmacro()
  endif()
endif()

if(EXISTS ${CastXML_SOURCE_DIR}/VERSION)
  # Read version provided in file with source tarball.
  file(STRINGS ${CastXML_SOURCE_DIR}/VERSION CastXML_VERSION
       LIMIT_COUNT 1 LIMIT_INPUT 1024)
elseif(COMMAND _git)
  # Compute version relative to annotated version tags, if any.
  _git(describe --match "v[0-9]*" --dirty)
  if(_git_out MATCHES "^v([0-9].*)$")
    # Use version computed by 'git describe'.
    set(CastXML_VERSION "${CMAKE_MATCH_1}")
  else()
    # Compute version currently checked out, possibly dirty.
    _git(rev-parse --verify -q --short=7 HEAD)
    if(_git_out MATCHES "^([0-9a-f]+)$")
      set(CastXML_VERSION "${CastXML_VERSION}-g${CMAKE_MATCH_1}")
      _git(update-index -q --refresh)
      _git(diff-index --name-only HEAD --)
      if(_git_out)
        set(CastXML_VERSION "${CastXML_VERSION}-dirty")
      endif()
    else()
      set(CastXML_VERSION "${CastXML_VERSION}-git")
    endif()
  endif()
elseif("$Format:%h$" MATCHES "^([0-9a-f]+)$")
  # Use version exported by 'git archive'.
  set(CastXML_VERSION "${CastXML_VERSION}-g${CMAKE_MATCH_1}")
else()
  # Generic development version.
  set(CastXML_VERSION "${CastXML_VERSION}-git")
endif()

if(CastXML_VERSION MATCHES "^([0-9]+)\\.([0-9]+)(\\.([0-9]+))?")
  set(CastXML_VERSION_MAJOR "${CMAKE_MATCH_1}")
  set(CastXML_VERSION_MINOR "${CMAKE_MATCH_2}")
  if(CMAKE_MATCH_4)
    set(CastXML_VERSION_PATCH "${CMAKE_MATCH_4}")
  else()
    set(CastXML_VERSION_PATCH 0)
  endif()
else()
  message(FATAL_ERROR "Failed to extract version components from '${CastXML_VERSION}'")
endif()