feat: include version in built executable

This commit is contained in:
Cat Flynn 2023-07-25 23:39:26 +02:00 committed by ktyl
parent c73f7f74db
commit f443c87cf9
2 changed files with 55 additions and 0 deletions

View File

@ -48,3 +48,22 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/vert.glsl
${CMAKE_CURRENT_BINARY_DIR}/vert.glsl COPYONLY) ${CMAKE_CURRENT_BINARY_DIR}/vert.glsl COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/frag.glsl configure_file(${CMAKE_CURRENT_SOURCE_DIR}/frag.glsl
${CMAKE_CURRENT_BINARY_DIR}/frag.glsl COPYONLY) ${CMAKE_CURRENT_BINARY_DIR}/frag.glsl COPYONLY)
# Append Git version to built executable after it has been built
find_package(Git)
if (GIT_FOUND)
if (WIN32)
set(TARGET_EXTENSION ".exe")
elseif(UNIX)
set(TARGET_EXTENSION "")
endif()
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND}
-DTARGET_DIR=$<TARGET_FILE_DIR:${PROJECT_NAME}>
-DPROJECT_NAME=${PROJECT_NAME}
-DTARGET_EXTENSION=${TARGET_EXTENSION}
-P ${CMAKE_CURRENT_SOURCE_DIR}/add_git_version_to_executable.cmake
)
else()
message("Git not found - will not rename the exectuable.")
endif()

View File

@ -0,0 +1,36 @@
cmake_minimum_required(VERSION 3.27)
set(CMAKE_VERBOSE_MAKEFILE ON)
# Original path to the target executable
set(TARGET_PATH ${TARGET_DIR}/${PROJECT_NAME})
# The name of the target we're renaming
set(TARGET "${TARGET_PATH}${TARGET_EXTENSION}")
# The command to get the version string from Git
find_package(Git REQUIRED)
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --always --dirty
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if (NOT GIT_VERSION)
message(FATAL_ERROR "Could not get the Git version")
else()
# New path containing the Git version
set(VERSIONED_PATH ${TARGET_PATH}-${GIT_VERSION})
# Rename the target to include the version string
execute_process(
COMMAND ${CMAKE_COMMAND} -E rename ${TARGET} "${VERSIONED_PATH}${TARGET_EXTENSION}"
)
# Rename program database file created by VS
if (EXISTS ${TARGET_PATH}.pdb)
file(RENAME "${TARGET_PATH}.pdb" "${VERSIONED_PATH}.pdb")
endif()
endif()