diff --git a/cmake/gitversion.cmake b/cmake/gitversion.cmake new file mode 100644 index 0000000..79a6bb7 --- /dev/null +++ b/cmake/gitversion.cmake @@ -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 () diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 11f26f7..6882117 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -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}) diff --git a/core/src/version.cpp.in b/core/src/version.cpp.in new file mode 100644 index 0000000..c722e4e --- /dev/null +++ b/core/src/version.cpp.in @@ -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 \ No newline at end of file diff --git a/core/src/version.h b/core/src/version.h new file mode 100644 index 0000000..54b7a49 --- /dev/null +++ b/core/src/version.h @@ -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 \ No newline at end of file