Skip to content

Commit

Permalink
python extension module is now installed into site-packages/gemmi/
Browse files Browse the repository at this point in the history
and it's organized in a peculiar way, as advised here:
wjakob/nanobind#730
  • Loading branch information
wojdyr committed Oct 2, 2024
1 parent 2a8aef9 commit 8d0bd2a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 15 deletions.
20 changes: 11 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,9 @@ if (USE_PYTHON)
else()
target_link_libraries(gemmi_py PRIVATE gemmi_cpp)
endif()
set_property(TARGET gemmi_py PROPERTY OUTPUT_NAME gemmi)
set_property(TARGET gemmi_py PROPERTY OUTPUT_NAME _gemmi)
set_property(TARGET gemmi_py PROPERTY LIBRARY_OUTPUT_DIRECTORY py/gemmi)
configure_file(python/gemmi/__init__.py py/gemmi/__init__.py COPYONLY)

# nanobind gives warnings with -Wpedantic and -Wshadow
if(CMAKE_CXX_FLAGS MATCHES "-Wshadow")
Expand Down Expand Up @@ -526,7 +528,7 @@ if (USE_PYTHON)
nanobind_add_stub(
gemmi_stub
MODULE gemmi
OUTPUT __init__.pyi
OUTPUT py/gemmi/__init__.pyi
PYTHON_PATH $<TARGET_FILE_DIR:gemmi_py>
DEPENDS gemmi_py
PATTERN_FILE "${CMAKE_CURRENT_SOURCE_DIR}/python/stubs.pat"
Expand All @@ -535,7 +537,7 @@ if (USE_PYTHON)
nanobind_add_stub(
gemmi_cif_stub
MODULE gemmi.cif
OUTPUT cif.pyi
OUTPUT py/gemmi/cif.pyi
PYTHON_PATH $<TARGET_FILE_DIR:gemmi_py>
DEPENDS gemmi_py
)
Expand Down Expand Up @@ -588,22 +590,22 @@ if (USE_PYTHON)
if (DEFINED SKBUILD)
# When building via pip, Python_SITEARCH is /tmp.../wheel/platlib,
# different from the actual install path. Best guess:
set(rpath_rel "../../../${CMAKE_INSTALL_LIBDIR}")
set(rpath_rel "../../../../${CMAKE_INSTALL_LIBDIR}")
else()
file(RELATIVE_PATH rpath_rel
"${Python_SITEARCH}" "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
"${Python_SITEARCH}/gemmi" "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
endif()
set_target_properties(gemmi_py PROPERTIES INSTALL_RPATH "${origin}/${rpath_rel}")
endif()
install(TARGETS gemmi_py DESTINATION "${Python_SITEARCH}" COMPONENT py)
install(TARGETS gemmi_py DESTINATION "${Python_SITEARCH}/gemmi" COMPONENT py)
install(DIRECTORY examples DESTINATION "${Python_SITEARCH}/gemmi" COMPONENT py
FILES_MATCHING PATTERN "*.py"
PATTERN "[._]*" EXCLUDE)
if (GENERATE_STUBS AND NOT CMAKE_CROSSCOMPILING)
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/py.typed"
"${CMAKE_CURRENT_BINARY_DIR}/__init__.pyi"
"${CMAKE_CURRENT_BINARY_DIR}/cif.pyi"
"${CMAKE_CURRENT_BINARY_DIR}/py/py.typed"
"${CMAKE_CURRENT_BINARY_DIR}/py/__init__.pyi"
"${CMAKE_CURRENT_BINARY_DIR}/py/cif.pyi"
DESTINATION "${Python_SITEARCH}/gemmi" COMPONENT py)
endif()
endif()
2 changes: 1 addition & 1 deletion docs/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ Code derived from the following projects is used in the library:
* `QCProt <https://theobald.brandeis.edu/qcp/>`_ -- superposition method
in `qcp.hpp` is taken from QCProt and adapted to our project. License: BSD.
* `Larch <https://github.com/xraypy/xraylarch>`_ -- calculation of f' and f"
in `fprime.hpp` is based on CromerLiberman code from Larch.
in `fprime.cpp` is based on CromerLiberman code from Larch.
License: 2-clause BSD.

Projects included under `third_party/` that are not used in the library
Expand Down
3 changes: 2 additions & 1 deletion python/gemmi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ void add_misc(nb::module_& m) {
}, nb::arg("nbins"), nb::arg("obs"), nb::arg("calc"));
}

NB_MODULE(gemmi, mg) {
NB_MODULE(_gemmi, mg_) {
nb::module_ mg = nb::module_::import_("gemmi");
mg.doc() = "Python bindings to GEMMI - a library used in macromolecular\n"
"crystallography and related fields";
mg.attr("__version__") = GEMMI_VERSION;
Expand Down
2 changes: 2 additions & 0 deletions python/gemmi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Importing C++ extension populates this module.
from . import _gemmi
8 changes: 4 additions & 4 deletions run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# PYTHONPATH. Locally, I'm using bash function "gpy" that starts python with
# gemmi imported ("gpy" for interactive mode, "gpy file.py" for script):
# echo "import gemmi" > $HOME/.import-gemmi.py
# gpy() { PYTHONPATH=$HOME/gemmi/gemmi/build PYTHONSTARTUP=$HOME/.import-gemmi.py python3 -q "$@"; }
# gpy() { PYTHONPATH=$HOME/gemmi/gemmi/build/py PYTHONSTARTUP=$HOME/.import-gemmi.py python3 -q "$@"; }

# set up variables
set -eu
Expand All @@ -33,19 +33,19 @@ if [ $# != 0 ] && [ $1 = n ]; then
shift
else
(cd $BUILD_DIR && make -j4 all check)
./tools/cmp-size.py build/gemmi build/*gemmi*.so
./tools/cmp-size.py build/gemmi build/libgemmi_cpp.so build/py/gemmi/_gemmi*
./tools/docs-help.sh
fi
./tools/header-list.py >docs/headers.rst
(cd docs && make -j4 html SPHINXOPTS="-q -n")

# Run tests and checks.
if [ $# = 0 ] || [ $1 != i ]; then
export PYTHONPATH=$BUILD_DIR
export PYTHONPATH=$BUILD_DIR/py
export PATH="$BUILD_DIR:$PATH"
fi
$PYTHON -m unittest discover -s tests
grep :: $BUILD_DIR/*.pyi ||:
grep :: $BUILD_DIR/py/gemmi/*.pyi ||:

if [ -z "${NO_DOCTEST-}" ]; then
# 'make doctest' works only if sphinx-build was installed for python3.
Expand Down

0 comments on commit 8d0bd2a

Please sign in to comment.