ctkMacroBuildQtPlugin.cmake 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. # cmake_parse_arguments ( >= CMake 2.8.3)
  23. #
  24. #! \ingroup CMakeAPI
  25. macro(ctkMacroBuildQtPlugin)
  26. cmake_parse_arguments(MY
  27. "" # no options
  28. "NAME;EXPORT_DIRECTIVE;FOLDER;PLUGIN_DIR" # one value args
  29. "SRCS;MOC_SRCS;UI_FORMS;INCLUDE_DIRECTORIES;TARGET_LIBRARIES;RESOURCES" # multi value args
  30. ${ARGN}
  31. )
  32. # Sanity checks
  33. if(NOT DEFINED MY_NAME)
  34. message(FATAL_ERROR "NAME is mandatory")
  35. endif()
  36. if(NOT DEFINED MY_EXPORT_DIRECTIVE)
  37. message(FATAL_ERROR "EXPORT_DIRECTIVE is mandatory")
  38. endif()
  39. if (NOT DEFINED MY_PLUGIN_DIR)
  40. message(FATAL_ERROR "PLUGIN_DIR (e.g. designer, iconengines, imageformats...) is mandatory")
  41. endif()
  42. set(MY_LIBRARY_TYPE "MODULE")
  43. # Define library name
  44. set(lib_name ${MY_NAME})
  45. # --------------------------------------------------------------------------
  46. # Include dirs
  47. set(my_includes
  48. ${QT_QTDESIGNER_INCLUDE_DIR}
  49. ${CMAKE_CURRENT_SOURCE_DIR}
  50. ${CMAKE_CURRENT_BINARY_DIR}
  51. ${MY_INCLUDE_DIRECTORIES}
  52. )
  53. if(CTK_SOURCE_DIR)
  54. # Add the include directories from the library dependencies
  55. ctkFunctionGetIncludeDirs(my_includes ${MY_TARGET_LIBRARIES})
  56. endif()
  57. include_directories(
  58. ${my_includes}
  59. )
  60. set(MY_LIBRARY_EXPORT_DIRECTIVE ${MY_EXPORT_DIRECTIVE})
  61. set(MY_EXPORT_HEADER_PREFIX ${MY_NAME})
  62. string(REGEX REPLACE "^CTK" "ctk" MY_EXPORT_HEADER_PREFIX ${MY_EXPORT_HEADER_PREFIX})
  63. set(MY_LIBNAME ${lib_name})
  64. if(NOT CTK_EXPORT_HEADER_TEMPLATE)
  65. message(FATAL_ERROR "CTK_EXPORT_HEADER_TEMPLATE is mandatory")
  66. endif()
  67. configure_file(
  68. ${CTK_EXPORT_HEADER_TEMPLATE}
  69. ${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h
  70. )
  71. set(dynamicHeaders
  72. "${dynamicHeaders};${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h")
  73. # Make sure variable are cleared
  74. set(MY_MOC_CPP)
  75. set(MY_UI_CPP)
  76. set(MY_QRC_SRCS)
  77. # Wrap
  78. set(MY_QRC_SRCS "")
  79. if(CTK_QT_VERSION VERSION_GREATER "4")
  80. set(target)
  81. if(Qt5Core_VERSION VERSION_GREATER "5.2.0")
  82. set(target TARGET ${MY_LIBNAME})
  83. endif()
  84. qt5_wrap_cpp(MY_MOC_CPP ${MY_MOC_SRCS} OPTIONS -DHAVE_QT5 ${target})
  85. if(DEFINED MY_RESOURCES)
  86. qt5_add_resources(MY_QRC_SRCS ${MY_RESOURCES})
  87. endif()
  88. else()
  89. QT4_WRAP_CPP(MY_MOC_CPP ${MY_MOC_SRCS})
  90. if(DEFINED MY_RESOURCES)
  91. QT4_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
  92. endif()
  93. endif()
  94. if(CTK_QT_VERSION VERSION_GREATER "4")
  95. if(Qt5Widgets_FOUND)
  96. qt5_wrap_ui(MY_UI_CPP ${MY_UI_FORMS})
  97. elseif(MY_UI_FORMS)
  98. message(WARNING "Argument UI_FORMS ignored because Qt5Widgets module was not specified")
  99. endif()
  100. else()
  101. QT4_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
  102. endif()
  103. source_group("Resources" FILES
  104. ${MY_RESOURCES}
  105. ${MY_UI_FORMS}
  106. )
  107. source_group("Generated" FILES
  108. ${MY_MOC_CPP}
  109. ${MY_QRC_SRCS}
  110. ${MY_UI_CPP}
  111. )
  112. add_library(${lib_name} ${MY_LIBRARY_TYPE}
  113. ${MY_SRCS}
  114. ${MY_MOC_CPP}
  115. ${MY_UI_CPP}
  116. ${MY_QRC_SRCS}
  117. )
  118. # Extract library name associated with the plugin and use it as label
  119. string(REGEX REPLACE "(.*)Plugin[s]?" "\\1" label ${lib_name})
  120. # Apply properties to the library target.
  121. set_target_properties(${lib_name} PROPERTIES
  122. COMPILE_FLAGS "-DQT_PLUGIN"
  123. LIBRARY_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${MY_PLUGIN_DIR}"
  124. LABELS ${label}
  125. )
  126. set(my_libs
  127. ${MY_TARGET_LIBRARIES}
  128. ${QT_QTDESIGNER_LIBRARY}
  129. )
  130. target_link_libraries(${lib_name} ${my_libs})
  131. if(NOT "${MY_FOLDER}" STREQUAL "")
  132. set_target_properties(${lib_name} PROPERTIES FOLDER ${MY_FOLDER})
  133. endif()
  134. # Install the library
  135. # CTK_INSTALL_QTPLUGIN_DIR:STRING can be passed when configuring CTK
  136. # By default, it is the same path as CTK_INSTALL_LIB_DIR
  137. # Plugins are installed in a subdirectory corresponding to their types (e.g. designer, iconengines, imageformats...)
  138. install(TARGETS ${lib_name}
  139. RUNTIME DESTINATION ${CTK_INSTALL_QTPLUGIN_DIR}/${MY_PLUGIN_DIR} COMPONENT RuntimePlugins
  140. LIBRARY DESTINATION ${CTK_INSTALL_QTPLUGIN_DIR}/${MY_PLUGIN_DIR} COMPONENT RuntimePlugins
  141. ARCHIVE DESTINATION ${CTK_INSTALL_QTPLUGIN_DIR}/${MY_PLUGIN_DIR} COMPONENT Development
  142. )
  143. # Install headers - Are headers required ?
  144. #file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
  145. #install(FILES
  146. # ${headers}
  147. # DESTINATION ${CTK_INSTALL_INCLUDE_DIR} COMPONENT Development
  148. # )
  149. # Since Qt expects plugins to be directly located under the
  150. # subdirectory (e.g. 'designer') but not deeper (e.g. designer/Debug), let's copy them.
  151. if(NOT CMAKE_CFG_INTDIR STREQUAL ".")
  152. get_target_property(DIR_PATH ${lib_name} LIBRARY_OUTPUT_DIRECTORY)
  153. add_custom_command(
  154. TARGET ${lib_name}
  155. POST_BUILD
  156. COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${lib_name}> ${DIR_PATH}/../${MY_PLUGIN_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}${lib_name}${CMAKE_BUILD_TYPE}${CMAKE_SHARED_LIBRARY_SUFFIX}
  157. )
  158. endif()
  159. endmacro()
  160. macro(ctkMacroBuildQtDesignerPlugin)
  161. if(CTK_QT_VERSION VERSION_GREATER "4")
  162. find_package(Qt5 COMPONENTS Designer REQUIRED)
  163. add_definitions(${Qt5Designer_DEFINITIONS})
  164. include_directories(${Qt5Designer_INCLUDE_DIRS})
  165. endif()
  166. ctkMacroBuildQtPlugin(
  167. PLUGIN_DIR designer
  168. ${ARGN})
  169. if(CTK_QT_VERSION VERSION_GREATER "4")
  170. cmake_parse_arguments(MY
  171. "" # no options
  172. "NAME;EXPORT_DIRECTIVE;FOLDER;PLUGIN_DIR" # one value args
  173. "SRCS;MOC_SRCS;UI_FORMS;INCLUDE_DIRECTORIES;TARGET_LIBRARIES;RESOURCES" # multi value args
  174. ${ARGN}
  175. )
  176. target_link_libraries(${MY_NAME} Qt5::Designer)
  177. endif()
  178. endmacro()
  179. macro(ctkMacroBuildQtIconEnginesPlugin)
  180. ctkMacroBuildQtPlugin(
  181. PLUGIN_DIR iconengines
  182. ${ARGN})
  183. endmacro()
  184. macro(ctkMacroBuildQtStylesPlugin)
  185. ctkMacroBuildQtPlugin(
  186. PLUGIN_DIR styles
  187. ${ARGN})
  188. endmacro()