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;LIBRARY_TYPE"
  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. IF(NOT DEFINED MY_LIBRARY_TYPE)
  58. SET(MY_LIBRARY_TYPE "SHARED")
  59. ENDIF()
  60. # Define library name
  61. SET(lib_name ${PROJECT_NAME})
  62. # Clear the variables for the manifest headers
  63. SET(Plugin-ActivationPolicy )
  64. SET(Plugin-Category )
  65. SET(Plugin-ContactAddress )
  66. SET(Plugin-Copyright )
  67. SET(Plugin-Description )
  68. SET(Plugin-DocURL )
  69. SET(Plugin-Icon )
  70. SET(Plugin-License )
  71. SET(Plugin-Name )
  72. SET(Require-Plugin )
  73. SET(Plugin-SymbolicName )
  74. SET(Plugin-Vendor )
  75. SET(Plugin-Version )
  76. SET(Custom-Headers )
  77. IF(MY_TEST_PLUGIN)
  78. # Since the test plug-ins are not considered when calculating
  79. # target dependencies via DGraph, we add the dependencies
  80. # manually here
  81. #MESSAGE("${lib_name}_DEPENDENCIES ${MY_TARGET_LIBRARIES}")
  82. LIST(APPEND ${lib_name}_DEPENDENCIES ${MY_TARGET_LIBRARIES})
  83. ENDIF()
  84. # If a file named manifest_headers.cmake exists, read it
  85. SET(manifest_headers_dep )
  86. IF(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/manifest_headers.cmake")
  87. INCLUDE(${CMAKE_CURRENT_SOURCE_DIR}/manifest_headers.cmake)
  88. SET(manifest_headers_dep "${CMAKE_CURRENT_SOURCE_DIR}/manifest_headers.cmake")
  89. ENDIF()
  90. STRING(REPLACE "_" "." Plugin-SymbolicName ${lib_name})
  91. # --------------------------------------------------------------------------
  92. # Include dirs
  93. SET(my_includes
  94. ${CMAKE_CURRENT_SOURCE_DIR}
  95. ${CMAKE_CURRENT_BINARY_DIR}
  96. ${MY_INCLUDE_DIRECTORIES}
  97. )
  98. # Add the include directories from the plugin dependencies
  99. # and external dependencies
  100. ctkFunctionGetIncludeDirs(my_includes ${lib_name})
  101. INCLUDE_DIRECTORIES(
  102. ${my_includes}
  103. )
  104. # Add the library directories from the external project
  105. ctkFunctionGetLibraryDirs(my_library_dirs ${lib_name})
  106. LINK_DIRECTORIES(
  107. ${my_library_dirs}
  108. )
  109. SET(MY_LIBRARY_EXPORT_DIRECTIVE ${MY_EXPORT_DIRECTIVE})
  110. SET(MY_EXPORT_HEADER_PREFIX "${lib_name}_")
  111. SET(MY_LIBNAME ${lib_name})
  112. CONFIGURE_FILE(
  113. ${CTK_EXPORT_HEADER_TEMPLATE}
  114. ${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h
  115. )
  116. SET(dynamicHeaders
  117. "${dynamicHeaders};${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h")
  118. # Make sure variable are cleared
  119. SET(MY_MOC_CPP)
  120. SET(MY_UI_CPP)
  121. SET(MY_QRC_SRCS)
  122. # Wrap
  123. QT4_WRAP_CPP(MY_MOC_CPP ${MY_MOC_SRCS})
  124. QT4_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
  125. IF(DEFINED MY_RESOURCES)
  126. QT4_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
  127. ENDIF()
  128. # Add the generated manifest qrc file
  129. SET(manifest_qrc_src )
  130. ctkFunctionGeneratePluginManifest(manifest_qrc_src
  131. ACTIVATIONPOLICY ${Plugin-ActivationPolicy}
  132. CATEGORY ${Plugin-Category}
  133. CONTACT_ADDRESS ${Plugin-ContactAddress}
  134. COPYRIGHT ${Plugin-Copyright}
  135. DESCRIPTION ${Plugin-Description}
  136. DOC_URL ${Plugin-DocURL}
  137. ICON ${Plugin-Icon}
  138. LICENSE ${Plugin-License}
  139. NAME ${Plugin-Name}
  140. REQUIRE_PLUGIN ${Require-Plugin}
  141. SYMBOLIC_NAME ${Plugin-SymbolicName}
  142. VENDOR ${Plugin-Vendor}
  143. VERSION ${Plugin-Version}
  144. CUSTOM_HEADERS ${Custom-Headers}
  145. )
  146. IF(manifest_headers_dep)
  147. SET_PROPERTY(SOURCE ${manifest_qrc_src} APPEND
  148. PROPERTY OBJECT_DEPENDS ${manifest_headers_dep})
  149. ENDIF()
  150. LIST(APPEND MY_QRC_SRCS ${manifest_qrc_src})
  151. # Create translation files (.ts and .qm)
  152. SET(_plugin_qm_files )
  153. SET(_plugin_relative_qm_files )
  154. SET(_translations_dir "${CMAKE_CURRENT_BINARY_DIR}/CTK-INF/l10n")
  155. IF(MY_TRANSLATIONS)
  156. SET_SOURCE_FILES_PROPERTIES(${MY_TRANSLATIONS}
  157. PROPERTIES OUTPUT_LOCATION ${_translations_dir})
  158. QT4_CREATE_TRANSLATION(_plugin_qm_files ${MY_SRCS} ${MY_UI_FORMS} ${MY_TRANSLATIONS})
  159. ENDIF()
  160. IF(_plugin_qm_files)
  161. FOREACH(_qm_file ${_plugin_qm_files})
  162. FILE(RELATIVE_PATH _relative_qm_file ${CMAKE_CURRENT_BINARY_DIR} ${_qm_file})
  163. LIST(APPEND _plugin_relative_qm_files ${_relative_qm_file})
  164. ENDFOREACH()
  165. ENDIF()
  166. # Add any other additional resource files
  167. IF(MY_CACHED_RESOURCEFILES OR _plugin_relative_qm_files)
  168. STRING(REPLACE "." "_" _plugin_symbolicname ${Plugin-SymbolicName})
  169. ctkMacroGeneratePluginResourceFile(MY_QRC_SRCS
  170. NAME ${_plugin_symbolicname}_cached.qrc
  171. PREFIX ${Plugin-SymbolicName}
  172. RESOURCES ${MY_CACHED_RESOURCEFILES}
  173. BINARY_RESOURCES ${_plugin_relative_qm_files})
  174. ENDIF()
  175. SOURCE_GROUP("Resources" FILES
  176. ${MY_RESOURCES}
  177. ${MY_UI_FORMS}
  178. ${MY_TRANSLATIONS}
  179. )
  180. SOURCE_GROUP("Generated" FILES
  181. ${MY_QRC_SRCS}
  182. ${MY_MOC_CPP}
  183. ${MY_UI_CPP}
  184. ${_plugin_qm_files}
  185. )
  186. ADD_LIBRARY(${lib_name} ${MY_LIBRARY_TYPE}
  187. ${MY_SRCS}
  188. ${MY_MOC_CPP}
  189. ${MY_UI_CPP}
  190. ${MY_QRC_SRCS}
  191. ${_plugin_qm_files}
  192. )
  193. # Set the output directory for the plugin
  194. SET(output_dir_suffix "plugins")
  195. IF(MY_TEST_PLUGIN)
  196. SET(output_dir_suffix "test_plugins")
  197. ENDIF()
  198. IF(CMAKE_RUNTIME_OUTPUT_DIRECTORY)
  199. SET(runtime_output_dir "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${output_dir_suffix}")
  200. ELSE()
  201. SET(runtime_output_dir "${CMAKE_CURRENT_BINARY_DIR}/${output_dir_suffix}")
  202. ENDIF()
  203. IF(CMAKE_LIBRARY_OUTPUT_DIRECTORY)
  204. SET(library_output_dir "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${output_dir_suffix}")
  205. ELSE()
  206. SET(library_output_dir "${CMAKE_CURRENT_BINARY_DIR}/${output_dir_suffix}")
  207. ENDIF()
  208. # Apply properties to the library target.
  209. SET_TARGET_PROPERTIES(${lib_name} PROPERTIES
  210. COMPILE_FLAGS "-DQT_PLUGIN"
  211. RUNTIME_OUTPUT_DIRECTORY ${runtime_output_dir}
  212. LIBRARY_OUTPUT_DIRECTORY ${library_output_dir}
  213. PREFIX "lib"
  214. )
  215. # Note: The plugin may be installed in some other location ???
  216. # Install rules
  217. # IF(CTK_BUILD_SHARED_LIBS)
  218. # INSTALL(TARGETS ${lib_name}
  219. # RUNTIME DESTINATION ${CTK_INSTALL_BIN_DIR} COMPONENT Runtime
  220. # LIBRARY DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT Runtime
  221. # ARCHIVE DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT Development)
  222. # ENDIF()
  223. SET(my_libs
  224. ${MY_TARGET_LIBRARIES}
  225. )
  226. IF(MINGW)
  227. LIST(APPEND my_libs ssp) # add stack smash protection lib
  228. ENDIF()
  229. TARGET_LINK_LIBRARIES(${lib_name} ${my_libs})
  230. # Update CTK_PLUGINS
  231. IF(NOT MY_TEST_PLUGIN)
  232. SET(CTK_PLUGIN_LIBRARIES ${CTK_PLUGIN_LIBRARIES} ${lib_name} CACHE INTERNAL "CTK plugins" FORCE)
  233. ENDIF()
  234. # Install headers
  235. #FILE(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
  236. #INSTALL(FILES
  237. # ${headers}
  238. # ${dynamicHeaders}
  239. # DESTINATION ${CTK_INSTALL_INCLUDE_DIR} COMPONENT Development
  240. # )
  241. ENDMACRO()