Skip to content

Commit

Permalink
Refactor versioning system and enhance build metadata.
Browse files Browse the repository at this point in the history
Replaced hardcoded versioning with a dynamic CMake-driven system using `Info.h` to embed version, parallelization, and commit metadata directly into the build. Simplified Python version parsing and renamed `LOGGING_LEVEL` to `POLYHEDRAL_GRAVITY_LOGGING_LEVEL` for consistency. Consolidated version management and improved Git commit hash handling.
  • Loading branch information
schuhmaj committed Dec 13, 2024
1 parent 9479f4a commit 0ce6c37
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 102 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ cmake-build-*
build
polyhedral_gravity.egg-info
dist
docs/Doxyfile
docs/Doxyfile
src/polyhedralGravity/Info.h
48 changes: 23 additions & 25 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# Appends the the module path to contain additional CMake modules for this project
# and include everything necessary
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
include(version)
include(git)

# Set and Print the Version number
polyhedral_gravity_parse_version(POLYHEDRAL_GRAVITY_VERSION)
get_git_commit_hash(POLYHEDRAL_GRAVITY_GIT_HASH)
message(STATUS "Polyhedral Gravity Version ${POLYHEDRAL_GRAVITY_VERSION}")
message(STATUS "Polyhedral Gravity Git Hash ${POLYHEDRAL_GRAVITY_GIT_HASH}")

include(CMakeDependentOption)

#####################################
Expand All @@ -27,38 +18,30 @@ set(POLYHEDRAL_GRAVITY_PARALLELIZATION "CPP" CACHE STRING "Host parallelization
set_property(CACHE POLYHEDRAL_GRAVITY_PARALLELIZATION PROPERTY STRINGS CPP, OMP, TBB)

# Set the Logging Level
set(LOGGING_LEVEL_LIST "TRACE" "DEBUG" "INFO" "WARN" "ERROR" "CRITICAL" "OFF")
set(LOGGING_LEVEL "INFO" CACHE STRING "Set the Logging level, default (INFO), available options: TRACE, DEBUG, INFO, WARN, ERROR, CRITICAL, OFF")
set_property(CACHE LOGGING_LEVEL PROPERTY STRINGS ${LOGGING_LEVEL_LIST})
set(POLYHEDRAL_GRAVITY_LOGGING_LEVEL_LIST "TRACE" "DEBUG" "INFO" "WARN" "ERROR" "CRITICAL" "OFF")
set(POLYHEDRAL_GRAVITY_LOGGING_LEVEL "INFO" CACHE STRING "Set the Logging level, default (INFO), available options: TRACE, DEBUG, INFO, WARN, ERROR, CRITICAL, OFF")
set_property(CACHE POLYHEDRAL_GRAVITY_LOGGING_LEVEL PROPERTY STRINGS ${POLYHEDRAL_GRAVITY_LOGGING_LEVEL_LIST})
# Convert the logging level string to its corresponding number
list(FIND LOGGING_LEVEL_LIST ${LOGGING_LEVEL} LOGGING_LEVEL_INDEX)
list(FIND POLYHEDRAL_GRAVITY_LOGGING_LEVEL_LIST ${POLYHEDRAL_GRAVITY_LOGGING_LEVEL} LOGGING_LEVEL_INDEX)
if (${LOGGING_LEVEL_INDEX} EQUAL -1)
message(FATAL_ERROR "Invalid logging level: ${LOGGING_LEVEL}")
message(FATAL_ERROR "Invalid logging level: ${POLYHEDRAL_GRAVITY_LOGGING_LEVEL}")
endif ()
add_compile_definitions(SPDLOG_ACTIVE_LEVEL=${LOGGING_LEVEL_INDEX})
message(STATUS "Logging level set to ${LOGGING_LEVEL} (=${LOGGING_LEVEL_INDEX})")

###################################
# What actually to build? - Options
###################################

#########################################################
# What actually to build? - Options, Versions and Output
#########################################################
# Build docs
option(BUILD_POLYHEDRAL_GRAVITY_DOCS "Builds the documentation (Default: OFF)" OFF)
message(STATUS "BUILD_POLYHEDRAL_GRAVITY_DOCS = ${BUILD_POLYHEDRAL_GRAVITY_DOCS}")
# Build C++ executable
option(BUILD_POLYHEDRAL_GRAVITY_EXECUTABLE "Builds the C++ executable (Default: ON)" ON)
message(STATUS "BUILD_POLYHEDRAL_GRAVITY_EXECUTABLE = ${BUILD_POLYHEDRAL_GRAVITY_EXECUTABLE}")
# Build library (default ON), if the executable or tests are built this forced to ON
cmake_dependent_option(BUILD_POLYHEDRAL_GRAVITY_LIBRARY "Builds the library (Default: ON)" ON
"NOT BUILD_POLYHEDRAL_GRAVITY_EXECUTABLE AND NOT BUILD_POLYHEDRAL_GRAVITY_TESTS" ON)
message(STATUS "BUILD_POLYHEDRAL_GRAVITY_LIBRARY = ${BUILD_POLYHEDRAL_GRAVITY_LIBRARY}")
# Option to build the python interface
option(BUILD_POLYHEDRAL_GRAVITY_PYTHON_INTERFACE "Set this to on if the python interface should be built (Default: ON)" ON)
message(STATUS "BUILD_POLYHEDRAL_GRAVITY_PYTHON_INTERFACE = ${BUILD_POLYHEDRAL_GRAVITY_PYTHON_INTERFACE}")
# Option to build tests or not
option(BUILD_POLYHEDRAL_GRAVITY_TESTS "Set to on if the tests should be built (Default: ON)" ON)
message(STATUS "BUILD_POLYHEDRAL_GRAVITY_TESTS = ${BUILD_POLYHEDRAL_GRAVITY_TESTS}")


IF(_LIBCPP_DISABLE_AVAILABILITY)
message(STATUS "Disabling availability macros for libc++")
Expand All @@ -69,6 +52,21 @@ endif ()
# Refer to https://github.com/gabime/spdlog/issues/660
add_compile_definitions(FMT_HEADER_ONLY)

include(git)
include(version.cmake)

message(STATUS "#################################################################")
message(STATUS "Polyhedral Gravity Version ${POLYHEDRAL_GRAVITY_VERSION}")
message(STATUS "Polyhedral Gravity Commit Hash ${POLYHEDRAL_GRAVITY_COMMIT_HASH}")
message(STATUS "Polyhedral Parallelization Backend ${POLYHEDRAL_GRAVITY_PARALLELIZATION}")
message(STATUS "Polyhedral Gravity Logging Level ${POLYHEDRAL_GRAVITY_LOGGING_LEVEL}")
message(STATUS "#################################################################")
message(STATUS "Polyhedral Gravity Documentation ${BUILD_POLYHEDRAL_GRAVITY_DOCS}")
message(STATUS "Polyhedral Gravity Library ${BUILD_POLYHEDRAL_GRAVITY_LIBRARY}")
message(STATUS "Polyhedral Gravity C++ Executable ${BUILD_POLYHEDRAL_GRAVITY_EXECUTABLE}")
message(STATUS "Polyhedral Gravity Python Interface ${BUILD_POLYHEDRAL_GRAVITY_PYTHON_INTERFACE}")
message(STATUS "Polyhedral Gravity Tests ${BUILD_POLYHEDRAL_GRAVITY_TESTS}")
message(STATUS "#################################################################")
#######################################################
# Including dependencies needed across multiple targets
#######################################################
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,16 +305,16 @@ cmake --build .

The following options are available:

| Name (Default) | Options |
|-------------------------------------------------:|:--------------------------------------------------------------------------------------------|
| POLYHEDRAL_GRAVITY_PARALLELIZATION (`CPP`) | `CPP` = Serial Execution / `OMP` or `TBB` = Parallel Execution with OpenMP or Intel\'s TBB |
| LOGGING_LEVEL (`INFO`) | `TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`, `CRITICAL`, `OFF` |
| BUILD_POLYHEDRAL_GRAVITY_DOCS (`OFF`) | Build this documentation |
| BUILD_POLYHEDRAL_GRAVITY_TESTS (`ON`) | Build the Tests |
| BUILD_POLYHEDRAL_GRAVITY_PYTHON_INTERFACE (`ON`) | Build the Python interface |
| Name (Default) | Options |
|-------------------------------------------------------------:|:--------------------------------------------------------------------------------------------|
| POLYHEDRAL_GRAVITY_PARALLELIZATION (`CPP`) | `CPP` = Serial Execution / `OMP` or `TBB` = Parallel Execution with OpenMP or Intel\'s TBB |
| POLYHEDRAL_GRAVITY_LOGGING_LEVEL (`INFO`) | `TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`, `CRITICAL`, `OFF` |
| BUILD_POLYHEDRAL_GRAVITY_DOCS (`OFF`) | Build this documentation |
| BUILD_POLYHEDRAL_GRAVITY_TESTS (`ON`) | Build the Tests |
| BUILD_POLYHEDRAL_GRAVITY_PYTHON_INTERFACE (`ON`) | Build the Python interface |

During testing POLYHEDRAL_GRAVITY_PARALLELIZATION=`TBB` has been the most performant.
It is further not recommend to change the LOGGING_LEVEL to something else than `INFO=2`.
It is further not recommend to change the POLYHEDRAL_GRAVITY_LOGGING_LEVEL to something else than `INFO=2`.

The recommended CMake settings using the `TBB` backend would look like this:

Expand Down
35 changes: 29 additions & 6 deletions cmake/git.cmake
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
function(get_git_commit_hash OUTPUT_VAR)
# Ensure Git is available
find_package(Git QUIET REQUIRED)
find_package(Git QUIET REQUIRED)

# Run a Git command to get the current commit hash
function(get_git_commit_hash OUTPUT_VAR)
# Run a Git command to get the first 8 characters of the current commit hash
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
COMMAND ${GIT_EXECUTABLE} rev-parse --short=8 HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
Expand All @@ -17,7 +16,31 @@ function(get_git_commit_hash OUTPUT_VAR)
message(WARNING "Failed to retrieve Git commit hash: ${GIT_ERROR}")
set(${OUTPUT_VAR} "UNKNOWN" PARENT_SCOPE)
else()
# Pass the hash back to the calling scope
# Pass the short hash back to the calling scope
set(${OUTPUT_VAR} "${GIT_COMMIT_HASH}" PARENT_SCOPE)
endif()
endfunction()

function(is_git_working_tree_clean OUTPUT_VAR)
# Run a Git command to check if the working tree is clean
execute_process(
COMMAND ${GIT_EXECUTABLE} diff-index --quiet HEAD --
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
RESULT_VARIABLE GIT_STATUS_RESULT
ERROR_VARIABLE GIT_ERROR
ERROR_STRIP_TRAILING_WHITESPACE
)

# Check the result of the Git command
if (NOT GIT_ERROR AND GIT_STATUS_RESULT EQUAL 0)
# Working tree is clean
set(${OUTPUT_VAR} TRUE PARENT_SCOPE)
else()
# Working tree has uncommitted changes or an error occurred
set(${OUTPUT_VAR} FALSE PARENT_SCOPE)

if (GIT_ERROR)
message(WARNING "Error while checking Git working tree: ${GIT_ERROR}")
endif()
endif()
endfunction()
12 changes: 0 additions & 12 deletions cmake/version.cmake

This file was deleted.

2 changes: 1 addition & 1 deletion docs/quickstart/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ The available options are the following:
Name (Default) Options
====================================================== ============================================================================================================
POLYHEDRAL_GRAVITY_PARALLELIZATION (:code:`CPP`) :code:`CPP` = Serial Execution / :code:`OMP` or :code:`TBB` = Parallel Execution with OpenMP or Intel's TBB
LOGGING_LEVEL (:code:`INFO`) :code:`TRACE`, :code:`DEBUG`, :code:`INFO`, :code:`WARN`, :code:`ERROR`, :code:`CRITICAL`, :code:`OFF`
POLYHEDRAL_GRAVITY_LOGGING_LEVEL (:code:`INFO`) :code:`TRACE`, :code:`DEBUG`, :code:`INFO`, :code:`WARN`, :code:`ERROR`, :code:`CRITICAL`, :code:`OFF`
BUILD_POLYHEDRAL_GRAVITY_DOCS (:code:`OFF`) Build this documentation
BUILD_POLYHEDRAL_GRAVITY_TESTS (:code:`ON`) Build the Tests
BUILD_POLYHEDRAL_GRAVITY_PYTHON_INTERFACE (:code:`ON`) Build the Python interface
Expand Down
22 changes: 11 additions & 11 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# Modify to change the parallelization (Default value: TBB)
"POLYHEDRAL_GRAVITY_PARALLELIZATION": "TBB",
# Default value (INFO=2)
"LOGGING_LEVEL": "INFO",
"POLYHEDRAL_GRAVITY_LOGGING_LEVEL": "INFO",
# Not required for the python interface (--> OFF)
"BUILD_POLYHEDRAL_GRAVITY_DOCS": "OFF",
# Not required for the python interface (--> OFF)
Expand Down Expand Up @@ -54,24 +54,24 @@ def get_cmake_generator():
return None

def get_version():
"""Returns the version of the polyhedral gravity package by reading Version.h."""
# Construct the path to Version.h relative to the current file
version_file = os.path.join(os.path.dirname(__file__), "src", "polyhedralGravity", "Version.h")
"""Returns the version of the polyhedral gravity package by reading the CMake file."""
# Path to the CMake file
cmake_file = os.path.join(os.path.dirname(__file__), "version.cmake" )

# Check if the Version.h file exists
if not os.path.exists(version_file):
raise FileNotFoundError(f"Version file not found: {version_file}")
# Check if the CMake file exists
if not os.path.exists(cmake_file):
raise FileNotFoundError(f"CMake file not found: {cmake_file}")

# Open and read the file
with open(version_file, "r") as file:
with open(cmake_file, "r") as file:
content = file.read()

# Use regex to extract the version string
version_match = re.search(r'POLYHEDRAL_GRAVITY_VERSION\s*=\s*\"([^\"]+)\"', content)
# Use regex to extract the PROJECT_VERSION
version_match = re.search(r'set\(PROJECT_VERSION\s+([^\s)]+)\)', content)
if version_match:
return version_match.group(1)
else:
raise ValueError("Version string not found in Version.h")
raise ValueError("Version string not found in CMakeLists.txt")


# -----------------------------------------------------------------------------------------
Expand Down
10 changes: 6 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#include <chrono>
#include "polyhedralGravity/Info.h"
#include "polyhedralGravity/input/ConfigSource.h"
#include "polyhedralGravity/input/YAMLConfigReader.h"
#include "polyhedralGravity/model/GravityModel.h"
#include "polyhedralGravity/output/Logging.h"
#include "polyhedralGravity/output/CSVWriter.h"
#include "polyhedralGravity/Version.h"
#include "polyhedralGravity/output/Logging.h"
#include <chrono>

int main(int argc, char *argv[]) {
using namespace polyhedralGravity;

SPDLOG_LOGGER_INFO(PolyhedralGravityLogger::DEFAULT_LOGGER.getLogger(), "Polyhedral Gravity Model Version " + std::string(POLYHEDRAL_GRAVITY_VERSION));
SPDLOG_LOGGER_INFO(PolyhedralGravityLogger::DEFAULT_LOGGER.getLogger(), "Polyhedral Gravity Model Version " + std::string(POLYHEDRAL_GRAVITY_VERSION));
SPDLOG_LOGGER_INFO(PolyhedralGravityLogger::DEFAULT_LOGGER.getLogger(), "Polyhedral Gravity Commit Hash " + std::string(POLYHEDRAL_GRAVITY_COMMIT_HASH));
SPDLOG_LOGGER_INFO(PolyhedralGravityLogger::DEFAULT_LOGGER.getLogger(), "Polyhedral Gravity Model Parallelization Backend: " + std::string(POLYHEDRAL_GRAVITY_PARALLELIZATION));
SPDLOG_LOGGER_INFO(PolyhedralGravityLogger::DEFAULT_LOGGER.getLogger(), "Polyhedral Gravity Logging Level " + std::string(POLYHEDRAL_GRAVITY_LOGGING_LEVEL));

if (argc != 2) {
SPDLOG_LOGGER_INFO(PolyhedralGravityLogger::DEFAULT_LOGGER.getLogger(), "Wrong program call! "
Expand Down
2 changes: 1 addition & 1 deletion src/polyhedralGravity/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ file(GLOB_RECURSE SRC
"${CMAKE_CURRENT_SOURCE_DIR}/*.h"
"${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")

add_library(${PROJECT_NAME}_lib OBJECT ${SRC} Version.h)
add_library(${PROJECT_NAME}_lib OBJECT ${SRC} Info.h)
36 changes: 36 additions & 0 deletions src/polyhedralGravity/Info.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <string_view>

// ##########################################################################################
// Please note! The content of this file is automatically modified by the CMake build system
// which replaces the variables
// ##########################################################################################

namespace polyhedralGravity {

/**
* The API version of the polyhedral gravity model's interface.
* The value is set by the CMake configuration.
*/
constexpr std::string_view POLYHEDRAL_GRAVITY_VERSION = "@POLYHEDRAL_GRAVITY_VERSION@";

/**
* The API parallelization backend of the polyhedral gravity model's interface.
* The value is set by the CMake configuration.
*/
constexpr std::string_view POLYHEDRAL_GRAVITY_PARALLELIZATION = "@POLYHEDRAL_GRAVITY_PARALLELIZATION@";

/**
* The API commit with which it was compiled.
* The value is set by the CMake configuration.
*/
constexpr std::string_view POLYHEDRAL_GRAVITY_COMMIT_HASH = "@POLYHEDRAL_GRAVITY_COMMIT_HASH@";

/**
* The API's Loggin Level. Determines the amount of output.
* The value is set by the CMake configuration.
*/
constexpr std::string_view POLYHEDRAL_GRAVITY_LOGGING_LEVEL = "@POLYHEDRAL_GRAVITY_LOGGING_LEVEL@";

}// namespace polyhedralGravity
29 changes: 0 additions & 29 deletions src/polyhedralGravity/Version.h

This file was deleted.

12 changes: 8 additions & 4 deletions src/polyhedralGravityPython/PolyhedralGravityPython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
#include "pybind11/pybind11.h"
#include "pybind11/stl.h"

#include "polyhedralGravity/model/Polyhedron.h"
#include "polyhedralGravity/model/GravityModelData.h"
#include "polyhedralGravity/model/GravityModel.h"
#include "polyhedralGravity/Info.h"
#include "polyhedralGravity/model/GravityEvaluable.h"
#include "polyhedralGravity/Version.h"
#include "polyhedralGravity/model/GravityModel.h"
#include "polyhedralGravity/model/GravityModelData.h"
#include "polyhedralGravity/model/Polyhedron.h"


namespace py = pybind11;
Expand Down Expand Up @@ -102,8 +102,12 @@ PYBIND11_MODULE(polyhedral_gravity, m) {
Accordingly, the second derivative tensor is defined as the derivative of :math:`\textbf{g}`.
)mydelimiter";

// We embedded the version & compilation information into the Python Interface
m.attr("__version__") = POLYHEDRAL_GRAVITY_VERSION;
m.attr("__parallelization__") = POLYHEDRAL_GRAVITY_PARALLELIZATION;
m.attr("__commit__") = POLYHEDRAL_GRAVITY_COMMIT_HASH;
m.attr("__logging__") = POLYHEDRAL_GRAVITY_LOGGING_LEVEL;

py::enum_<NormalOrientation>(m, "NormalOrientation", R"mydelimiter(
The orientation of the plane unit normals of the polyhedron.
Expand Down
18 changes: 18 additions & 0 deletions version.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Modify the version with a new release
set(PROJECT_VERSION 3.2.1)
set(POLYHEDRAL_GRAVITY_VERSION ${PROJECT_VERSION})

# Get the Git information
get_git_commit_hash(POLYHEDRAL_GRAVITY_COMMIT_HASH)
is_git_working_tree_clean(POLYHEDRAL_GRAVITY_WORKING_TREE)

# Append "-modified" to the commit hash if the working tree is not clean
if (NOT ${POLYHEDRAL_GRAVITY_WORKING_TREE})
set(POLYHEDRAL_GRAVITY_COMMIT_HASH "${POLYHEDRAL_GRAVITY_COMMIT_HASH}+modified")
endif ()

# Configure the output header file
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/src/polyhedralGravity/Info.h.in"
"${CMAKE_CURRENT_SOURCE_DIR}/src/polyhedralGravity/Info.h"
)

0 comments on commit 0ce6c37

Please sign in to comment.