Skip to content

Commit a2a5c2a

Browse files
ahnaf-tahmid-chowdhuryshimwellgonukepaulromano
authored
Add Versioning Support from version.txt (#3140)
Co-authored-by: Jonathan Shimwell <mail@jshimwell.com> Co-authored-by: Paul Wilson <paul.wilson@wisc.edu> Co-authored-by: Paul Romano <paul.k.romano@gmail.com>
1 parent fefe825 commit a2a5c2a

File tree

10 files changed

+153
-44
lines changed

10 files changed

+153
-44
lines changed

.git_archival.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
commit: $Format:%H$
2+
commit-date: $Format:%cI$
3+
describe-name: $Format:%(describe:tags=true,match=*[0-9]*)$

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.git_archival.txt export-subst

.github/workflows/ci.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ jobs:
8787
RDMAV_FORK_SAFE: 1
8888

8989
steps:
90-
- uses: actions/checkout@v4
90+
- name: Checkout repository
91+
uses: actions/checkout@v4
9192
with:
92-
fetch-depth: 2
93+
fetch-depth: 0
9394

9495
- name: Set up Python ${{ matrix.python-version }}
9596
uses: actions/setup-python@v5

CMakeLists.txt

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
cmake_minimum_required(VERSION 3.16 FATAL_ERROR)
22
project(openmc C CXX)
33

4-
# Set version numbers
5-
set(OPENMC_VERSION_MAJOR 0)
6-
set(OPENMC_VERSION_MINOR 15)
7-
set(OPENMC_VERSION_RELEASE 1)
8-
set(OPENMC_VERSION ${OPENMC_VERSION_MAJOR}.${OPENMC_VERSION_MINOR}.${OPENMC_VERSION_RELEASE})
4+
# Set module path
5+
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules)
6+
7+
include(GetVersionFromGit)
8+
9+
# Output version information
10+
message(STATUS "OpenMC version: ${OPENMC_VERSION}")
11+
message(STATUS "OpenMC dev state: ${OPENMC_DEV_STATE}")
12+
message(STATUS "OpenMC commit hash: ${OPENMC_COMMIT_HASH}")
13+
message(STATUS "OpenMC commit count: ${OPENMC_COMMIT_COUNT}")
14+
15+
# Generate version.h
916
configure_file(include/openmc/version.h.in "${CMAKE_BINARY_DIR}/include/openmc/version.h" @ONLY)
1017

1118
# Setup output directories
1219
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
1320
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
1421
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
1522

16-
# Set module path
17-
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules)
18-
1923
# Enable correct usage of CXX_EXTENSIONS
2024
if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.22)
2125
cmake_policy(SET CMP0128 NEW)
@@ -240,8 +244,6 @@ endif()
240244
#===============================================================================
241245
# Update git submodules as needed
242246
#===============================================================================
243-
244-
find_package(Git)
245247
if(GIT_FOUND AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
246248
option(GIT_SUBMODULE "Check submodules during build" ON)
247249
if(GIT_SUBMODULE)
@@ -493,18 +495,6 @@ if (OPENMC_USE_MPI)
493495
target_compile_definitions(libopenmc PUBLIC -DOPENMC_MPI)
494496
endif()
495497

496-
# Set git SHA1 hash as a compile definition
497-
if(GIT_FOUND)
498-
execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
499-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
500-
RESULT_VARIABLE GIT_SHA1_SUCCESS
501-
OUTPUT_VARIABLE GIT_SHA1
502-
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
503-
if(GIT_SHA1_SUCCESS EQUAL 0)
504-
target_compile_definitions(libopenmc PRIVATE -DGIT_SHA1="${GIT_SHA1}")
505-
endif()
506-
endif()
507-
508498
# target_link_libraries treats any arguments starting with - but not -l as
509499
# linker flags. Thus, we can pass both linker flags and libraries together.
510500
target_link_libraries(libopenmc ${ldflags} ${HDF5_LIBRARIES} ${HDF5_HL_LIBRARIES}

cmake/Modules/GetVersionFromGit.cmake

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# GetVersionFromGit.cmake
2+
# Standalone script to retrieve versioning information from Git or .git_archival.txt.
3+
# Customizable for any project by setting variables before including this file.
4+
5+
# Configurable variables:
6+
# - VERSION_PREFIX: Prefix for version tags (default: "v").
7+
# - VERSION_SUFFIX: Suffix for version tags (default: "[~+-]([a-zA-Z0-9]+)").
8+
# - VERSION_REGEX: Regex to extract version (default: "(?[0-9]+\\.[0-9]+\\.[0-9]+)").
9+
# - ARCHIVAL_FILE: Path to .git_archival.txt (default: "${CMAKE_SOURCE_DIR}/.git_archival.txt").
10+
# - DESCRIBE_NAME_KEY: Key for describe name in .git_archival.txt (default: "describe-name: ").
11+
# - COMMIT_HASH_KEY: Key for commit hash in .git_archival.txt (default: "commit: ").
12+
13+
# Default Format Example:
14+
# 1.2.3 v1.2.3 v1.2.3-rc1
15+
16+
set(VERSION_PREFIX "v" CACHE STRING "Prefix used in version tags")
17+
set(VERSION_SUFFIX "[~+-]([a-zA-Z0-9]+)" CACHE STRING "Suffix used in version tags")
18+
set(VERSION_REGEX "?([0-9]+\\.[0-9]+\\.[0-9]+)" CACHE STRING "Regex for extracting version")
19+
set(ARCHIVAL_FILE "${CMAKE_SOURCE_DIR}/.git_archival.txt" CACHE STRING "Path to .git_archival.txt")
20+
set(DESCRIBE_NAME_KEY "describe-name: " CACHE STRING "Key for describe name in .git_archival.txt")
21+
set(COMMIT_HASH_KEY "commit: " CACHE STRING "Key for commit hash in .git_archival.txt")
22+
23+
24+
# Combine prefix and regex
25+
set(VERSION_REGEX_WITH_PREFIX "^${VERSION_PREFIX}${VERSION_REGEX}")
26+
27+
# Find Git
28+
find_package(Git)
29+
30+
# Attempt to retrieve version from Git
31+
if(EXISTS "${CMAKE_SOURCE_DIR}/.git" AND GIT_FOUND)
32+
message(STATUS "Using git describe for versioning")
33+
34+
# Extract the version string
35+
execute_process(
36+
COMMAND git describe --tags --dirty
37+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
38+
OUTPUT_VARIABLE VERSION_STRING
39+
OUTPUT_STRIP_TRAILING_WHITESPACE
40+
)
41+
42+
# Extract the commit hash
43+
execute_process(
44+
COMMAND git rev-parse HEAD
45+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
46+
OUTPUT_VARIABLE COMMIT_HASH
47+
OUTPUT_STRIP_TRAILING_WHITESPACE
48+
)
49+
else()
50+
message(STATUS "Using archival file for versioning: ${ARCHIVAL_FILE}")
51+
if(EXISTS "${ARCHIVAL_FILE}")
52+
file(READ "${ARCHIVAL_FILE}" ARCHIVAL_CONTENT)
53+
54+
# Extract the describe-name line
55+
string(REGEX MATCH "${DESCRIBE_NAME_KEY}([^\\n]+)" VERSION_STRING "${ARCHIVAL_CONTENT}")
56+
if(VERSION_STRING MATCHES "${DESCRIBE_NAME_KEY}(.*)")
57+
set(VERSION_STRING "${CMAKE_MATCH_1}")
58+
else()
59+
message(FATAL_ERROR "Could not extract version from ${ARCHIVAL_FILE}")
60+
endif()
61+
62+
# Extract the commit hash
63+
string(REGEX MATCH "${COMMIT_HASH_KEY}([a-f0-9]+)" COMMIT_HASH "${ARCHIVAL_CONTENT}")
64+
if(COMMIT_HASH MATCHES "${COMMIT_HASH_KEY}([a-f0-9]+)")
65+
set(COMMIT_HASH "${CMAKE_MATCH_1}")
66+
else()
67+
message(FATAL_ERROR "Could not extract commit hash from ${ARCHIVAL_FILE}")
68+
endif()
69+
else()
70+
message(FATAL_ERROR "Neither git describe nor ${ARCHIVAL_FILE} is available for versioning.")
71+
endif()
72+
endif()
73+
74+
# Ensure version string format
75+
if(VERSION_STRING MATCHES "${VERSION_REGEX_WITH_PREFIX}")
76+
set(VERSION_NO_SUFFIX "${CMAKE_MATCH_1}")
77+
else()
78+
message(FATAL_ERROR "Invalid version format: Missing base version in ${VERSION_STRING}")
79+
endif()
80+
81+
# Check for development state
82+
if(VERSION_STRING MATCHES "-([0-9]+)-g([0-9a-f]+)")
83+
set(DEV_STATE "true")
84+
set(COMMIT_COUNT "${CMAKE_MATCH_1}")
85+
string(REGEX REPLACE "-([0-9]+)-g([0-9a-f]+)" "" VERSION_WITHOUT_META "${VERSION_STRING}")
86+
else()
87+
set(DEV_STATE "false")
88+
set(VERSION_WITHOUT_META "${VERSION_STRING}")
89+
endif()
90+
91+
# Split and set version components
92+
string(REPLACE "." ";" VERSION_LIST "${VERSION_NO_SUFFIX}")
93+
list(GET VERSION_LIST 0 VERSION_MAJOR)
94+
list(GET VERSION_LIST 1 VERSION_MINOR)
95+
list(GET VERSION_LIST 2 VERSION_PATCH)
96+
97+
# Increment patch number for dev versions
98+
if(DEV_STATE)
99+
math(EXPR VERSION_PATCH "${VERSION_PATCH} + 1")
100+
endif()
101+
102+
# Export variables
103+
set(OPENMC_VERSION_MAJOR "${VERSION_MAJOR}")
104+
set(OPENMC_VERSION_MINOR "${VERSION_MINOR}")
105+
set(OPENMC_VERSION_PATCH "${VERSION_PATCH}")
106+
set(OPENMC_VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
107+
set(OPENMC_COMMIT_HASH "${COMMIT_HASH}")
108+
set(OPENMC_DEV_STATE "${DEV_STATE}")
109+
set(OPENMC_COMMIT_COUNT "${COMMIT_COUNT}")

docs/source/conf.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,14 @@
6868
# |version| and |release|, also used in various other places throughout the
6969
# built documents.
7070
#
71+
72+
import openmc
73+
7174
# The short X.Y version.
72-
version = "0.15"
75+
version = ".".join(openmc.__version__.split('.')[:2])
76+
7377
# The full version, including alpha/beta/rc tags.
74-
release = "0.15.1-dev"
78+
release = openmc.__version__
7579

7680
# The language for content autogenerated by Sphinx. Refer to documentation
7781
# for a list of supported languages.

include/openmc/version.h.in

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ namespace openmc {
99
// clang-format off
1010
constexpr int VERSION_MAJOR {@OPENMC_VERSION_MAJOR@};
1111
constexpr int VERSION_MINOR {@OPENMC_VERSION_MINOR@};
12-
constexpr int VERSION_RELEASE {@OPENMC_VERSION_RELEASE@};
13-
constexpr bool VERSION_DEV {true};
12+
constexpr int VERSION_RELEASE {@OPENMC_VERSION_PATCH@};
13+
constexpr bool VERSION_DEV {@OPENMC_DEV_STATE@};
14+
constexpr const char* VERSION_COMMIT_COUNT = "@OPENMC_COMMIT_COUNT@";
15+
constexpr const char* VERSION_COMMIT_HASH = "@OPENMC_COMMIT_HASH@";
1416
constexpr std::array<int, 3> VERSION {VERSION_MAJOR, VERSION_MINOR, VERSION_RELEASE};
1517
// clang-format on
1618

pyproject.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[build-system]
2-
requires = ["setuptools", "wheel"]
2+
requires = ["setuptools", "setuptools-scm", "wheel"]
33
build-backend = "setuptools.build_meta"
44

55
[project]
@@ -8,7 +8,7 @@ authors = [
88
{name = "The OpenMC Development Team", email = "openmc@anl.gov"},
99
]
1010
description = "OpenMC"
11-
version = "0.15.1-dev"
11+
dynamic = ["version"]
1212
requires-python = ">=3.11"
1313
license = {file = "LICENSE"}
1414
classifiers = [
@@ -66,3 +66,5 @@ exclude = ['tests*']
6666
"openmc.data.effective_dose" = ["**/*.txt"]
6767
"openmc.data" = ["*.txt", "*.DAT", "*.json", "*.h5"]
6868
"openmc.lib" = ["libopenmc.dylib", "libopenmc.so"]
69+
70+
[tool.setuptools_scm]

src/mcpl_interface.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ void write_mcpl_source_point(const char* filename, span<SourceSite> source_bank,
207207
if (mpi::master) {
208208
file_id = mcpl_create_outfile(filename_.c_str());
209209
if (VERSION_DEV) {
210-
line = fmt::format("OpenMC {0}.{1}.{2}-development", VERSION_MAJOR,
211-
VERSION_MINOR, VERSION_RELEASE);
210+
line = fmt::format("OpenMC {0}.{1}.{2}-dev{3}", VERSION_MAJOR,
211+
VERSION_MINOR, VERSION_RELEASE, VERSION_COMMIT_COUNT);
212212
} else {
213213
line = fmt::format(
214214
"OpenMC {0}.{1}.{2}", VERSION_MAJOR, VERSION_MINOR, VERSION_RELEASE);

src/output.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,12 @@ void title()
7575
// Write version information
7676
fmt::print(
7777
" | The OpenMC Monte Carlo Code\n"
78-
" Copyright | 2011-2024 MIT, UChicago Argonne LLC, and contributors\n"
78+
" Copyright | 2011-2025 MIT, UChicago Argonne LLC, and contributors\n"
7979
" License | https://docs.openmc.org/en/latest/license.html\n"
80-
" Version | {}.{}.{}{}\n",
81-
VERSION_MAJOR, VERSION_MINOR, VERSION_RELEASE, VERSION_DEV ? "-dev" : "");
82-
#ifdef GIT_SHA1
83-
fmt::print(" Git SHA1 | {}\n", GIT_SHA1);
84-
#endif
80+
" Version | {}.{}.{}{}{}\n",
81+
VERSION_MAJOR, VERSION_MINOR, VERSION_RELEASE, VERSION_DEV ? "-dev" : "",
82+
VERSION_COMMIT_COUNT);
83+
fmt::print(" Commit Hash | {}\n", VERSION_COMMIT_HASH);
8584

8685
// Write the date and time
8786
fmt::print(" Date/Time | {}\n", time_stamp());
@@ -291,12 +290,10 @@ void print_usage()
291290
void print_version()
292291
{
293292
if (mpi::master) {
294-
fmt::print("OpenMC version {}.{}.{}\n", VERSION_MAJOR, VERSION_MINOR,
295-
VERSION_RELEASE);
296-
#ifdef GIT_SHA1
297-
fmt::print("Git SHA1: {}\n", GIT_SHA1);
298-
#endif
299-
fmt::print("Copyright (c) 2011-2024 MIT, UChicago Argonne LLC, and "
293+
fmt::print("OpenMC version {}.{}.{}{}{}\n", VERSION_MAJOR, VERSION_MINOR,
294+
VERSION_RELEASE, VERSION_DEV ? "-dev" : "", VERSION_COMMIT_COUNT);
295+
fmt::print("Commit hash: {}\n", VERSION_COMMIT_HASH);
296+
fmt::print("Copyright (c) 2011-2025 MIT, UChicago Argonne LLC, and "
300297
"contributors\nMIT/X license at "
301298
"<https://docs.openmc.org/en/latest/license.html>\n");
302299
}

0 commit comments

Comments
 (0)