|
30 | 30 | # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31 | 31 | #
|
32 | 32 |
|
| 33 | +#======================================== |
| 34 | +# Setup PyChaste project |
| 35 | +#======================================== |
| 36 | + |
| 37 | +# PyChaste needs the cell_based component (and its dependencies) |
33 | 38 | find_package(Chaste COMPONENTS cell_based)
|
34 | 39 |
|
35 | 40 | # PyChaste needs some additional VTK libraries
|
@@ -73,12 +78,149 @@ else()
|
73 | 78 | endif()
|
74 | 79 |
|
75 | 80 | if (VTK_MAJOR_VERSION LESS 9)
|
76 |
| - list(APPEND Chaste_INCLUDES ${VTK_INCLUDE_DIRS}) |
77 |
| - list(APPEND Chaste_project_PyChaste_INCLUDE_DIRS ${VTK_INCLUDE_DIRS}) |
78 |
| - list(APPEND Chaste_THIRD_PARTY_LIBRARIES ${VTK_LIBRARIES}) |
| 81 | + list(APPEND Chaste_INCLUDES "${VTK_INCLUDE_DIRS}") |
| 82 | + list(APPEND Chaste_project_PyChaste_INCLUDE_DIRS "${VTK_INCLUDE_DIRS}") |
| 83 | + list(APPEND Chaste_THIRD_PARTY_LIBRARIES "${VTK_LIBRARIES}") |
| 84 | +else() |
| 85 | + target_link_libraries(Chaste_COMMON_DEPS INTERFACE ${VTK_LIBRARIES}) |
79 | 86 | endif ()
|
80 | 87 |
|
| 88 | +# Do Chaste project preprocessing, which results in something like: |
| 89 | +# add_custom_target(project_PyChaste) |
| 90 | +# set(Chaste_project_PyChaste_SOURCE_DIR ...) |
| 91 | +# set(Chaste_project_PyChaste_INCLUDE_DIRS ...) |
| 92 | +# include_directories("${Chaste_THIRD_PARTY_INCLUDE_DIRS}") |
| 93 | +# include_directories("${Chaste_project_PyChaste_INCLUDE_DIRS}") |
| 94 | +# include_directories("${Chaste_INCLUDE_DIRS}") |
| 95 | +# add_library(chaste_project_PyChaste ...) |
| 96 | +# target_link_libraries(chaste_project_PyChaste ...) |
81 | 97 | chaste_do_project(PyChaste)
|
82 | 98 |
|
83 |
| -# Include the Python wrapping build logic |
84 |
| -include(${CMAKE_CURRENT_SOURCE_DIR}/WrapPython.cmake) |
| 99 | +#======================================== |
| 100 | +# Compiler options |
| 101 | +#======================================== |
| 102 | +add_compile_options(-Wno-unused-local-typedefs) |
| 103 | + |
| 104 | +if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") |
| 105 | + # https://stackoverflow.com/questions/25365160/boostmultiprecisionfloat128-and-c11 |
| 106 | + add_compile_options(-fext-numeric-literals) |
| 107 | +endif() |
| 108 | + |
| 109 | +#======================================== |
| 110 | +# PyChaste non-wrapper C++ code |
| 111 | +#======================================== |
| 112 | + |
| 113 | +# Non-wrapper code locations |
| 114 | +set(PYCHASTE_INCLUDE_DIRS |
| 115 | + ${CMAKE_CURRENT_SOURCE_DIR}/src/ |
| 116 | + ${CMAKE_CURRENT_SOURCE_DIR}/src/cell_based/ |
| 117 | + ${CMAKE_CURRENT_SOURCE_DIR}/src/ode/ |
| 118 | + ${CMAKE_CURRENT_SOURCE_DIR}/src/tutorial/ |
| 119 | + ${CMAKE_CURRENT_SOURCE_DIR}/src/visualization/ |
| 120 | + ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/) |
| 121 | + |
| 122 | +# Non-wrapper code needs to be put in a separate shared library |
| 123 | +set(PYCHASTE_SHARED_LIB |
| 124 | + "${CMAKE_CURRENT_BINARY_DIR}/libchaste_project_PyChaste${CMAKE_SHARED_LIBRARY_SUFFIX}") |
| 125 | + |
| 126 | +#======================================== |
| 127 | +# Target for wrapper auto-generation |
| 128 | +#======================================== |
| 129 | +add_custom_target(project_PyChaste_Python_Bindings) |
| 130 | +add_custom_command(TARGET project_PyChaste_Python_Bindings |
| 131 | + COMMAND cppwg ${CMAKE_SOURCE_DIR} |
| 132 | + -w ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers |
| 133 | + -p ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrapper_generators/package_info.yaml |
| 134 | + -i ${PYCHASTE_INCLUDE_DIRS} ${Chaste_INCLUDE_DIRS} ${Chaste_THIRD_PARTY_INCLUDE_DIRS} |
| 135 | + --std c++17 |
| 136 | +) |
| 137 | + |
| 138 | +#======================================== |
| 139 | +# Build python modules from wrappers |
| 140 | +#======================================== |
| 141 | +include_directories(${PYCHASTE_INCLUDE_DIRS}) |
| 142 | +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers) |
| 143 | +include_directories(${PYTHON3_INCLUDE_DIRS}) |
| 144 | + |
| 145 | +add_subdirectory(dynamic/pybind11) |
| 146 | + |
| 147 | +# Copy python package structure to build directory, ignoring existing shared libraries |
| 148 | +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/src/python/ |
| 149 | + DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/python/ |
| 150 | + PATTERN "*${CMAKE_SHARED_LIBRARY_SUFFIX}" EXCLUDE) |
| 151 | + |
| 152 | +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/test/python/ |
| 153 | + DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/python/test/) |
| 154 | + |
| 155 | +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/doc/ |
| 156 | + DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/python/doc/) |
| 157 | + |
| 158 | +# List of modules to build into shared libraries |
| 159 | +set (PYCHASTE_PYTHON_MODULES |
| 160 | + # Modules with auto-generated wrappers |
| 161 | + core |
| 162 | + ode |
| 163 | + pde |
| 164 | + mesh |
| 165 | + cell_based |
| 166 | + visualization |
| 167 | + tutorial |
| 168 | + # Modules with manual wrappers |
| 169 | + preload |
| 170 | + tutorial_manual) |
| 171 | + |
| 172 | +# Locations to put each module library |
| 173 | +set (PYCHASTE_PYTHON_MODULE_LOCATIONS |
| 174 | + # Modules with auto-generated wrappers |
| 175 | + ${CMAKE_CURRENT_BINARY_DIR}/python/chaste/core/ |
| 176 | + ${CMAKE_CURRENT_BINARY_DIR}/python/chaste/ode/ |
| 177 | + ${CMAKE_CURRENT_BINARY_DIR}/python/chaste/pde/ |
| 178 | + ${CMAKE_CURRENT_BINARY_DIR}/python/chaste/mesh/ |
| 179 | + ${CMAKE_CURRENT_BINARY_DIR}/python/chaste/cell_based/ |
| 180 | + ${CMAKE_CURRENT_BINARY_DIR}/python/chaste/visualization/ |
| 181 | + ${CMAKE_CURRENT_BINARY_DIR}/python/chaste/tutorial/ |
| 182 | + # Modules with manual wrappers |
| 183 | + ${CMAKE_CURRENT_BINARY_DIR}/python/chaste/ |
| 184 | + ${CMAKE_CURRENT_BINARY_DIR}/python/chaste/tutorial/) |
| 185 | + |
| 186 | +# Create a shared library target for each module |
| 187 | +list(LENGTH PYCHASTE_PYTHON_MODULES max_module_idx) |
| 188 | +math(EXPR max_module_idx "${max_module_idx} - 1") |
| 189 | +set(module_prefix "_chaste_project_PyChaste_") |
| 190 | + |
| 191 | +foreach(idx RANGE ${max_module_idx}) |
| 192 | + list(GET PYCHASTE_PYTHON_MODULES ${idx} module_name) |
| 193 | + list(GET PYCHASTE_PYTHON_MODULE_LOCATIONS ${idx} module_dir) |
| 194 | + |
| 195 | + # Glob the module's wrapper code from the `dynamic` directory |
| 196 | + file(GLOB module_sources ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers/${module_name}/*.cpp) |
| 197 | + |
| 198 | + # The module library name here must be the same as the pybind11 module name |
| 199 | + # defined in the main wrapper e.g. `dynamic/wrappers/ode/ode.main.cpp` |
| 200 | + # defines a pybind11 module `_chaste_project_PyChaste_ode`. By convention, |
| 201 | + # the name starts with an underscore. The usual 'lib' prefix is disabled. |
| 202 | + add_library(${module_prefix}${module_name} SHARED ${module_sources}) |
| 203 | + |
| 204 | + set_target_properties(${module_prefix}${module_name} |
| 205 | + PROPERTIES |
| 206 | + LIBRARY_OUTPUT_DIRECTORY ${module_dir} |
| 207 | + PREFIX "${PYTHON_MODULE_PREFIX}" |
| 208 | + SUFFIX "${CMAKE_SHARED_LIBRARY_SUFFIX}") |
| 209 | + |
| 210 | + # The order here is important - pybind11 and python come first |
| 211 | + target_link_libraries(${module_prefix}${module_name} |
| 212 | + pybind11::module |
| 213 | + ${PYTHON3_LIBRARIES} |
| 214 | + ${Chaste_THIRD_PARTY_LIBRARIES} |
| 215 | + ${Chaste_LIBRARIES} |
| 216 | + ${PYCHASTE_SHARED_LIB}) |
| 217 | + |
| 218 | + add_dependencies(${module_prefix}${module_name} chaste_project_PyChaste) |
| 219 | +endforeach() |
| 220 | + |
| 221 | +# Target for building all module shared libraries |
| 222 | +add_custom_target(project_PyChaste_Python) |
| 223 | +foreach(idx RANGE ${max_module_idx}) |
| 224 | + list(GET PYCHASTE_PYTHON_MODULES ${idx} module_name) |
| 225 | + add_dependencies(project_PyChaste_Python ${module_prefix}${module_name}) |
| 226 | +endforeach() |
0 commit comments