From f443c87cf9745f433cb638a1b350c106d0e6597e Mon Sep 17 00:00:00 2001 From: Cat Flynn Date: Tue, 25 Jul 2023 23:39:26 +0200 Subject: [PATCH] feat: include version in built executable --- CMakeLists.txt | 19 +++++++++++++++ add_git_version_to_executable.cmake | 36 +++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 add_git_version_to_executable.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 001e9da..d14d213 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,3 +48,22 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/vert.glsl ${CMAKE_CURRENT_BINARY_DIR}/vert.glsl COPYONLY) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/frag.glsl ${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=$ + -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() diff --git a/add_git_version_to_executable.cmake b/add_git_version_to_executable.cmake new file mode 100644 index 0000000..2db7fa9 --- /dev/null +++ b/add_git_version_to_executable.cmake @@ -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() +