vtkObjectFactory.cmake 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #[==[
  2. @file vtkObjectFactory.cmake
  3. In VTK, certain classes can have what are called "implementations". When the
  4. base class is requested, it instead looks into a list of available
  5. implementations. One of the implementations is then constructed and returned
  6. instead.
  7. For instance, there is a `vtkRenderWindow` class in VTK. When this is
  8. constructed, it instead actually returns a window for the X window system,
  9. Cocoa, or Win32 depending on what is available.
  10. VTK's implementation utilizes the [autoinit](@ref module-autoinit) logic of the
  11. module system. A module which contains an object factory must declare itself as
  12. `IMPLEMENTABLE` and modules which contain an implementation of an object
  13. factory must claim that they `IMPLEMENTS` modules containing those base object
  14. factories (a module may contain the object factory and an implementation; it
  15. then says that it `IMPLEMENTS` itself).
  16. #]==]
  17. set(_vtkObjectFactory_source_dir "${CMAKE_CURRENT_LIST_DIR}")
  18. #[==[
  19. @brief Declare a factory override
  20. Declare that a class in this module (the implementation) is an `OVERRIDE` for a
  21. base class.
  22. ~~~
  23. vtk_object_factory_declare(
  24. BASE <base>
  25. OVERRIDE <implementation>)
  26. ~~~
  27. #]==]
  28. function (vtk_object_factory_declare)
  29. cmake_parse_arguments(_vtk_object_factory_declare
  30. ""
  31. "BASE;OVERRIDE"
  32. ""
  33. ${ARGN})
  34. if (_vtk_object_factory_declare_UNPARSED_ARGUMENTS)
  35. message(FATAL_ERROR
  36. "Unparsed arguments for vtk_object_factory_declare: "
  37. "${_vtk_object_factory_declare_UNPARSED_ARGUMENTS}")
  38. endif ()
  39. if (NOT DEFINED _vtk_object_factory_declare_BASE)
  40. message(FATAL_ERROR
  41. "The `BASE` argument is required.")
  42. endif ()
  43. if (NOT DEFINED _vtk_object_factory_declare_OVERRIDE)
  44. message(FATAL_ERROR
  45. "The `OVERRIDE` argument is required.")
  46. endif ()
  47. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND
  48. PROPERTY
  49. _vtk_object_factory_overrides "${_vtk_object_factory_declare_OVERRIDE}")
  50. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND
  51. PROPERTY
  52. "_vtk_object_factory_override_${_vtk_object_factory_declare_OVERRIDE}" "${_vtk_object_factory_declare_BASE}")
  53. endfunction ()
  54. #[==[
  55. @brief Generate source for overrides in a module
  56. A module may only have a single declaration of all its object factory
  57. implementations. This function generates the source for all of the overrides
  58. declared using @ref vtk_object_factory_declare.
  59. ~~~
  60. vtk_object_factory_configure(
  61. SOURCE_FILE <variable>
  62. [HEADER_FILE <variable>]
  63. [EXPORT_MACRO <macro>]
  64. [INITIAL_CODE <code>]
  65. [EXTRA_INCLUDES <include>...])
  66. ~~~
  67. - `SOURCE_FILE`: (Required) A variable to set to the path to generated source
  68. file.
  69. - `HEADER_FILE`: (Recommended) A variable to set to the path to generated
  70. header file. This should not be treated as a public header.
  71. - `EXPORT_MACRO`: (Recommended) The export macro to add to the generated
  72. class.
  73. - `INITIAL_CODE`: C++ code to run when the object factory is initialized.
  74. - `EXTRA_INCLUDES`: A list of headers to include. The header names need to
  75. include the `<>` or `""` quoting.
  76. #]==]
  77. function (vtk_object_factory_configure)
  78. if (NOT DEFINED _vtk_build_module)
  79. message(FATAL_ERROR
  80. "The `vtk_object_factory_configure` function needs to be run within a module context.")
  81. endif ()
  82. cmake_parse_arguments(_vtk_object_factory_configure
  83. ""
  84. "SOURCE_FILE;HEADER_FILE;INITIAL_CODE;EXPORT_MACRO"
  85. "EXTRA_INCLUDES"
  86. ${ARGN})
  87. if (_vtk_object_factory_configure_UNPARSED_ARGUMENTS)
  88. message(FATAL_ERROR
  89. "Unparsed arguments for vtk_object_factory_configure: "
  90. "${_vtk_object_factory_configure_UNPARSED_ARGUMENTS}")
  91. endif ()
  92. get_property(_vtk_object_factory_done
  93. GLOBAL
  94. PROPERTY "_vtk_object_factory_${_vtk_build_module}"
  95. SET)
  96. if (_vtk_object_factory_done)
  97. message(FATAL_ERROR
  98. "An object factory has already been created for ${_vtk_build_module}.")
  99. else ()
  100. set_property(GLOBAL
  101. PROPERTY
  102. "_vtk_object_factory_${_vtk_build_module}" 1)
  103. endif ()
  104. get_property(_vtk_object_factory_overrides
  105. DIRECTORY
  106. PROPERTY _vtk_object_factory_overrides)
  107. if (NOT _vtk_object_factory_overrides)
  108. message(WARNING
  109. "The ${_vtk_build_module} is generating an object factory, but does not have any declared overrides.")
  110. endif ()
  111. set(_vtk_object_factory_doc
  112. "Override for ${_vtk_build_module} module")
  113. set(_vtk_object_factory_includes "")
  114. set(_vtk_object_factory_functions "")
  115. set(_vtk_object_factory_calls "")
  116. foreach (_vtk_object_factory_extra_include IN LISTS _vtk_object_factory_configure_EXTRA_INCLUDES)
  117. set(_vtk_object_factory_includes
  118. "${_vtk_object_factory_includes}#include ${_vtk_object_factory_extra_include}\n")
  119. endforeach ()
  120. foreach (_vtk_object_factory_override IN LISTS _vtk_object_factory_overrides)
  121. get_property(_vtk_object_factory_base
  122. DIRECTORY
  123. PROPERTY "_vtk_object_factory_override_${_vtk_object_factory_override}")
  124. set(_vtk_object_factory_includes
  125. "${_vtk_object_factory_includes}#include \"${_vtk_object_factory_override}.h\"\n")
  126. set(_vtk_object_factory_functions
  127. "${_vtk_object_factory_functions}VTK_CREATE_CREATE_FUNCTION(${_vtk_object_factory_override})\n")
  128. set(_vtk_object_factory_calls
  129. "${_vtk_object_factory_calls}this->RegisterOverride(\"${_vtk_object_factory_base}\", \"${_vtk_object_factory_override}\", \"${_vtk_object_factory_doc}\", 1, vtkObjectFactoryCreate${_vtk_object_factory_override});\n")
  130. endforeach ()
  131. get_property(_vtk_object_factory_library_name GLOBAL
  132. PROPERTY "_vtk_module_${_vtk_build_module}_library_name")
  133. set(_vtk_object_factory_overrides_header
  134. "${CMAKE_CURRENT_BINARY_DIR}/${_vtk_object_factory_library_name}ObjectFactory.h")
  135. set(_vtk_object_factory_overrides_source
  136. "${CMAKE_CURRENT_BINARY_DIR}/${_vtk_object_factory_library_name}ObjectFactory.cxx")
  137. configure_file(
  138. "${_vtkObjectFactory_source_dir}/vtkObjectFactory.h.in"
  139. "${_vtk_object_factory_overrides_header}"
  140. @ONLY)
  141. configure_file(
  142. "${_vtkObjectFactory_source_dir}/vtkObjectFactory.cxx.in"
  143. "${_vtk_object_factory_overrides_source}"
  144. @ONLY)
  145. if (_vtk_object_factory_configure_HEADER_FILE)
  146. set("${_vtk_object_factory_configure_HEADER_FILE}"
  147. "${_vtk_object_factory_overrides_header}"
  148. PARENT_SCOPE)
  149. endif ()
  150. set("${_vtk_object_factory_configure_SOURCE_FILE}"
  151. "${_vtk_object_factory_overrides_source}"
  152. PARENT_SCOPE)
  153. endfunction ()