]> git.treefish.org Git - cmake-modules/cmake-getgitversion.git/blob - GetGitVersion.cmake
formatting
[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 be in the format .*([0-9]+)\.([0-9]+)\.([0-9]+)-(.*), as in
7 # 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]+-(.*)" "\\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(${_gitversionprefix}_MAJOR "${VERSION_MAJOR}" PARENT_SCOPE)
43     set(${_gitversionprefix}_MINOR "${VERSION_MINOR}" PARENT_SCOPE)
44     set(${_gitversionprefix}_PATCH "${VERSION_PATCH}" PARENT_SCOPE)
45     set(${_gitversionprefix}_EXTRA "${VERSION_EXTRA}" PARENT_SCOPE)
46
47     set(${_gitversionprefix}
48         "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}-${VERSION_EXTRA}" PARENT_SCOPE)
49 endfunction()