2014-08-01 22:00:43 +08:00
|
|
|
# - Returns a version string from Git tags
|
|
|
|
#
|
|
|
|
# This function inspects the annotated git tags for the project and returns a string
|
|
|
|
# into a CMake variable
|
|
|
|
#
|
|
|
|
# get_git_version(<var>)
|
|
|
|
#
|
|
|
|
# - Example
|
|
|
|
#
|
|
|
|
# include(GetGitVersion)
|
|
|
|
# get_git_version(GIT_VERSION)
|
|
|
|
#
|
2015-10-02 23:05:58 +08:00
|
|
|
# Requires CMake 2.8.11+
|
|
|
|
find_package(Git)
|
2014-08-01 22:00:43 +08:00
|
|
|
|
|
|
|
if(__get_git_version)
|
|
|
|
return()
|
|
|
|
endif()
|
|
|
|
set(__get_git_version INCLUDED)
|
|
|
|
|
|
|
|
function(get_git_version var)
|
2015-10-02 23:05:58 +08:00
|
|
|
if(GIT_EXECUTABLE)
|
2021-05-21 00:59:29 +08:00
|
|
|
execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --match "v[0-9]*.[0-9]*.[0-9]*" --abbrev=8
|
2018-05-08 18:29:09 +08:00
|
|
|
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
|
2015-10-02 23:05:58 +08:00
|
|
|
RESULT_VARIABLE status
|
2021-05-21 00:59:29 +08:00
|
|
|
OUTPUT_VARIABLE GIT_DESCRIBE_VERSION
|
2015-10-02 23:05:58 +08:00
|
|
|
ERROR_QUIET)
|
2021-05-21 00:59:29 +08:00
|
|
|
if(status)
|
|
|
|
set(GIT_DESCRIBE_VERSION "v0.0.0")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
string(STRIP ${GIT_DESCRIBE_VERSION} GIT_DESCRIBE_VERSION)
|
|
|
|
if(GIT_DESCRIBE_VERSION MATCHES v[^-]*-)
|
|
|
|
string(REGEX REPLACE "v([^-]*)-([0-9]+)-.*" "\\1.\\2" GIT_VERSION ${GIT_DESCRIBE_VERSION})
|
2015-10-02 23:05:58 +08:00
|
|
|
else()
|
2021-05-21 00:59:29 +08:00
|
|
|
string(REGEX REPLACE "v(.*)" "\\1" GIT_VERSION ${GIT_DESCRIBE_VERSION})
|
2015-10-02 23:05:58 +08:00
|
|
|
endif()
|
2015-10-18 08:13:37 +08:00
|
|
|
|
|
|
|
# Work out if the repository is dirty
|
|
|
|
execute_process(COMMAND ${GIT_EXECUTABLE} update-index -q --refresh
|
2018-05-08 18:29:09 +08:00
|
|
|
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
|
2015-10-18 08:13:37 +08:00
|
|
|
OUTPUT_QUIET
|
|
|
|
ERROR_QUIET)
|
|
|
|
execute_process(COMMAND ${GIT_EXECUTABLE} diff-index --name-only HEAD --
|
2018-05-08 18:29:09 +08:00
|
|
|
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
|
2015-10-18 08:13:37 +08:00
|
|
|
OUTPUT_VARIABLE GIT_DIFF_INDEX
|
|
|
|
ERROR_QUIET)
|
|
|
|
string(COMPARE NOTEQUAL "${GIT_DIFF_INDEX}" "" GIT_DIRTY)
|
|
|
|
if (${GIT_DIRTY})
|
2021-05-21 00:59:29 +08:00
|
|
|
set(GIT_DESCRIBE_VERSION "${GIT_DESCRIBE_VERSION}-dirty")
|
2015-10-18 08:13:37 +08:00
|
|
|
endif()
|
2021-05-21 00:59:29 +08:00
|
|
|
message(STATUS "git version: ${GIT_DESCRIBE_VERSION} normalized to ${GIT_VERSION}")
|
2014-08-01 22:00:43 +08:00
|
|
|
else()
|
2021-05-21 00:59:29 +08:00
|
|
|
set(GIT_VERSION "0.0.0")
|
2014-08-01 22:00:43 +08:00
|
|
|
endif()
|
|
|
|
|
|
|
|
set(${var} ${GIT_VERSION} PARENT_SCOPE)
|
|
|
|
endfunction()
|