Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake: get version from git, or git archive #207

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ jobs:

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: true

- name: Build project
run: |
git config --global --add safe.directory $(pwd) # make git work inside container.
mkdir build
cd build
cmake -DRMG_BUILD_EXAMPLES=1 -DBUILD_TESTING=ON -DRMG_BUILD_DOCS=OFF ..
Expand Down
7 changes: 5 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/RMGGitVersion.cmake)

project(
remage
VERSION 0.6.2 # TODO: get this dynamically
VERSION "${RMG_GIT_VERSION}"
DESCRIPTION "Simulation framework for HPGe-based experiments"
LANGUAGES C CXX) # C is needed for Geant4's HDF5 support

message(STATUS "remage version ${CMAKE_PROJECT_VERSION}")
message(STATUS "remage version ${CMAKE_PROJECT_VERSION} (${RMG_GIT_VERSION_FULL})")

if(WIN32)
message(FATAL_ERROR "remage is not supported on Windows")
Expand Down
1 change: 1 addition & 0 deletions cmake/RMGConfig.hh.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// G4GEOM_USE_USOLIDS
// G4MULTITHREADED
#define RMG_PROJECT_VERSION "@CMAKE_PROJECT_VERSION@"
#define RMG_PROJECT_VERSION_FULL "@RMG_GIT_VERSION_FULL@"
#define RMG_HAS_ROOT @ROOT_FOUND@
#define RMG_HAS_BXDECAY0 @BxDecay0_FOUND@
#define RMG_HAS_BXDECAY0_THREADSAFE @BxDecay0_THREADSAFE@
Expand Down
37 changes: 37 additions & 0 deletions cmake/RMGGitVersion.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
set(RMG_GIT_VERSION "v0.0.0")

# try to get the special value replaced by git archive (if this is an archive, after all).
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/.git_archival.txt" GIT_ARCHIVAL)

while(GIT_ARCHIVAL)
list(POP_FRONT GIT_ARCHIVAL GIT_LINE)
if(GIT_LINE MATCHES "^describe-name: [^$]")
string(SUBSTRING ${GIT_LINE} 15 -1 RMG_GIT_VERSION)
endif()
endwhile()

# try to get the tag ourselves from git describe, if we did not have it set above
# (i.e. we are not in a git-created archive).
if(${RMG_GIT_VERSION} STREQUAL "v0.0.0" AND IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/.git")
find_package(Git)
if(Git_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --tags --match "*[0-9]*"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_OUTPUT
RESULT_VARIABLE GIT_STATUS
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(GIT_STATUS EQUAL 0)
set(RMG_GIT_VERSION ${GIT_OUTPUT})
endif()
endif()
endif()

# cleanup git describe output to match cmake's expectations.
string(REGEX REPLACE "^v" "" RMG_GIT_VERSION ${RMG_GIT_VERSION})
set(RMG_GIT_VERSION_FULL ${RMG_GIT_VERSION})
string(REGEX REPLACE "-.*$" "" RMG_GIT_VERSION ${RMG_GIT_VERSION})

if(${RMG_GIT_VERSION} STREQUAL "0.0.0")
message(FATAL_ERROR "failed to find version from git or git archive")
endif()
2 changes: 1 addition & 1 deletion src/RMGLog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ bool RMGLog::fHadError = false;

bool RMGLog::fUsePrefix = true;

std::string RMGLog::fVersion = RMG_PROJECT_VERSION;
std::string RMGLog::fVersion = RMG_PROJECT_VERSION_FULL;

// initialize them at start of program - mandatory
// so that even if user redirects, we've got a copy
Expand Down
Loading