ctkMacroSetupPlugins.cmake 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. set(QT_INSTALLED_LIBRARY_DIR ${QT_LIBRARY_DIR})
  105. if(WIN32)
  106. get_filename_component(QT_INSTALLED_LIBRARY_DIR ${QT_QMAKE_EXECUTABLE} PATH)
  107. endif()
  108. set(plugin_symbolic_names )
  109. set(plugin_targets )
  110. foreach(plugin ${plugin_list})
  111. ctkFunctionExtractOptionNameAndValue(${plugin} plugin_dir plugin_value)
  112. string(REPLACE "/" ";" _tokens ${plugin_dir})
  113. list(GET _tokens -1 plugin_symbolic_name)
  114. list(APPEND plugin_symbolic_names ${plugin_symbolic_name})
  115. string(REPLACE "." "_" plugin_target ${plugin_symbolic_name})
  116. list(APPEND plugin_targets ${plugin_target})
  117. set(${plugin_symbolic_name}_plugin_dir ${plugin_dir})
  118. set(${plugin_symbolic_name}_plugin_value ${plugin_value})
  119. endforeach()
  120. # Check if the plugin symbolic names are valid for the current project
  121. ctkMacroGetAllNonProjectTargetLibraries("${plugin_targets}" invalid_targets)
  122. if(invalid_targets)
  123. set(invalid_plugins )
  124. foreach(plugin_target ${invalid_targets})
  125. string(REPLACE "_" "." plugin_symbolic_name ${plugin_target})
  126. list(APPEND invalid_plugins ${plugin_symbolic_name})
  127. endforeach()
  128. message(FATAL_ERROR "The following plug-ins are using invalid symbolic names: ${invalid_plugins}")
  129. endif()
  130. set(plugin_dirswithoption )
  131. set(plugin_subdirs )
  132. foreach(plugin_symbolic_name ${plugin_symbolic_names})
  133. if(MY_COMPACT_OPTIONS)
  134. set(option_name ${MY_BUILD_OPTION_PREFIX}${plugin_symbolic_name})
  135. else()
  136. set(option_name ${MY_BUILD_OPTION_PREFIX}${${plugin_symbolic_name}_plugin_dir})
  137. endif()
  138. # This variable may have the form "Plugins/org.commontk.bla_option_name"
  139. set(${${plugin_symbolic_name}_plugin_dir}_option_name ${option_name})
  140. # Additionally create a variable of the form "org_commontk_bla_option_name"
  141. string(REPLACE "." "_" plugin_target ${plugin_symbolic_name})
  142. set(${plugin_target}_option_name ${option_name})
  143. option(${option_name} "Build the ${plugin_symbolic_name} Plugin." ${${plugin_symbolic_name}_plugin_value})
  144. if(MY_BUILD_ALL)
  145. set(${option_name} 1)
  146. endif()
  147. list(APPEND plugin_subdirs "${${plugin_symbolic_name}_plugin_dir}")
  148. if(IS_ABSOLUTE ${${plugin_symbolic_name}_plugin_dir})
  149. list(APPEND plugin_dirswithoption "${${plugin_symbolic_name}_plugin_dir}^^${option_name}")
  150. else()
  151. list(APPEND plugin_dirswithoption "${CMAKE_CURRENT_SOURCE_DIR}/${${plugin_symbolic_name}_plugin_dir}^^${option_name}")
  152. endif()
  153. endforeach()
  154. # Get plugin info from possible previous invocations of this macro for
  155. # validation purposes below
  156. get_property(previous_plugin_dirswithoption GLOBAL PROPERTY ctkMacroSetupExternalPlugins_dirswithoption)
  157. # Fill the CTK_EXTERNAL_PLUGIN_LIBRARIES variable with external plug-in target names.
  158. # It will be used in ctkMacroValidateBuildOptions to be able to validate agains plug-ins
  159. # from external projects.
  160. ctkFunctionGetAllPluginTargets(CTK_EXTERNAL_PLUGIN_LIBRARIES)
  161. ctkFunctionGenerateDGraphInput(${CMAKE_CURRENT_BINARY_DIR}
  162. "${plugin_dirswithoption};${previous_plugin_dirswithoption};${MY_APPS}"
  163. WITH_EXTERNALS)
  164. ctkMacroValidateBuildOptions("${CMAKE_CURRENT_BINARY_DIR}" "${CTK_DGRAPH_EXECUTABLE}"
  165. "${MY_APPS};${plugin_dirswithoption};${previous_plugin_dirswithoption}")
  166. # Record the current set of plug-ins and their option names
  167. set_property(GLOBAL APPEND PROPERTY ctkMacroSetupExternalPlugins_dirswithoption ${plugin_dirswithoption})
  168. # Get the gcc version (GCC_VERSION will be empty if the compiler is not gcc).
  169. # This will be used in the ctkMacroBuildPlugin macro to conditionally set compiler flags.
  170. ctkFunctionGetGccversion(${CMAKE_CXX_COMPILER} GCC_VERSION)
  171. foreach(plugin ${plugin_subdirs})
  172. if(${${plugin}_option_name})
  173. if(IS_ABSOLUTE ${plugin})
  174. # get last directory component
  175. get_filename_component(_dirname ${plugin} NAME)
  176. add_subdirectory(${plugin} private_plugins/${_dirname})
  177. else()
  178. add_subdirectory(${plugin})
  179. endif()
  180. endif()
  181. endforeach()
  182. endmacro()