|
| 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 create a library for the 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 module, the sources are appended to the already defined module. The module name is returned |
| 11 | +# in the variable AUTO_DEFINED_MODULE_NAME which is set at the call site. |
| 12 | +function(four_c_auto_define_pybind11_module) |
| 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_MODULE}" 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_MODULE}") |
| 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 | + set(_bindings_name_base "unittests_${_bindings_for_module}") |
| 52 | + |
| 53 | + file(GLOB_RECURSE _sources CONFIGURE_DEPENDS *.cpp) |
| 54 | + |
| 55 | + foreach(_source ${_sources}) |
| 56 | + list(APPEND _four_c_python_bindings_sources ${_source}) |
| 57 | + endforeach() |
| 58 | + |
| 59 | + set(_target pybind11_${_bindings_name_base}) |
| 60 | + add_library(${_target}_objs OBJECT ${PROJECT_SOURCE_DIR}/cmake/dummy.cpp) |
| 61 | + target_sources(${_target}_objs PRIVATE ${_sources}) |
| 62 | + target_link_libraries(${_target}_objs PUBLIC pybind11_config) |
| 63 | + target_link_libraries(${_target}_objs PRIVATE ${_bindings_for_module}_unit_test_deps) |
| 64 | + set_target_properties(${_target}_objs PROPERTIES POSITION_INDEPENDENT_CODE ON) |
| 65 | + |
| 66 | + target_link_libraries(${FOUR_C_PYTHON_BINDINGS_PROJECT_NAME} PRIVATE ${_target}_objs) |
| 67 | + |
| 68 | + # Recursively add all subdirectories that contain CMakeLists.txt files. |
| 69 | + # N.B. We need to directly glob for CMakeLists.txt files here to ensure |
| 70 | + # the glob reruns when a new CMakeLists.txt is added. |
| 71 | + file( |
| 72 | + GLOB children |
| 73 | + RELATIVE ${CMAKE_CURRENT_LIST_DIR} |
| 74 | + CONFIGURE_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/*/CMakeLists.txt |
| 75 | + ) |
| 76 | + foreach(child ${children}) |
| 77 | + get_filename_component(_subdir ${child} DIRECTORY) |
| 78 | + add_subdirectory(${_subdir}) |
| 79 | + endforeach() |
| 80 | + |
| 81 | + # Simulate a "return" by setting a variable at the call site |
| 82 | + set(AUTO_DEFINED_SUBMODULE_NAME |
| 83 | + ${_bindings_name_base} |
| 84 | + PARENT_SCOPE |
| 85 | + ) |
| 86 | +endfunction() |
0 commit comments