ctkMacroBuildPlugin.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 "")
  230. endif()
  231. foreach(type RUNTIME LIBRARY ARCHIVE)
  232. if(NOT DEFINED CTK_PLUGIN_${type}_OUTPUT_DIRECTORY AND CMAKE_${type}_OUTPUT_DIRECTORY)
  233. # Put plug-ins by default into a "plugins" subdirectory
  234. set(CTK_PLUGIN_${type}_OUTPUT_DIRECTORY "${CMAKE_${type}_OUTPUT_DIRECTORY}/plugins")
  235. endif()
  236. if(IS_ABSOLUTE "${CTK_PLUGIN_${type}_OUTPUT_DIRECTORY}")
  237. set(plugin_${type}_output_dir "${CTK_PLUGIN_${type}_OUTPUT_DIRECTORY}${output_dir_suffix}")
  238. elseif(CMAKE_${type}_OUTPUT_DIRECTORY)
  239. set(plugin_${type}_output_dir "${CMAKE_${type}_OUTPUT_DIRECTORY}/${CTK_PLUGIN_${type}_OUTPUT_DIRECTORY}${output_dir_suffix}")
  240. else()
  241. set(plugin_${type}_output_dir "${CMAKE_CURRENT_BINARY_DIR}/${CTK_PLUGIN_${type}_OUTPUT_DIRECTORY}${output_dir_suffix}")
  242. endif()
  243. if(MY_TEST_PLUGIN)
  244. # Test plug-ins will always be put in a separate directory
  245. if(CMAKE_${type}_OUTPUT_DIRECTORY)
  246. set(plugin_${type}_output_dir "${CMAKE_${type}_OUTPUT_DIRECTORY}/test_plugins")
  247. else()
  248. set(plugin_${type}_output_dir "${PROJECT_BINARY_DIR}/test_plugins")
  249. endif()
  250. endif()
  251. endforeach()
  252. set(plugin_compile_flags "-DQT_PLUGIN")
  253. # MinGW does not export all symbols automatically, so no need to set flags.
  254. #
  255. # With gcc < 4.5, RTTI symbols from classes declared in third-party libraries
  256. # which are not "gcc visibility aware" are marked with hidden visibility in
  257. # DSOs which include the class declaration and which are compiled with
  258. # hidden visibility. This leads to dynamic_cast and exception handling problems.
  259. # While this problem could be worked around by sandwiching the include
  260. # directives for the third-party headers between "#pragma visibility push/pop"
  261. # statements, it is generally safer to just use default visibility with
  262. # gcc < 4.5.
  263. if(CMAKE_COMPILER_IS_GNUCXX AND NOT ${GCC_VERSION} VERSION_LESS "4.5" AND NOT MINGW)
  264. set(plugin_compile_flags "${plugin_compile_flags} -fvisibility=hidden -fvisibility-inlines-hidden")
  265. endif()
  266. # Apply properties to the library target.
  267. set_target_properties(${lib_name} PROPERTIES
  268. COMPILE_FLAGS "${plugin_compile_flags}"
  269. RUNTIME_OUTPUT_DIRECTORY ${plugin_RUNTIME_output_dir}
  270. LIBRARY_OUTPUT_DIRECTORY ${plugin_LIBRARY_output_dir}
  271. ARCHIVE_OUTPUT_DIRECTORY ${plugin_ARCHIVE_output_dir}
  272. PREFIX "lib"
  273. )
  274. # Note: The plugin may be installed in some other location ???
  275. # Install rules
  276. # if(MY_LIBRARY_TYPE STREQUAL "SHARED")
  277. # install(TARGETS ${lib_name}
  278. # RUNTIME DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT RuntimePlugins
  279. # LIBRARY DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT RuntimePlugins
  280. # ARCHIVE DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT Development)
  281. # endif()
  282. set(my_libs
  283. ${MY_TARGET_LIBRARIES}
  284. )
  285. if(MINGW)
  286. list(APPEND my_libs ssp) # add stack smash protection lib
  287. endif()
  288. target_link_libraries(${lib_name} ${my_libs})
  289. if(NOT MY_TEST_PLUGIN)
  290. set(${CMAKE_PROJECT_NAME}_PLUGIN_LIBRARIES ${${CMAKE_PROJECT_NAME}_PLUGIN_LIBRARIES} ${lib_name} CACHE INTERNAL "CTK plugins" FORCE)
  291. endif()
  292. # Install headers
  293. #file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
  294. #install(FILES
  295. # ${headers}
  296. # ${dynamicHeaders}
  297. # DESTINATION ${CTK_INSTALL_INCLUDE_DIR} COMPONENT Development
  298. # )
  299. endmacro()