ctkMacroSetupPlugins.cmake 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. ###########################################################################
  2. #
  3. # Library: CTK
  4. #
  5. # Copyright (c) German Cancer Research Center,
  6. # Division of Medical and Biological Informatics
  7. #
  8. # Licensed under the Apache License, Version 2.0 (the "License");
  9. # you may not use this file except in compliance with the License.
  10. # You may obtain a copy of the License at
  11. #
  12. # http://www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS,
  16. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. # See the License for the specific language governing permissions and
  18. # limitations under the License.
  19. #
  20. ###########################################################################
  21. macro(ctkMacroSetupExternalPlugins )
  22. message(SEND_ERROR "This macro has been renamed. Please use ctkMacroSetupPlugins instead")
  23. endmacro()
  24. #! This is the main macro to set up your CTK plug-ins inside your own CMake project.
  25. #!
  26. #! This macro takes care of validating the current set of plug-in build options,
  27. #! enables and/or checks required plug-ins and handles all aspects of plug-in
  28. #! dependencies. Additionally, it calls add_subdirectory() on each given plug-in.
  29. #!
  30. #! Macro signature:
  31. #! \code
  32. #! ctkMacroSetupPlugins(plugins...
  33. #! [BUILD_OPTION_PREFIX <option_prefix>]
  34. #! [APPS <apps...>]
  35. #! [BUILD_ALL <build_all_flag>]
  36. #! [COMPACT_OPTIONS])
  37. #! \endcode
  38. #!
  39. #! \param plugins (required) A list of directories (absolute or relative to the current
  40. #! source dir) with default build options. E.g. an item of the list may look like
  41. #! "Plugins/org.myproject.example:ON"
  42. #! \param BUILD_OPTION_PREFIX (optional) The prefix to use for the build option of th
  43. #! plug-in. Defaults to "BUILD_".
  44. #! \param APPS (optional) A list of directories with build option names containing
  45. #! CTK-style applications. This can be used to automatically enable plug-ins
  46. #! required by the application. The application directoy must contain a CMakeLists.txt
  47. #! file containing a "project(...)" command and a target_libraries.cmake file
  48. #! containing a list of required plug-in targets, e.g. "set(target_libraries org_myproject_example)".
  49. #! An item of the list may look like "Apps/MyExampleApp^^BUILD_APP_MyExampleApp" where
  50. #! Apps/MyExampleApp is the directory containing the applicatios CMakeLists.txt file
  51. #! and BUILD_APP_MyExampleApp is the build option used to conditionally build the app.
  52. #! \param BUILD_ALL (optional) If the build_all_flag is true, all entries in the plugins list
  53. #! will be enabled (but their build option will not be forced to change). Default is OFF
  54. #! \param COMPACT_OPTIONS (optional) If this flag is given, the created build options for the
  55. #! plugins will not contain the relative path but just the last directory entry which
  56. #! usually is the plug-in symbolic name.
  57. #!
  58. #! Example invocation:
  59. #!
  60. #! \code
  61. #! set(plugin_list Plugins/org.myproject.example:OFF)
  62. #! set(app_list Apps/MyExampleApp^^MYPROJECT_BUILD_MyExampleApp)
  63. #!
  64. #! ctkMacroSetupPlugins(${plugin_list}
  65. #! BUILD_OPTION_PREFIX MYPROJECT_BUILD_
  66. #! APPS ${app_list}
  67. #! COMPACT_OPTIONS
  68. #! )
  69. #! \endcode
  70. #!
  71. #! \ingroup CMakeAPI
  72. macro(ctkMacroSetupPlugins )
  73. ctkMacroParseArguments(MY "BUILD_OPTION_PREFIX;APPS;BUILD_ALL" "COMPACT_OPTIONS" ${ARGN})
  74. if(NOT MY_DEFAULT_ARGS)
  75. message(FATAL_ERROR "Empty plugin list")
  76. endif()
  77. set(plugin_list ${MY_DEFAULT_ARGS})
  78. if(NOT MY_BUILD_OPTION_PREFIX)
  79. set(MY_BUILD_OPTION_PREFIX "BUILD_")
  80. endif()
  81. if(NOT MY_BUILD_ALL)
  82. set(MY_BUILD_ALL 0)
  83. endif()
  84. # Check if this is the first invocation of this macro
  85. get_property(_repeated GLOBAL PROPERTY ctkMacroSetupExternalPlugins_called SET)
  86. if(NOT _repeated)
  87. # Clear the internal cache variable containing all enabled plug-in targets
  88. # This variable will be set in ctkMacroBuildPlugin
  89. set(${CMAKE_PROJECT_NAME}_PLUGIN_LIBRARIES CACHE INTERNAL "CTK plug-in targets" FORCE)
  90. set_property(GLOBAL PROPERTY ctkMacroSetupExternalPlugins_called 1)
  91. # Add the project specific variable name containing plug-in targets to the list
  92. set_property(GLOBAL APPEND PROPERTY CTK_PLUGIN_LIBRARIES_VARS ${CMAKE_PROJECT_NAME}_PLUGIN_LIBRARIES)
  93. endif()
  94. # Set up Qt, if not already done
  95. #if(NOT QT4_FOUND)
  96. # set(minimum_required_qt_version "4.6")
  97. # find_package(Qt4 REQUIRED)
  98. # if("${QT_VERSION_MAJOR}.${QT_VERSION_MINOR}" VERSION_LESS "${minimum_required_qt_version}")
  99. # message(FATAL_ERROR "error: CTK requires Qt >= ${minimum_required_qt_version} -- you cannot use Qt ${QT_VERSION_MAJOR}.${QT_VERSION_MINOR}.${QT_VERSION_PATCH}.")
  100. # endif()
  101. #endif()
  102. # Set the variable QT_INSTALLED_LIBRARY_DIR that contains all
  103. # Qt shared libraries
  104. if (CTK_QT_VERSION VERSION_GREATER "4")
  105. if(WIN32)
  106. get_target_property(_qt5_moc_executable Qt5::moc LOCATION)
  107. get_filename_component(QT_INSTALLED_LIBRARY_DIR ${_qt5_moc_executable} PATH)
  108. else()
  109. get_target_property(_qt5_core_lib Qt5::Core LOCATION)
  110. get_filename_component(QT_INSTALLED_LIBRARY_DIR ${_qt5_core_lib} PATH)
  111. endif()
  112. else()
  113. set(QT_INSTALLED_LIBRARY_DIR ${QT_LIBRARY_DIR})
  114. if(WIN32)
  115. get_filename_component(QT_INSTALLED_LIBRARY_DIR ${QT_QMAKE_EXECUTABLE} PATH)
  116. endif()
  117. endif()
  118. set(plugin_symbolic_names )
  119. set(plugin_targets )
  120. foreach(plugin ${plugin_list})
  121. ctkFunctionExtractOptionNameAndValue(${plugin} plugin_dir plugin_value)
  122. string(REPLACE "/" ";" _tokens ${plugin_dir})
  123. list(GET _tokens -1 plugin_symbolic_name)
  124. list(APPEND plugin_symbolic_names ${plugin_symbolic_name})
  125. string(REPLACE "." "_" plugin_target ${plugin_symbolic_name})
  126. list(APPEND plugin_targets ${plugin_target})
  127. set(${plugin_symbolic_name}_plugin_dir ${plugin_dir})
  128. set(${plugin_symbolic_name}_plugin_value ${plugin_value})
  129. endforeach()
  130. # Check if the plugin symbolic names are valid for the current project
  131. ctkMacroGetAllNonProjectTargetLibraries("${plugin_targets}" invalid_targets)
  132. if(invalid_targets)
  133. set(invalid_plugins )
  134. foreach(plugin_target ${invalid_targets})
  135. string(REPLACE "_" "." plugin_symbolic_name ${plugin_target})
  136. list(APPEND invalid_plugins ${plugin_symbolic_name})
  137. endforeach()
  138. message(FATAL_ERROR "The following plug-ins are using invalid symbolic names: ${invalid_plugins}")
  139. endif()
  140. set(plugin_dirswithoption )
  141. set(plugin_subdirs )
  142. foreach(plugin_symbolic_name ${plugin_symbolic_names})
  143. if(MY_COMPACT_OPTIONS)
  144. set(option_name ${MY_BUILD_OPTION_PREFIX}${plugin_symbolic_name})
  145. else()
  146. set(option_name ${MY_BUILD_OPTION_PREFIX}${${plugin_symbolic_name}_plugin_dir})
  147. endif()
  148. # This variable may have the form "Plugins/org.commontk.bla_option_name"
  149. set(${${plugin_symbolic_name}_plugin_dir}_option_name ${option_name})
  150. # Additionally create a variable of the form "org_commontk_bla_option_name"
  151. string(REPLACE "." "_" plugin_target ${plugin_symbolic_name})
  152. set(${plugin_target}_option_name ${option_name})
  153. option(${option_name} "Build the ${plugin_symbolic_name} Plugin." ${${plugin_symbolic_name}_plugin_value})
  154. if(MY_BUILD_ALL)
  155. set(${option_name} 1)
  156. endif()
  157. list(APPEND plugin_subdirs "${${plugin_symbolic_name}_plugin_dir}")
  158. if(IS_ABSOLUTE ${${plugin_symbolic_name}_plugin_dir})
  159. list(APPEND plugin_dirswithoption "${${plugin_symbolic_name}_plugin_dir}^^${option_name}")
  160. else()
  161. list(APPEND plugin_dirswithoption "${CMAKE_CURRENT_SOURCE_DIR}/${${plugin_symbolic_name}_plugin_dir}^^${option_name}")
  162. endif()
  163. endforeach()
  164. # Get plugin info from possible previous invocations of this macro for
  165. # validation purposes below
  166. get_property(previous_plugin_dirswithoption GLOBAL PROPERTY ctkMacroSetupExternalPlugins_dirswithoption)
  167. # Fill the CTK_EXTERNAL_PLUGIN_LIBRARIES variable with external plug-in target names.
  168. # It will be used in ctkMacroValidateBuildOptions to be able to validate agains plug-ins
  169. # from external projects.
  170. ctkFunctionGetAllPluginTargets(CTK_EXTERNAL_PLUGIN_LIBRARIES)
  171. ctkFunctionGenerateDGraphInput(${CMAKE_CURRENT_BINARY_DIR}
  172. "${plugin_dirswithoption};${previous_plugin_dirswithoption};${MY_APPS}"
  173. WITH_EXTERNALS)
  174. ctkMacroValidateBuildOptions("${CMAKE_CURRENT_BINARY_DIR}" "${CTK_DGRAPH_EXECUTABLE}"
  175. "${MY_APPS};${plugin_dirswithoption};${previous_plugin_dirswithoption}")
  176. # Record the current set of plug-ins and their option names
  177. set_property(GLOBAL APPEND PROPERTY ctkMacroSetupExternalPlugins_dirswithoption ${plugin_dirswithoption})
  178. # Get the gcc version (GCC_VERSION will be empty if the compiler is not gcc).
  179. # This will be used in the ctkMacroBuildPlugin macro to conditionally set compiler flags.
  180. ctkFunctionGetGccversion(${CMAKE_CXX_COMPILER} GCC_VERSION)
  181. foreach(plugin ${plugin_subdirs})
  182. if(${${plugin}_option_name})
  183. if(IS_ABSOLUTE ${plugin})
  184. # get last directory component
  185. get_filename_component(_dirname ${plugin} NAME)
  186. add_subdirectory(${plugin} private_plugins/${_dirname})
  187. else()
  188. add_subdirectory(${plugin})
  189. endif()
  190. endif()
  191. endforeach()
  192. endmacro()