|
| 1 | +# This file is part of 4C multiphysics licensed under the |
| 2 | +# GNU Lesser General Public License v3.0 or later. |
| 3 | +# |
| 4 | +# See the LICENSE.md file in the top-level for license information. |
| 5 | +# |
| 6 | +# SPDX-License-Identifier: LGPL-3.0-or-later |
| 7 | + |
| 8 | +# Automatically creates a python binding submodule for all sources and headers in the current directory. The target |
| 9 | +# will be named based on the folder name. If this function is called recursively inside an already |
| 10 | +# defined submodule, the sources are appended to the already defined submodule. The submodule name is returned |
| 11 | +# in the variable AUTO_DEFINED_SUBMODULE_NAME which is set at the call site. |
| 12 | +function(four_c_auto_define_python_binding_submodule) |
| 13 | + if(NOT FOUR_C_WITH_PYBIND11) |
| 14 | + return() |
| 15 | + endif() |
| 16 | + |
| 17 | + set(options "") |
| 18 | + set(oneValueArgs MODULE) |
| 19 | + set(multiValueArgs "") |
| 20 | + cmake_parse_arguments( |
| 21 | + _parsed |
| 22 | + "${options}" |
| 23 | + "${oneValueArgs}" |
| 24 | + "${multiValueArgs}" |
| 25 | + ${ARGN} |
| 26 | + ) |
| 27 | + if(DEFINED _parsed_UNPARSED_ARGUMENTS) |
| 28 | + message(FATAL_ERROR "There are unparsed arguments: ${_parsed_UNPARSED_ARGUMENTS}") |
| 29 | + endif() |
| 30 | + |
| 31 | + if(_parsed_MODULE) |
| 32 | + set(_bindings_for_module ${_parsed_MODULE}) |
| 33 | + else() |
| 34 | + if("${FOUR_C_CURRENTLY_DEFINED_PARENT_SUBMODULE}" STREQUAL "") |
| 35 | + message( |
| 36 | + FATAL_ERROR |
| 37 | + "No parent module is set. Either give the module these bindings belongs to or call this functions inside a module." |
| 38 | + ) |
| 39 | + endif() |
| 40 | + |
| 41 | + set(_bindings_for_module "${FOUR_C_CURRENTLY_DEFINED_PARENT_SUBMODULE}") |
| 42 | + endif() |
| 43 | + |
| 44 | + if(NOT TARGET "${_bindings_for_module}_objs") |
| 45 | + message( |
| 46 | + FATAL_ERROR |
| 47 | + "Tried to add bindings for a module named '${_bindings_for_module}' which is not a known module name." |
| 48 | + ) |
| 49 | + endif() |
| 50 | + |
| 51 | + # create the name of the current binding |
| 52 | + set(_target pybind11_${_bindings_for_module}) |
| 53 | + |
| 54 | + # create an object target for the current submodule (and fill it with a dummy cpp-file) |
| 55 | + add_library(${_target}_objs OBJECT ${PROJECT_SOURCE_DIR}/cmake/dummy.cpp) |
| 56 | + |
| 57 | + # Add all source files of this folder to this target |
| 58 | + file(GLOB_RECURSE _sources CONFIGURE_DEPENDS *.cpp) |
| 59 | + target_sources(${_target}_objs PRIVATE ${_sources}) |
| 60 | + |
| 61 | + # Link the python binding configuration to this target |
| 62 | + target_link_libraries(${_target}_objs PUBLIC py4C_config) |
| 63 | + |
| 64 | + # Python bindings require position independent code so that Python can dynamically link the library |
| 65 | + target_link_libraries(${_target}_objs PRIVATE four_c_private_compile_interface) |
| 66 | + |
| 67 | + # Link all dependencies of the respective 4C module to this python binding submodule |
| 68 | + target_link_libraries(${_target}_objs PRIVATE ${_bindings_for_module}_module) |
| 69 | + |
| 70 | + # Add the current submodule object file to the main python bindings target |
| 71 | + target_link_libraries(${FOUR_C_PYTHON_BINDINGS_PROJECT_NAME} PRIVATE ${_target}_objs) |
| 72 | + |
| 73 | + # Now check if there are more directories that contain CMakeLists.txt. If yes, we also add those. |
| 74 | + # For this action, we become the parent submodule of the sub-submodules we are about to define. |
| 75 | + set(FOUR_C_CURRENTLY_DEFINED_PARENT_SUBMODULE ${_target}) |
| 76 | + # Recursively add all subdirectories that contain CMakeLists.txt files. |
| 77 | + # N.B. We need to directly glob for CMakeLists.txt files here to ensure |
| 78 | + # the glob reruns when a new CMakeLists.txt is added. |
| 79 | + file( |
| 80 | + GLOB children |
| 81 | + RELATIVE ${CMAKE_CURRENT_LIST_DIR} |
| 82 | + CONFIGURE_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/*/CMakeLists.txt |
| 83 | + ) |
| 84 | + foreach(child ${children}) |
| 85 | + get_filename_component(_subdir ${child} DIRECTORY) |
| 86 | + add_subdirectory(${_subdir}) |
| 87 | + endforeach() |
| 88 | + |
| 89 | + # Simulate a "return" by setting a variable at the call site |
| 90 | + set(AUTO_DEFINED_SUBMODULE_NAME |
| 91 | + ${_target} |
| 92 | + PARENT_SCOPE |
| 93 | + ) |
| 94 | +endfunction() |
0 commit comments