Генерация заголовочного файла с информацией о репозитории git

This commit is contained in:
Andrei Astafev 2021-06-17 15:44:08 +03:00
parent 6c6f48c468
commit 5651a4767e
4 changed files with 74 additions and 0 deletions

View File

@ -50,6 +50,7 @@ include(CMLibDocDoxygen)
include(CMLibDocBreathe) include(CMLibDocBreathe)
include(CMLibQtTranslation) include(CMLibQtTranslation)
include(CMLibToday) include(CMLibToday)
include(CMLibGit)
include(CMLibAuxilarySymlinks) include(CMLibAuxilarySymlinks)
include(CMLibGeneratePrivateConfigHPP) include(CMLibGeneratePrivateConfigHPP)
include(CMLibNinjaGeneratorHelper) include(CMLibNinjaGeneratorHelper)

14
CMLibGit.cmake Normal file
View File

@ -0,0 +1,14 @@
set(output_file ${CMAKE_BINARY_DIR}/include/cmlib_git_version.hpp)
if(CMLIB_GENERATED_HEADERS_PATH)
set(output_file ${CMLIB_GENERATED_HEADERS_PATH}/cmlib_git_version.hpp)
elseif(ARGV0)
set(output_file ${ARGV0})
endif()
add_custom_target(git-version ALL ${CMAKE_COMMAND}
-DCMLIB_PROJECT_NAME_UPPER=${CMLIB_PROJECT_NAME_UPPER}
-DCMLIB_MODULE_DIR=${CMLIB_MODULE_DIR}
-DCMLIB_GIT_VERSION_FILE=${output_file} -P ${CMLIB_MODULE_DIR}/CMLibGitVersion.cmake
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
unset(output_file)

28
CMLibGitVersion.cmake Normal file
View File

@ -0,0 +1,28 @@
set(CMLIB_GIT_REV "N/A")
set(CMLIB_GIT_DIFF "")
set(CMLIB_GIT_TAG "N/A")
set(CMLIB_GIT_BRANCH "N/A")
message("aaaaaa")
find_program(GIT_EXECUTABLE git)
if(GIT_EXECUTABLE)
execute_process(COMMAND git log --pretty=format:'%h' -n 1 OUTPUT_VARIABLE CMLIB_GIT_REV ERROR_QUIET)
# Check whether we got any revision (which isn't
# always the case, e.g. when someone downloaded a zip
# file from Github instead of a checkout)
if(NOT "${CMLIB_GIT_REV}" STREQUAL "")
execute_process(COMMAND bash -c "git diff --quiet --exit-code || echo +" OUTPUT_VARIABLE CMLIB_GIT_DIFF)
execute_process(COMMAND git describe --exact-match --tags OUTPUT_VARIABLE CMLIB_GIT_TAG ERROR_QUIET)
execute_process(COMMAND git rev-parse --abbrev-ref HEAD OUTPUT_VARIABLE CMLIB_GIT_BRANCH)
string(STRIP "${CMLIB_GIT_REV}" CMLIB_GIT_REV)
string(SUBSTRING "${CMLIB_GIT_REV}" 1 8 CMLIB_GIT_REV)
string(STRIP "${CMLIB_GIT_DIFF}" CMLIB_GIT_DIFF)
string(STRIP "${CMLIB_GIT_TAG}" CMLIB_GIT_TAG)
string(STRIP "${CMLIB_GIT_BRANCH}" CMLIB_GIT_BRANCH)
endif()
endif()
configure_file(${CMLIB_MODULE_DIR}/hpp/cmlib_git_version.hpp.in ${CMLIB_GIT_VERSION_FILE})

View File

@ -0,0 +1,31 @@
#ifndef @CMLIB_PROJECT_NAME_UPPER@_CMLIB_GIT_VERSION_HPP_
#define @CMLIB_PROJECT_NAME_UPPER@_CMLIB_GIT_VERSION_HPP_
#pragma once
#if defined (@CMLIB_PROJECT_NAME_UPPER@_GIT_REV)
#error "Duplicate definition of macros @CMLIB_PROJECT_NAME_UPPER@_GIT_REV"
#else
#define @CMLIB_PROJECT_NAME_UPPER@_GIT_REV "@CMLIB_GIT_REV@"
#endif
#if defined (@CMLIB_PROJECT_NAME_UPPER@_GIT_DIFF)
#error "Duplicate definition of macros @CMLIB_PROJECT_NAME_UPPER@_GIT_DIFF"
#else
#define @CMLIB_PROJECT_NAME_UPPER@_GIT_DIFF "@CMLIB_GIT_DIFF@"
#endif
#if defined (@CMLIB_PROJECT_NAME_UPPER@_GIT_BRANCH)
#error "Duplicate definition of macros @CMLIB_PROJECT_NAME_UPPER@_GIT_BRANCH"
#else
#define @CMLIB_PROJECT_NAME_UPPER@_GIT_BRANCH "@CMLIB_GIT_BRANCH@"
#endif
#if defined (@CMLIB_PROJECT_NAME_UPPER@_GIT_TAG)
#error "Duplicate definition of macros @CMLIB_PROJECT_NAME_UPPER@_GIT_TAG"
#else
#define @CMLIB_PROJECT_NAME_UPPER@_GIT_TAG "@CMLIB_GIT_TAG@"
#endif
#endif /* @CMLIB_PROJECT_NAME_CANONICAL@_CMLIB_GIT_VERSION_HPP_ */