diff --git a/CMLibCommon.cmake b/CMLibCommon.cmake index 1851fd0..53d088d 100644 --- a/CMLibCommon.cmake +++ b/CMLibCommon.cmake @@ -50,6 +50,7 @@ include(CMLibDocDoxygen) include(CMLibDocBreathe) include(CMLibQtTranslation) include(CMLibToday) +include(CMLibGit) include(CMLibAuxilarySymlinks) include(CMLibGeneratePrivateConfigHPP) include(CMLibNinjaGeneratorHelper) diff --git a/CMLibGit.cmake b/CMLibGit.cmake new file mode 100644 index 0000000..7733432 --- /dev/null +++ b/CMLibGit.cmake @@ -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) diff --git a/CMLibGitVersion.cmake b/CMLibGitVersion.cmake new file mode 100644 index 0000000..880c071 --- /dev/null +++ b/CMLibGitVersion.cmake @@ -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}) diff --git a/hpp/cmlib_git_version.hpp.in b/hpp/cmlib_git_version.hpp.in new file mode 100644 index 0000000..bfcba46 --- /dev/null +++ b/hpp/cmlib_git_version.hpp.in @@ -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_ */ +