1 # This script sets two variables:
3 # - CCACHE_VERSION (version string)
4 # - CCACHE_VERSION_ORIGIN (archive or git)
6 # There are three main scenarios:
8 # 1. Building from a source code archive generated by "git archive", e.g. the
9 # official source code archive or one downloaded directly from GitHub via
10 # <https://github.com/ccache/ccache/archive/VERSION.tar.gz>. In this case the
11 # version_info info string below will be substituted because of export-subst
12 # in the .gitattributes file. The version will then be correct if VERSION
13 # refers to a tagged commit. If the commit is not tagged the version will be
14 # set to the commit hash instead.
15 # 2. Building from a source code archive not generated by "git archive" (i.e.,
16 # version_info has not been substituted). In this case we fail the
18 # 3. Building from a Git repository. In this case the version will be a proper
19 # version if building a tagged commit, otherwise "branch.hash(+dirty)". In
20 # case Git is not available, the version will be "unknown".
22 # CCACHE_VERSION_ORIGIN is set to "archive" in scenario 1 and "git" in scenario
25 set(version_info "044558e647b49dbc8fc89b810a7d22f526101e6b tag: v4.8.3, 4.8-maint")
26 set(CCACHE_VERSION "unknown")
28 if(version_info MATCHES "^([0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f])[0-9a-f]* (.*)")
30 set(CCACHE_VERSION_ORIGIN archive)
32 set(hash "${CMAKE_MATCH_1}")
33 set(ref_names "${CMAKE_MATCH_2}")
34 if(ref_names MATCHES "tag: v([^,]+)")
36 set(CCACHE_VERSION "${CMAKE_MATCH_1}")
39 set(CCACHE_VERSION "${hash}")
41 elseif(EXISTS "${CMAKE_SOURCE_DIR}/.git")
43 set(CCACHE_VERSION_ORIGIN git)
45 find_package(Git QUIET)
47 message(WARNING "Could not find git")
51 COMMAND "${GIT_EXECUTABLE}" ${ARGN}
52 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
53 OUTPUT_VARIABLE git_stdout OUTPUT_STRIP_TRAILING_WHITESPACE
54 ERROR_VARIABLE git_stderr ERROR_STRIP_TRAILING_WHITESPACE)
57 git(describe --tags --abbrev=8 --dirty)
58 if(git_stdout MATCHES "^v([^-]+)(-dirty)?$")
59 set(CCACHE_VERSION "${CMAKE_MATCH_1}")
60 if(NOT "${CMAKE_MATCH_2}" STREQUAL "")
61 set(CCACHE_VERSION "${CCACHE_VERSION}+dirty")
63 elseif(git_stdout MATCHES "^v[^-]+-[0-9]+-g([0-9a-f]+)(-dirty)?$")
64 set(hash "${CMAKE_MATCH_1}")
65 set(dirty "${CMAKE_MATCH_2}")
66 string(REPLACE "-" "+" dirty "${dirty}")
68 git(rev-parse --abbrev-ref HEAD)
69 set(branch "${git_stdout}")
71 set(CCACHE_VERSION "${branch}.${hash}${dirty}")
72 endif() # else: fail below
76 if("${CCACHE_VERSION}" STREQUAL "unknown")
77 # Scenario 2 or unexpected error.
78 message(WARNING "Could not determine ccache version")
81 message(STATUS "Ccache version: ${CCACHE_VERSION}")