Generate version via git describe

This commit is contained in:
2026-08-17 15:15:13 +02:00
parent 6112f0375f
commit b657239e12
8 changed files with 42 additions and 4 deletions
+3
View File
@@ -7,6 +7,9 @@
*.o
*.obj
# Generated headers
version.h
# Precompiled Headers
*.gch
*.pch
+9 -1
View File
@@ -11,7 +11,7 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
include(FindROOT.cmake)
include(cmake/FindROOT.cmake)
# Find Geant4, activating all available UI and visualization drivers by default.
option(WITH_GEANT4_UIVIS "Build with Geant4 UI and visualization drivers" ON)
@@ -21,6 +21,12 @@ else()
find_package(Geant4 REQUIRED)
endif()
add_custom_target(version
${CMAKE_COMMAND} -D SRC=${CMAKE_SOURCE_DIR}/version.h.in
-D DST=${CMAKE_SOURCE_DIR}/version.h
-P ${CMAKE_SOURCE_DIR}/cmake/GenerateVersion.cmake
)
add_executable(musrSim
musrSim.cc
src/F04ElementField.cc
@@ -54,6 +60,8 @@ add_executable(musrSim
src/yields.cc
)
add_dependencies(musrSim version)
target_include_directories(musrSim
PRIVATE
${PROJECT_SOURCE_DIR}/include
+21
View File
@@ -0,0 +1,21 @@
# Thanks to https://www.marcusfolkesson.se/blog/git-version-in-cmake/
find_package(Git)
if(GIT_EXECUTABLE)
get_filename_component(WORKING_DIR ${SRC} DIRECTORY)
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --tags --dirty
WORKING_DIRECTORY ${WORKING_DIR}
OUTPUT_VARIABLE MUSRSIM_VERSION
RESULT_VARIABLE ERROR_CODE
OUTPUT_STRIP_TRAILING_WHITESPACE
)
endif()
if(FOO_VERSION STREQUAL "")
set(FOO_VERSION 0.0.0-unknown)
message(WARNING "Failed to determine version from Git tags. Using default version \"${FOO_VERSION}\".")
endif()
configure_file(${SRC} ${DST} @ONLY)
+7 -3
View File
@@ -1,3 +1,5 @@
#include "version.h"
#include "musrDetectorConstruction.hh"
#include "musrPhysicsList.hh"
#include "musrPrimaryGeneratorAction.hh"
@@ -33,6 +35,11 @@
int main(int argc, char **argv)
{
G4String steeringFileName;
G4cout << "\n\n*************************************************************" << G4endl;
G4cout << " musrSim version " << MUSRSIM_VERSION << " for Geant 11.4" << G4endl;
G4cout << " WWW: https://www.psi.ch/lmu/geant4-simulations\n" << G4endl;
if (argc > 1) {
steeringFileName = argv[1];
} else {
@@ -40,9 +47,6 @@ int main(int argc, char **argv)
return 1;
}
G4cout << "\n\n*************************************************************" << G4endl;
G4cout << " musrSim version 1.0.6 for Geant 11.4" << G4endl;
G4cout << " WWW: https://www.psi.ch/lmu/geant4-simulations" << G4endl;
// choose the Random engine
G4Random::setTheEngine(new CLHEP::HepJamesRandom);
+2
View File
@@ -0,0 +1,2 @@
#pragma once
#define MUSRSIM_VERSION "@MUSRSIM_VERSION@"