]> git.treefish.org Git - cmake-modules/cmake-getgitversion.git/blob - GetGitVersion.cmake
various improvements
[cmake-modules/cmake-getgitversion.git] / GetGitVersion.cmake
1 # __________________________________________________________________________________ __ _
2 # get_git_version:
3 # Obtain version string from git tags via git_describe and parse result into
4 # convenient version variables.
5 #
6 # Tags have to match the regex ^[^0-9]*([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9a-zA-Z]+),
7 # as in v1.2.3-rc5, where the expressions in parentheses are interpreted as major,
8 # minor, patch and extra (package release) version numbers.
9 #
10 # Usage:
11 # include(GetGitVersion)
12 # get_git_version(VERSION_VARIABLE_PREFIX [<additional args for git_describe>])
13 #
14 # Following variables are set by get_git_version:
15 # VERSION_VARIABLE_PREFIX_MAJOR, VERSION_VARIABLE_PREFIX_MINOR,
16 # VERSION_VARIABLE_PREFIX_PATCH, VERSION_VARIABLE_PREFIX_EXTRA,
17 # VERSION_VARIABLE_PREFIX
18 #
19 # "THE BEER-WARE LICENSE" (Revision 42):
20 # <cptiglo@treefish.org> wrote this file.  As long as you retain this notice
21 # you can do whatever you want with this stuff. If we meet some day, and you
22 # think this stuff is worth it, you can buy me a beer in return.
23 # __________________________________________________________________________________ __ _
24
25 function(get_git_version _gitversionprefix)
26     include(GetGitRevisionDescription)
27     git_describe(GIT_DESCRIPTION ${ARGN})
28
29     string(REGEX REPLACE
30            "^[^0-9]*([0-9]+)\\..*" "\\1" VERSION_MAJOR "${GIT_DESCRIPTION}")
31     string(REGEX REPLACE
32            "^[^0-9]*[0-9]+\\.([0-9]+).*" "\\1" VERSION_MINOR "${GIT_DESCRIPTION}")
33     string(REGEX REPLACE
34            "^[^0-9]*[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" VERSION_PATCH "${GIT_DESCRIPTION}")
35     string(REGEX REPLACE
36            "^[^0-9]*[0-9]+\\.[0-9]+\\.[0-9]+-([0-9a-zA-Z-]+).*" "\\1" VERSION_EXTRA "${GIT_DESCRIPTION}")
37     string(REGEX REPLACE
38            "([^-]*-g)" "r\\1" VERSION_EXTRA "${VERSION_EXTRA}")
39     string(REGEX REPLACE
40            "-" "." VERSION_EXTRA "${VERSION_EXTRA}")
41            
42     set(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}-${VERSION_EXTRA}")
43
44     if(NOT "${VERSION}" MATCHES
45        "^[0-9]+\\.[0-9]+\\.[0-9]+-[0-9a-zA-Z]+\\.?r?[0-9]*\\.?g?[0-9a-zA-Z]*$")
46
47         message(FATAL_ERROR 
48                 "\nCOULD NOT EXTRACT VALID VERSION STRING FROM git_describe!"
49                 "\n git_describe returned: \"${GIT_DESCRIPTION}\""
50                 "\n Extracted invalid version string: \"${VERSION}\"\n")
51
52     endif(NOT "${VERSION}" MATCHES
53           "^[0-9]+\\.[0-9]+\\.[0-9]+-[0-9a-zA-Z]+\\.?r?[0-9]*\\.?g?[0-9a-zA-Z]*$")
54
55     set(${_gitversionprefix}       "${VERSION}"       PARENT_SCOPE)
56     set(${_gitversionprefix}_MAJOR "${VERSION_MAJOR}" PARENT_SCOPE)
57     set(${_gitversionprefix}_MINOR "${VERSION_MINOR}" PARENT_SCOPE)
58     set(${_gitversionprefix}_PATCH "${VERSION_PATCH}" PARENT_SCOPE)
59     set(${_gitversionprefix}_EXTRA "${VERSION_EXTRA}" PARENT_SCOPE)
60 endfunction()