-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVersionForProject.cmake
78 lines (59 loc) · 2.46 KB
/
VersionForProject.cmake
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#[=======================================================================[.rst:
VersionForProject
-----------------
Provides a function to set the version of a subproject from a Git tag
specific to that project.
This module provides the function ``version_for_project``
version_for_project
^^^^^^^^^^^^^^^^^^^
``version_for_project`` uses ``git describe`` to find tags matching the
project name and cmake string functions to pull the version out of it.
Input Variables
+++++++++++++++
``PROJECT_NAME``
A mandatory positional argument specifying the name of the project,
which is used as the ``match`` argument to ``git describe --tags``.
``OUTPUT_VARIABLE``
An optional second positional argument which will set the name of the
output variable.
Result Variables
^^^^^^^^^^^^^^^^
This will define the following variables:
``${PROJECT_NAME}_VERSION``
If the second argument is not set, this is where the version goes
``${OUTPUT_VARIABLE}``
If the second argument is set, a variable named by the second
argument is used for the output.
#]=======================================================================]
find_package(Git QUIET)
if(GIT_NOTFOUND)
fatal_error("Couldn't find git, which is needed for version parsing")
endif()
# version_for_project takes a mandatory function naming the project (which
# should be the version tag prefix) and optionally a second argument naming
# an output variable. If the second variable is not specified, the output
# variable is ${PROJECT_NAME}_VERSION
function(VERSION_FOR_PROJECT PROJECT_NAME)
# Use the optional arg if present, otherwise default
if(${ARGC} GREATER 1)
set(OUTPUT_NAME ${ARGV1})
else()
set(OUTPUT_NAME "${PROJECT_NAME}_VERSION")
endif()
# Actually pull the version from git
execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --match "${PROJECT_NAME}*"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_VARIABLE TAGNAME
COMMAND_ERROR_IS_FATAL ANY
OUTPUT_STRIP_TRAILING_WHITESPACE)
# Use cmake internal functions rather than shelling out to cut
# This first call returns @v2.0.0-4-geeb62a0, for instance
string(REGEX MATCH "@.*$" PREPENDED_VERSION ${TAGNAME})
# This second call removes the @
string(SUBSTRING ${PREPENDED_VERSION} 1 -1 VERSION)
# Use a macro to dynamically determine a variable name
macro(SET_OUTPUT OUTPUT_VAR_NAME OUTPUT_VAR_VALUE)
set(${OUTPUT_VAR_NAME} ${OUTPUT_VAR_VALUE} PARENT_SCOPE)
endmacro()
set_output(${OUTPUT_NAME} ${VERSION})
endfunction()