From a52cd0c70292598f7c8cee6f441476453e62aed3 Mon Sep 17 00:00:00 2001 From: Cristian Le Date: Thu, 25 Jan 2024 17:39:37 +0100 Subject: [PATCH 1/2] Fix python install path convention - Added potentially missing _version.py file Signed-off-by: Cristian Le --- ChangeLog.md | 4 ++++ pyproject.toml | 1 - python/CMakeLists.txt | 19 ++++++++++++------- python/_version.py.in | 13 +++++++++++++ 4 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 python/_version.py.in diff --git a/ChangeLog.md b/ChangeLog.md index 015a85a24..682091229 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -19,6 +19,10 @@ GitHub release pages and in the git history. (Note: previously the project was not tested for the minimum CMake version) - Migrated the example and package test to the ctest test-suite +### Python API + +- Fixed the Python module build and installation in the pure CMake environment (without scikit-build-core) + ### CI - [\[#425\]](https://github.com/spglib/spglib/pull/425) - Update to codecov v4 diff --git a/pyproject.toml b/pyproject.toml index a27b4eac7..e9452d4ee 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -81,7 +81,6 @@ testing = [ [tool.scikit-build] wheel.packages = ["python/spglib"] -wheel.install-dir = "spglib" metadata.version.provider = "scikit_build_core.metadata.setuptools_scm" sdist.include = ["python/spglib/_version.py"] diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 6df9a8ce0..423ecd690 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -69,6 +69,11 @@ set_target_properties(Spglib_python PROPERTIES target_link_libraries(Spglib_python PRIVATE Spglib::symspg Python::NumPy ) +# _version.py may not have been populated in source yet, use a dummy file for the build environment +# TODO: Use a scikit-build-cli or other CLI to populate the metadata files +if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/spglib/_version.py) + configure_file(_version.py.in spglib/_version.py) +endif () #[=============================================================================[ # Install or Export # @@ -80,7 +85,7 @@ if (NOT Python_INSTALL_DIR) set(Python_INSTALL_DIR ".") else () # Otherwise try to install in current python executable's setup - set(Python_INSTALL_DIR ${Python_SITEARCH}/spglib) + set(Python_INSTALL_DIR ${Python_SITEARCH}) endif () endif () if (SPGLIB_INSTALL) @@ -89,19 +94,19 @@ if (SPGLIB_INSTALL) # TODO: Cmake forces to install PUBLIC_HEADER when defined # https://gitlab.kitware.com/cmake/cmake/-/issues/24326 install(TARGETS Spglib_symspg - LIBRARY DESTINATION ${Python_INSTALL_DIR} COMPONENT Spglib_Runtime + LIBRARY DESTINATION ${Python_INSTALL_DIR}/spglib COMPONENT Spglib_Runtime NAMELINK_COMPONENT Spglib_Development - ARCHIVE DESTINATION ${Python_INSTALL_DIR} COMPONENT Spglib_Development - PUBLIC_HEADER DESTINATION ${Python_INSTALL_DIR} COMPONENT Spglib_Development - RUNTIME DESTINATION ${Python_INSTALL_DIR} COMPONENT Spglib_Runtime + ARCHIVE DESTINATION ${Python_INSTALL_DIR}/spglib COMPONENT Spglib_Development + PUBLIC_HEADER DESTINATION ${Python_INSTALL_DIR}/spglib COMPONENT Spglib_Development + RUNTIME DESTINATION ${Python_INSTALL_DIR}/spglib COMPONENT Spglib_Runtime ) endif () install(TARGETS Spglib_python - LIBRARY DESTINATION ${Python_INSTALL_DIR} COMPONENT Spglib_Runtime + LIBRARY DESTINATION ${Python_INSTALL_DIR}/spglib COMPONENT Spglib_Runtime ) # Install spglib module to local python path if (NOT SKBUILD) - install(DIRECTORY spglib/ DESTINATION ${Python_INSTALL_DIR} COMPONENT Spglib_Runtime) + install(DIRECTORY spglib/ DESTINATION ${Python_INSTALL_DIR}/spglib COMPONENT Spglib_Runtime) endif () endif () diff --git a/python/_version.py.in b/python/_version.py.in new file mode 100644 index 000000000..4ea160fa1 --- /dev/null +++ b/python/_version.py.in @@ -0,0 +1,13 @@ +# File generated by CMake Spglib_Python project +# This file is for debugging build purposes only +# Do NOT use this file for production builds. Use the file generated from scikit-build-core instead + +from __future__ import annotations + +version: str +__version__: str +__version_tuple__: tuple[int | str, ...] +version_tuple: tuple[int | str, ...] + +__version__ = version = "0.0.0" +__version_tuple__ = version_tuple = (0, 0, 0) From b21cb6f1619d1b47e011b0fbee17b8fdf3b13ed5 Mon Sep 17 00:00:00 2001 From: Cristian Le Date: Thu, 25 Jan 2024 17:46:53 +0100 Subject: [PATCH 2/2] Refactor ctest use of pytest Signed-off-by: Cristian Le --- .github/workflows/step_test.yaml | 11 +++++------ python/CMakeLists.txt | 10 ++++++++++ test/example/CMakeLists.txt | 4 ++++ test/functional/python/CMakeLists.txt | 3 +++ 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/.github/workflows/step_test.yaml b/.github/workflows/step_test.yaml index 0e78c0986..c81934e51 100644 --- a/.github/workflows/step_test.yaml +++ b/.github/workflows/step_test.yaml @@ -73,12 +73,11 @@ jobs: with: python-version: ${{ matrix.python-version }} allow-prereleases: ${{ matrix.pre || false }} - # TODO: Replace with editable install once configure presets are available - - name: Install python bindings - run: > - python3 -m pip install - ${{ matrix.pre && '--pre' }} - .[test] + - name: Install dependencies + run: | + python3 -m pip install pip-tools + pip-compile --extra=test --strip-extras pyproject.toml ${{ matrix.pre && '--pre' }} + python3 -m pip install -r requirements.txt - uses: lukka/get-cmake@latest - name: Run CMake configuration for ${{ matrix.toolchain }} toolchain uses: lukka/run-cmake@v10.3 diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 423ecd690..f7ea336a8 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -75,6 +75,16 @@ if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/spglib/_version.py) configure_file(_version.py.in spglib/_version.py) endif () +# Copy all python packages to the build directory +file(COPY spglib + DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/) +# Link the built library to the package in the binary directory +add_custom_command(TARGET Spglib_python POST_BUILD + COMMAND ${CMAKE_COMMAND} -E create_symlink + $ + ${CMAKE_CURRENT_BINARY_DIR}/spglib/$ +) + #[=============================================================================[ # Install or Export # ]=============================================================================] diff --git a/test/example/CMakeLists.txt b/test/example/CMakeLists.txt index 74c3161b1..2e1fea310 100644 --- a/test/example/CMakeLists.txt +++ b/test/example/CMakeLists.txt @@ -25,4 +25,8 @@ if (SPGLIB_WITH_Python) add_test (NAME example-python-full COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/python_api/example_full.py ) + set_property(TEST example-python example-python-full + APPEND PROPERTY + ENVIRONMENT PYTHONPATH=${Spglib_Python_BINARY_DIR} + ) endif () diff --git a/test/functional/python/CMakeLists.txt b/test/functional/python/CMakeLists.txt index 391584a59..2237e38cf 100644 --- a/test/functional/python/CMakeLists.txt +++ b/test/functional/python/CMakeLists.txt @@ -5,3 +5,6 @@ add_test(NAME pytests COMMAND ${Python_EXECUTABLE} -m pytest WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/.. ) +set_property(TEST pytests APPEND PROPERTY + ENVIRONMENT PYTHONPATH=${Spglib_Python_BINARY_DIR} +)