feat(core): added vresion tracking

This commit is contained in:
maelstrom 2025-07-19 04:13:52 +02:00
parent 259c14294c
commit 4b6fa1d3ff
4 changed files with 60 additions and 1 deletions

40
cmake/gitversion.cmake Normal file
View file

@ -0,0 +1,40 @@
# https://jonathanhamberg.com/post/cmake-embedding-git-hash/
# Detect current version from git
execute_process(
COMMAND git rev-parse HEAD
OUTPUT_VARIABLE GIT_COMMIT_HASH RESULT_VARIABLE GIT_RESULT OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
execute_process(
COMMAND git describe --abbrev=0
OUTPUT_VARIABLE GIT_VERSION RESULT_VARIABLE GIT_RESULT OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
execute_process(
COMMAND git describe --dirty
OUTPUT_VARIABLE GIT_VERSION_LONG RESULT_VARIABLE GIT_RESULT OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
# For some reason, CMake sets CMAKE_*_DIR all to be CMAKE_CURRENT_BINARY_DIR
# so we have to bypass this by passing in custom "orig_" variables
if (NOT GIT_STATE_WITHIN)
# Re-run this target always so that the version can be checked
add_custom_target(recheck_git_version ALL COMMAND ${CMAKE_COMMAND}
-DGIT_STATE_WITHIN=1
-DORIG_BINARY_DIR=${CMAKE_BINARY_DIR}
-DORIG_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}
-DORIG_SOURCE_DIR=${CMAKE_SOURCE_DIR}
-DORIG_CURRENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}
-P ${CMAKE_MODULE_PATH}/gitversion.cmake
BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/src/version.cpp
)
else ()
# # Set defaults if the git commands fail
if (NOT GIT_RESULT EQUAL 0)
set(GIT_COMMIT_HASH "unknown")
set(GIT_VERSION "unknown")
set(GIT_VERSION_LONG "unknown")
endif ()
# configure_file only touches the file if it has been changed, so no caching is necessary
configure_file(${ORIG_CURRENT_SOURCE_DIR}/src/version.cpp.in ${ORIG_CURRENT_BINARY_DIR}/src/version.cpp @ONLY)
endif ()

View file

@ -17,7 +17,7 @@ find_package(PkgConfig REQUIRED)
pkg_check_modules(LUAJIT REQUIRED luajit)
link_directories(${LUAJIT_LIBRARY_DIRS})
# Run autogen
### Autogen
file(GLOB_RECURSE AUTOGEN_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/src" "src/objects/*.h" "src/datatypes/*.h" "src/enum/*.h")
# https://cmake.org/cmake/help/book/mastering-cmake/chapter/Custom%20Commands.html
@ -36,12 +36,18 @@ foreach (SRC ${AUTOGEN_SOURCES})
list(APPEND AUTOGEN_OUTS "${OUT_PATH}")
endforeach()
### /Autogen
# Add version info into the build
include(gitversion)
add_custom_target(autogen_build ALL
DEPENDS ${AUTOGEN_OUTS}
)
file(GLOB_RECURSE SOURCES "src/*.cpp" "src/*.h")
list(APPEND SOURCES ${AUTOGEN_OUTS})
list(APPEND SOURCES ${CMAKE_CURRENT_BINARY_DIR}/src/version.cpp)
add_library(openblocks STATIC ${SOURCES})
set_target_properties(openblocks PROPERTIES OUTPUT_NAME "openblocks")
target_link_directories(openblocks PUBLIC ${LUAJIT_LIBRARY_DIRS})

5
core/src/version.cpp.in Normal file
View file

@ -0,0 +1,5 @@
#include "version.h"
const char* BUILD_COMMIT_HASH = "@GIT_COMMIT_HASH@"; // Commit hash of the current build
const char* BUILD_VERSION = "@GIT_VERSION@"; // Short form of the build version v1.2.3
const char* BUILD_VERSION_LONG = "@GIT_VERSION_LONG@"; // Long form of the build version v1.2.3-12-g1234567

8
core/src/version.h Normal file
View file

@ -0,0 +1,8 @@
#pragma once
// Allows files to read the version of the current build from git
// https://jonathanhamberg.com/post/cmake-embedding-git-hash/
extern const char* BUILD_COMMIT_HASH; // Commit hash of the current build
extern const char* BUILD_VERSION; // Short form of the build version v1.2.3
extern const char* BUILD_VERSION_LONG; // Long form of the build version v1.2.3-12-g1234567