-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
118 lines (105 loc) · 4.37 KB
/
Copy pathCMakeLists.txt
File metadata and controls
118 lines (105 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
cmake_minimum_required(VERSION 3.13)
# Python bindings for VCellMovingBoundary via pybind11.
# pybind11 and Python3 are found in the parent CMakeLists.txt.
#
# The bindings are shipped as the `pyvcell_mbsolver` package: the compiled
# extension is the private submodule `pyvcell_mbsolver._core`, and the
# pure-Python wrapper is the package's __init__.py.
set(MB_PY_PACKAGE pyvcell_mbsolver)
pybind11_add_module(_core
pyvcellmbsolver.cpp
)
target_link_libraries(_core
PRIVATE
MovingBoundaryLib
vcommons # for tinyxml2
)
# Assemble the package in the build tree (bin/pyvcell_mbsolver/) so it is
# importable as `pyvcell_mbsolver` with PYTHONPATH=bin, without installing.
set(MB_PY_BUILD_DIR ${CMAKE_BINARY_DIR}/bin/${MB_PY_PACKAGE})
set_target_properties(_core PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${MB_PY_BUILD_DIR}
)
add_custom_command(TARGET _core POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/${MB_PY_PACKAGE}/__init__.py
${MB_PY_BUILD_DIR}/__init__.py
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/${MB_PY_PACKAGE}/py.typed
${MB_PY_BUILD_DIR}/py.typed
COMMENT "Copying pyvcell_mbsolver/{__init__.py,py.typed} into the build tree"
)
# When building a wheel via scikit-build-core, SKBUILD is defined and the
# install prefix already points inside the wheel's platlib, so destinations
# must be relative. For a plain `cmake --install`, install into site-packages.
if(SKBUILD)
set(MB_PY_INSTALL_DEST "${MB_PY_PACKAGE}")
else()
set(MB_PY_INSTALL_DEST "${Python3_SITEARCH}/${MB_PY_PACKAGE}")
endif()
install(TARGETS _core
LIBRARY DESTINATION ${MB_PY_INSTALL_DEST}
COMPONENT python
)
# Install the pure-Python wrapper (the package __init__.py) and the PEP 561
# py.typed marker so type checkers use the package's inline annotations.
install(FILES
${CMAKE_CURRENT_SOURCE_DIR}/${MB_PY_PACKAGE}/__init__.py
${CMAKE_CURRENT_SOURCE_DIR}/${MB_PY_PACKAGE}/py.typed
DESTINATION ${MB_PY_INSTALL_DEST}
COMPONENT python
)
# ---------------------------------------------------------------------------
# Python tests (pytest) — run inside a dedicated virtual environment.
#
# The venv is created (or refreshed) at configure time in the build tree so
# the test environment is reproducible and isolated from the system Python.
# pytest is installed into the venv; the extension .so is made importable
# via PYTHONPATH rather than by installing it into the venv.
# ---------------------------------------------------------------------------
if(BUILD_TESTING)
set(VENV_DIR ${CMAKE_BINARY_DIR}/python_venv)
# Platform-specific paths inside the venv
if(WIN32)
set(VENV_PYTHON ${VENV_DIR}/Scripts/python.exe)
set(VENV_PIP ${VENV_DIR}/Scripts/pip.exe)
else()
set(VENV_PYTHON ${VENV_DIR}/bin/python)
set(VENV_PIP ${VENV_DIR}/bin/pip)
endif()
# Create the venv (idempotent: safe to re-run on reconfigure)
execute_process(
COMMAND ${Python3_EXECUTABLE} -m venv ${VENV_DIR}
RESULT_VARIABLE _venv_rc
)
if(NOT _venv_rc EQUAL 0)
message(WARNING "Python venv creation failed (exit ${_venv_rc}). "
"Python tests may not run correctly.")
endif()
# Install pytest into the venv
execute_process(
COMMAND ${VENV_PIP} install --quiet pytest
RESULT_VARIABLE _pip_rc
)
if(NOT _pip_rc EQUAL 0)
message(WARNING "pip install pytest failed (exit ${_pip_rc}). "
"Python tests may not run correctly.")
endif()
# --import-mode=importlib keeps pytest from prepending the source tree to
# sys.path, which would otherwise shadow the built pyvcell_mbsolver package
# (the source dir has the wrapper but not the compiled _core extension).
add_test(
NAME PyVcellMbSolver
COMMAND ${VENV_PYTHON} -m pytest
${CMAKE_CURRENT_SOURCE_DIR}/tests
-v --tb=short --import-mode=importlib
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
# PYTHONPATH points at bin/, where the assembled `pyvcell_mbsolver` package
# (_core extension + __init__.py) lives, so it is importable without
# installing into the venv.
set_tests_properties(PyVcellMbSolver PROPERTIES
ENVIRONMENT
"PYTHONPATH=${CMAKE_BINARY_DIR}/bin"
)
endif()