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>
This commit is contained in:
Ahnaf Tahmid Chowdhury 2025-02-22 05:48:11 +06:00 committed by GitHub
parent fefe825e65
commit a2a5c2af19
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 153 additions and 44 deletions

3
.git_archival.txt Normal file
View file

@ -0,0 +1,3 @@
commit: $Format:%H$
commit-date: $Format:%cI$
describe-name: $Format:%(describe:tags=true,match=*[0-9]*)$

1
.gitattributes vendored Normal file
View file

@ -0,0 +1 @@
.git_archival.txt export-subst

View file

@ -87,9 +87,10 @@ jobs:
RDMAV_FORK_SAFE: 1 RDMAV_FORK_SAFE: 1
steps: steps:
- uses: actions/checkout@v4 - name: Checkout repository
uses: actions/checkout@v4
with: with:
fetch-depth: 2 fetch-depth: 0
- name: Set up Python ${{ matrix.python-version }} - name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5 uses: actions/setup-python@v5

View file

@ -1,11 +1,18 @@
cmake_minimum_required(VERSION 3.16 FATAL_ERROR) cmake_minimum_required(VERSION 3.16 FATAL_ERROR)
project(openmc C CXX) project(openmc C CXX)
# Set version numbers # Set module path
set(OPENMC_VERSION_MAJOR 0) set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules)
set(OPENMC_VERSION_MINOR 15)
set(OPENMC_VERSION_RELEASE 1) include(GetVersionFromGit)
set(OPENMC_VERSION ${OPENMC_VERSION_MAJOR}.${OPENMC_VERSION_MINOR}.${OPENMC_VERSION_RELEASE})
# Output version information
message(STATUS "OpenMC version: ${OPENMC_VERSION}")
message(STATUS "OpenMC dev state: ${OPENMC_DEV_STATE}")
message(STATUS "OpenMC commit hash: ${OPENMC_COMMIT_HASH}")
message(STATUS "OpenMC commit count: ${OPENMC_COMMIT_COUNT}")
# Generate version.h
configure_file(include/openmc/version.h.in "${CMAKE_BINARY_DIR}/include/openmc/version.h" @ONLY) configure_file(include/openmc/version.h.in "${CMAKE_BINARY_DIR}/include/openmc/version.h" @ONLY)
# Setup output directories # Setup output directories
@ -13,9 +20,6 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# Set module path
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules)
# Enable correct usage of CXX_EXTENSIONS # Enable correct usage of CXX_EXTENSIONS
if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.22) if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.22)
cmake_policy(SET CMP0128 NEW) cmake_policy(SET CMP0128 NEW)
@ -240,8 +244,6 @@ endif()
#=============================================================================== #===============================================================================
# Update git submodules as needed # Update git submodules as needed
#=============================================================================== #===============================================================================
find_package(Git)
if(GIT_FOUND AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git") if(GIT_FOUND AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
option(GIT_SUBMODULE "Check submodules during build" ON) option(GIT_SUBMODULE "Check submodules during build" ON)
if(GIT_SUBMODULE) if(GIT_SUBMODULE)
@ -493,18 +495,6 @@ if (OPENMC_USE_MPI)
target_compile_definitions(libopenmc PUBLIC -DOPENMC_MPI) target_compile_definitions(libopenmc PUBLIC -DOPENMC_MPI)
endif() endif()
# Set git SHA1 hash as a compile definition
if(GIT_FOUND)
execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE GIT_SHA1_SUCCESS
OUTPUT_VARIABLE GIT_SHA1
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
if(GIT_SHA1_SUCCESS EQUAL 0)
target_compile_definitions(libopenmc PRIVATE -DGIT_SHA1="${GIT_SHA1}")
endif()
endif()
# target_link_libraries treats any arguments starting with - but not -l as # target_link_libraries treats any arguments starting with - but not -l as
# linker flags. Thus, we can pass both linker flags and libraries together. # linker flags. Thus, we can pass both linker flags and libraries together.
target_link_libraries(libopenmc ${ldflags} ${HDF5_LIBRARIES} ${HDF5_HL_LIBRARIES} target_link_libraries(libopenmc ${ldflags} ${HDF5_LIBRARIES} ${HDF5_HL_LIBRARIES}

View file

@ -0,0 +1,109 @@
# GetVersionFromGit.cmake
# Standalone script to retrieve versioning information from Git or .git_archival.txt.
# Customizable for any project by setting variables before including this file.
# Configurable variables:
# - VERSION_PREFIX: Prefix for version tags (default: "v").
# - VERSION_SUFFIX: Suffix for version tags (default: "[~+-]([a-zA-Z0-9]+)").
# - VERSION_REGEX: Regex to extract version (default: "(?[0-9]+\\.[0-9]+\\.[0-9]+)").
# - ARCHIVAL_FILE: Path to .git_archival.txt (default: "${CMAKE_SOURCE_DIR}/.git_archival.txt").
# - DESCRIBE_NAME_KEY: Key for describe name in .git_archival.txt (default: "describe-name: ").
# - COMMIT_HASH_KEY: Key for commit hash in .git_archival.txt (default: "commit: ").
# Default Format Example:
# 1.2.3 v1.2.3 v1.2.3-rc1
set(VERSION_PREFIX "v" CACHE STRING "Prefix used in version tags")
set(VERSION_SUFFIX "[~+-]([a-zA-Z0-9]+)" CACHE STRING "Suffix used in version tags")
set(VERSION_REGEX "?([0-9]+\\.[0-9]+\\.[0-9]+)" CACHE STRING "Regex for extracting version")
set(ARCHIVAL_FILE "${CMAKE_SOURCE_DIR}/.git_archival.txt" CACHE STRING "Path to .git_archival.txt")
set(DESCRIBE_NAME_KEY "describe-name: " CACHE STRING "Key for describe name in .git_archival.txt")
set(COMMIT_HASH_KEY "commit: " CACHE STRING "Key for commit hash in .git_archival.txt")
# Combine prefix and regex
set(VERSION_REGEX_WITH_PREFIX "^${VERSION_PREFIX}${VERSION_REGEX}")
# Find Git
find_package(Git)
# Attempt to retrieve version from Git
if(EXISTS "${CMAKE_SOURCE_DIR}/.git" AND GIT_FOUND)
message(STATUS "Using git describe for versioning")
# Extract the version string
execute_process(
COMMAND git describe --tags --dirty
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE VERSION_STRING
OUTPUT_STRIP_TRAILING_WHITESPACE
)
# Extract the commit hash
execute_process(
COMMAND git rev-parse HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE COMMIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
else()
message(STATUS "Using archival file for versioning: ${ARCHIVAL_FILE}")
if(EXISTS "${ARCHIVAL_FILE}")
file(READ "${ARCHIVAL_FILE}" ARCHIVAL_CONTENT)
# Extract the describe-name line
string(REGEX MATCH "${DESCRIBE_NAME_KEY}([^\\n]+)" VERSION_STRING "${ARCHIVAL_CONTENT}")
if(VERSION_STRING MATCHES "${DESCRIBE_NAME_KEY}(.*)")
set(VERSION_STRING "${CMAKE_MATCH_1}")
else()
message(FATAL_ERROR "Could not extract version from ${ARCHIVAL_FILE}")
endif()
# Extract the commit hash
string(REGEX MATCH "${COMMIT_HASH_KEY}([a-f0-9]+)" COMMIT_HASH "${ARCHIVAL_CONTENT}")
if(COMMIT_HASH MATCHES "${COMMIT_HASH_KEY}([a-f0-9]+)")
set(COMMIT_HASH "${CMAKE_MATCH_1}")
else()
message(FATAL_ERROR "Could not extract commit hash from ${ARCHIVAL_FILE}")
endif()
else()
message(FATAL_ERROR "Neither git describe nor ${ARCHIVAL_FILE} is available for versioning.")
endif()
endif()
# Ensure version string format
if(VERSION_STRING MATCHES "${VERSION_REGEX_WITH_PREFIX}")
set(VERSION_NO_SUFFIX "${CMAKE_MATCH_1}")
else()
message(FATAL_ERROR "Invalid version format: Missing base version in ${VERSION_STRING}")
endif()
# Check for development state
if(VERSION_STRING MATCHES "-([0-9]+)-g([0-9a-f]+)")
set(DEV_STATE "true")
set(COMMIT_COUNT "${CMAKE_MATCH_1}")
string(REGEX REPLACE "-([0-9]+)-g([0-9a-f]+)" "" VERSION_WITHOUT_META "${VERSION_STRING}")
else()
set(DEV_STATE "false")
set(VERSION_WITHOUT_META "${VERSION_STRING}")
endif()
# Split and set version components
string(REPLACE "." ";" VERSION_LIST "${VERSION_NO_SUFFIX}")
list(GET VERSION_LIST 0 VERSION_MAJOR)
list(GET VERSION_LIST 1 VERSION_MINOR)
list(GET VERSION_LIST 2 VERSION_PATCH)
# Increment patch number for dev versions
if(DEV_STATE)
math(EXPR VERSION_PATCH "${VERSION_PATCH} + 1")
endif()
# Export variables
set(OPENMC_VERSION_MAJOR "${VERSION_MAJOR}")
set(OPENMC_VERSION_MINOR "${VERSION_MINOR}")
set(OPENMC_VERSION_PATCH "${VERSION_PATCH}")
set(OPENMC_VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
set(OPENMC_COMMIT_HASH "${COMMIT_HASH}")
set(OPENMC_DEV_STATE "${DEV_STATE}")
set(OPENMC_COMMIT_COUNT "${COMMIT_COUNT}")

View file

@ -68,10 +68,14 @@ copyright = '2011-2024, Massachusetts Institute of Technology, UChicago Argonne
# |version| and |release|, also used in various other places throughout the # |version| and |release|, also used in various other places throughout the
# built documents. # built documents.
# #
import openmc
# The short X.Y version. # The short X.Y version.
version = "0.15" version = ".".join(openmc.__version__.split('.')[:2])
# The full version, including alpha/beta/rc tags. # The full version, including alpha/beta/rc tags.
release = "0.15.1-dev" release = openmc.__version__
# The language for content autogenerated by Sphinx. Refer to documentation # The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages. # for a list of supported languages.

View file

@ -9,8 +9,10 @@ namespace openmc {
// clang-format off // clang-format off
constexpr int VERSION_MAJOR {@OPENMC_VERSION_MAJOR@}; constexpr int VERSION_MAJOR {@OPENMC_VERSION_MAJOR@};
constexpr int VERSION_MINOR {@OPENMC_VERSION_MINOR@}; constexpr int VERSION_MINOR {@OPENMC_VERSION_MINOR@};
constexpr int VERSION_RELEASE {@OPENMC_VERSION_RELEASE@}; constexpr int VERSION_RELEASE {@OPENMC_VERSION_PATCH@};
constexpr bool VERSION_DEV {true}; constexpr bool VERSION_DEV {@OPENMC_DEV_STATE@};
constexpr const char* VERSION_COMMIT_COUNT = "@OPENMC_COMMIT_COUNT@";
constexpr const char* VERSION_COMMIT_HASH = "@OPENMC_COMMIT_HASH@";
constexpr std::array<int, 3> VERSION {VERSION_MAJOR, VERSION_MINOR, VERSION_RELEASE}; constexpr std::array<int, 3> VERSION {VERSION_MAJOR, VERSION_MINOR, VERSION_RELEASE};
// clang-format on // clang-format on

View file

@ -1,5 +1,5 @@
[build-system] [build-system]
requires = ["setuptools", "wheel"] requires = ["setuptools", "setuptools-scm", "wheel"]
build-backend = "setuptools.build_meta" build-backend = "setuptools.build_meta"
[project] [project]
@ -8,7 +8,7 @@ authors = [
{name = "The OpenMC Development Team", email = "openmc@anl.gov"}, {name = "The OpenMC Development Team", email = "openmc@anl.gov"},
] ]
description = "OpenMC" description = "OpenMC"
version = "0.15.1-dev" dynamic = ["version"]
requires-python = ">=3.11" requires-python = ">=3.11"
license = {file = "LICENSE"} license = {file = "LICENSE"}
classifiers = [ classifiers = [
@ -66,3 +66,5 @@ exclude = ['tests*']
"openmc.data.effective_dose" = ["**/*.txt"] "openmc.data.effective_dose" = ["**/*.txt"]
"openmc.data" = ["*.txt", "*.DAT", "*.json", "*.h5"] "openmc.data" = ["*.txt", "*.DAT", "*.json", "*.h5"]
"openmc.lib" = ["libopenmc.dylib", "libopenmc.so"] "openmc.lib" = ["libopenmc.dylib", "libopenmc.so"]
[tool.setuptools_scm]

View file

@ -207,8 +207,8 @@ void write_mcpl_source_point(const char* filename, span<SourceSite> source_bank,
if (mpi::master) { if (mpi::master) {
file_id = mcpl_create_outfile(filename_.c_str()); file_id = mcpl_create_outfile(filename_.c_str());
if (VERSION_DEV) { if (VERSION_DEV) {
line = fmt::format("OpenMC {0}.{1}.{2}-development", VERSION_MAJOR, line = fmt::format("OpenMC {0}.{1}.{2}-dev{3}", VERSION_MAJOR,
VERSION_MINOR, VERSION_RELEASE); VERSION_MINOR, VERSION_RELEASE, VERSION_COMMIT_COUNT);
} else { } else {
line = fmt::format( line = fmt::format(
"OpenMC {0}.{1}.{2}", VERSION_MAJOR, VERSION_MINOR, VERSION_RELEASE); "OpenMC {0}.{1}.{2}", VERSION_MAJOR, VERSION_MINOR, VERSION_RELEASE);

View file

@ -75,13 +75,12 @@ void title()
// Write version information // Write version information
fmt::print( fmt::print(
" | The OpenMC Monte Carlo Code\n" " | The OpenMC Monte Carlo Code\n"
" Copyright | 2011-2024 MIT, UChicago Argonne LLC, and contributors\n" " Copyright | 2011-2025 MIT, UChicago Argonne LLC, and contributors\n"
" License | https://docs.openmc.org/en/latest/license.html\n" " License | https://docs.openmc.org/en/latest/license.html\n"
" Version | {}.{}.{}{}\n", " Version | {}.{}.{}{}{}\n",
VERSION_MAJOR, VERSION_MINOR, VERSION_RELEASE, VERSION_DEV ? "-dev" : ""); VERSION_MAJOR, VERSION_MINOR, VERSION_RELEASE, VERSION_DEV ? "-dev" : "",
#ifdef GIT_SHA1 VERSION_COMMIT_COUNT);
fmt::print(" Git SHA1 | {}\n", GIT_SHA1); fmt::print(" Commit Hash | {}\n", VERSION_COMMIT_HASH);
#endif
// Write the date and time // Write the date and time
fmt::print(" Date/Time | {}\n", time_stamp()); fmt::print(" Date/Time | {}\n", time_stamp());
@ -291,12 +290,10 @@ void print_usage()
void print_version() void print_version()
{ {
if (mpi::master) { if (mpi::master) {
fmt::print("OpenMC version {}.{}.{}\n", VERSION_MAJOR, VERSION_MINOR, fmt::print("OpenMC version {}.{}.{}{}{}\n", VERSION_MAJOR, VERSION_MINOR,
VERSION_RELEASE); VERSION_RELEASE, VERSION_DEV ? "-dev" : "", VERSION_COMMIT_COUNT);
#ifdef GIT_SHA1 fmt::print("Commit hash: {}\n", VERSION_COMMIT_HASH);
fmt::print("Git SHA1: {}\n", GIT_SHA1); fmt::print("Copyright (c) 2011-2025 MIT, UChicago Argonne LLC, and "
#endif
fmt::print("Copyright (c) 2011-2024 MIT, UChicago Argonne LLC, and "
"contributors\nMIT/X license at " "contributors\nMIT/X license at "
"<https://docs.openmc.org/en/latest/license.html>\n"); "<https://docs.openmc.org/en/latest/license.html>\n");
} }