From f8ba7bafecb2a8f9e22e0ac2f5770a56ef0af8a1 Mon Sep 17 00:00:00 2001 From: Jose Diaz Rohena Date: Thu, 22 Feb 2024 22:07:13 +0100 Subject: [PATCH] CHERRYPICK_ME generate version info based on commit hash --- CMakeLists.txt | 22 +++++++++++++++++++++- version.h.in | 2 ++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 version.h.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 9463b911..b9cfca92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,25 @@ set(CMAKE_OSX_DEPLOYMENT_TARGET "10.13" CACHE STRING "Support macOS down to High # Be sure the file has no newlines file(STRINGS VERSION CURRENT_VERSION) +# Find Git and if possible, get the current commit hash +find_package(Git QUIET) +if(GIT_FOUND) + execute_process( + COMMAND ${GIT_EXECUTABLE} rev-parse HEAD + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + OUTPUT_VARIABLE GIT_COMMIT_HASH + OUTPUT_STRIP_TRAILING_WHITESPACE + ) +else() + set(GIT_COMMIT_HASH "unknown") +endif() + +# Configure a header file to pass the Git commit hash to the source code +configure_file( + ${CMAKE_SOURCE_DIR}/version.h.in + ${CMAKE_BINARY_DIR}/generated/version.h +) + # For simplicity, the name of the project is also the name of the target project(${PROJECT_NAME} VERSION ${CURRENT_VERSION}) @@ -198,7 +217,8 @@ target_compile_definitions("${PROJECT_NAME}" target_include_directories("${PROJECT_NAME}" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src" - "${CMAKE_CURRENT_SOURCE_DIR}/libs") + "${CMAKE_CURRENT_SOURCE_DIR}/libs" + "${CMAKE_BINARY_DIR}") target_link_libraries("${PROJECT_NAME}" PRIVATE diff --git a/version.h.in b/version.h.in new file mode 100644 index 00000000..e73a03cc --- /dev/null +++ b/version.h.in @@ -0,0 +1,2 @@ +#pragma once +#define BUILD_ID "@GIT_COMMIT_HASH@" \ No newline at end of file