ctkMacroBuildQtPlugin.cmake 6.2 KB

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