Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 275 lines (247 sloc) 12.073 kB
0618045 @mikemcquaid Add DeployQt4 module.
mikemcquaid authored
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>])
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 # See documentation of FIXUP_QT4_BUNDLE.
65
66 #=============================================================================
67 # Copyright 2011 Mike McQuaid <mike@mikemcquaid.com>
68 #
69 # Distributed under the OSI-approved BSD License (the "License");
70 # see accompanying file Copyright.txt for details.
71 #
72 # This software is distributed WITHOUT ANY WARRANTY; without even the
73 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
74 # See the License for more information.
75 #=============================================================================
76 # (To distribute this file outside of CMake, substitute the full
77 # License text for the above reference.)
78
79 # The functions defined in this file depend on the fixup_bundle function
80 # (and others) found in BundleUtilities.cmake
81
82 include(BundleUtilities)
83 set(DeployQt4_cmake_dir "${CMAKE_CURRENT_LIST_DIR}")
84
85 function(write_qt4_conf qt_conf_dir qt_conf_contents)
86 set(qt_conf_path "${qt_conf_dir}/qt.conf")
87 message(STATUS "Writing ${qt_conf_path}")
88 file(WRITE "${qt_conf_path}" "${qt_conf_contents}")
89 endfunction()
90
91 function(resolve_qt4_paths paths_var)
92 set(executable_path ${ARGV1})
93
94 set(paths_resolved)
95 foreach(path ${${paths_var}})
96 if(EXISTS "${path}")
97 list(APPEND paths_resolved "${path}")
98 else()
99 if(${executable_path})
100 list(APPEND paths_resolved "${executable_path}/${path}")
101 else()
102 list(APPEND paths_resolved "\${CMAKE_INSTALL_PREFIX}/${path}")
103 endif()
104 endif()
105 endforeach()
106 set(${paths_var} ${paths_resolved} PARENT_SCOPE)
107 endfunction()
108
109 function(fixup_qt4_executable executable qtplugins)
110 set(libs ${ARGV2})
111 set(dirs ${ARGV3})
112 set(plugins_dir ${ARGV4})
113 set(request_qt_conf ${ARGV5})
114
115 message(STATUS "fixup_qt4_executable")
116 message(STATUS " executable='${executable}'")
117 message(STATUS " qtplugins='${qtplugins}'")
118 message(STATUS " libs='${libs}'")
119 message(STATUS " dirs='${dirs}'")
120 message(STATUS " plugins_dir='${plugins_dir}'")
121 message(STATUS " request_qt_conf='${request_qt_conf}'")
122
123 if(QT_LIBRARY_DIR)
124 list(APPEND dirs "${QT_LIBRARY_DIR}")
125 endif()
126
127 if(APPLE)
128 set(qt_conf_dir "${executable}/Contents/Resources")
129 set(executable_path "${executable}")
130 set(write_qt_conf TRUE)
131 else()
132 get_filename_component(executable_path "${executable}" PATH)
133 if(NOT executable_path)
134 set(executable_path ".")
135 endif()
136 set(qt_conf_dir "${executable_path}")
137 set(write_qt_conf ${request_qt_conf})
138 endif()
139
140 foreach(plugin ${qtplugins})
141 set(installed_plugin_path "")
142 install_qt4_plugin("${plugin}" "${plugins_dir}" "${executable}" 1 installed_plugin_path)
143 list(APPEND libs ${installed_plugin_path})
144 endforeach()
145
146 foreach(lib ${libs})
147 if(NOT EXISTS "${lib}")
148 message(FATAL_ERROR "Library does not exist: ${lib}")
149 endif()
150 endforeach()
151
152 resolve_qt4_paths(libs "${executable_path}")
153 resolve_qt4_paths(dirs "${executable_path}")
154
155 if(write_qt_conf)
156 set(qt_conf_contents "[Paths]\nPlugins = ${plugins_dir}")
157 write_qt4_conf("${qt_conf_dir}" "${qt_conf_contents}")
158 endif()
159
160 fixup_bundle("${executable}" "${libs}" "${dirs}")
161 endfunction()
162
163 function(install_qt4_plugin_path plugin executable copy installed_plugin_path_var)
164 set(plugins_dir ${ARGV4})
165 set(component ${ARGV5})
166 set(configurations ${ARGV6})
167 if(EXISTS "${plugin}")
168 if(plugins_dir)
169 set(plugins_dir "${plugins_dir}")
170 else()
171 if(APPLE)
172 set(plugins_dir "PlugIns")
173 else()
174 set(plugins_dir "plugins")
175 endif()
176 endif()
177 if(APPLE)
178 set(plugins_path "${executable}/Contents/${plugins_dir}")
179 else()
180 get_filename_component(executable_path "${executable}" PATH)
181 if(NOT executable_path)
182 set(executable_path ".")
183 endif()
184 set(plugins_path "${executable_path}/${plugins_dir}")
185 endif()
186
187 set(plugin_group "")
188
189 get_filename_component(plugin_path "${plugin}" PATH)
190 get_filename_component(plugin_parent_path "${plugin_path}" PATH)
191 get_filename_component(plugin_parent_dir_name "${plugin_parent_path}" NAME)
192 get_filename_component(plugin_name "${plugin}" NAME)
193 string(TOLOWER "${plugin_parent_dir_name}" plugin_parent_dir_name)
194
195 if("${plugin_parent_dir_name}" STREQUAL "plugins")
196 get_filename_component(plugin_group "${plugin_path}" NAME)
197 set(${plugin_group_var} "${plugin_group}")
198 endif()
199 set(plugins_path "${plugins_path}/${plugin_group}")
200
201 if(${copy})
202 file(MAKE_DIRECTORY "${plugins_path}")
203 file(COPY "${plugin}" DESTINATION "${plugins_path}")
204 else()
205 if(configurations AND (CMAKE_CONFIGURATION_TYPES OR CMAKE_BUILD_TYPE))
206 set(configurations CONFIGURATIONS ${configurations})
207 endif()
208 if(component)
209 set(component COMPONENT ${component})
210 endif()
211 install(FILES "${plugin}" DESTINATION "${plugins_path}" ${configurations} ${component})
212 endif()
213 set(${installed_plugin_path_var} ${${installed_path_var}} "${plugins_path}/${plugin_name}" PARENT_SCOPE)
214 endif()
215 endfunction()
216
217 function(install_qt4_plugin plugin executable copy installed_plugin_path_var)
218 set(plugins_dir ${ARGV4})
219 set(component ${ARGV5})
220 if(EXISTS "${plugin}")
221 install_qt4_plugin_path("${plugin}" "${executable}" "${copy}" "${installed_plugin_path_var}" "${plugins_dir}" "${component}")
222 else()
223 if(QT_IS_STATIC)
224 string(TOUPPER "QT_${plugin}_LIBRARY" plugin_var)
225 else()
226 string(TOUPPER "QT_${plugin}_PLUGIN" plugin_var)
227 endif()
228 set(plugin_release "${${plugin_var}_RELEASE}")
229 set(plugin_debug "${${plugin_var}_DEBUG}")
230 if(NOT EXISTS "${plugin_release}" AND NOT EXISTS "${plugin_debug}")
231 message(WARNING "Qt plugin \"${plugin}\" not recognized or found.")
232 endif()
233 install_qt4_plugin_path("${plugin_release}" "${executable}" "${copy}" "${installed_plugin_path_var}" "${plugins_dir}" "${component}" "Release|RelWithDebInfo|MinSizeRel")
234 install_qt4_plugin_path("${plugin_debug}" "${executable}" "${copy}" "${installed_plugin_path_var}" "${plugins_dir}" "${component}" "Debug")
235 endif()
236 set(installed_plugin_path_var "${installed_plugin_path_var}" PARENT_SCOPE)
237 endfunction()
238
239 function(install_qt4_executable executable)
240 set(qtplugins ${ARGV1})
241 set(libs ${ARGV2})
242 set(dirs ${ARGV3})
243 set(plugins_dir ${ARGV4})
244 set(request_qt_conf ${ARGV5})
245 set(plugin_component ${ARGV6})
246 if(QT_LIBRARY_DIR)
247 list(APPEND dirs "${QT_LIBRARY_DIR}")
248 endif()
249
250 get_filename_component(executable_absolute "${executable}" ABSOLUTE)
251 gp_file_type("${executable_absolute}" "${QT_QTCORE_LIBRARY}" qtcore_type)
252 if(qtcore_type STREQUAL "system")
253 set(qt_plugins_dir "")
254 endif()
255
256 if(NOT qtplugins AND QT_LIBRARIES_PLUGINS)
257 set(qtplugins "${QT_LIBRARIES_PLUGINS}")
258 endif()
259
260 foreach(plugin ${qtplugins})
261 set(installed_plugin_paths "")
262 install_qt4_plugin("${plugin}" "${executable}" 0 installed_plugin_paths "${plugins_dir}" "${plugin_component}")
263 list(APPEND libs ${installed_plugin_paths})
264 endforeach()
265
266 resolve_qt4_paths(libs)
267 resolve_qt4_paths(dirs)
268
269 install(CODE
270 " INCLUDE( \"${DeployQt4_cmake_dir}/DeployQt4.cmake\" )
271 SET( BU_CHMOD_BUNDLE_ITEMS TRUE )
272 FIXUP_QT4_EXECUTABLE( \"\${CMAKE_INSTALL_PREFIX}/${executable}\" \"\" \"${libs}\" \"${dirs}\" \"${plugins_dir}\" \"${request_qt_conf}\" ) "
273 )
274 endfunction()
Something went wrong with that request. Please try again.