DeployQt4.cmake 13 KB

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