Skip to content

Commit

Permalink
unify Version handling of the implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
schuhmaj committed Dec 10, 2024
1 parent d7a705c commit a3bf30c
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 13 deletions.
16 changes: 12 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

#####################################
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions cmake/git.cmake
Original file line number Diff line number Diff line change
@@ -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()
12 changes: 12 additions & 0 deletions cmake/version.cmake
Original file line number Diff line number Diff line change
@@ -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()
23 changes: 22 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
# -----------------------------------------------------------------------------------------
Expand Down Expand Up @@ -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="[email protected]",
description="Package to compute full gravity tensor of a given constant density polyhedron for arbitrary points "
Expand Down
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -60,7 +60,7 @@ if(BUILD_POLYHEDRAL_GRAVITY_PYTHON_INTERFACE)

target_link_libraries(polyhedral_gravity PUBLIC
spdlog::spdlog
tetgen
tetgen_lib
xsimd
Thrust
)
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})
add_library(${PROJECT_NAME}_lib OBJECT ${SRC} Version.h)
15 changes: 15 additions & 0 deletions src/polyhedralGravity/Version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <string_view>

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
10 changes: 5 additions & 5 deletions src/polyhedralGravityPython/PolyhedralGravityPython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}]`.
Expand All @@ -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
Expand Down Expand Up @@ -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_<NormalOrientation>(m, "NormalOrientation", R"mydelimiter(
The orientation of the plane unit normals of the polyhedron.
Expand Down

0 comments on commit a3bf30c

Please sign in to comment.