ctkMacroSetupPlugins.cmake 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_dirswithoption )
  109. SET(plugin_subdirs )
  110. FOREACH(plugin ${plugin_list})
  111. ctkFunctionExtractOptionNameAndValue(${plugin} plugin_name plugin_value)
  112. IF(MY_COMPACT_OPTIONS)
  113. STRING(REPLACE "/" ";" _tokens ${plugin_name})
  114. LIST(GET _tokens -1 option_name)
  115. SET(option_name ${MY_BUILD_OPTION_PREFIX}${option_name})
  116. ELSE()
  117. SET(option_name ${MY_BUILD_OPTION_PREFIX}${plugin_name})
  118. ENDIF()
  119. # This variable may have the form "Plugins/org.commontk.bla_option_name"
  120. SET(${plugin_name}_option_name ${option_name})
  121. # Additionally create a variable of the form "org_commontk_bla_option_name"
  122. STRING(REPLACE "/" ";" _tokens ${plugin_name})
  123. LIST(GET _tokens -1 plugin_symbolic_name)
  124. STRING(REPLACE "." "_" plugin_target ${plugin_symbolic_name})
  125. SET(${plugin_target}_option_name ${option_name})
  126. OPTION(${option_name} "Build ${plugin_name} Plugin." ${plugin_value})
  127. IF(MY_BUILD_ALL)
  128. SET(${option_name} 1)
  129. ENDIF()
  130. LIST(APPEND plugin_subdirs "${plugin_name}")
  131. IF(IS_ABSOLUTE ${plugin_name})
  132. LIST(APPEND plugin_dirswithoption "${plugin_name}^^${option_name}")
  133. ELSE()
  134. LIST(APPEND plugin_dirswithoption "${CMAKE_CURRENT_SOURCE_DIR}/${plugin_name}^^${option_name}")
  135. ENDIF()
  136. ENDFOREACH()
  137. # Get plugin info from possible previous invocations of this macro for
  138. # validation purposes below
  139. GET_PROPERTY(previous_plugin_dirswithoption GLOBAL PROPERTY ctkMacroSetupExternalPlugins_dirswithoption)
  140. # Fill the CTK_EXTERNAL_PLUGIN_LIBRARIES variable with external plug-in target names.
  141. # It will be used in ctkMacroValidateBuildOptions to be able to validate agains plug-ins
  142. # from external projects.
  143. ctkFunctionGetAllPluginTargets(CTK_EXTERNAL_PLUGIN_LIBRARIES)
  144. ctkFunctionGenerateDGraphInput(${CMAKE_CURRENT_BINARY_DIR}
  145. "${plugin_dirswithoption};${previous_plugin_dirswithoption};${MY_APPS}"
  146. WITH_EXTERNALS)
  147. ctkMacroValidateBuildOptions("${CMAKE_CURRENT_BINARY_DIR}" "${CTK_DGRAPH_EXECUTABLE}"
  148. "${MY_APPS};${plugin_dirswithoption};${previous_plugin_dirswithoption}")
  149. # Record the current set of plug-ins and their option names
  150. SET_PROPERTY(GLOBAL APPEND PROPERTY ctkMacroSetupExternalPlugins_dirswithoption ${plugin_dirswithoption})
  151. # Get the gcc version (GCC_VERSION will be empty if the compiler is not gcc).
  152. # This will be used in the ctkMacroBuildPlugin macro to conditionally set compiler flags.
  153. ctkFunctionGetGccVersion(${CMAKE_CXX_COMPILER} GCC_VERSION)
  154. FOREACH(plugin ${plugin_subdirs})
  155. IF(${${plugin}_option_name})
  156. IF(IS_ABSOLUTE ${plugin})
  157. # get last directory component
  158. GET_FILENAME_COMPONENT(_dirname ${plugin} NAME)
  159. ADD_SUBDIRECTORY(${plugin} private_plugins/${_dirname})
  160. ELSE()
  161. ADD_SUBDIRECTORY(${plugin})
  162. ENDIF()
  163. ENDIF()
  164. ENDFOREACH()
  165. ENDMACRO()