diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2024-10-03 09:28:01 -0500 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2024-10-03 09:28:01 -0500 |
commit | b50980ad70684530d55b7adf20de6047ebf53ba2 (patch) | |
tree | 576d284c732e2b5bfc7638a23d9bca4c86f5437c /cmake | |
parent | e420eca2b244c303af51534ab09632045a186b21 (diff) |
Use a configure file for versioning including git revision.
Diffstat (limited to 'cmake')
-rw-r--r-- | cmake/CheckGit.cmake | 84 | ||||
-rw-r--r-- | cmake/git_version.hpp.in | 15 |
2 files changed, 99 insertions, 0 deletions
diff --git a/cmake/CheckGit.cmake b/cmake/CheckGit.cmake new file mode 100644 index 0000000..bd17447 --- /dev/null +++ b/cmake/CheckGit.cmake @@ -0,0 +1,84 @@ +set(CURRENT_LIST_DIR ${CMAKE_CURRENT_LIST_DIR}) +if (NOT DEFINED pre_configure_dir) + set(pre_configure_dir ${CMAKE_CURRENT_LIST_DIR}) +endif () + +if (NOT DEFINED post_configure_dir) + set(post_configure_dir ${CMAKE_BINARY_DIR}/include) +endif () + +set(pre_configure_file ${pre_configure_dir}/git_version.hpp.in) +set(post_configure_file ${post_configure_dir}/git_version.hpp) + +function(CheckGitWrite git_hash) + file(WRITE ${CMAKE_BINARY_DIR}/git-state.txt ${git_hash}) +endfunction() + +function(CheckGitRead git_hash) + if (EXISTS ${CMAKE_BINARY_DIR}/git-state.txt) + file(STRINGS ${CMAKE_BINARY_DIR}/git-state.txt CONTENT) + LIST(GET CONTENT 0 var) + + set(${git_hash} ${var} PARENT_SCOPE) + endif () +endfunction() + +function(CheckGitVersion git_hash) + # Get the latest abbreviated commit hash of the working branch + execute_process( + COMMAND git log -1 --format=%H + WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} + OUTPUT_VARIABLE GIT_HASH + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + + CheckGitRead(GIT_HASH_CACHE) + if (NOT EXISTS ${post_configure_dir}) + file(MAKE_DIRECTORY ${post_configure_dir}) + endif () + + # if (NOT EXISTS ${post_configure_dir}/git_version.h) + # file(COPY ${pre_configure_dir}/git_version.h DESTINATION ${post_configure_dir}) + # endif() + + if (NOT DEFINED GIT_HASH_CACHE) + set(GIT_HASH_CACHE "INVALID") + endif () + + # Only update the git_version.cpp if the hash has changed. This will + # prevent us from rebuilding the project more than we need to. + if (NOT ${GIT_HASH} STREQUAL ${GIT_HASH_CACHE} OR NOT EXISTS ${post_configure_file}) + # Set che GIT_HASH_CACHE variable the next build won't have + # to regenerate the source file. + CheckGitWrite(${GIT_HASH}) + + configure_file(${pre_configure_file} ${post_configure_file} @ONLY) + endif () + set(${git_hash} ${GIT_HASH} PARENT_SCOPE) + + +endfunction() + +function(CheckGitSetup top_git_hash) + + add_custom_target(AlwaysCheckGit COMMAND ${CMAKE_COMMAND} + -DRUN_CHECK_GIT_VERSION=1 + -Dpre_configure_dir=${pre_configure_dir} + -Dpost_configure_file=${post_configure_dir} + -DGIT_HASH_CACHE=${GIT_HASH_CACHE} + -P ${CURRENT_LIST_DIR}/CheckGit.cmake + BYPRODUCTS ${post_configure_file} + ) + + add_library(git_version INTERFACE ${CMAKE_BINARY_DIR}/include/git_version.hpp) + add_dependencies(git_version AlwaysCheckGit) + + CheckGitVersion(git_hash) + set(${top_git_hash} ${git_hash} PARENT_SCOPE) +endfunction() + +# This is used to run this function from an external cmake process. +if (RUN_CHECK_GIT_VERSION) + CheckGitVersion(git_hash) + set(${GIT_HASH} ${git_hash}) +endif () diff --git a/cmake/git_version.hpp.in b/cmake/git_version.hpp.in new file mode 100644 index 0000000..ede9623 --- /dev/null +++ b/cmake/git_version.hpp.in @@ -0,0 +1,15 @@ +#ifndef GIT_VERSION_HPP +#define GIT_VERSION_HPP + +#include <cstdint> + +namespace fud { + +constexpr uint8_t FudVersionMajor = @PROJECT_VERSION_MAJOR@; +constexpr uint8_t FudVersionMinor = @PROJECT_VERSION_MINOR@; +constexpr uint8_t FudVersionPatch = @PROJECT_VERSION_PATCH@; +constexpr const char GitHash[] = "@GIT_HASH@"; + +} // namespace fud + +#endif |