xref: /freebsd/contrib/ntp/sntp/libevent/cmake/VersionViaGit.cmake (revision a466cc55373fc3cf86837f09da729535b57e69a1)
1# This module defines the following variables utilizing
2# git to determine the parent tag. And if found the macro
3# will attempt to parse them in the github tag fomat
4#
5# Useful for auto-versioning in our CMakeLists
6#
7#  EVENT_GIT___VERSION_MAJOR - Major version.
8#  EVENT_GIT___VERSION_MINOR - Minor version
9#  EVENT_GIT___VERSION_STAGE - Stage version
10#
11# Example usage:
12#
13# event_fuzzy_version_from_git()
14#    message("Libvent major=${EVENT_GIT___VERSION_MAJOR}")
15#    message("        minor=${EVENT_GIT___VERSION_MINOR}")
16#    message("        patch=${EVENT_GIT___VERSION_PATCH}")
17#    message("        stage=${EVENT_GIT___VERSION_STAGE}")
18# endif()
19
20include(FindGit)
21
22macro(event_fuzzy_version_from_git)
23	# set our defaults.
24	set(EVENT_GIT___VERSION_MAJOR 2)
25	set(EVENT_GIT___VERSION_MINOR 1)
26	set(EVENT_GIT___VERSION_PATCH 12)
27	set(EVENT_GIT___VERSION_STAGE "stable")
28
29	find_package(Git)
30
31	if (GIT_FOUND)
32		execute_process(
33			COMMAND
34				${GIT_EXECUTABLE} describe --abbrev=0 --always
35			WORKING_DIRECTORY
36				${PROJECT_SOURCE_DIR}
37			RESULT_VARIABLE
38				GITRET
39			OUTPUT_VARIABLE
40				GITVERSION
41			OUTPUT_STRIP_TRAILING_WHITESPACE
42		)
43
44		string(REGEX REPLACE "[\\._-]" ";" VERSION_LIST "${GITVERSION}")
45		if(VERSION_LIST)
46			list(LENGTH VERSION_LIST VERSION_LIST_LENGTH)
47		endif()
48
49		if ((GITRET EQUAL 0) AND (VERSION_LIST_LENGTH EQUAL 5))
50			list(GET VERSION_LIST 1 _MAJOR)
51			list(GET VERSION_LIST 2 _MINOR)
52			list(GET VERSION_LIST 3 _PATCH)
53			list(GET VERSION_LIST 4 _STAGE)
54
55			set(_DEFAULT_VERSION "${EVENT_GIT___VERSION_MAJOR}.${EVENT_GIT___VERSION_MINOR}.${EVENT_GIT___VERSION_PATCH}-${EVENT_GIT___VERSION_STAGE}")
56			set(_GIT_VERSION     "${_MAJOR}.${_MINOR}.${_PATCH}-${_STAGE}")
57
58			if (${_DEFAULT_VERSION} VERSION_LESS ${_GIT_VERSION})
59				set(EVENT_GIT___VERSION_MAJOR ${_MAJOR})
60				set(EVENT_GIT___VERSION_MINOR ${_MINOR})
61				set(EVENT_GIT___VERSION_PATCH ${_PATCH})
62				set(EVENT_GIT___VERSION_STAGE ${_STAGE})
63			endif()
64		endif()
65	endif()
66endmacro()
67