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(compile_flags "-DQT_PLUGIN")
140  if(CTK_QT_VERSION VERSION_GREATER "4")
141  set(compile_flags "${compile_flags} -DHAVE_QT5")
142  endif()
143  set_target_properties(${lib_name} PROPERTIES
144  COMPILE_FLAGS "${compile_flags}"
145  LIBRARY_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${MY_PLUGIN_DIR}"
146  LABELS ${label}
147  )
148 
149  set(my_libs
150  ${MY_TARGET_LIBRARIES}
151  ${QT_QTDESIGNER_LIBRARY}
152  )
153  target_link_libraries(${lib_name} ${my_libs})
154 
155  if(NOT "${MY_FOLDER}" STREQUAL "")
156  set_target_properties(${lib_name} PROPERTIES FOLDER ${MY_FOLDER})
157  endif()
158 
159  # Install the library
160  # CTK_INSTALL_QTPLUGIN_DIR:STRING can be passed when configuring CTK
161  # By default, it is the same path as CTK_INSTALL_LIB_DIR
162  # Plugins are installed in a subdirectory corresponding to their types (e.g. designer, iconengines, imageformats...)
163  install(TARGETS ${lib_name}
164  RUNTIME DESTINATION ${CTK_INSTALL_QTPLUGIN_DIR}/${MY_PLUGIN_DIR} COMPONENT RuntimePlugins
165  LIBRARY DESTINATION ${CTK_INSTALL_QTPLUGIN_DIR}/${MY_PLUGIN_DIR} COMPONENT RuntimePlugins
166  ARCHIVE DESTINATION ${CTK_INSTALL_QTPLUGIN_DIR}/${MY_PLUGIN_DIR} COMPONENT Development
167  )
168 
169  # Install headers - Are headers required ?
170  #file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
171  #install(FILES
172  # ${headers}
173  # DESTINATION ${CTK_INSTALL_INCLUDE_DIR} COMPONENT Development
174  # )
175 
176 
177  # Since Qt expects plugins to be directly located under the
178  # subdirectory (e.g. 'designer') but not deeper (e.g. designer/Debug), let's copy them.
179 
180  if(NOT CMAKE_CFG_INTDIR STREQUAL ".")
181  get_target_property(DIR_PATH ${lib_name} LIBRARY_OUTPUT_DIRECTORY)
182 
183  add_custom_command(
184  TARGET ${lib_name}
185  POST_BUILD
186  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}
187  )
188  endif()
189 
190 endmacro()
191 
192 macro(ctkMacroBuildQtDesignerPlugin)
193  if(CTK_QT_VERSION VERSION_GREATER "4")
194  find_package(Qt5 COMPONENTS Designer REQUIRED)
195  add_definitions(${Qt5Designer_DEFINITIONS})
196  include_directories(${Qt5Designer_INCLUDE_DIRS})
197  endif()
198  ctkMacroBuildQtPlugin(
199  PLUGIN_DIR designer
200  ${ARGN})
201  if(CTK_QT_VERSION VERSION_GREATER "4")
202  cmake_parse_arguments(MY
203  "" # no options
204  "NAME;EXPORT_DIRECTIVE;FOLDER;PLUGIN_DIR" # one value args
205  "SRCS;MOC_SRCS;UI_FORMS;INCLUDE_DIRECTORIES;TARGET_LIBRARIES;RESOURCES" # multi value args
206  ${ARGN}
207  )
208  target_link_libraries(${MY_NAME} Qt5::Designer)
209  endif()
210 endmacro()
211 
212 macro(ctkMacroBuildQtIconEnginesPlugin)
213  ctkMacroBuildQtPlugin(
214  PLUGIN_DIR iconengines
215  ${ARGN})
216 endmacro()
217 
218 macro(ctkMacroBuildQtStylesPlugin)
219  ctkMacroBuildQtPlugin(
220  PLUGIN_DIR styles
221  ${ARGN})
222 endmacro()
223