1## ---------------------------------------------------------------------
2##
3## Copyright (C) 2015 - 2017 by the deal.II authors
4##
5## This file is part of the deal.II library.
6##
7## The deal.II library is free software; you can use it, redistribute
8## it, and/or modify it under the terms of the GNU Lesser General
9## Public License as published by the Free Software Foundation; either
10## version 2.1 of the License, or (at your option) any later version.
11## The full text of the license can be found in the file LICENSE.md at
12## the top level directory of deal.II.
13##
14## ---------------------------------------------------------------------
15
16#
17# This file implements the DEAL_II_QUERY_GIT_INFORMATION macro, which is
18# part of the deal.II library.
19#
20# Usage:
21#       DEAL_II_QUERY_GIT_INFORMATION()
22#       DEAL_II_QUERY_GIT_INFORMATION("CUSTOM_PREFIX")
23#
24# This will try to gather information about current branch, as well as
25# short and long revision. If ${CMAKE_SOURCE_DIR} is the root of a git
26# repository the following variables will be populated:
27#
28#       GIT_BRANCH
29#       GIT_REVISION
30#       GIT_SHORTREV
31#       GIT_TAG
32#
33# The macro can be called with an optional PREFIX argument to prefix the
34# variables:
35#
36#       PREFIX_GIT_BRANCH
37#       PREFIX_GIT_REVISION
38#       PREFIX_GIT_SHORTREV
39#       PREFIX_GIT_TAG
40#
41
42MACRO(DEAL_II_QUERY_GIT_INFORMATION)
43
44  MESSAGE(STATUS "Query git repository information.")
45
46  # Set prefix.
47  SET(_prefix "")
48  IF(NOT "${ARGN}" STREQUAL "")
49    SET(_prefix "${ARGN}_")
50  ENDIF()
51
52  FIND_PACKAGE(Git)
53
54  #
55  # Only run the following if we have git and the source directory seems to
56  # be under version control.
57  #
58  IF(GIT_FOUND AND EXISTS ${CMAKE_SOURCE_DIR}/.git/HEAD)
59    #
60    # Bogus configure_file calls to trigger a reconfigure, and thus an
61    # update of branch and commit information every time HEAD has changed.
62    #
63    CONFIGURE_FILE(
64      ${CMAKE_SOURCE_DIR}/.git/HEAD
65      ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/HEAD
66      )
67    FILE(STRINGS ${CMAKE_SOURCE_DIR}/.git/HEAD _head_ref LIMIT_COUNT 1)
68    STRING(REPLACE "ref: " "" _head_ref ${_head_ref})
69    IF(EXISTS ${CMAKE_SOURCE_DIR}/.git/${_head_ref})
70      CONFIGURE_FILE(
71        ${CMAKE_SOURCE_DIR}/.git/${_head_ref}
72        ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/HEAD_REF
73        )
74    ENDIF()
75
76    #
77    # Query for revision:
78    #
79
80    EXECUTE_PROCESS(
81       COMMAND ${GIT_EXECUTABLE} log -n 1 --pretty=format:"%H %h"
82       WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
83       OUTPUT_VARIABLE _info
84       RESULT_VARIABLE _result
85       OUTPUT_STRIP_TRAILING_WHITESPACE
86       )
87    IF(${_result} EQUAL 0)
88      STRING(REGEX REPLACE "^\"([^ ]+) ([^ ]+)\"$"
89        "\\1" ${_prefix}GIT_REVISION "${_info}")
90      STRING(REGEX REPLACE "^\"([^ ]+) ([^ ]+)\"$"
91        "\\2" ${_prefix}GIT_SHORTREV "${_info}")
92    ENDIF()
93
94    #
95    # Query for branch:
96    #
97
98    EXECUTE_PROCESS(
99       COMMAND ${GIT_EXECUTABLE} symbolic-ref HEAD
100       WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
101       OUTPUT_VARIABLE _branch
102       RESULT_VARIABLE _result
103       OUTPUT_STRIP_TRAILING_WHITESPACE
104       )
105    IF(${_result} EQUAL 0)
106      STRING(REGEX REPLACE "refs/heads/" "" ${_prefix}GIT_BRANCH "${_branch}")
107    ENDIF()
108
109    #
110    # Query for tag:
111    #
112    SET(_script "")
113    IF(EXISTS     ${CMAKE_BINARY_DIR}/${DEAL_II_SHARE_RELDIR}/scripts/get_latest_tag.sh)
114      SET(_script ${CMAKE_BINARY_DIR}/${DEAL_II_SHARE_RELDIR}/scripts/get_latest_tag.sh)
115    ELSEIF(EXISTS ${DEAL_II_PATH}/${DEAL_II_SHARE_RELDIR}/scripts/get_latest_tag.sh)
116      SET(_script ${DEAL_II_PATH}/${DEAL_II_SHARE_RELDIR}/scripts/get_latest_tag.sh)
117    ENDIF()
118    IF(NOT "${_script}" STREQUAL "")
119       EXECUTE_PROCESS(
120          COMMAND ${_script}
121          WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
122          OUTPUT_VARIABLE _tag
123          RESULT_VARIABLE _result
124          OUTPUT_STRIP_TRAILING_WHITESPACE
125          )
126       IF(${_result} EQUAL 0)
127         SET(${_prefix}GIT_TAG ${_tag})
128       ENDIF()
129    ELSE()
130       MESSAGE(STATUS "Could not locate get_latest_tag.sh. " ${_prefix}GIT_TAG " will not be set.")
131    ENDIF()
132
133  ENDIF()
134
135ENDMACRO()
136