Skip to content

Commit

Permalink
Set module version with setuptools-scm
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloferz committed Oct 9, 2024
1 parent 026ddb3 commit b0cc5e1
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
uses: docker/build-push-action@v2
with:
context: .

trunk:
runs-on: ubuntu-latest
steps:
Expand Down
11 changes: 7 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
FROM ssages/pysages-hoomd:latest
WORKDIR /openmm-dlex/.docker_build
WORKDIR /openmm-dlext

COPY . ../
RUN cmake .. && make install
RUN python -c "import openmm_dlext"
# Build the plugin
COPY . .
RUN cmake -S . -B build && cmake --build build --target install && rm -rf build

# Test it can be loaded
RUN python -c "import openmm.dlext"
23 changes: 23 additions & 0 deletions cmake/Modules/OpenMMTools.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,26 @@ print(os.path.normpath(os.path.join(openmm.__file__, os.pardir, os.pardir)), end
)
set(OpenMMDLExt_Python_PATH "${OpenMM_Python_PATH}" PARENT_SCOPE)
endfunction()

# This requires setuptools-scm to be installed as we will use it to setup the module version
function(set_version target)
find_package(Python QUIET COMPONENTS Interpreter)
set(GET_VERSION_SCRIPT "
from setuptools_scm import get_version
print(get_version(), end='')"
)
execute_process(
COMMAND ${Python_EXECUTABLE} -c ${GET_VERSION_SCRIPT}
WORKING_DIRECTORY ${${PROJECT_NAME}_SOURCE_DIR}
ERROR_VARIABLE error
OUTPUT_VARIABLE GIT_VERSION
RESULT_VARIABLE exit_code
)
if(NOT exit_code EQUAL 0)
message(FATAL_ERROR
"The build process depends on setuptools-scm, make sure it is installed. "
"Got the following error:\n${error}"
)
endif()
target_compile_definitions(${target} PUBLIC PYPROJECT_VERSION=${GIT_VERSION})
endfunction()
2 changes: 2 additions & 0 deletions wrappers/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ set(pybind11_MODULE_NAME "_api")

pybind11_add_module(${pybind11_MODULE_NAME}_fallback MODULE "")

set_version(${pybind11_MODULE_NAME}_fallback)
target_sources(${pybind11_MODULE_NAME}_fallback PRIVATE PyDLExt.cpp)
target_compile_features(${pybind11_MODULE_NAME}_fallback PRIVATE cxx_std_11)
target_link_libraries(${pybind11_MODULE_NAME}_fallback PRIVATE ${PROJECT_NAME})
Expand All @@ -63,6 +64,7 @@ install(TARGETS ${pybind11_MODULE_NAME}_fallback
if(BUILD_CUDA_LIB)
pybind11_add_module(${pybind11_MODULE_NAME} MODULE "")

set_version(${pybind11_MODULE_NAME})
target_sources(${pybind11_MODULE_NAME} PRIVATE PyDLExt.cpp)
target_compile_features(${pybind11_MODULE_NAME} PRIVATE cxx_std_11)
target_link_libraries(${pybind11_MODULE_NAME} PRIVATE ${PROJECT_NAME}CUDA)
Expand Down
1 change: 1 addition & 0 deletions wrappers/python/PyDLExt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ PYBIND11_MODULE(_api, m)
// instead of `openmm.dlext._api.x`.
py::str module_name = m.attr("__name__");
m.attr("__name__") = "openmm.dlext";
m.attr("__version__") = Py_STRINGIFY(PYPROJECT_VERSION);

// Enums
py::enum_<DLDeviceType>(m, "DeviceType").value("CPU", kDLCPU).value("GPU", kDLCUDA);
Expand Down

0 comments on commit b0cc5e1

Please sign in to comment.