myx/MyxCMake/lib/generators/GitInfoHeader.cmake

57 lines
2.3 KiB
CMake
Raw Normal View History

2022-10-03 12:06:53 +00:00
#[=======================================================================[.rst:
myx_generate_git_info_header
----------------------------------
Вспомогательная функция для автоматической генерации заголовочного
файла, содержащего информацию о текущем состоянии репозитория git::
2022-10-15 07:14:08 +00:00
myx_generate_git_info_header(TARGET_NAME BASE_FILENAME
2022-10-03 12:06:53 +00:00
[ PREFIX prefix ] )
2022-10-15 07:14:08 +00:00
Обязательные параметры: `TARGET_NAME` - имя цели, с которой связан
заголовочный файл, и `BASE_FILENAME` - имя генерируемого заголовочного
файла. Дополнительный аргумент `PREFIX` добавляет префикс к генерируемым
именам переменных.
2022-10-03 12:06:53 +00:00
#]=======================================================================]
include_guard(GLOBAL)
2022-10-06 08:10:43 +00:00
if(${CMAKE_VERSION} VERSION_LESS "3.17.0")
set(MYX_CMAKE_LIB_GENERATORS_DIR_BACKPORT "${CMAKE_CURRENT_LIST_DIR}")
endif()
2022-10-15 07:14:08 +00:00
function(myx_generate_git_info_header TARGET_NAME BASE_FILENAME)
2022-10-06 08:10:43 +00:00
if(${CMAKE_VERSION} VERSION_LESS "3.17.0")
set(CMAKE_CURRENT_FUNCTION_LIST_DIR ${MYX_CMAKE_LIB_GENERATORS_DIR_BACKPORT})
endif()
2022-10-03 12:06:53 +00:00
set(options)
set(oneValueArgs PREFIX)
set(multiValueArgs)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
2022-10-15 07:14:08 +00:00
set(__filename "${PROJECT_BINARY_DIR}/include/${BASE_FILENAME}")
2022-10-18 04:48:16 +00:00
file(APPEND ${__filename} "")
2022-10-03 12:06:53 +00:00
2022-10-15 07:14:08 +00:00
set(__prefix "")
2022-10-04 19:53:44 +00:00
if(ARG_PREFIX)
string(APPEND ARG_PREFIX "_")
2022-10-15 07:14:08 +00:00
string(REPLACE "-" "_" __prefix ${ARG_PREFIX})
2022-10-03 12:06:53 +00:00
endif()
# cmake-format: off
2022-10-15 07:14:08 +00:00
if(NOT TARGET ${TARGET_NAME}-git-info-header)
add_custom_target(${TARGET_NAME}-git-info-header ALL
${CMAKE_COMMAND} -DGIT_INFO_FILE=${__filename} -DPREFIX=${__prefix}
2022-10-06 08:10:43 +00:00
-P ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/GitInfo.cmake
2022-10-15 07:14:08 +00:00
BYPRODUCTS ${__filename}
2022-10-06 08:10:43 +00:00
WORKING_DIRECTORY ${CMAKE_CURRENT_FUNCTION_LIST_DIR})
2022-10-03 12:06:53 +00:00
endif()
# cmake-format: on
2022-10-15 07:14:08 +00:00
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY PRIVATE_HEADER_FILES ${__filename})
target_sources(${TARGET_NAME} PRIVATE ${__filename})
2022-10-21 00:15:34 +00:00
add_dependencies(${TARGET_NAME} ${TARGET_NAME}-git-info-header)
2022-10-03 12:06:53 +00:00
endfunction()