xref: /aosp_15_r20/external/libcxx/utils/google-benchmark/cmake/GetGitVersion.cmake (revision 58b9f456b02922dfdb1fad8a988d5fd8765ecb80)
1*58b9f456SAndroid Build Coastguard Worker# - Returns a version string from Git tags
2*58b9f456SAndroid Build Coastguard Worker#
3*58b9f456SAndroid Build Coastguard Worker# This function inspects the annotated git tags for the project and returns a string
4*58b9f456SAndroid Build Coastguard Worker# into a CMake variable
5*58b9f456SAndroid Build Coastguard Worker#
6*58b9f456SAndroid Build Coastguard Worker#  get_git_version(<var>)
7*58b9f456SAndroid Build Coastguard Worker#
8*58b9f456SAndroid Build Coastguard Worker# - Example
9*58b9f456SAndroid Build Coastguard Worker#
10*58b9f456SAndroid Build Coastguard Worker# include(GetGitVersion)
11*58b9f456SAndroid Build Coastguard Worker# get_git_version(GIT_VERSION)
12*58b9f456SAndroid Build Coastguard Worker#
13*58b9f456SAndroid Build Coastguard Worker# Requires CMake 2.8.11+
14*58b9f456SAndroid Build Coastguard Workerfind_package(Git)
15*58b9f456SAndroid Build Coastguard Worker
16*58b9f456SAndroid Build Coastguard Workerif(__get_git_version)
17*58b9f456SAndroid Build Coastguard Worker  return()
18*58b9f456SAndroid Build Coastguard Workerendif()
19*58b9f456SAndroid Build Coastguard Workerset(__get_git_version INCLUDED)
20*58b9f456SAndroid Build Coastguard Worker
21*58b9f456SAndroid Build Coastguard Workerfunction(get_git_version var)
22*58b9f456SAndroid Build Coastguard Worker  if(GIT_EXECUTABLE)
23*58b9f456SAndroid Build Coastguard Worker      execute_process(COMMAND ${GIT_EXECUTABLE} describe --match "v[0-9]*.[0-9]*.[0-9]*" --abbrev=8
24*58b9f456SAndroid Build Coastguard Worker          WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
25*58b9f456SAndroid Build Coastguard Worker          RESULT_VARIABLE status
26*58b9f456SAndroid Build Coastguard Worker          OUTPUT_VARIABLE GIT_VERSION
27*58b9f456SAndroid Build Coastguard Worker          ERROR_QUIET)
28*58b9f456SAndroid Build Coastguard Worker      if(${status})
29*58b9f456SAndroid Build Coastguard Worker          set(GIT_VERSION "v0.0.0")
30*58b9f456SAndroid Build Coastguard Worker      else()
31*58b9f456SAndroid Build Coastguard Worker          string(STRIP ${GIT_VERSION} GIT_VERSION)
32*58b9f456SAndroid Build Coastguard Worker          string(REGEX REPLACE "-[0-9]+-g" "-" GIT_VERSION ${GIT_VERSION})
33*58b9f456SAndroid Build Coastguard Worker      endif()
34*58b9f456SAndroid Build Coastguard Worker
35*58b9f456SAndroid Build Coastguard Worker      # Work out if the repository is dirty
36*58b9f456SAndroid Build Coastguard Worker      execute_process(COMMAND ${GIT_EXECUTABLE} update-index -q --refresh
37*58b9f456SAndroid Build Coastguard Worker          WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
38*58b9f456SAndroid Build Coastguard Worker          OUTPUT_QUIET
39*58b9f456SAndroid Build Coastguard Worker          ERROR_QUIET)
40*58b9f456SAndroid Build Coastguard Worker      execute_process(COMMAND ${GIT_EXECUTABLE} diff-index --name-only HEAD --
41*58b9f456SAndroid Build Coastguard Worker          WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
42*58b9f456SAndroid Build Coastguard Worker          OUTPUT_VARIABLE GIT_DIFF_INDEX
43*58b9f456SAndroid Build Coastguard Worker          ERROR_QUIET)
44*58b9f456SAndroid Build Coastguard Worker      string(COMPARE NOTEQUAL "${GIT_DIFF_INDEX}" "" GIT_DIRTY)
45*58b9f456SAndroid Build Coastguard Worker      if (${GIT_DIRTY})
46*58b9f456SAndroid Build Coastguard Worker          set(GIT_VERSION "${GIT_VERSION}-dirty")
47*58b9f456SAndroid Build Coastguard Worker      endif()
48*58b9f456SAndroid Build Coastguard Worker  else()
49*58b9f456SAndroid Build Coastguard Worker      set(GIT_VERSION "v0.0.0")
50*58b9f456SAndroid Build Coastguard Worker  endif()
51*58b9f456SAndroid Build Coastguard Worker
52*58b9f456SAndroid Build Coastguard Worker  message(STATUS "git Version: ${GIT_VERSION}")
53*58b9f456SAndroid Build Coastguard Worker  set(${var} ${GIT_VERSION} PARENT_SCOPE)
54*58b9f456SAndroid Build Coastguard Workerendfunction()
55