ctkMacroBuildPlugin.cmake 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. ###########################################################################
  2. #
  3. # Library: CTK
  4. #
  5. # Copyright (c) Kitware Inc.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.commontk.org/LICENSE
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. #
  19. ###########################################################################
  20. #
  21. # Depends on:
  22. # CTK/CMake/ctkMacroParseArguments.cmake
  23. # CTK/CMake/ctkMacroGeneratePluginManifest.cmake
  24. #
  25. # This macro takes the usual arguments for building
  26. # a shared library using Qt. Additionally, it generates
  27. # plugin meta-data by creating a MANIFEST.MF text file
  28. # which is embedded in the share library as a Qt resource.
  29. #
  30. # The following variables can be set in a file named
  31. # manifest_headers.cmake, which will then be read by
  32. # this macro:
  33. #
  34. # Plugin-ActivationPolicy
  35. # Plugin-Category
  36. # Plugin-ContactAddress
  37. # Plugin-Copyright
  38. # Plugin-Description
  39. # Plugin-DocURL
  40. # Plugin-Icon
  41. # Plugin-License
  42. # Plugin-Name
  43. # Require-Plugin
  44. # Plugin-Vendor
  45. # Plugin-Version
  46. #
  47. MACRO(ctkMacroBuildPlugin)
  48. CtkMacroParseArguments(MY
  49. "EXPORT_DIRECTIVE;SRCS;MOC_SRCS;UI_FORMS;INCLUDE_DIRECTORIES;TARGET_LIBRARIES;RESOURCES;CACHED_RESOURCEFILES;TRANSLATIONS"
  50. "TEST_PLUGIN"
  51. ${ARGN}
  52. )
  53. # Sanity checks
  54. IF(NOT DEFINED MY_EXPORT_DIRECTIVE)
  55. MESSAGE(SEND_ERROR "EXPORT_DIRECTIVE is mandatory")
  56. ENDIF()
  57. # Plugin are expected to be shared library
  58. SET(MY_LIBRARY_TYPE "SHARED")
  59. # Define library name
  60. SET(lib_name ${PROJECT_NAME})
  61. # Clear the variables for the manifest headers
  62. SET(Plugin-ActivationPolicy )
  63. SET(Plugin-Category )
  64. SET(Plugin-ContactAddress )
  65. SET(Plugin-Copyright )
  66. SET(Plugin-Description )
  67. SET(Plugin-DocURL )
  68. SET(Plugin-Icon )
  69. SET(Plugin-License )
  70. SET(Plugin-Name )
  71. SET(Require-Plugin )
  72. SET(Plugin-SymbolicName )
  73. SET(Plugin-Vendor )
  74. SET(Plugin-Version )
  75. SET(Custom-Headers )
  76. IF(MY_TEST_PLUGIN)
  77. # Since the test plug-ins are not considered when calculating
  78. # target dependencies via DGraph, we add the dependencies
  79. # manually here
  80. #MESSAGE("${lib_name}_DEPENDENCIES ${MY_TARGET_LIBRARIES}")
  81. LIST(APPEND ${lib_name}_DEPENDENCIES ${MY_TARGET_LIBRARIES})
  82. ENDIF()
  83. # If a file named manifest_headers.cmake exists, read it
  84. SET(manifest_headers_dep )
  85. IF(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/manifest_headers.cmake")
  86. INCLUDE(${CMAKE_CURRENT_SOURCE_DIR}/manifest_headers.cmake)
  87. SET(manifest_headers_dep "${CMAKE_CURRENT_SOURCE_DIR}/manifest_headers.cmake")
  88. ENDIF()
  89. STRING(REPLACE "_" "." Plugin-SymbolicName ${lib_name})
  90. # --------------------------------------------------------------------------
  91. # Include dirs
  92. SET(my_includes
  93. ${CMAKE_CURRENT_SOURCE_DIR}
  94. ${CMAKE_CURRENT_BINARY_DIR}
  95. ${MY_INCLUDE_DIRECTORIES}
  96. )
  97. # Add the include directories from the plugin dependencies
  98. # and external dependencies
  99. ctkFunctionGetIncludeDirs(my_includes ${lib_name})
  100. INCLUDE_DIRECTORIES(
  101. ${my_includes}
  102. )
  103. # Add the library directories from the external project
  104. ctkFunctionGetLibraryDirs(my_library_dirs ${lib_name})
  105. LINK_DIRECTORIES(
  106. ${my_library_dirs}
  107. )
  108. SET(MY_LIBRARY_EXPORT_DIRECTIVE ${MY_EXPORT_DIRECTIVE})
  109. SET(MY_EXPORT_HEADER_PREFIX "${lib_name}_")
  110. SET(MY_LIBNAME ${lib_name})
  111. CONFIGURE_FILE(
  112. ${CTK_EXPORT_HEADER_TEMPLATE}
  113. ${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h
  114. )
  115. SET(dynamicHeaders
  116. "${dynamicHeaders};${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h")
  117. # Make sure variable are cleared
  118. SET(MY_MOC_CPP)
  119. SET(MY_UI_CPP)
  120. SET(MY_QRC_SRCS)
  121. # Wrap
  122. QT4_WRAP_CPP(MY_MOC_CPP ${MY_MOC_SRCS})
  123. QT4_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
  124. IF(DEFINED MY_RESOURCES)
  125. QT4_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
  126. ENDIF()
  127. # Add the generated manifest qrc file
  128. SET(manifest_qrc_src )
  129. ctkFunctionGeneratePluginManifest(manifest_qrc_src
  130. ACTIVATIONPOLICY ${Plugin-ActivationPolicy}
  131. CATEGORY ${Plugin-Category}
  132. CONTACT_ADDRESS ${Plugin-ContactAddress}
  133. COPYRIGHT ${Plugin-Copyright}
  134. DESCRIPTION ${Plugin-Description}
  135. DOC_URL ${Plugin-DocURL}
  136. ICON ${Plugin-Icon}
  137. LICENSE ${Plugin-License}
  138. NAME ${Plugin-Name}
  139. REQUIRE_PLUGIN ${Require-Plugin}
  140. SYMBOLIC_NAME ${Plugin-SymbolicName}
  141. VENDOR ${Plugin-Vendor}
  142. VERSION ${Plugin-Version}
  143. CUSTOM_HEADERS ${Custom-Headers}
  144. )
  145. IF(manifest_headers_dep)
  146. SET_PROPERTY(SOURCE ${manifest_qrc_src} APPEND
  147. PROPERTY OBJECT_DEPENDS ${manifest_headers_dep})
  148. ENDIF()
  149. LIST(APPEND MY_QRC_SRCS ${manifest_qrc_src})
  150. # Create translation files (.ts and .qm)
  151. SET(_plugin_qm_files )
  152. SET(_plugin_relative_qm_files )
  153. SET(_translations_dir "${CMAKE_CURRENT_BINARY_DIR}/CTK-INF/l10n")
  154. IF(MY_TRANSLATIONS)
  155. SET_SOURCE_FILES_PROPERTIES(${MY_TRANSLATIONS}
  156. PROPERTIES OUTPUT_LOCATION ${_translations_dir})
  157. QT4_CREATE_TRANSLATION(_plugin_qm_files ${MY_SRCS} ${MY_UI_FORMS} ${MY_TRANSLATIONS})
  158. ENDIF()
  159. IF(_plugin_qm_files)
  160. FOREACH(_qm_file ${_plugin_qm_files})
  161. FILE(RELATIVE_PATH _relative_qm_file ${CMAKE_CURRENT_BINARY_DIR} ${_qm_file})
  162. LIST(APPEND _plugin_relative_qm_files ${_relative_qm_file})
  163. ENDFOREACH()
  164. ENDIF()
  165. # Add any other additional resource files
  166. IF(MY_CACHED_RESOURCEFILES OR _plugin_relative_qm_files)
  167. STRING(REPLACE "." "_" _plugin_symbolicname ${Plugin-SymbolicName})
  168. ctkMacroGeneratePluginResourceFile(MY_QRC_SRCS
  169. NAME ${_plugin_symbolicname}_cached.qrc
  170. PREFIX ${Plugin-SymbolicName}
  171. RESOURCES ${MY_CACHED_RESOURCEFILES}
  172. BINARY_RESOURCES ${_plugin_relative_qm_files})
  173. ENDIF()
  174. SOURCE_GROUP("Resources" FILES
  175. ${MY_RESOURCES}
  176. ${MY_UI_FORMS}
  177. ${MY_TRANSLATIONS}
  178. )
  179. SOURCE_GROUP("Generated" FILES
  180. ${MY_QRC_SRCS}
  181. ${MY_MOC_CPP}
  182. ${MY_UI_CPP}
  183. ${_plugin_qm_files}
  184. )
  185. ADD_LIBRARY(${lib_name} ${MY_LIBRARY_TYPE}
  186. ${MY_SRCS}
  187. ${MY_MOC_CPP}
  188. ${MY_UI_CPP}
  189. ${MY_QRC_SRCS}
  190. ${_plugin_qm_files}
  191. )
  192. # Set the output directory for the plugin
  193. SET(output_dir_suffix "plugins")
  194. IF(MY_TEST_PLUGIN)
  195. SET(output_dir_suffix "test_plugins")
  196. ENDIF()
  197. IF(CMAKE_RUNTIME_OUTPUT_DIRECTORY)
  198. SET(runtime_output_dir "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${output_dir_suffix}")
  199. ELSE()
  200. SET(runtime_output_dir "${CMAKE_CURRENT_BINARY_DIR}/${output_dir_suffix}")
  201. ENDIF()
  202. IF(CMAKE_LIBRARY_OUTPUT_DIRECTORY)
  203. SET(library_output_dir "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${output_dir_suffix}")
  204. ELSE()
  205. SET(library_output_dir "${CMAKE_CURRENT_BINARY_DIR}/${output_dir_suffix}")
  206. ENDIF()
  207. # Apply properties to the library target.
  208. SET_TARGET_PROPERTIES(${lib_name} PROPERTIES
  209. COMPILE_FLAGS "-DQT_PLUGIN"
  210. RUNTIME_OUTPUT_DIRECTORY ${runtime_output_dir}
  211. LIBRARY_OUTPUT_DIRECTORY ${library_output_dir}
  212. PREFIX "lib"
  213. )
  214. # Note: The plugin may be installed in some other location ???
  215. # Install rules
  216. # IF(MY_LIBRARY_TYPE STREQUAL "SHARED")
  217. # INSTALL(TARGETS ${lib_name}
  218. # RUNTIME DESTINATION ${CTK_INSTALL_BIN_DIR} COMPONENT Runtime
  219. # LIBRARY DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT Runtime
  220. # ARCHIVE DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT Development)
  221. # ENDIF()
  222. SET(my_libs
  223. ${MY_TARGET_LIBRARIES}
  224. )
  225. IF(MINGW)
  226. LIST(APPEND my_libs ssp) # add stack smash protection lib
  227. ENDIF()
  228. TARGET_LINK_LIBRARIES(${lib_name} ${my_libs})
  229. # Update CTK_PLUGINS
  230. IF(NOT MY_TEST_PLUGIN)
  231. SET(CTK_PLUGIN_LIBRARIES ${CTK_PLUGIN_LIBRARIES} ${lib_name} CACHE INTERNAL "CTK plugins" FORCE)
  232. ENDIF()
  233. # Install headers
  234. #FILE(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
  235. #INSTALL(FILES
  236. # ${headers}
  237. # ${dynamicHeaders}
  238. # DESTINATION ${CTK_INSTALL_INCLUDE_DIR} COMPONENT Development
  239. # )
  240. ENDMACRO()