myx-cmake/MyxCMake/modules/MyxCMakeGenerateGitInfoHeader.cmake

86 lines
3.5 KiB
CMake

include_guard(GLOBAL)
function(myx_cmake_generate_git_info_header)
set(_filename ${CMAKE_BINARY_DIR}/${CMAKE_INCLUDE_DIR}/${MYX_CMAKE_PROJECT_NAME_LOWER}/myx_cmake_git_info_p.hpp)
if(MYX_CMAKE_GENERATED_HEADERS_PATH)
set(_filename ${MYX_CMAKE_GENERATED_HEADERS_PATH}/${MYX_CMAKE_PROJECT_NAME_LOWER}/myx_cmake_git_info_p.hpp)
elseif(ARGV0)
set(_filename ${ARGV0})
endif()
set(_git_info_hpp_in
"#ifndef @MYX_CMAKE_PROJECT_NAME_UPPER@_MYX_CMAKE_GIT_INFO_HPP_
#define @MYX_CMAKE_PROJECT_NAME_UPPER@_MYX_CMAKE_GIT_INFO_HPP_
#pragma once
#if defined (@MYX_CMAKE_PROJECT_NAME_UPPER@_GIT_REV)
#error \"Duplicate definition of macros @MYX_CMAKE_PROJECT_NAME_UPPER@_GIT_REV\"
#else
#define @MYX_CMAKE_PROJECT_NAME_UPPER@_GIT_REV \"@MYX_CMAKE_GIT_REV@\"
#endif
#if defined (@MYX_CMAKE_PROJECT_NAME_UPPER@_GIT_DIFF)
#error \"Duplicate definition of macros @MYX_CMAKE_PROJECT_NAME_UPPER@_GIT_DIFF\"
#else
#define @MYX_CMAKE_PROJECT_NAME_UPPER@_GIT_DIFF \"@MYX_CMAKE_GIT_DIFF@\"
#endif
#if defined (@MYX_CMAKE_PROJECT_NAME_UPPER@_GIT_BRANCH)
#error \"Duplicate definition of macros @MYX_CMAKE_PROJECT_NAME_UPPER@_GIT_BRANCH\"
#else
#define @MYX_CMAKE_PROJECT_NAME_UPPER@_GIT_BRANCH \"@MYX_CMAKE_GIT_BRANCH@\"
#endif
#if defined (@MYX_CMAKE_PROJECT_NAME_UPPER@_GIT_TAG)
#error \"Duplicate definition of macros @MYX_CMAKE_PROJECT_NAME_UPPER@_GIT_TAG\"
#else
#define @MYX_CMAKE_PROJECT_NAME_UPPER@_GIT_TAG \"@MYX_CMAKE_GIT_TAG@\"
#endif
#endif /* @MYX_CMAKE_PROJECT_NAME_UPPER@_MYX_CMAKE_GIT_INFO_HPP_ */
")
set(_git_info_hpp_in_path ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/myx_cmake_git_info_p.hpp.in)
file(WRITE ${_git_info_hpp_in_path} ${_git_info_hpp_in})
set(_git_info_cmake_in
"set(MYX_CMAKE_GIT_REV \"N/A\")
set(MYX_CMAKE_GIT_DIFF \"\")
set(MYX_CMAKE_GIT_TAG \"N/A\")
set(MYX_CMAKE_GIT_BRANCH \"N/A\")
find_program(GIT_EXECUTABLE git)
if(GIT_EXECUTABLE)
execute_process(COMMAND git log --pretty=format:'%h' -n 1 OUTPUT_VARIABLE MYX_CMAKE_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 ${MYX_CMAKE_GIT_REV} STREQUAL \"\")
execute_process(COMMAND bash -c \"git diff --quiet --exit-code || echo +\" OUTPUT_VARIABLE MYX_CMAKE_GIT_DIFF)
execute_process(COMMAND git describe --exact-match --tags OUTPUT_VARIABLE MYX_CMAKE_GIT_TAG ERROR_QUIET)
execute_process(COMMAND git rev-parse --abbrev-ref HEAD OUTPUT_VARIABLE MYX_CMAKE_GIT_BRANCH)
string(STRIP \"${MYX_CMAKE_GIT_REV}\" MYX_CMAKE_GIT_REV)
string(SUBSTRING \"${MYX_CMAKE_GIT_REV}\" 1 7 MYX_CMAKE_GIT_REV)
string(STRIP \"${MYX_CMAKE_GIT_DIFF}\" MYX_CMAKE_GIT_DIFF)
string(STRIP \"${MYX_CMAKE_GIT_TAG}\" MYX_CMAKE_GIT_TAG)
string(STRIP \"${MYX_CMAKE_GIT_BRANCH}\" MYX_CMAKE_GIT_BRANCH)
endif()
endif()
configure_file(${_git_info_hpp_in_path} ${MYX_CMAKE_GIT_INFO_FILE})
")
set(_git_info_cmake_in_path ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/MyxCMakeGitInfo.cmake.in)
file(WRITE ${_git_info_cmake_in_path} ${_git_info_cmake_in})
# cmake-format: off
if(NOT TARGET myx-cmake-git-info-header-${MYX_CMAKE_PROJECT_NAME_LOWER})
add_custom_target(myx-cmake-git-info-header-${MYX_CMAKE_PROJECT_NAME_LOWER} ALL
${CMAKE_COMMAND} -DMYX_CMAKE_PROJECT_NAME_UPPER=${MYX_CMAKE_PROJECT_NAME_UPPER}
-DMYX_CMAKE_GIT_INFO_FILE=${_filename} -P ${_git_info_cmake_in_path}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
endif()
# cmake-format: on
endfunction()