@@ -21,6 +21,14 @@ FlibcppVersion
2121 ${PROJNAME}_VERSION
2222 ${PROJNAME}_VERSION_STRING
2323
24+ The project version string uses PEP-440 semantic versioning, and will look
25+ like v0.1.2 if the version is actually a tagged release, or v0.1.3+abcdef if
26+ it's not.
27+
28+ If a non-tagged version is exported, or an untagged shallow git clone is used,
29+ it's impossible to determine the version from the tag, so a warning will be
30+ issued and the version will be set to 0.0.0.
31+
2432#]=======================================================================]
2533
2634function (flibcpp_find_version PROJNAME GIT_VERSION_FILE)
@@ -31,8 +39,8 @@ function(flibcpp_find_version PROJNAME GIT_VERSION_FILE)
3139 if (_TEXTFILE MATCHES "\\ $Format:" )
3240 # Not a "git archive": use live git information
3341 set (_CACHE_VAR "${PROJNAME} _GIT_DESCRIBE" )
34- set (_VERSION_STRING "${${_CACHE_VAR} }" )
35- if (NOT _VERSION_STRING )
42+ set (_CACHED_VERSION "${${_CACHE_VAR} }" )
43+ if (NOT _CACHED_VERSION )
3644 # Building from a git checkout rather than a distribution
3745 find_package (Git REQUIRED)
3846 execute_process (
@@ -44,27 +52,38 @@ function(flibcpp_find_version PROJNAME GIT_VERSION_FILE)
4452 OUTPUT_STRIP_TRAILING_WHITESPACE
4553 )
4654 if (NOT _GIT_RESULT EQUAL "0" )
47- message (FATAL_ERROR "Failed to get ${PROJNAME} version from git: "
55+ message (WARNING "Failed to get ${PROJNAME} version from git: "
4856 "${_GIT_ERR} " )
49- endif ()
50- if (NOT _VERSION_STRING)
51- message (FATAL_ERROR "Failed to get ${PROJNAME} version from git: "
57+ elseif (NOT _VERSION_STRING)
58+ message (WARNING "Failed to get ${PROJNAME} version from git: "
5259 "git describe returned an empty string" )
60+ else ()
61+ # Process description tag: e.g. v0.4.0-2-gc4af497 or v0.4.0
62+ string (REGEX MATCH "v([0-9.]+)(-[0-9]+-g([0-9a-f]+))?" _MATCH
63+ "${_VERSION_STRING} "
64+ )
65+ if (_MATCH)
66+ set (_VERSION_STRING "${CMAKE_MATCH_1} " )
67+ if (CMAKE_MATCH_2)
68+ # *not* a tagged release
69+ set (_VERSION_HASH "${CMAKE_MATCH_3} " )
70+ endif ()
71+ endif ()
5372 endif ()
54- set ("${_CACHE_VAR} " "${_VERSION_STRING} " CACHE INTERNAL
55- "Git description for ${PROJNAME} " )
56- endif ()
57- # Process description tag: e.g. v0.4.0-2-gc4af497 or v0.4.0
58- string (REGEX MATCH "v([0-9.]+)(-[0-9]+-g([0-9a-f]+))?" _MATCH
59- "${_VERSION_STRING} "
60- )
61- if (_MATCH)
62- set (_VERSION_STRING "${CMAKE_MATCH_1} " )
63- if (CMAKE_MATCH_2)
64- # *not* a tagged release
65- set (_VERSION_HASH "${CMAKE_MATCH_3} " )
73+ if (NOT _VERSION_STRING)
74+ execute_process (
75+ COMMAND "${GIT_EXECUTABLE} " "log" "-1" "--format=%h" "HEAD"
76+ WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR} "
77+ OUTPUT_VARIABLE _VERSION_HASH
78+ OUTPUT_STRIP_TRAILING_WHITESPACE
79+ )
6680 endif ()
81+ set (_CACHED_VERSION "${_VERSION_STRING} " "${_VERSION_HASH} " )
82+ set ("${_CACHE_VAR} " "${_CACHED_VERSION} " CACHE INTERNAL
83+ "Version string and hash for ${PROJNAME} " )
6784 endif ()
85+ list (GET _CACHED_VERSION 0 _VERSION_STRING)
86+ list (GET _CACHED_VERSION 1 _VERSION_HASH)
6887 else ()
6988 # First line are decorators, second is hash.
7089 list (GET _TEXTFILE 0 _TAG)
@@ -82,7 +101,9 @@ function(flibcpp_find_version PROJNAME GIT_VERSION_FILE)
82101 endif ()
83102
84103 if (NOT _VERSION_STRING)
85- message (FATAL_ERROR "Could not determine version for ${PROJNAME} " )
104+ message (WARNING "Could not determine version number ${PROJNAME} : "
105+ "perhaps a non-release archive?" )
106+ set (_VERSION_STRING "0.0.0" )
86107 endif ()
87108
88109 if (_VERSION_HASH)
0 commit comments