Warning, /office/kbibtex/src/getgit.cmake is written in an unsupported language. File is not indexed.

0001 #############################################################################
0002 #   SPDX-License-Identifier: GPL-2.0-or-later
0003 #                                                                           #
0004 #   SPDX-FileCopyrightText: 2004-2019 Thomas Fischer <fischer@unix-ag.uni-kl.de>
0005 #                                                                           #
0006 #   This program is free software; you can redistribute it and/or modify    #
0007 #   it under the terms of the GNU General Public License as published by    #
0008 #   the Free Software Foundation; either version 2 of the License, or       #
0009 #   (at your option) any later version.                                     #
0010 #                                                                           #
0011 #   This program is distributed in the hope that it will be useful,         #
0012 #   but WITHOUT ANY WARRANTY; without even the implied warranty of          #
0013 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           #
0014 #   GNU General Public License for more details.                            #
0015 #                                                                           #
0016 #   You should have received a copy of the GNU General Public License       #
0017 #   along with this program; if not, see <https://www.gnu.org/licenses/>.   #
0018 #############################################################################
0019 
0020 # Inspired by:
0021 # https://stackoverflow.com/questions/3780667/use-cmake-to-get-build-time-svn-revision
0022 
0023 if(DEFINED ENV{GIT_REV} AND DEFINED ENV{GIT_BRANCH} AND NOT("${GIT_REV}" STREQUAL "" OR "${GIT_BRANCH}" STREQUAL ""))
0024     message (STATUS "Git information set by environment variables GIT_REV and GIT_BRANCH")
0025     set (GIT_REV $ENV{GIT_REV})
0026     set (GIT_BRANCH $ENV{GIT_BRANCH})
0027     set (GIT_COMMIT_COUNT "0")
0028 else()
0029     set(GIT_REV "")
0030     set(GIT_BRANCH "")
0031     set(GIT_COMMIT_COUNT "0")
0032 
0033     if(EXISTS ${SOURCE_DIR}/.git)
0034         # Git
0035         find_program( GIT_EXECUTABLE NAMES git.bat git ) # for Windows, "git.bat" must be found before "git"
0036         if(GIT_EXECUTABLE)
0037             execute_process (
0038                 WORKING_DIRECTORY
0039                 "${SOURCE_DIR}"
0040                 COMMAND
0041                 ${GIT_EXECUTABLE} rev-parse --short HEAD
0042                 OUTPUT_VARIABLE GIT_REV
0043                 OUTPUT_STRIP_TRAILING_WHITESPACE
0044             )
0045             execute_process (
0046                 WORKING_DIRECTORY
0047                 "${SOURCE_DIR}"
0048                 COMMAND
0049                 ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
0050                 OUTPUT_VARIABLE GIT_BRANCH
0051                 OUTPUT_STRIP_TRAILING_WHITESPACE
0052             )
0053             execute_process (
0054                 WORKING_DIRECTORY
0055                 "${SOURCE_DIR}"
0056                 COMMAND
0057                 ${GIT_EXECUTABLE} rev-list --count HEAD
0058                 OUTPUT_VARIABLE GIT_COMMIT_COUNT
0059                 OUTPUT_STRIP_TRAILING_WHITESPACE
0060             )
0061         else()
0062             message( "No Git executable" )
0063         endif()
0064     else()
0065         message( "Not a Git source directory" )
0066     endif()
0067 endif()
0068 
0069 # write a header file defining VERSION
0070 file(
0071     WRITE
0072     "${BINARY_DIR}/kbibtex-git-info.h.tmp"
0073     "/// This file has been automatically generated by 'getgit.cmake'.\n/// Do not edit or modify it.\n\n"
0074 )
0075 file(
0076     APPEND
0077     "${BINARY_DIR}/kbibtex-git-info.h.tmp"
0078     "#ifndef KBIBTEX_GIT_INFO_H\n"
0079 )
0080 file(
0081     APPEND
0082     "${BINARY_DIR}/kbibtex-git-info.h.tmp"
0083     "#define KBIBTEX_GIT_INFO_H\n"
0084 )
0085 
0086 if("${GIT_REV}" STREQUAL "" OR "${GIT_BRANCH}" STREQUAL "")
0087     set(GIT_REV "")
0088     set(GIT_BRANCH "")
0089     set(GIT_COMMIT_COUNT "0")
0090     message(
0091         STATUS
0092         "Source does not come from a Git checkout or determining the Git revision or branch failed"
0093     )
0094     file(
0095         APPEND
0096         "${BINARY_DIR}/kbibtex-git-info.h.tmp"
0097         "/// This source code does not come from a Git checkout or\n/// determining the Git revision or branch failed.\n/// Please consider setting environment variables GIT_REV and\n/// GIT_BRANCH before running the build tool (make, ninja, ...).\n\n"
0098     )
0099 else()
0100     message(
0101         STATUS
0102         "Git revision is "
0103         ${GIT_REV}
0104         "\nGit branch is "
0105         ${GIT_BRANCH}
0106         "\nGit commit count is "
0107         ${GIT_COMMIT_COUNT}
0108     )
0109 endif()
0110 
0111 file(
0112     APPEND
0113     "${BINARY_DIR}/kbibtex-git-info.h.tmp"
0114     "#define KBIBTEX_GIT_REV_STRING \"${GIT_REV}\"\n"
0115 )
0116 file(
0117     APPEND
0118     "${BINARY_DIR}/kbibtex-git-info.h.tmp"
0119     "#define KBIBTEX_GIT_BRANCH_STRING \"${GIT_BRANCH}\"\n"
0120 )
0121 file(
0122     APPEND
0123     "${BINARY_DIR}/kbibtex-git-info.h.tmp"
0124     "#define KBIBTEX_GIT_COMMIT_COUNT ${GIT_COMMIT_COUNT}\n"
0125 )
0126 if("${GIT_REV}" STREQUAL "" OR "${GIT_BRANCH}" STREQUAL "")
0127     file(
0128         APPEND
0129         "${BINARY_DIR}/kbibtex-git-info.h.tmp"
0130         "#define KBIBTEX_GIT_INFO_STRING \"\"\n"
0131     )
0132 else()
0133     file(
0134         APPEND
0135         "${BINARY_DIR}/kbibtex-git-info.h.tmp"
0136         "#define KBIBTEX_GIT_INFO_STRING \"${GIT_REV} (${GIT_BRANCH}, ${GIT_COMMIT_COUNT} commits in history)\"\n"
0137     )
0138 endif()
0139 file(
0140     APPEND
0141     "${BINARY_DIR}/kbibtex-git-info.h.tmp"
0142     "#endif // KBIBTEX_GIT_INFO_H\n"
0143 )
0144 
0145 if(
0146     EXISTS
0147     "${BINARY_DIR}/kbibtex-git-info.h.tmp"
0148 )
0149     execute_process(
0150         COMMAND
0151         ${CMAKE_COMMAND}
0152         -E
0153         copy_if_different
0154         "kbibtex-git-info.h.tmp"
0155         "kbibtex-git-info.h"
0156         WORKING_DIRECTORY
0157         "${BINARY_DIR}"
0158     )
0159     execute_process(
0160         COMMAND
0161         ${CMAKE_COMMAND}
0162         -E
0163         remove
0164         "kbibtex-git-info.h.tmp"
0165         WORKING_DIRECTORY
0166         "${BINARY_DIR}"
0167     )
0168 else()
0169     message(
0170         STATUS
0171         "${BINARY_DIR}/kbibtex-git-info.h.tmp does not exist"
0172     )
0173 endif()