From a3bf30ccca46b1c6a64e836df5dfdd14e8804dcf Mon Sep 17 00:00:00 2001 From: Jonas Schuhmacher Date: Tue, 10 Dec 2024 15:06:19 +0100 Subject: [PATCH] unify Version handling of the implementation --- CMakeLists.txt | 16 +++++++++---- cmake/git.cmake | 23 +++++++++++++++++++ cmake/version.cmake | 12 ++++++++++ setup.py | 23 ++++++++++++++++++- src/CMakeLists.txt | 4 ++-- src/polyhedralGravity/CMakeLists.txt | 2 +- src/polyhedralGravity/Version.h | 15 ++++++++++++ .../PolyhedralGravityPython.cpp | 10 ++++---- 8 files changed, 92 insertions(+), 13 deletions(-) create mode 100644 cmake/git.cmake create mode 100644 cmake/version.cmake create mode 100644 src/polyhedralGravity/Version.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 362ec95..f46e2c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,18 @@ project(polyhedralGravity) set(CMAKE_CXX_STANDARD 17) 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) ##################################### @@ -60,11 +72,7 @@ add_compile_definitions(FMT_HEADER_ONLY) ####################################################### # Including dependencies needed across multiple targets ####################################################### -# 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 dependecies include(thrust) include(spdlog) include(tetgen) diff --git a/cmake/git.cmake b/cmake/git.cmake new file mode 100644 index 0000000..aec6d4c --- /dev/null +++ b/cmake/git.cmake @@ -0,0 +1,23 @@ +function(get_git_commit_hash OUTPUT_VAR) + # Ensure Git is available + find_package(Git QUIET REQUIRED) + + # Run a Git command to get the current commit hash + execute_process( + COMMAND ${GIT_EXECUTABLE} rev-parse HEAD + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + OUTPUT_VARIABLE GIT_COMMIT_HASH + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_VARIABLE GIT_ERROR + ERROR_STRIP_TRAILING_WHITESPACE + ) + + # Check if the Git command was successful + if (NOT GIT_COMMIT_HASH OR GIT_ERROR) + 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 + set(${OUTPUT_VAR} "${GIT_COMMIT_HASH}" PARENT_SCOPE) + endif() +endfunction() \ No newline at end of file diff --git a/cmake/version.cmake b/cmake/version.cmake new file mode 100644 index 0000000..4ed3736 --- /dev/null +++ b/cmake/version.cmake @@ -0,0 +1,12 @@ +function(polyhedral_gravity_parse_version OUTPUT_VAR) + # Read the content of the given header file + file(READ "${PROJECT_SOURCE_DIR}/src/polyhedralGravity/Version.h" HEADER_CONTENTS) + # Extract the version using regex + string(REGEX MATCH "constexpr std::string_view POLYHEDRAL_GRAVITY_VERSION = \"([0-9]+\\.[0-9]+\\.[0-9]+[a-zA-Z0-9]*)\"" VERSION_MATCH "${HEADER_CONTENTS}") + # Set the output variable to the matched version group + if(CMAKE_MATCH_1) + set(${OUTPUT_VAR} "${CMAKE_MATCH_1}" PARENT_SCOPE) + else() + message(FATAL_ERROR "Failed to parse POLYHEDRAL_GRAVITY_VERSION from '${HEADER_FILE}'") + endif() +endfunction() \ No newline at end of file diff --git a/setup.py b/setup.py index 26cd85f..736081c 100644 --- a/setup.py +++ b/setup.py @@ -53,6 +53,27 @@ def get_cmake_generator(): else: 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") + + # Check if the Version.h file exists + if not os.path.exists(version_file): + raise FileNotFoundError(f"Version file not found: {version_file}") + + # Open and read the file + with open(version_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) + if version_match: + return version_match.group(1) + else: + raise ValueError("Version string not found in Version.h") + + # ----------------------------------------------------------------------------------------- # The following is adapted from https://github.com/pybind/cmake_example/blob/master/setup.py # ----------------------------------------------------------------------------------------- @@ -173,7 +194,7 @@ def build_extension(self, ext): # -------------------------------------------------------------------------------- setup( name="polyhedral_gravity", - version="3.2.1", + version=get_version(), author="Jonas Schuhmacher", author_email="jonas.schuhmacher@tum.de", description="Package to compute full gravity tensor of a given constant density polyhedron for arbitrary points " diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 08b5bbb..4a64eea 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -18,7 +18,7 @@ if (BUILD_POLYHEDRAL_GRAVITY_LIBRARY) target_link_libraries(${PROJECT_NAME}_lib spdlog::spdlog yaml-cpp::yaml-cpp - tetgen + tetgen_lib xsimd Thrust ) @@ -60,7 +60,7 @@ if(BUILD_POLYHEDRAL_GRAVITY_PYTHON_INTERFACE) target_link_libraries(polyhedral_gravity PUBLIC spdlog::spdlog - tetgen + tetgen_lib xsimd Thrust ) diff --git a/src/polyhedralGravity/CMakeLists.txt b/src/polyhedralGravity/CMakeLists.txt index efd3fb1..30bbb4e 100644 --- a/src/polyhedralGravity/CMakeLists.txt +++ b/src/polyhedralGravity/CMakeLists.txt @@ -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}) +add_library(${PROJECT_NAME}_lib OBJECT ${SRC} Version.h) diff --git a/src/polyhedralGravity/Version.h b/src/polyhedralGravity/Version.h new file mode 100644 index 0000000..49e57d9 --- /dev/null +++ b/src/polyhedralGravity/Version.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +namespace polyhedralGravity { + + /** + * The API version of the polyhedral gravity model's interface. + * This value is utilized across C++ and Python Interface and the single value + * to change in case of updates. + */ + constexpr std::string_view POLYHEDRAL_GRAVITY_VERSION = "3.2.1"; + + +}// namespace polyhedralGravity diff --git a/src/polyhedralGravityPython/PolyhedralGravityPython.cpp b/src/polyhedralGravityPython/PolyhedralGravityPython.cpp index e6f4d79..1386baf 100644 --- a/src/polyhedralGravityPython/PolyhedralGravityPython.cpp +++ b/src/polyhedralGravityPython/PolyhedralGravityPython.cpp @@ -10,6 +10,7 @@ #include "polyhedralGravity/model/GravityModelData.h" #include "polyhedralGravity/model/GravityModel.h" #include "polyhedralGravity/model/GravityEvaluable.h" +#include "polyhedralGravity/Version.h" namespace py = pybind11; @@ -43,12 +44,10 @@ PYBIND11_MODULE(polyhedral_gravity, m) { .. note:: *Tsoulis et al.*'s formulation requires that the normals point :code:`OUTWARDS`. - The implementation **can handle both cases and also can automatically determine the property** if initiall set wrong. + The implementation **can handle both cases and also can automatically determine the property** if initially set wrong. Using :code:`AUTOMATIC` (default for first-time-user) or :code:`VERIFY` raises a :code:`ValueError` if the :py:class:`polyhedral_gravity.NormalOrientation` is wrong. Using :code:`HEAL` will re-order the vertex sorting to fix errors. - Using :code:`DISABLE` will turn this check off and avoid :math:`O(n^2)` runtime complexcity of this check! Highly recommened, when you "know your mesh"! - - + Using :code:`DISABLE` will turn this check off and avoid :math:`O(n^2)` runtime complexity of this check! Highly recommended, when you "know your mesh"! The polyhedron's mesh's units must match with the constant density! For example, if the mesh is in :math:`[m]`, then the constant density should be in :math:`[\frac{kg}{m^3}]`. @@ -63,7 +62,7 @@ PYBIND11_MODULE(polyhedral_gravity, m) { parallel=True, ) - or via use the cached approach :py:class:`polyhedral_gravity.GravityEvaluable` (desriable for subsequent evaluations using the same :py:class:`polyhedral_gravity.Polyhedron`) + or via use the cached approach :py:class:`polyhedral_gravity.GravityEvaluable` (desirable for subsequent evaluations using the same :py:class:`polyhedral_gravity.Polyhedron`) .. code-block:: python @@ -103,6 +102,7 @@ PYBIND11_MODULE(polyhedral_gravity, m) { Accordingly, the second derivative tensor is defined as the derivative of :math:`\textbf{g}`. )mydelimiter"; + m.attr("__version__") = POLYHEDRAL_GRAVITY_VERSION; py::enum_(m, "NormalOrientation", R"mydelimiter( The orientation of the plane unit normals of the polyhedron.