Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / cmake_modules / DeployQt4.cmake
blob83f322c2d21f2d35e81c20e599b0437a7eee1b0e
1 # - Functions to help assemble a standalone Qt4 executable.
2 # A collection of CMake utility functions useful for deploying
3 # Qt4 executables.
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.
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>.
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.
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.
28 # <executable> should point to the executable to be fixed-up.
30 # <qtplugins> should contain a list of the names or paths of any Qt plugins
31 # to be installed.
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.
36 # <dirs> will be passed to BundleUtilities and should contain and directories
37 # to be searched to find library dependencies.
39 # <plugins_dir> allows an custom plugins directory to be used.
41 # <request_qt_conf> will force a qt.conf file to be written even if not needed.
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>.
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.
52 # If <component> is set then anything installed will use this COMPONENT.
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.
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.
67 #=============================================================================
68 # Copyright 2011 Mike McQuaid <mike@mikemcquaid.com>
70 # Distributed under the OSI-approved BSD License (the "License");
71 # see accompanying file Copyright.txt for details.
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.)
80 # The functions defined in this file depend on the fixup_bundle function
81 # (and others) found in BundleUtilities.cmake
83 include(BundleUtilities)
84 set(DeployQt4_cmake_dir "${CMAKE_CURRENT_LIST_DIR}")
86 function(write_qt4_conf qt_conf_dir qt_conf_contents)
87         set(qt_conf_path "${qt_conf_dir}/qt.conf")
88         message(STATUS "Writing ${qt_conf_path}")
89         file(WRITE "${qt_conf_path}" "${qt_conf_contents}")
90 endfunction()
92 function(resolve_qt4_paths paths_var)
93         set(executable_path ${ARGV1})
95         set(paths_resolved)
96         foreach(path ${${paths_var}})
97                 if(EXISTS "${path}")
98                         list(APPEND paths_resolved "${path}")
99                 else()
100                         if(${executable_path})
101                                 list(APPEND paths_resolved "${executable_path}/${path}")
102                         else()
103                                 list(APPEND paths_resolved "\${CMAKE_INSTALL_PREFIX}/${path}")
104                         endif()
105                 endif()
106         endforeach()
107         set(${paths_var} ${paths_resolved} PARENT_SCOPE)
108 endfunction()
110 function(fixup_qt4_executable executable)
111         set(qtplugins ${ARGV1})
112         set(libs ${ARGV2})
113         set(dirs ${ARGV3})
114         set(plugins_dir ${ARGV4})
115         set(request_qt_conf ${ARGV5})
117         message(STATUS "fixup_qt4_executable")
118         message(STATUS "  executable='${executable}'")
119         message(STATUS "  qtplugins='${qtplugins}'")
120         message(STATUS "  libs='${libs}'")
121         message(STATUS "  dirs='${dirs}'")
122         message(STATUS "  plugins_dir='${plugins_dir}'")
123         message(STATUS "  request_qt_conf='${request_qt_conf}'")
125         if(QT_LIBRARY_DIR)
126                 list(APPEND dirs "${QT_LIBRARY_DIR}")
127         endif()
129         if(APPLE)
130                 set(qt_conf_dir "${executable}/Contents/Resources")
131                 set(executable_path "${executable}")
132                 set(write_qt_conf TRUE)
133         else()
134                 get_filename_component(executable_path "${executable}" PATH)
135                 if(NOT executable_path)
136                         set(executable_path ".")
137                 endif()
138                 set(qt_conf_dir "${executable_path}")
139                 set(write_qt_conf ${request_qt_conf})
140         endif()
142         foreach(plugin ${qtplugins})
143                 set(installed_plugin_path "")
144                 install_qt4_plugin("${plugin}" "${plugins_dir}" "${executable}" 1 installed_plugin_path)
145                 list(APPEND libs ${installed_plugin_path})
146         endforeach()
148         foreach(lib ${libs})
149                 if(NOT EXISTS "${lib}")
150                         message(FATAL_ERROR "Library does not exist: ${lib}")
151                 endif()
152         endforeach()
154         resolve_qt4_paths(libs "${executable_path}")
156         if(write_qt_conf)
157                 set(qt_conf_contents "[Paths]\nPlugins = ${plugins_dir}")
158                 write_qt4_conf("${qt_conf_dir}" "${qt_conf_contents}")
159         endif()
161         fixup_bundle("${executable}" "${libs}" "${dirs}")
162 endfunction()
164 function(install_qt4_plugin_path plugin executable copy installed_plugin_path_var)
165         set(plugins_dir ${ARGV4})
166         set(component ${ARGV5})
167         set(configurations ${ARGV6})
168         if(EXISTS "${plugin}")
169                 if(plugins_dir)
170                         set(plugins_dir "${plugins_dir}")
171                 else()
172                         if(APPLE)
173                                 set(plugins_dir "PlugIns")
174                         else()
175                                 set(plugins_dir "plugins")
176                         endif()
177                 endif()
178                 if(APPLE)
179                         set(plugins_path "${executable}/Contents/${plugins_dir}")
180                 else()
181                         get_filename_component(executable_path "${executable}" PATH)
182                         if(NOT executable_path)
183                                 set(executable_path ".")
184                         endif()
185                         set(plugins_path "${executable_path}/${plugins_dir}")
186                 endif()
188                 set(plugin_group "")
190                 get_filename_component(plugin_path "${plugin}" PATH)
191                 get_filename_component(plugin_parent_path "${plugin_path}" PATH)
192                 get_filename_component(plugin_parent_dir_name "${plugin_parent_path}" NAME)
193                 get_filename_component(plugin_name "${plugin}" NAME)
194                 string(TOLOWER "${plugin_parent_dir_name}" plugin_parent_dir_name)
196                 if("${plugin_parent_dir_name}" STREQUAL "plugins")
197                         get_filename_component(plugin_group "${plugin_path}" NAME)
198                         set(${plugin_group_var} "${plugin_group}")
199                 endif()
200                 set(plugins_path "${plugins_path}/${plugin_group}")
202                 if(${copy})
203                         file(MAKE_DIRECTORY "${plugins_path}")
204                         file(COPY "${plugin}" DESTINATION "${plugins_path}")
205                 else()
206                         if(configurations AND (CMAKE_CONFIGURATION_TYPES OR CMAKE_BUILD_TYPE))
207                                 set(configurations CONFIGURATIONS ${configurations})
208                         else()
209                                 unset(configurations)
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()
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_var "${plugin_var}_RELEASE")
229                 set(plugin_debug_var "${plugin_var}_DEBUG")
230                 set(plugin_release "${${plugin_release_var}}")
231                 set(plugin_debug "${${plugin_debug_var}}")
232                 if(DEFINED "${plugin_release_var}" AND DEFINED "${plugin_debug_var}" AND NOT EXISTS "${plugin_release}" AND NOT EXISTS "${plugin_debug}")
233                         message(WARNING "Qt plugin \"${plugin}\" not recognized or found.")
234                 endif()
235                 install_qt4_plugin_path("${plugin_release}" "${executable}" "${copy}" "${installed_plugin_path_var}" "${plugins_dir}" "${component}" "Release|RelWithDebInfo|MinSizeRel")
236                 install_qt4_plugin_path("${plugin_debug}" "${executable}" "${copy}" "${installed_plugin_path_var}" "${plugins_dir}" "${component}" "Debug")
237         endif()
238         set(installed_plugin_path_var "${installed_plugin_path_var}" PARENT_SCOPE)
239 endfunction()
241 function(install_qt4_executable executable)
242         set(qtplugins ${ARGV1})
243         set(libs ${ARGV2})
244         set(dirs ${ARGV3})
245         set(plugins_dir ${ARGV4})
246         set(request_qt_conf ${ARGV5})
247         set(component ${ARGV6})
248         if(QT_LIBRARY_DIR)
249                 list(APPEND dirs "${QT_LIBRARY_DIR}")
250         endif()
251         if(component)
252                 set(component COMPONENT ${component})
253         else()
254                 unset(component)
255         endif()
257         get_filename_component(executable_absolute "${executable}" ABSOLUTE)
258         if(EXISTS "${QT_QTCORE_LIBRARY_RELEASE}")
259             gp_file_type("${executable_absolute}" "${QT_QTCORE_LIBRARY_RELEASE}" qtcore_type)
260         elseif(EXISTS "${QT_QTCORE_LIBRARY_DEBUG}")
261             gp_file_type("${executable_absolute}" "${QT_QTCORE_LIBRARY_DEBUG}" qtcore_type)
262         endif()
263         if(qtcore_type STREQUAL "system")
264                 set(qt_plugins_dir "")
265         endif()
267         if(NOT qtplugins AND QT_LIBRARIES_PLUGINS)
268                 set(qtplugins "${QT_LIBRARIES_PLUGINS}")
269         endif()
271         foreach(plugin ${qtplugins})
272                 set(installed_plugin_paths "")
273                 install_qt4_plugin("${plugin}" "${executable}" 0 installed_plugin_paths "${plugins_dir}" "${component}")
274                 list(APPEND libs ${installed_plugin_paths})
275         endforeach()
277         resolve_qt4_paths(libs)
279         install(CODE
280   "INCLUDE(\"${DeployQt4_cmake_dir}/DeployQt4.cmake\")
281   SET(BU_CHMOD_BUNDLE_ITEMS TRUE)
282   FIXUP_QT4_EXECUTABLE(\"\${CMAKE_INSTALL_PREFIX}/${executable}\" \"\" \"${libs}\" \"${dirs}\" \"${plugins_dir}\" \"${request_qt_conf}\")"
283                 ${component}
284         )
285 endfunction()