ctkMacroBuildPlugin.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. # Sanity checks
  57. if(NOT DEFINED MY_EXPORT_DIRECTIVE)
  58. message(FATAL_ERROR "EXPORT_DIRECTIVE is mandatory")
  59. endif()
  60. # Print a warning if the project name does not match the directory name
  61. get_filename_component(_dir_name ${CMAKE_CURRENT_SOURCE_DIR} NAME)
  62. string(REPLACE "." "_" _dir_name_with_ ${_dir_name})
  63. if(NOT _dir_name_with_ STREQUAL ${PROJECT_NAME})
  64. message(WARNING "Discouraged mismatch of plug-in project name [${PROJECT_NAME}] and top-level directory name [${CMAKE_CURRENT_SOURCE_DIR}].")
  65. endif()
  66. # Define library name
  67. set(lib_name ${PROJECT_NAME})
  68. # Plug-in target names must contain at leas one _
  69. if(NOT lib_name MATCHES _)
  70. message(FATAL_ERROR "The plug-in project name ${lib_name} must contain at least one '_' character")
  71. endif()
  72. # Plugin are expected to be shared library
  73. set(MY_LIBRARY_TYPE "SHARED")
  74. # Clear the variables for the manifest headers
  75. set(Plugin-ActivationPolicy )
  76. set(Plugin-Category )
  77. set(Plugin-ContactAddress )
  78. set(Plugin-Copyright )
  79. set(Plugin-Description )
  80. set(Plugin-DocURL )
  81. set(Plugin-Icon )
  82. set(Plugin-License )
  83. set(Plugin-Name )
  84. set(Require-Plugin )
  85. set(Plugin-SymbolicName )
  86. set(Plugin-Vendor )
  87. set(Plugin-Version )
  88. set(Custom-Headers )
  89. if(MY_TEST_PLUGIN)
  90. # Since the test plug-ins are not considered when calculating
  91. # target dependencies via DGraph, we add the dependencies
  92. # manually here
  93. #message("${lib_name}_DEPENDENCIES ${MY_TARGET_LIBRARIES}")
  94. list(APPEND ${lib_name}_DEPENDENCIES ${MY_TARGET_LIBRARIES})
  95. endif()
  96. # If a file named manifest_headers.cmake exists, read it
  97. set(manifest_headers_dep )
  98. if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/manifest_headers.cmake")
  99. include(${CMAKE_CURRENT_SOURCE_DIR}/manifest_headers.cmake)
  100. set(manifest_headers_dep "${CMAKE_CURRENT_SOURCE_DIR}/manifest_headers.cmake")
  101. endif()
  102. string(REPLACE "_" "." Plugin-SymbolicName ${lib_name})
  103. # --------------------------------------------------------------------------
  104. # Include dirs
  105. if(MY_EXPORTED_INCLUDE_SUFFIXES)
  106. set(${lib_name}_INCLUDE_SUFFIXES ${MY_EXPORTED_INCLUDE_SUFFIXES}
  107. CACHE INTERNAL "List of exported plugin include dirs")
  108. set(my_includes )
  109. foreach(_suffix ${MY_EXPORTED_INCLUDE_SUFFIXES})
  110. list(APPEND my_includes ${CMAKE_CURRENT_SOURCE_DIR}/${_suffix})
  111. endforeach()
  112. else()
  113. set(${lib_name}_INCLUDE_SUFFIXES ""
  114. CACHE INTERNAL "List of exported plugin include dirs")
  115. set(my_includes )
  116. endif()
  117. list(APPEND my_includes
  118. ${CMAKE_CURRENT_SOURCE_DIR}
  119. ${CMAKE_CURRENT_BINARY_DIR}
  120. ${MY_INCLUDE_DIRECTORIES}
  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. # Add Qt include dirs and defines
  129. include(${QT_USE_FILE})
  130. # Add the library directories from the external project
  131. ctkFunctionGetLibraryDirs(my_library_dirs ${lib_name})
  132. link_directories(
  133. ${my_library_dirs}
  134. )
  135. set(MY_LIBRARY_EXPORT_DIRECTIVE ${MY_EXPORT_DIRECTIVE})
  136. set(MY_EXPORT_HEADER_PREFIX "${lib_name}_")
  137. set(MY_LIBNAME ${lib_name})
  138. configure_file(
  139. ${CTK_EXPORT_HEADER_TEMPLATE}
  140. ${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h
  141. )
  142. set(dynamicHeaders
  143. "${dynamicHeaders};${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h")
  144. # Make sure variable are cleared
  145. set(MY_MOC_CPP)
  146. set(MY_UI_CPP)
  147. set(MY_QRC_SRCS)
  148. # Wrap
  149. if(MY_MOC_SRCS)
  150. # this is a workaround for Visual Studio. The relative include paths in the generated
  151. # moc files can get very long and can't be resolved by the MSVC compiler.
  152. foreach(moc_src ${MY_MOC_SRCS})
  153. QT4_WRAP_CPP(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src} ${MY_MOC_OPTIONS})
  154. endforeach()
  155. endif()
  156. QT4_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
  157. if(DEFINED MY_RESOURCES)
  158. QT4_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
  159. endif()
  160. # Add the generated manifest qrc file
  161. set(manifest_qrc_src )
  162. ctkFunctionGeneratePluginManifest(manifest_qrc_src
  163. ACTIVATIONPOLICY ${Plugin-ActivationPolicy}
  164. CATEGORY ${Plugin-Category}
  165. CONTACT_ADDRESS ${Plugin-ContactAddress}
  166. COPYRIGHT ${Plugin-Copyright}
  167. DESCRIPTION ${Plugin-Description}
  168. DOC_URL ${Plugin-DocURL}
  169. ICON ${Plugin-Icon}
  170. LICENSE ${Plugin-License}
  171. NAME ${Plugin-Name}
  172. REQUIRE_PLUGIN ${Require-Plugin}
  173. SYMBOLIC_NAME ${Plugin-SymbolicName}
  174. VENDOR ${Plugin-Vendor}
  175. VERSION ${Plugin-Version}
  176. CUSTOM_HEADERS ${Custom-Headers}
  177. )
  178. if(manifest_headers_dep)
  179. set_property(SOURCE ${manifest_qrc_src} APPEND
  180. PROPERTY OBJECT_DEPENDS ${manifest_headers_dep})
  181. endif()
  182. list(APPEND MY_QRC_SRCS ${manifest_qrc_src})
  183. # Create translation files (.ts and .qm)
  184. set(_plugin_qm_files )
  185. set(_plugin_cached_resources_in_binary_tree )
  186. set(_translations_dir "${CMAKE_CURRENT_BINARY_DIR}/CTK-INF/l10n")
  187. if(MY_TRANSLATIONS)
  188. set_source_files_properties(${MY_TRANSLATIONS}
  189. PROPERTIES OUTPUT_LOCATION ${_translations_dir})
  190. QT4_CREATE_TRANSLATION(_plugin_qm_files ${MY_SRCS} ${MY_UI_FORMS} ${MY_TRANSLATIONS})
  191. endif()
  192. if(_plugin_qm_files)
  193. foreach(_qm_file ${_plugin_qm_files})
  194. file(RELATIVE_PATH _relative_qm_file ${CMAKE_CURRENT_BINARY_DIR} ${_qm_file})
  195. list(APPEND _plugin_cached_resources_in_binary_tree ${_relative_qm_file})
  196. endforeach()
  197. endif()
  198. set(_plugin_cached_resources_in_source_tree )
  199. if(MY_CACHED_RESOURCEFILES)
  200. foreach(_cached_resource ${MY_CACHED_RESOURCEFILES})
  201. if(IS_ABSOLUTE "${_cached_resource}")
  202. # create a path relative to the current binary dir
  203. file(RELATIVE_PATH _relative_cached_resource ${CMAKE_CURRENT_BINARY_DIR} ${_cached_resource})
  204. list(APPEND _plugin_cached_resources_in_binary_tree ${_relative_cached_resource})
  205. else()
  206. list(APPEND _plugin_cached_resources_in_source_tree ${_cached_resource})
  207. endif()
  208. endforeach()
  209. endif()
  210. # Add any other additional resource files
  211. if(_plugin_cached_resources_in_source_tree OR _plugin_cached_resources_in_binary_tree)
  212. string(REPLACE "." "_" _plugin_symbolicname ${Plugin-SymbolicName})
  213. ctkMacroGeneratePluginResourcefile(MY_QRC_SRCS
  214. NAME ${_plugin_symbolicname}_cached.qrc
  215. PREFIX ${Plugin-SymbolicName}
  216. RESOURCES ${_plugin_cached_resources_in_source_tree}
  217. BINARY_RESOURCES ${_plugin_cached_resources_in_binary_tree})
  218. endif()
  219. source_group("Resources" FILES
  220. ${MY_RESOURCES}
  221. ${MY_UI_FORMS}
  222. ${MY_TRANSLATIONS}
  223. )
  224. source_group("Generated" FILES
  225. ${MY_QRC_SRCS}
  226. ${MY_MOC_CPP}
  227. ${MY_UI_CPP}
  228. ${_plugin_qm_files}
  229. )
  230. add_library(${lib_name} ${MY_LIBRARY_TYPE}
  231. ${MY_SRCS}
  232. ${MY_MOC_CPP}
  233. ${MY_UI_CPP}
  234. ${MY_QRC_SRCS}
  235. ${_plugin_qm_files}
  236. )
  237. # Set the output directory for the plugin
  238. if(MY_OUTPUT_DIR)
  239. set(output_dir_suffix "/${MY_OUTPUT_DIR}")
  240. else()
  241. set(output_dir_suffix "")
  242. endif()
  243. foreach(type RUNTIME LIBRARY ARCHIVE)
  244. if(NOT DEFINED CTK_PLUGIN_${type}_OUTPUT_DIRECTORY AND CMAKE_${type}_OUTPUT_DIRECTORY)
  245. # Put plug-ins by default into a "plugins" subdirectory
  246. set(CTK_PLUGIN_${type}_OUTPUT_DIRECTORY "${CMAKE_${type}_OUTPUT_DIRECTORY}/plugins")
  247. endif()
  248. if(IS_ABSOLUTE "${CTK_PLUGIN_${type}_OUTPUT_DIRECTORY}")
  249. set(plugin_${type}_output_dir "${CTK_PLUGIN_${type}_OUTPUT_DIRECTORY}${output_dir_suffix}")
  250. elseif(CMAKE_${type}_OUTPUT_DIRECTORY)
  251. set(plugin_${type}_output_dir "${CMAKE_${type}_OUTPUT_DIRECTORY}/${CTK_PLUGIN_${type}_OUTPUT_DIRECTORY}${output_dir_suffix}")
  252. else()
  253. set(plugin_${type}_output_dir "${CMAKE_CURRENT_BINARY_DIR}/${CTK_PLUGIN_${type}_OUTPUT_DIRECTORY}${output_dir_suffix}")
  254. endif()
  255. if(MY_TEST_PLUGIN)
  256. # Test plug-ins will always be put in a separate directory
  257. if(CMAKE_${type}_OUTPUT_DIRECTORY)
  258. set(plugin_${type}_output_dir "${CMAKE_${type}_OUTPUT_DIRECTORY}/test_plugins")
  259. else()
  260. set(plugin_${type}_output_dir "${PROJECT_BINARY_DIR}/test_plugins")
  261. endif()
  262. endif()
  263. endforeach()
  264. set(plugin_compile_flags "-DQT_PLUGIN")
  265. ctkFunctionGetCompilerVisibilityFlags(plugin_compile_flags)
  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. if(NOT MY_TEST_PLUGIN)
  275. # Install rules
  276. install(TARGETS ${lib_name} EXPORT CTKExports
  277. RUNTIME DESTINATION ${CTK_INSTALL_PLUGIN_DIR} COMPONENT RuntimePlugins
  278. LIBRARY DESTINATION ${CTK_INSTALL_PLUGIN_DIR} COMPONENT RuntimePlugins
  279. ARCHIVE DESTINATION ${CTK_INSTALL_PLUGIN_DIR} COMPONENT Development)
  280. endif()
  281. set(my_libs
  282. ${MY_TARGET_LIBRARIES}
  283. )
  284. if(MINGW)
  285. list(APPEND my_libs ssp) # add stack smash protection lib
  286. endif()
  287. target_link_libraries(${lib_name} ${my_libs})
  288. if(NOT MY_TEST_PLUGIN)
  289. set(${CMAKE_PROJECT_NAME}_PLUGIN_LIBRARIES ${${CMAKE_PROJECT_NAME}_PLUGIN_LIBRARIES} ${lib_name} CACHE INTERNAL "CTK plugins" FORCE)
  290. endif()
  291. if(NOT MY_TEST_PLUGIN)
  292. # Install headers
  293. file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.tpp")
  294. install(FILES
  295. ${headers}
  296. ${dynamicHeaders}
  297. DESTINATION ${CTK_INSTALL_PLUGIN_INCLUDE_DIR}/${Plugin-SymbolicName} COMPONENT Development
  298. )
  299. endif()
  300. endmacro()