ctkMacroBuildPlugin.cmake 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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.apache.org/licenses/LICENSE-2.0.txt
  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. #! \ingroup CMakeAPI
  48. MACRO(ctkMacroBuildPlugin)
  49. CtkMacroParseArguments(MY
  50. "EXPORT_DIRECTIVE;SRCS;MOC_SRCS;UI_FORMS;INCLUDE_DIRECTORIES;EXPORTED_INCLUDE_SUFFIXES;TARGET_LIBRARIES;RESOURCES;CACHED_RESOURCEFILES;TRANSLATIONS;OUTPUT_DIR"
  51. "TEST_PLUGIN"
  52. ${ARGN}
  53. )
  54. # Sanity checks
  55. IF(NOT DEFINED MY_EXPORT_DIRECTIVE)
  56. MESSAGE(FATAL_ERROR "EXPORT_DIRECTIVE is mandatory")
  57. ENDIF()
  58. # Plugin are expected to be shared library
  59. SET(MY_LIBRARY_TYPE "SHARED")
  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. IF(MY_EXPORTED_INCLUDE_SUFFIXES)
  94. SET(${lib_name}_INCLUDE_SUFFIXES ${MY_EXPORTED_INCLUDE_SUFFIXES}
  95. CACHE INTERNAL "List of exported plugin include dirs")
  96. SET(my_includes )
  97. FOREACH(_suffix ${MY_EXPORTED_INCLUDE_SUFFIXES})
  98. LIST(APPEND my_includes ${CMAKE_CURRENT_SOURCE_DIR}/${_suffix})
  99. ENDFOREACH()
  100. ELSE()
  101. SET(${lib_name}_INCLUDE_SUFFIXES ""
  102. CACHE INTERNAL "List of exported plugin include dirs")
  103. SET(my_includes )
  104. ENDIF()
  105. LIST(APPEND my_includes
  106. ${CMAKE_CURRENT_SOURCE_DIR}
  107. ${CMAKE_CURRENT_BINARY_DIR}
  108. ${MY_INCLUDE_DIRECTORIES}
  109. )
  110. # Add the include directories from the plugin dependencies
  111. # and external dependencies
  112. ctkFunctionGetIncludeDirs(my_includes ${lib_name})
  113. INCLUDE_DIRECTORIES(
  114. ${my_includes}
  115. )
  116. # Add Qt include dirs and defines
  117. INCLUDE(${QT_USE_FILE})
  118. # Add the library directories from the external project
  119. ctkFunctionGetLibraryDirs(my_library_dirs ${lib_name})
  120. LINK_DIRECTORIES(
  121. ${my_library_dirs}
  122. )
  123. SET(MY_LIBRARY_EXPORT_DIRECTIVE ${MY_EXPORT_DIRECTIVE})
  124. SET(MY_EXPORT_HEADER_PREFIX "${lib_name}_")
  125. SET(MY_LIBNAME ${lib_name})
  126. CONFIGURE_FILE(
  127. ${CTK_EXPORT_HEADER_TEMPLATE}
  128. ${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h
  129. )
  130. SET(dynamicHeaders
  131. "${dynamicHeaders};${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h")
  132. # Make sure variable are cleared
  133. SET(MY_MOC_CPP)
  134. SET(MY_UI_CPP)
  135. SET(MY_QRC_SRCS)
  136. # Wrap
  137. IF(MY_MOC_SRCS)
  138. # this is a workaround for Visual Studio. The relative include paths in the generated
  139. # moc files can get very long and can't be resolved by the MSVC compiler.
  140. FOREACH(moc_src ${MY_MOC_SRCS})
  141. QT4_WRAP_CPP(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src})
  142. ENDFOREACH()
  143. ENDIF()
  144. QT4_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
  145. IF(DEFINED MY_RESOURCES)
  146. QT4_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
  147. ENDIF()
  148. # Add the generated manifest qrc file
  149. SET(manifest_qrc_src )
  150. ctkFunctionGeneratePluginManifest(manifest_qrc_src
  151. ACTIVATIONPOLICY ${Plugin-ActivationPolicy}
  152. CATEGORY ${Plugin-Category}
  153. CONTACT_ADDRESS ${Plugin-ContactAddress}
  154. COPYRIGHT ${Plugin-Copyright}
  155. DESCRIPTION ${Plugin-Description}
  156. DOC_URL ${Plugin-DocURL}
  157. ICON ${Plugin-Icon}
  158. LICENSE ${Plugin-License}
  159. NAME ${Plugin-Name}
  160. REQUIRE_PLUGIN ${Require-Plugin}
  161. SYMBOLIC_NAME ${Plugin-SymbolicName}
  162. VENDOR ${Plugin-Vendor}
  163. VERSION ${Plugin-Version}
  164. CUSTOM_HEADERS ${Custom-Headers}
  165. )
  166. IF(manifest_headers_dep)
  167. SET_PROPERTY(SOURCE ${manifest_qrc_src} APPEND
  168. PROPERTY OBJECT_DEPENDS ${manifest_headers_dep})
  169. ENDIF()
  170. LIST(APPEND MY_QRC_SRCS ${manifest_qrc_src})
  171. # Create translation files (.ts and .qm)
  172. SET(_plugin_qm_files )
  173. SET(_plugin_cached_resources_in_binary_tree )
  174. SET(_translations_dir "${CMAKE_CURRENT_BINARY_DIR}/CTK-INF/l10n")
  175. IF(MY_TRANSLATIONS)
  176. SET_SOURCE_FILES_PROPERTIES(${MY_TRANSLATIONS}
  177. PROPERTIES OUTPUT_LOCATION ${_translations_dir})
  178. QT4_CREATE_TRANSLATION(_plugin_qm_files ${MY_SRCS} ${MY_UI_FORMS} ${MY_TRANSLATIONS})
  179. ENDIF()
  180. IF(_plugin_qm_files)
  181. FOREACH(_qm_file ${_plugin_qm_files})
  182. FILE(RELATIVE_PATH _relative_qm_file ${CMAKE_CURRENT_BINARY_DIR} ${_qm_file})
  183. LIST(APPEND _plugin_cached_resources_in_binary_tree ${_relative_qm_file})
  184. ENDFOREACH()
  185. ENDIF()
  186. SET(_plugin_cached_resources_in_source_tree )
  187. IF(MY_CACHED_RESOURCEFILES)
  188. FOREACH(_cached_resource ${MY_CACHED_RESOURCEFILES})
  189. IF(IS_ABSOLUTE "${_cached_resource}")
  190. # create a path relative to the current binary dir
  191. FILE(RELATIVE_PATH _relative_cached_resource ${CMAKE_CURRENT_BINARY_DIR} ${_cached_resource})
  192. LIST(APPEND _plugin_cached_resources_in_binary_tree ${_relative_cached_resource})
  193. ELSE()
  194. LIST(APPEND _plugin_cached_resources_in_source_tree ${_cached_resource})
  195. ENDIF()
  196. ENDFOREACH()
  197. ENDIF()
  198. # Add any other additional resource files
  199. IF(_plugin_cached_resources_in_source_tree OR _plugin_cached_resources_in_binary_tree)
  200. STRING(REPLACE "." "_" _plugin_symbolicname ${Plugin-SymbolicName})
  201. ctkMacroGeneratePluginResourceFile(MY_QRC_SRCS
  202. NAME ${_plugin_symbolicname}_cached.qrc
  203. PREFIX ${Plugin-SymbolicName}
  204. RESOURCES ${_plugin_cached_resources_in_source_tree}
  205. BINARY_RESOURCES ${_plugin_cached_resources_in_binary_tree})
  206. ENDIF()
  207. SOURCE_GROUP("Resources" FILES
  208. ${MY_RESOURCES}
  209. ${MY_UI_FORMS}
  210. ${MY_TRANSLATIONS}
  211. )
  212. SOURCE_GROUP("Generated" FILES
  213. ${MY_QRC_SRCS}
  214. ${MY_MOC_CPP}
  215. ${MY_UI_CPP}
  216. ${_plugin_qm_files}
  217. )
  218. ADD_LIBRARY(${lib_name} ${MY_LIBRARY_TYPE}
  219. ${MY_SRCS}
  220. ${MY_MOC_CPP}
  221. ${MY_UI_CPP}
  222. ${MY_QRC_SRCS}
  223. ${_plugin_qm_files}
  224. )
  225. # Set the output directory for the plugin
  226. IF(MY_OUTPUT_DIR)
  227. SET(output_dir_suffix ${MY_OUTPUT_DIR})
  228. ELSE()
  229. SET(output_dir_suffix "plugins")
  230. ENDIF()
  231. IF(MY_TEST_PLUGIN)
  232. SET(output_dir_suffix "test_${output_dir_suffix}")
  233. ENDIF()
  234. IF(CMAKE_RUNTIME_OUTPUT_DIRECTORY)
  235. SET(runtime_output_dir "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${output_dir_suffix}")
  236. ELSE()
  237. SET(runtime_output_dir "${CMAKE_CURRENT_BINARY_DIR}/${output_dir_suffix}")
  238. ENDIF()
  239. IF(CMAKE_LIBRARY_OUTPUT_DIRECTORY)
  240. SET(library_output_dir "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${output_dir_suffix}")
  241. ELSE()
  242. SET(library_output_dir "${CMAKE_CURRENT_BINARY_DIR}/${output_dir_suffix}")
  243. ENDIF()
  244. SET(plugin_compile_flags "-DQT_PLUGIN")
  245. # MinGW does not export all symbols automatically, so no need to set flags
  246. IF(CMAKE_COMPILER_IS_GNUCXX AND NOT MINGW)
  247. SET(plugin_compile_flags "${plugin_compile_flags} -fvisibility=hidden -fvisibility-inlines-hidden")
  248. ENDIF()
  249. # Apply properties to the library target.
  250. SET_TARGET_PROPERTIES(${lib_name} PROPERTIES
  251. COMPILE_FLAGS "${plugin_compile_flags}"
  252. RUNTIME_OUTPUT_DIRECTORY ${runtime_output_dir}
  253. LIBRARY_OUTPUT_DIRECTORY ${library_output_dir}
  254. PREFIX "lib"
  255. )
  256. # Note: The plugin may be installed in some other location ???
  257. # Install rules
  258. # IF(MY_LIBRARY_TYPE STREQUAL "SHARED")
  259. # INSTALL(TARGETS ${lib_name}
  260. # RUNTIME DESTINATION ${CTK_INSTALL_BIN_DIR} COMPONENT Runtime
  261. # LIBRARY DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT Runtime
  262. # ARCHIVE DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT Development)
  263. # ENDIF()
  264. SET(my_libs
  265. ${MY_TARGET_LIBRARIES}
  266. )
  267. IF(MINGW)
  268. LIST(APPEND my_libs ssp) # add stack smash protection lib
  269. ENDIF()
  270. TARGET_LINK_LIBRARIES(${lib_name} ${my_libs})
  271. # Update CTK_PLUGINS
  272. IF(NOT MY_TEST_PLUGIN)
  273. SET(CTK_PLUGIN_LIBRARIES ${CTK_PLUGIN_LIBRARIES} ${lib_name} CACHE INTERNAL "CTK plugins" FORCE)
  274. ENDIF()
  275. # Install headers
  276. #FILE(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
  277. #INSTALL(FILES
  278. # ${headers}
  279. # ${dynamicHeaders}
  280. # DESTINATION ${CTK_INSTALL_INCLUDE_DIR} COMPONENT Development
  281. # )
  282. ENDMACRO()