ctkMacroBuildPlugin.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. #! \brief Build a CTK plug-in.
  26. #!
  27. #! This macro takes the usual arguments for building
  28. #! a shared library using Qt. Additionally, it generates
  29. #! plugin meta-data by creating a MANIFEST.MF text file
  30. #! which is embedded in the share library as a Qt resource.
  31. #!
  32. #! The following variables can be set in a file named
  33. #! manifest_headers.cmake, which will then be read by
  34. #! this macro:
  35. #!
  36. #! - Plugin-ActivationPolicy
  37. #! - Plugin-Category
  38. #! - Plugin-ContactAddress
  39. #! - Plugin-Copyright
  40. #! - Plugin-Description
  41. #! - Plugin-DocURL
  42. #! - Plugin-Icon
  43. #! - Plugin-License
  44. #! - Plugin-Name
  45. #! - Require-Plugin
  46. #! - Plugin-Vendor
  47. #! - Plugin-Version
  48. #!
  49. #! \ingroup CMakeAPI
  50. macro(ctkMacroBuildPlugin)
  51. CtkMacroParseArguments(MY
  52. "EXPORT_DIRECTIVE;SRCS;MOC_SRCS;MOC_OPTIONS;UI_FORMS;INCLUDE_DIRECTORIES;EXPORTED_INCLUDE_SUFFIXES;TARGET_LIBRARIES;RESOURCES;CACHED_RESOURCEFILES;TRANSLATIONS;OUTPUT_DIR"
  53. "TEST_PLUGIN"
  54. ${ARGN}
  55. )
  56. # Keep parameter 'INCLUDE_DIRECTORIES' for backward compatiblity
  57. # Sanity checks
  58. if(NOT DEFINED MY_EXPORT_DIRECTIVE)
  59. message(FATAL_ERROR "EXPORT_DIRECTIVE is mandatory")
  60. endif()
  61. # Print a warning if the project name does not match the directory name
  62. get_filename_component(_dir_name ${CMAKE_CURRENT_SOURCE_DIR} NAME)
  63. string(REPLACE "." "_" _dir_name_with_ ${_dir_name})
  64. if(NOT _dir_name_with_ STREQUAL ${PROJECT_NAME})
  65. message(WARNING "Discouraged mismatch of plug-in project name [${PROJECT_NAME}] and top-level directory name [${CMAKE_CURRENT_SOURCE_DIR}].")
  66. endif()
  67. # Define library name
  68. set(lib_name ${PROJECT_NAME})
  69. # Plug-in target names must contain at leas one _
  70. if(NOT lib_name MATCHES _)
  71. message(FATAL_ERROR "The plug-in project name ${lib_name} must contain at least one '_' character")
  72. endif()
  73. # Plugin are expected to be shared library
  74. set(MY_LIBRARY_TYPE "SHARED")
  75. # Clear the variables for the manifest headers
  76. set(Plugin-ActivationPolicy )
  77. set(Plugin-Category )
  78. set(Plugin-ContactAddress )
  79. set(Plugin-Copyright )
  80. set(Plugin-Description )
  81. set(Plugin-DocURL )
  82. set(Plugin-Icon )
  83. set(Plugin-License )
  84. set(Plugin-Name )
  85. set(Require-Plugin )
  86. set(Plugin-SymbolicName )
  87. set(Plugin-Vendor )
  88. set(Plugin-Version )
  89. set(Custom-Headers )
  90. if(MY_TEST_PLUGIN)
  91. # Since the test plug-ins are not considered when calculating
  92. # target dependencies via DGraph, we add the dependencies
  93. # manually here
  94. #message("${lib_name}_DEPENDENCIES ${MY_TARGET_LIBRARIES}")
  95. list(APPEND ${lib_name}_DEPENDENCIES ${MY_TARGET_LIBRARIES})
  96. endif()
  97. # If a file named manifest_headers.cmake exists, read it
  98. set(manifest_headers_dep )
  99. if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/manifest_headers.cmake")
  100. include(${CMAKE_CURRENT_SOURCE_DIR}/manifest_headers.cmake)
  101. set(manifest_headers_dep "${CMAKE_CURRENT_SOURCE_DIR}/manifest_headers.cmake")
  102. endif()
  103. string(REPLACE "_" "." Plugin-SymbolicName ${lib_name})
  104. # --------------------------------------------------------------------------
  105. # Include dirs
  106. if(MY_EXPORTED_INCLUDE_SUFFIXES)
  107. set(${lib_name}_INCLUDE_SUFFIXES ${MY_EXPORTED_INCLUDE_SUFFIXES}
  108. CACHE INTERNAL "List of exported plugin include dirs")
  109. set(my_includes )
  110. foreach(_suffix ${MY_EXPORTED_INCLUDE_SUFFIXES})
  111. list(APPEND my_includes ${CMAKE_CURRENT_SOURCE_DIR}/${_suffix})
  112. endforeach()
  113. else()
  114. set(${lib_name}_INCLUDE_SUFFIXES ""
  115. CACHE INTERNAL "List of exported plugin include dirs")
  116. set(my_includes )
  117. endif()
  118. list(APPEND my_includes
  119. ${CMAKE_CURRENT_SOURCE_DIR}
  120. ${CMAKE_CURRENT_BINARY_DIR}
  121. )
  122. # Add the include directories from the plugin dependencies
  123. # and external dependencies
  124. ctkFunctionGetIncludeDirs(my_includes ${lib_name})
  125. include_directories(
  126. ${my_includes}
  127. )
  128. if(CTK_QT_VERSION VERSION_LESS "5")
  129. # Add Qt include dirs and defines
  130. include(${QT_USE_FILE})
  131. else()
  132. find_package(Qt5LinguistTools REQUIRED)
  133. endif()
  134. # Add the library directories from the external project
  135. ctkFunctionGetLibraryDirs(my_library_dirs ${lib_name})
  136. link_directories(
  137. ${my_library_dirs}
  138. )
  139. set(MY_LIBRARY_EXPORT_DIRECTIVE ${MY_EXPORT_DIRECTIVE})
  140. set(MY_EXPORT_HEADER_PREFIX "${lib_name}_")
  141. set(MY_LIBNAME ${lib_name})
  142. configure_file(
  143. ${CTK_EXPORT_HEADER_TEMPLATE}
  144. ${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h
  145. )
  146. set(dynamicHeaders
  147. "${dynamicHeaders};${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h")
  148. # Make sure variable are cleared
  149. set(MY_MOC_CPP)
  150. set(MY_UI_CPP)
  151. set(MY_QRC_SRCS)
  152. # Wrap
  153. if (CTK_QT_VERSION VERSION_GREATER "4")
  154. if(MY_MOC_SRCS)
  155. # this is a workaround for Visual Studio. The relative include paths in the generated
  156. # moc files can get very long and can't be resolved by the MSVC compiler.
  157. foreach(moc_src ${MY_MOC_SRCS})
  158. QT5_WRAP_CPP(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src} -DHAVE_QT5)
  159. endforeach()
  160. endif()
  161. QT5_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
  162. if(DEFINED MY_RESOURCES)
  163. QT5_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
  164. endif()
  165. else()
  166. if(MY_MOC_SRCS)
  167. # this is a workaround for Visual Studio. The relative include paths in the generated
  168. # moc files can get very long and can't be resolved by the MSVC compiler.
  169. foreach(moc_src ${MY_MOC_SRCS})
  170. QT4_WRAP_CPP(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src})
  171. endforeach()
  172. endif()
  173. QT4_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
  174. if(DEFINED MY_RESOURCES)
  175. QT4_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
  176. endif()
  177. endif()
  178. # Add the generated manifest qrc file
  179. set(manifest_qrc_src )
  180. ctkFunctionGeneratePluginManifest(manifest_qrc_src
  181. ACTIVATIONPOLICY ${Plugin-ActivationPolicy}
  182. CATEGORY ${Plugin-Category}
  183. CONTACT_ADDRESS ${Plugin-ContactAddress}
  184. COPYRIGHT ${Plugin-Copyright}
  185. DESCRIPTION ${Plugin-Description}
  186. DOC_URL ${Plugin-DocURL}
  187. ICON ${Plugin-Icon}
  188. LICENSE ${Plugin-License}
  189. NAME ${Plugin-Name}
  190. REQUIRE_PLUGIN ${Require-Plugin}
  191. SYMBOLIC_NAME ${Plugin-SymbolicName}
  192. VENDOR ${Plugin-Vendor}
  193. VERSION ${Plugin-Version}
  194. CUSTOM_HEADERS ${Custom-Headers}
  195. )
  196. if(manifest_headers_dep)
  197. set_property(SOURCE ${manifest_qrc_src} APPEND
  198. PROPERTY OBJECT_DEPENDS ${manifest_headers_dep})
  199. endif()
  200. list(APPEND MY_QRC_SRCS ${manifest_qrc_src})
  201. # Create translation files (.ts and .qm)
  202. set(_plugin_qm_files )
  203. set(_plugin_cached_resources_in_binary_tree )
  204. set(_translations_dir "${CMAKE_CURRENT_BINARY_DIR}/CTK-INF/l10n")
  205. if(MY_TRANSLATIONS)
  206. set_source_files_properties(${MY_TRANSLATIONS}
  207. PROPERTIES OUTPUT_LOCATION ${_translations_dir})
  208. if(CTK_QT_VERSION VERSION_GREATER "4")
  209. qt5_create_translation(_plugin_qm_files ${MY_SRCS} ${MY_UI_FORMS} ${MY_TRANSLATIONS})
  210. else()
  211. QT4_CREATE_TRANSLATION(_plugin_qm_files ${MY_SRCS} ${MY_UI_FORMS} ${MY_TRANSLATIONS})
  212. endif()
  213. endif()
  214. if(_plugin_qm_files)
  215. foreach(_qm_file ${_plugin_qm_files})
  216. file(RELATIVE_PATH _relative_qm_file ${CMAKE_CURRENT_BINARY_DIR} ${_qm_file})
  217. list(APPEND _plugin_cached_resources_in_binary_tree ${_relative_qm_file})
  218. endforeach()
  219. endif()
  220. set(_plugin_cached_resources_in_source_tree )
  221. if(MY_CACHED_RESOURCEFILES)
  222. foreach(_cached_resource ${MY_CACHED_RESOURCEFILES})
  223. if(IS_ABSOLUTE "${_cached_resource}")
  224. # create a path relative to the current binary dir
  225. file(RELATIVE_PATH _relative_cached_resource ${CMAKE_CURRENT_BINARY_DIR} ${_cached_resource})
  226. list(APPEND _plugin_cached_resources_in_binary_tree ${_relative_cached_resource})
  227. else()
  228. list(APPEND _plugin_cached_resources_in_source_tree ${_cached_resource})
  229. endif()
  230. endforeach()
  231. endif()
  232. # Add any other additional resource files
  233. if(_plugin_cached_resources_in_source_tree OR _plugin_cached_resources_in_binary_tree)
  234. string(REPLACE "." "_" _plugin_symbolicname ${Plugin-SymbolicName})
  235. ctkMacroGeneratePluginResourcefile(MY_QRC_SRCS
  236. NAME ${_plugin_symbolicname}_cached.qrc
  237. PREFIX ${Plugin-SymbolicName}
  238. RESOURCES ${_plugin_cached_resources_in_source_tree}
  239. BINARY_RESOURCES ${_plugin_cached_resources_in_binary_tree})
  240. endif()
  241. source_group("Resources" FILES
  242. ${MY_RESOURCES}
  243. ${MY_UI_FORMS}
  244. ${MY_TRANSLATIONS}
  245. )
  246. source_group("Generated" FILES
  247. ${MY_QRC_SRCS}
  248. ${MY_MOC_CPP}
  249. ${MY_UI_CPP}
  250. ${_plugin_qm_files}
  251. )
  252. add_library(${lib_name} ${MY_LIBRARY_TYPE}
  253. ${MY_SRCS}
  254. ${MY_MOC_CPP}
  255. ${MY_UI_CPP}
  256. ${MY_QRC_SRCS}
  257. ${_plugin_qm_files}
  258. )
  259. if(MY_TEST_PLUGIN AND CTK_QT_VERSION VERSION_GREATER "4")
  260. find_package(Qt5Test REQUIRED)
  261. target_link_libraries(${lib_name} Qt5::Test)
  262. endif()
  263. # Set the output directory for the plugin
  264. if(MY_OUTPUT_DIR)
  265. set(output_dir_suffix "/${MY_OUTPUT_DIR}")
  266. else()
  267. set(output_dir_suffix "")
  268. endif()
  269. foreach(type RUNTIME LIBRARY ARCHIVE)
  270. if(NOT DEFINED CTK_PLUGIN_${type}_OUTPUT_DIRECTORY AND CMAKE_${type}_OUTPUT_DIRECTORY)
  271. # Put plug-ins by default into a "plugins" subdirectory
  272. set(CTK_PLUGIN_${type}_OUTPUT_DIRECTORY "${CMAKE_${type}_OUTPUT_DIRECTORY}/plugins")
  273. endif()
  274. if(IS_ABSOLUTE "${CTK_PLUGIN_${type}_OUTPUT_DIRECTORY}")
  275. set(plugin_${type}_output_dir "${CTK_PLUGIN_${type}_OUTPUT_DIRECTORY}${output_dir_suffix}")
  276. elseif(CMAKE_${type}_OUTPUT_DIRECTORY)
  277. set(plugin_${type}_output_dir "${CMAKE_${type}_OUTPUT_DIRECTORY}/${CTK_PLUGIN_${type}_OUTPUT_DIRECTORY}${output_dir_suffix}")
  278. else()
  279. set(plugin_${type}_output_dir "${CMAKE_CURRENT_BINARY_DIR}/${CTK_PLUGIN_${type}_OUTPUT_DIRECTORY}${output_dir_suffix}")
  280. endif()
  281. if(MY_TEST_PLUGIN)
  282. # Test plug-ins will always be put in a separate directory
  283. if(CMAKE_${type}_OUTPUT_DIRECTORY)
  284. set(plugin_${type}_output_dir "${CMAKE_${type}_OUTPUT_DIRECTORY}/test_plugins")
  285. else()
  286. set(plugin_${type}_output_dir "${PROJECT_BINARY_DIR}/test_plugins")
  287. endif()
  288. endif()
  289. endforeach()
  290. set(plugin_compile_flags "-DQT_PLUGIN")
  291. ctkFunctionGetCompilerVisibilityFlags(plugin_compile_flags)
  292. # Apply properties to the library target.
  293. set_target_properties(${lib_name} PROPERTIES
  294. COMPILE_FLAGS "${plugin_compile_flags}"
  295. RUNTIME_OUTPUT_DIRECTORY ${plugin_RUNTIME_output_dir}
  296. LIBRARY_OUTPUT_DIRECTORY ${plugin_LIBRARY_output_dir}
  297. ARCHIVE_OUTPUT_DIRECTORY ${plugin_ARCHIVE_output_dir}
  298. PREFIX "lib"
  299. )
  300. if(NOT MY_TEST_PLUGIN)
  301. # Install rules
  302. install(TARGETS ${lib_name} EXPORT CTKExports
  303. RUNTIME DESTINATION ${CTK_INSTALL_PLUGIN_DIR} COMPONENT RuntimePlugins
  304. LIBRARY DESTINATION ${CTK_INSTALL_PLUGIN_DIR} COMPONENT RuntimePlugins
  305. ARCHIVE DESTINATION ${CTK_INSTALL_PLUGIN_DIR} COMPONENT Development)
  306. endif()
  307. set(my_libs
  308. ${MY_TARGET_LIBRARIES}
  309. )
  310. if(MINGW)
  311. list(APPEND my_libs ssp) # add stack smash protection lib
  312. endif()
  313. target_link_libraries(${lib_name} ${my_libs})
  314. if(NOT MY_TEST_PLUGIN)
  315. set(${CMAKE_PROJECT_NAME}_PLUGIN_LIBRARIES ${${CMAKE_PROJECT_NAME}_PLUGIN_LIBRARIES} ${lib_name} CACHE INTERNAL "CTK plugins" FORCE)
  316. endif()
  317. if(NOT MY_TEST_PLUGIN)
  318. # Install headers
  319. file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.tpp")
  320. install(FILES
  321. ${headers}
  322. ${dynamicHeaders}
  323. DESTINATION ${CTK_INSTALL_PLUGIN_INCLUDE_DIR}/${Plugin-SymbolicName} COMPONENT Development
  324. )
  325. endif()
  326. endmacro()