C++QED  v2 Milestone 10
a framework for simulating open quantum dynamics
GetGitRevisionDescription.cmake
1 #! \file
2 #! \ingroup Helpers
3 #! \brief Returns a version string from Git
4 #!
5 #! These functions force a re-configure on each git commit so that you can
6 #! trust the values of the variables in your build system.
7 #!
8 #! Requires %CMake 2.6 or newer (uses the 'function' command)
9 
10 # Original Author:
11 # 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
12 # http://academic.cleardefinition.com
13 # Iowa State University HCI Graduate Program/VRAC
14 #
15 # Copyright Iowa State University 2009-2010.
16 # Distributed under the Boost Software License, Version 1.0.
17 # (See accompanying file LICENSE.txt or copy at
18 # http://www.boost.org/LICENSE_1_0.txt)
19 
20 if(__get_git_revision_description)
21  return()
22 endif()
23 set(__get_git_revision_description YES)
24 
25 # We must run the following at "include" time, not at function call time,
26 # to find the path to this module rather than the path to a calling list file
27 get_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH)
28 
29 #! \brief Returns the refspec and sha hash of the current head revision
30 #! \ingroup Helpers
31 #! \param _refspecvar The refspec is returned in this variable.
32 #! \param _hashvar The sha hash is returned in this variable.
33 #!
34 #! Additional arguments are passed through to `git describe`.
35 #!
36 #! If the variable \ref CMake::GIT_SHA_OVERRIDE "GIT_SHA_OVERRIDE" is defined, both `_refspecvar` and
37 #! `_hashvar` are set to this value. This is used to set something meaningful
38 #! in automated Debian builds, where the git repository is not available.
39 function(get_git_head_revision _refspecvar _hashvar)
40  if(GIT_SHA_OVERRIDE)
41  set(${_refspecvar} "${GIT_SHA_OVERRIDE}" PARENT_SCOPE)
42  set(${_hashvar} "${GIT_SHA_OVERRIDE}" PARENT_SCOPE)
43  return()
44  endif()
45 
46  set(GIT_PARENT_DIR "${CMAKE_CURRENT_LIST_DIR}")
47  set(GIT_DIR "${GIT_PARENT_DIR}/.git")
48  while(NOT EXISTS "${GIT_DIR}") # .git dir not found, search parent directories
49  set(GIT_PREVIOUS_PARENT "${GIT_PARENT_DIR}")
50  get_filename_component(GIT_PARENT_DIR ${GIT_PARENT_DIR} PATH)
51  if(GIT_PARENT_DIR STREQUAL GIT_PREVIOUS_PARENT)
52  # We have reached the root directory, we are not in git
53  set(${_refspecvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
54  set(${_hashvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
55  return()
56  endif()
57  set(GIT_DIR "${GIT_PARENT_DIR}/.git")
58  endwhile()
59  # check if this is a submodule
60  if(NOT IS_DIRECTORY ${GIT_DIR})
61  file(READ ${GIT_DIR} submodule)
62  string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" GIT_DIR_RELATIVE ${submodule})
63  get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH)
64  get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE} ABSOLUTE)
65  endif()
66  set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
67  if(NOT EXISTS "${GIT_DATA}")
68  file(MAKE_DIRECTORY "${GIT_DATA}")
69  endif()
70 
71  if(NOT EXISTS "${GIT_DIR}/HEAD")
72  return()
73  endif()
74  set(HEAD_FILE "${GIT_DATA}/HEAD")
75  configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY)
76 
77  configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in"
78  "${GIT_DATA}/grabRef.cmake"
79  @ONLY)
80  include("${GIT_DATA}/grabRef.cmake")
81 
82  set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE)
83  set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE)
84 endfunction()
85 
86 #! \brief Returns the results of `git describe` on the source tree.
87 #! \ingroup Helpers
88 #! \param _var The result variable.
89 #!
90 #! The output is adjusted so that it tests false if an error occurs.
91 function(git_describe _var)
92  if(NOT GIT_FOUND)
93  find_package(Git QUIET)
94  endif()
95  get_git_head_revision(refspec hash)
96  if(NOT GIT_FOUND)
97  set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
98  return()
99  endif()
100  if(NOT hash)
101  set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
102  return()
103  endif()
104 
105  #message(STATUS "Arguments to execute_process: ${ARGN}")
106 
107  execute_process(COMMAND
108  "${GIT_EXECUTABLE}"
109  describe
110  ${hash}
111  ${ARGN}
112  WORKING_DIRECTORY
113  "${CMAKE_SOURCE_DIR}"
114  RESULT_VARIABLE
115  res
116  OUTPUT_VARIABLE
117  out
118  ERROR_QUIET
119  OUTPUT_STRIP_TRAILING_WHITESPACE)
120  if(NOT res EQUAL 0)
121  set(out "${out}-${res}-NOTFOUND")
122  endif()
123 
124  set(${_var} "${out}" PARENT_SCOPE)
125 endfunction()
126 
127 #! \brief Returns the results of `git describe --exact-match` on the source tree.
128 #! \ingroup Helpers
129 #! \param _var The result variable.
130 #!
131 #! The output is adjusted so that it tests false if there was no exact
132 #! matching tag.
133 function(git_get_exact_tag _var)
134  git_describe(out --exact-match ${ARGN})
135  set(${_var} "${out}" PARENT_SCOPE)
136 endfunction()