Bump to 4.8.3
[platform/upstream/ccache.git] / cmake / CcacheVersion.cmake
1 # This script sets two variables:
2 #
3 # - CCACHE_VERSION (version string)
4 # - CCACHE_VERSION_ORIGIN (archive or git)
5 #
6 # There are three main scenarios:
7 #
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
17 #    configuration.
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".
21 #
22 # CCACHE_VERSION_ORIGIN is set to "archive" in scenario 1 and "git" in scenario
23 # 3.
24
25 set(version_info "044558e647b49dbc8fc89b810a7d22f526101e6b tag: v4.8.3, 4.8-maint")
26 set(CCACHE_VERSION "unknown")
27
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]* (.*)")
29   # Scenario 1.
30   set(CCACHE_VERSION_ORIGIN archive)
31
32   set(hash "${CMAKE_MATCH_1}")
33   set(ref_names "${CMAKE_MATCH_2}")
34   if(ref_names MATCHES "tag: v([^,]+)")
35     # Tagged commit.
36     set(CCACHE_VERSION "${CMAKE_MATCH_1}")
37   else()
38     # Untagged commit.
39     set(CCACHE_VERSION "${hash}")
40   endif()
41 elseif(EXISTS "${CMAKE_SOURCE_DIR}/.git")
42   # Scenario 3.
43   set(CCACHE_VERSION_ORIGIN git)
44
45   find_package(Git QUIET)
46   if(NOT GIT_FOUND)
47     message(WARNING "Could not find git")
48   else()
49     macro(git)
50       execute_process(
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)
55     endmacro()
56
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")
62       endif()
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}")
67
68       git(rev-parse --abbrev-ref HEAD)
69       set(branch "${git_stdout}")
70
71       set(CCACHE_VERSION "${branch}.${hash}${dirty}")
72     endif() # else: fail below
73   endif()
74 endif()
75
76 if("${CCACHE_VERSION}" STREQUAL "unknown")
77   # Scenario 2 or unexpected error.
78   message(WARNING "Could not determine ccache version")
79 endif()
80
81 message(STATUS "Ccache version: ${CCACHE_VERSION}")