Imported Upstream version 2.8.11.2
[platform/upstream/cmake.git] / Modules / DeployQt4.cmake
1 # - Functions to help assemble a standalone Qt4 executable.
2 # A collection of CMake utility functions useful for deploying
3 # Qt4 executables.
4 #
5 # The following functions are provided by this module:
6 #   write_qt4_conf
7 #   resolve_qt4_paths
8 #   fixup_qt4_executable
9 #   install_qt4_plugin_path
10 #   install_qt4_plugin
11 #   install_qt4_executable
12 # Requires CMake 2.6 or greater because it uses function and
13 # PARENT_SCOPE. Also depends on BundleUtilities.cmake.
14 #
15 #  WRITE_QT4_CONF(<qt_conf_dir> <qt_conf_contents>)
16 # Writes a qt.conf file with the <qt_conf_contents> into <qt_conf_dir>.
17 #
18 #  RESOLVE_QT4_PATHS(<paths_var> [<executable_path>])
19 # Loop through <paths_var> list and if any don't exist resolve them
20 # relative to the <executable_path> (if supplied) or the CMAKE_INSTALL_PREFIX.
21 #
22 #  FIXUP_QT4_EXECUTABLE(<executable> [<qtplugins> <libs> <dirs> <plugins_dir> <request_qt_conf>])
23 # Copies Qt plugins, writes a Qt configuration file (if needed) and fixes up a
24 # Qt4 executable using BundleUtilities so it is standalone and can be
25 # drag-and-drop copied to another machine as long as all of the system
26 # libraries are compatible.
27 #
28 # <executable> should point to the executable to be fixed-up.
29 #
30 # <qtplugins> should contain a list of the names or paths of any Qt plugins
31 # to be installed.
32 #
33 # <libs> will be passed to BundleUtilities and should be a list of any already
34 # installed plugins, libraries or executables to also be fixed-up.
35 #
36 # <dirs> will be passed to BundleUtilities and should contain and directories
37 # to be searched to find library dependencies.
38 #
39 # <plugins_dir> allows an custom plugins directory to be used.
40 #
41 # <request_qt_conf> will force a qt.conf file to be written even if not needed.
42 #
43 #  INSTALL_QT4_PLUGIN_PATH(plugin executable copy installed_plugin_path_var <plugins_dir> <component> <configurations>)
44 # Install (or copy) a resolved <plugin> to the default plugins directory
45 # (or <plugins_dir>) relative to <executable> and store the result in
46 # <installed_plugin_path_var>.
47 #
48 # If <copy> is set to TRUE then the plugins will be copied rather than
49 # installed. This is to allow this module to be used at CMake time rather than
50 # install time.
51 #
52 # If <component> is set then anything installed will use this COMPONENT.
53 #
54 #  INSTALL_QT4_PLUGIN(plugin executable copy installed_plugin_path_var <plugins_dir> <component>)
55 # Install (or copy) an unresolved <plugin> to the default plugins directory
56 # (or <plugins_dir>) relative to <executable> and store the result in
57 # <installed_plugin_path_var>. See documentation of INSTALL_QT4_PLUGIN_PATH.
58 #
59 #  INSTALL_QT4_EXECUTABLE(<executable> [<qtplugins> <libs> <dirs> <plugins_dir> <request_qt_conf> <component>])
60 # Installs Qt plugins, writes a Qt configuration file (if needed) and fixes up
61 # a Qt4 executable using BundleUtilities so it is standalone and can be
62 # drag-and-drop copied to another machine as long as all of the system
63 # libraries are compatible. The executable will be fixed-up at install time.
64 # <component> is the COMPONENT used for bundle fixup and plugin installation.
65 # See documentation of FIXUP_QT4_BUNDLE.
66
67 #=============================================================================
68 # Copyright 2011 Mike McQuaid <mike@mikemcquaid.com>
69 #
70 # Distributed under the OSI-approved BSD License (the "License");
71 # see accompanying file Copyright.txt for details.
72 #
73 # This software is distributed WITHOUT ANY WARRANTY; without even the
74 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
75 # See the License for more information.
76 #=============================================================================
77 # (To distribute this file outside of CMake, substitute the full
78 #  License text for the above reference.)
79
80 # The functions defined in this file depend on the fixup_bundle function
81 # (and others) found in BundleUtilities.cmake
82
83 include("${CMAKE_CURRENT_LIST_DIR}/BundleUtilities.cmake")
84 set(DeployQt4_cmake_dir "${CMAKE_CURRENT_LIST_DIR}")
85 set(DeployQt4_apple_plugins_dir "PlugIns")
86
87 function(write_qt4_conf qt_conf_dir qt_conf_contents)
88         set(qt_conf_path "${qt_conf_dir}/qt.conf")
89         message(STATUS "Writing ${qt_conf_path}")
90         file(WRITE "${qt_conf_path}" "${qt_conf_contents}")
91 endfunction()
92
93 function(resolve_qt4_paths paths_var)
94         set(executable_path ${ARGV1})
95
96         set(paths_resolved)
97         foreach(path ${${paths_var}})
98                 if(EXISTS "${path}")
99                         list(APPEND paths_resolved "${path}")
100                 else()
101                         if(${executable_path})
102                                 list(APPEND paths_resolved "${executable_path}/${path}")
103                         else()
104                                 list(APPEND paths_resolved "\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${path}")
105                         endif()
106                 endif()
107         endforeach()
108         set(${paths_var} ${paths_resolved} PARENT_SCOPE)
109 endfunction()
110
111 function(fixup_qt4_executable executable)
112         set(qtplugins ${ARGV1})
113         set(libs ${ARGV2})
114         set(dirs ${ARGV3})
115         set(plugins_dir ${ARGV4})
116         set(request_qt_conf ${ARGV5})
117
118         message(STATUS "fixup_qt4_executable")
119         message(STATUS "  executable='${executable}'")
120         message(STATUS "  qtplugins='${qtplugins}'")
121         message(STATUS "  libs='${libs}'")
122         message(STATUS "  dirs='${dirs}'")
123         message(STATUS "  plugins_dir='${plugins_dir}'")
124         message(STATUS "  request_qt_conf='${request_qt_conf}'")
125
126         if(QT_LIBRARY_DIR)
127                 list(APPEND dirs "${QT_LIBRARY_DIR}")
128         endif()
129         if(QT_BINARY_DIR)
130                 list(APPEND dirs "${QT_BINARY_DIR}")
131         endif()
132
133         if(APPLE)
134                 set(qt_conf_dir "${executable}/Contents/Resources")
135                 set(executable_path "${executable}")
136                 set(write_qt_conf TRUE)
137                 if(NOT plugins_dir)
138                         set(plugins_dir "${DeployQt4_apple_plugins_dir}")
139                 endif()
140         else()
141                 get_filename_component(executable_path "${executable}" PATH)
142                 if(NOT executable_path)
143                         set(executable_path ".")
144                 endif()
145                 set(qt_conf_dir "${executable_path}")
146                 set(write_qt_conf ${request_qt_conf})
147         endif()
148
149         foreach(plugin ${qtplugins})
150                 set(installed_plugin_path "")
151                 install_qt4_plugin("${plugin}" "${executable}" 1 installed_plugin_path)
152                 list(APPEND libs ${installed_plugin_path})
153         endforeach()
154
155         foreach(lib ${libs})
156                 if(NOT EXISTS "${lib}")
157                         message(FATAL_ERROR "Library does not exist: ${lib}")
158                 endif()
159         endforeach()
160
161         resolve_qt4_paths(libs "${executable_path}")
162
163         if(write_qt_conf)
164                 set(qt_conf_contents "[Paths]\nPlugins = ${plugins_dir}")
165                 write_qt4_conf("${qt_conf_dir}" "${qt_conf_contents}")
166         endif()
167
168         fixup_bundle("${executable}" "${libs}" "${dirs}")
169 endfunction()
170
171 function(install_qt4_plugin_path plugin executable copy installed_plugin_path_var)
172         set(plugins_dir ${ARGV4})
173         set(component ${ARGV5})
174         set(configurations ${ARGV6})
175         if(EXISTS "${plugin}")
176                 if(APPLE)
177                         if(NOT plugins_dir)
178                                 set(plugins_dir "${DeployQt4_apple_plugins_dir}")
179                         endif()
180                         set(plugins_path "${executable}/Contents/${plugins_dir}")
181                 else()
182                         get_filename_component(plugins_path "${executable}" PATH)
183                         if(NOT plugins_path)
184                                 set(plugins_path ".")
185                         endif()
186                         if(plugins_dir)
187                                 set(plugins_path "${plugins_path}/${plugins_dir}")
188                         endif()
189                 endif()
190
191                 set(plugin_group "")
192
193                 get_filename_component(plugin_path "${plugin}" PATH)
194                 get_filename_component(plugin_parent_path "${plugin_path}" PATH)
195                 get_filename_component(plugin_parent_dir_name "${plugin_parent_path}" NAME)
196                 get_filename_component(plugin_name "${plugin}" NAME)
197                 string(TOLOWER "${plugin_parent_dir_name}" plugin_parent_dir_name)
198
199                 if("${plugin_parent_dir_name}" STREQUAL "plugins")
200                         get_filename_component(plugin_group "${plugin_path}" NAME)
201                         set(${plugin_group_var} "${plugin_group}")
202                 endif()
203                 set(plugins_path "${plugins_path}/${plugin_group}")
204
205                 if(${copy})
206                         file(MAKE_DIRECTORY "${plugins_path}")
207                         file(COPY "${plugin}" DESTINATION "${plugins_path}")
208                 else()
209                         if(configurations AND (CMAKE_CONFIGURATION_TYPES OR CMAKE_BUILD_TYPE))
210                                 set(configurations CONFIGURATIONS ${configurations})
211                         else()
212                                 unset(configurations)
213                         endif()
214                         install(FILES "${plugin}" DESTINATION "${plugins_path}" ${configurations} ${component})
215                 endif()
216                 set(${installed_plugin_path_var} "${plugins_path}/${plugin_name}" PARENT_SCOPE)
217         endif()
218 endfunction()
219
220 function(install_qt4_plugin plugin executable copy installed_plugin_path_var)
221         set(plugins_dir ${ARGV4})
222         set(component ${ARGV5})
223         if(EXISTS "${plugin}")
224                 install_qt4_plugin_path("${plugin}" "${executable}" "${copy}" "${installed_plugin_path_var}" "${plugins_dir}" "${component}")
225         else()
226                 string(TOUPPER "QT_${plugin}_PLUGIN" plugin_var)
227                 set(plugin_release_var "${plugin_var}_RELEASE")
228                 set(plugin_debug_var "${plugin_var}_DEBUG")
229                 set(plugin_release "${${plugin_release_var}}")
230                 set(plugin_debug "${${plugin_debug_var}}")
231                 if(DEFINED "${plugin_release_var}" AND DEFINED "${plugin_debug_var}" AND NOT EXISTS "${plugin_release}" AND NOT EXISTS "${plugin_debug}")
232                         message(WARNING "Qt plugin \"${plugin}\" not recognized or found.")
233                 endif()
234                 if(NOT EXISTS "${${plugin_debug_var}}")
235                         set(plugin_debug "${plugin_release}")
236                 endif()
237
238                 if(CMAKE_CONFIGURATION_TYPES OR CMAKE_BUILD_TYPE)
239                         install_qt4_plugin_path("${plugin_release}" "${executable}" "${copy}" "${installed_plugin_path_var}_release" "${plugins_dir}" "${component}" "Release|RelWithDebInfo|MinSizeRel")
240                         install_qt4_plugin_path("${plugin_debug}" "${executable}" "${copy}" "${installed_plugin_path_var}_debug" "${plugins_dir}" "${component}" "Debug")
241
242                         if(CMAKE_BUILD_TYPE MATCHES "^Debug$")
243                                 set(${installed_plugin_path_var} ${${installed_plugin_path_var}_debug})
244                         else()
245                                 set(${installed_plugin_path_var} ${${installed_plugin_path_var}_release})
246                         endif()
247                 else()
248                         install_qt4_plugin_path("${plugin_release}" "${executable}" "${copy}" "${installed_plugin_path_var}" "${plugins_dir}" "${component}")
249                 endif()
250         endif()
251         set(${installed_plugin_path_var} ${${installed_plugin_path_var}} PARENT_SCOPE)
252 endfunction()
253
254 function(install_qt4_executable executable)
255         set(qtplugins ${ARGV1})
256         set(libs ${ARGV2})
257         set(dirs ${ARGV3})
258         set(plugins_dir ${ARGV4})
259         set(request_qt_conf ${ARGV5})
260         set(component ${ARGV6})
261         if(QT_LIBRARY_DIR)
262                 list(APPEND dirs "${QT_LIBRARY_DIR}")
263         endif()
264         if(QT_BINARY_DIR)
265                 list(APPEND dirs "${QT_BINARY_DIR}")
266         endif()
267         if(component)
268                 set(component COMPONENT ${component})
269         else()
270                 unset(component)
271         endif()
272
273         get_filename_component(executable_absolute "${executable}" ABSOLUTE)
274         if(EXISTS "${QT_QTCORE_LIBRARY_RELEASE}")
275             gp_file_type("${executable_absolute}" "${QT_QTCORE_LIBRARY_RELEASE}" qtcore_type)
276         elseif(EXISTS "${QT_QTCORE_LIBRARY_DEBUG}")
277             gp_file_type("${executable_absolute}" "${QT_QTCORE_LIBRARY_DEBUG}" qtcore_type)
278         endif()
279         if(qtcore_type STREQUAL "system")
280                 set(qt_plugins_dir "")
281         endif()
282
283         if(QT_IS_STATIC)
284                 message(WARNING "Qt built statically: not installing plugins.")
285         else()
286                 foreach(plugin ${qtplugins})
287                         set(installed_plugin_paths "")
288                         install_qt4_plugin("${plugin}" "${executable}" 0 installed_plugin_paths "${plugins_dir}" "${component}")
289                         list(APPEND libs ${installed_plugin_paths})
290                 endforeach()
291         endif()
292
293         resolve_qt4_paths(libs "")
294
295         install(CODE
296   "include(\"${DeployQt4_cmake_dir}/DeployQt4.cmake\")
297   set(BU_CHMOD_BUNDLE_ITEMS TRUE)
298   FIXUP_QT4_EXECUTABLE(\"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${executable}\" \"\" \"${libs}\" \"${dirs}\" \"${plugins_dir}\" \"${request_qt_conf}\")"
299                 ${component}
300         )
301 endfunction()