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