From 50bc89bab17de2a954d9acfff957b279aede4889 Mon Sep 17 00:00:00 2001 From: Dmitry Kalinkin Date: Tue, 23 Jul 2024 17:03:49 -0400 Subject: [PATCH] cmake: set CMAKE_PROJECT_VERSION from git, add it back to thisepic.sh (#758) ### Briefly, what does this PR introduce? This uses same code as in EICrecon. This partially undoes of #733, geometry version is useful. ### What kind of change does this PR introduce? - [ ] Bug fix (issue #__) - [x] New feature (issue #__) - [ ] Documentation update - [ ] Other: __ ### Please check if this PR fulfills the following: - [ ] Tests for the changes have been added - [ ] Documentation has been added / updated - [ ] Changes have been communicated to collaborators ### Does this PR introduce breaking changes? What changes might users need to make to their code? No ### Does this PR change default behavior? No --------- Co-authored-by: Wouter Deconinck --- CMakeLists.txt | 18 ++++++++++++------ cmake/git_version.cmake | 29 +++++++++++++++++++++++++++++ templates/epic.xml.jinja2 | 1 + templates/thisepic.sh.in | 1 + templates/version.xml.in | 4 ++++ 5 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 cmake/git_version.cmake create mode 100644 templates/version.xml.in diff --git a/CMakeLists.txt b/CMakeLists.txt index fa06a7105..49e5778d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,10 @@ PROJECT(epic DESCRIPTION "DD4hep Geometry Description of the EPIC Experiment" ) +# Set version based on git +include(cmake/git_version.cmake) +set_git_version(CMAKE_PROJECT_VERSION) + # C++ standard set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17 CACHE STRING "Set the C++ standard to be used") @@ -101,14 +105,16 @@ install(DIRECTORY calibrations/ ) #----------------------------------------------------------------------------------- -# Configure and install detector setup script +# Configure and install compact/version.xml # -execute_process( - COMMAND git rev-parse --abbrev-ref HEAD - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} - OUTPUT_VARIABLE GIT_BRANCH - OUTPUT_STRIP_TRAILING_WHITESPACE +configure_file(templates/version.xml.in ${CMAKE_CURRENT_BINARY_DIR}/version.xml @ONLY) +install(FILES ${CMAKE_CURRENT_BINARY_DIR}/version.xml + DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/compact ) + +#----------------------------------------------------------------------------------- +# Configure and install detector setup script +# configure_file(templates/thisepic.sh.in ${CMAKE_CURRENT_BINARY_DIR}/thisepic.sh @ONLY) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/thisepic.sh DESTINATION ${CMAKE_INSTALL_BINDIR} diff --git a/cmake/git_version.cmake b/cmake/git_version.cmake new file mode 100644 index 000000000..98e013cfe --- /dev/null +++ b/cmake/git_version.cmake @@ -0,0 +1,29 @@ +# Determine version from git describe cmake-lint: disable=C0103 +macro(set_git_version VERSION) + if(NOT Git_Found) + find_package(Git) + endif() + + if(GIT_EXECUTABLE) + # Generate a git-describe version string from Git repository tags + execute_process( + COMMAND ${GIT_EXECUTABLE} describe --tags --dirty --match "v*" + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + OUTPUT_VARIABLE GIT_DESCRIBE_VERSION + RESULT_VARIABLE GIT_DESCRIBE_ERROR_CODE + OUTPUT_STRIP_TRAILING_WHITESPACE) + if(NOT GIT_DESCRIBE_ERROR_CODE) + set(${VERSION} ${GIT_DESCRIBE_VERSION}) + endif() + endif() + + # Final fallback: Just use a bogus version string that is semantically older + # than anything else and spit out a warning to the developer. + if(NOT DEFINED ${VERSION}) + set(${VERSION} v0.0.0-unknown) + message( + WARNING + "Failed to determine VERSION from Git tags. Using default version \"${${VERSION}}\"." + ) + endif() +endmacro() diff --git a/templates/epic.xml.jinja2 b/templates/epic.xml.jinja2 index ad376206d..0472526f2 100644 --- a/templates/epic.xml.jinja2 +++ b/templates/epic.xml.jinja2 @@ -48,6 +48,7 @@ + diff --git a/templates/thisepic.sh.in b/templates/thisepic.sh.in index 9a6013f05..f3ef9fde5 100644 --- a/templates/thisepic.sh.in +++ b/templates/thisepic.sh.in @@ -3,6 +3,7 @@ export DETECTOR=@PROJECT_NAME@ export DETECTOR_PATH=@CMAKE_INSTALL_FULL_DATADIR@/@PROJECT_NAME@ export DETECTOR_CONFIG=${1:-@PROJECT_NAME@} +export DETECTOR_VERSION=@CMAKE_PROJECT_VERSION@ ## Warn is not the right name (this script is sourced, hence $1) if [[ "$(basename ${BASH_SOURCE[0]})" != "thisepic.sh" ]]; then diff --git a/templates/version.xml.in b/templates/version.xml.in new file mode 100644 index 000000000..6398540f7 --- /dev/null +++ b/templates/version.xml.in @@ -0,0 +1,4 @@ + + + +