Skip to content

Commit

Permalink
#24 restructure cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
kwabenantim committed May 17, 2024
1 parent f74cb66 commit a611e13
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 180 deletions.
152 changes: 147 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

#========================================
# Setup PyChaste project
#========================================

# PyChaste needs the cell_based component (and its dependencies)
find_package(Chaste COMPONENTS cell_based)

# PyChaste needs some additional VTK libraries
Expand Down Expand Up @@ -73,12 +78,149 @@ else()
endif()

if (VTK_MAJOR_VERSION LESS 9)
list(APPEND Chaste_INCLUDES ${VTK_INCLUDE_DIRS})
list(APPEND Chaste_project_PyChaste_INCLUDE_DIRS ${VTK_INCLUDE_DIRS})
list(APPEND Chaste_THIRD_PARTY_LIBRARIES ${VTK_LIBRARIES})
list(APPEND Chaste_INCLUDES "${VTK_INCLUDE_DIRS}")
list(APPEND Chaste_project_PyChaste_INCLUDE_DIRS "${VTK_INCLUDE_DIRS}")
list(APPEND Chaste_THIRD_PARTY_LIBRARIES "${VTK_LIBRARIES}")
else()
target_link_libraries(Chaste_COMMON_DEPS INTERFACE ${VTK_LIBRARIES})
endif ()

# Do Chaste project preprocessing, which results in something like:
# add_custom_target(project_PyChaste)
# set(Chaste_project_PyChaste_SOURCE_DIR ...)
# set(Chaste_project_PyChaste_INCLUDE_DIRS ...)
# include_directories("${Chaste_THIRD_PARTY_INCLUDE_DIRS}")
# include_directories("${Chaste_project_PyChaste_INCLUDE_DIRS}")
# include_directories("${Chaste_INCLUDE_DIRS}")
# add_library(chaste_project_PyChaste ...)
# target_link_libraries(chaste_project_PyChaste ...)
chaste_do_project(PyChaste)

# Include the Python wrapping build logic
include(${CMAKE_CURRENT_SOURCE_DIR}/WrapPython.cmake)
#========================================
# Compiler options
#========================================
add_compile_options(-Wno-unused-local-typedefs)

if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# https://stackoverflow.com/questions/25365160/boostmultiprecisionfloat128-and-c11
add_compile_options(-fext-numeric-literals)
endif()

#========================================
# PyChaste non-wrapper C++ code
#========================================

# Non-wrapper code locations
set(PYCHASTE_INCLUDE_DIRS
${CMAKE_CURRENT_SOURCE_DIR}/src/
${CMAKE_CURRENT_SOURCE_DIR}/src/cell_based/
${CMAKE_CURRENT_SOURCE_DIR}/src/ode/
${CMAKE_CURRENT_SOURCE_DIR}/src/tutorial/
${CMAKE_CURRENT_SOURCE_DIR}/src/visualization/
${CMAKE_CURRENT_SOURCE_DIR}/dynamic/)

# Non-wrapper code needs to be put in a separate shared library
set(PYCHASTE_SHARED_LIB
"${CMAKE_CURRENT_BINARY_DIR}/libchaste_project_PyChaste${CMAKE_SHARED_LIBRARY_SUFFIX}")

#========================================
# Target for wrapper auto-generation
#========================================
add_custom_target(project_PyChaste_Python_Bindings)
add_custom_command(TARGET project_PyChaste_Python_Bindings
COMMAND cppwg ${CMAKE_SOURCE_DIR}
-w ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers
-p ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrapper_generators/package_info.yaml
-i ${PYCHASTE_INCLUDE_DIRS} ${Chaste_INCLUDE_DIRS} ${Chaste_THIRD_PARTY_INCLUDE_DIRS}
--std c++17
)

#========================================
# Build python modules from wrappers
#========================================
include_directories(${PYCHASTE_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers)
include_directories(${PYTHON3_INCLUDE_DIRS})

add_subdirectory(dynamic/pybind11)

# Copy python package structure to build directory, ignoring existing shared libraries
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/src/python/
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/python/
PATTERN "*${CMAKE_SHARED_LIBRARY_SUFFIX}" EXCLUDE)

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/test/python/
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/python/test/)

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/doc/
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/python/doc/)

# List of modules to build into shared libraries
set (PYCHASTE_PYTHON_MODULES
# Modules with auto-generated wrappers
core
ode
pde
mesh
cell_based
visualization
tutorial
# Modules with manual wrappers
preload
tutorial_manual)

# Locations to put each module library
set (PYCHASTE_PYTHON_MODULE_LOCATIONS
# Modules with auto-generated wrappers
${CMAKE_CURRENT_BINARY_DIR}/python/chaste/core/
${CMAKE_CURRENT_BINARY_DIR}/python/chaste/ode/
${CMAKE_CURRENT_BINARY_DIR}/python/chaste/pde/
${CMAKE_CURRENT_BINARY_DIR}/python/chaste/mesh/
${CMAKE_CURRENT_BINARY_DIR}/python/chaste/cell_based/
${CMAKE_CURRENT_BINARY_DIR}/python/chaste/visualization/
${CMAKE_CURRENT_BINARY_DIR}/python/chaste/tutorial/
# Modules with manual wrappers
${CMAKE_CURRENT_BINARY_DIR}/python/chaste/
${CMAKE_CURRENT_BINARY_DIR}/python/chaste/tutorial/)

# Create a shared library target for each module
list(LENGTH PYCHASTE_PYTHON_MODULES max_module_idx)
math(EXPR max_module_idx "${max_module_idx} - 1")
set(module_prefix "_chaste_project_PyChaste_")

foreach(idx RANGE ${max_module_idx})
list(GET PYCHASTE_PYTHON_MODULES ${idx} module_name)
list(GET PYCHASTE_PYTHON_MODULE_LOCATIONS ${idx} module_dir)

# Glob the module's wrapper code from the `dynamic` directory
file(GLOB module_sources ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers/${module_name}/*.cpp)

# The module library name here must be the same as the pybind11 module name
# defined in the main wrapper e.g. `dynamic/wrappers/ode/ode.main.cpp`
# defines a pybind11 module `_chaste_project_PyChaste_ode`. By convention,
# the name starts with an underscore. The usual 'lib' prefix is disabled.
add_library(${module_prefix}${module_name} SHARED ${module_sources})

set_target_properties(${module_prefix}${module_name}
PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${module_dir}
PREFIX "${PYTHON_MODULE_PREFIX}"
SUFFIX "${CMAKE_SHARED_LIBRARY_SUFFIX}")

# The order here is important - pybind11 and python come first
target_link_libraries(${module_prefix}${module_name}
pybind11::module
${PYTHON3_LIBRARIES}
${Chaste_THIRD_PARTY_LIBRARIES}
${Chaste_LIBRARIES}
${PYCHASTE_SHARED_LIB})

add_dependencies(${module_prefix}${module_name} chaste_project_PyChaste)
endforeach()

# Target for building all module shared libraries
add_custom_target(project_PyChaste_Python)
foreach(idx RANGE ${max_module_idx})
list(GET PYCHASTE_PYTHON_MODULES ${idx} module_name)
add_dependencies(project_PyChaste_Python ${module_prefix}${module_name})
endforeach()
39 changes: 0 additions & 39 deletions ProjectIncludes.cmake

This file was deleted.

136 changes: 0 additions & 136 deletions WrapPython.cmake

This file was deleted.

0 comments on commit a611e13

Please sign in to comment.