DeployQt5.cmake 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. # - Functions to help assemble a standalone Qt5 executable.
  2. # A collection of CMake utility functions useful for deploying
  3. # Qt5 executables.
  4. #
  5. # The following functions are provided by this module:
  6. # write_qt5_conf
  7. # resolve_qt5_paths
  8. # fixup_qt5_executable
  9. # install_qt5_plugin_path
  10. # install_qt5_plugin
  11. # install_qt5_executable
  12. # Requires CMake 2.6 or greater because it uses function and
  13. # PARENT_SCOPE. Also depends on BundleUtilities.cmake.
  14. #
  15. # WRITE_QT5_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_QT5_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_QT5_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. # Qt5 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_QT5_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_QT5_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_QT5_PLUGIN_PATH.
  58. #
  59. # INSTALL_QT5_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 Qt5 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_QT5_BUNDLE.
  66. #=============================================================================
  67. # Copyright 2011 Mike McQuaid <m...@mikemcquaid.com>
  68. # Copyright 2013 Mihai Moldovan <io...@ionic.de>
  69. # CMake - Cross Platform Makefile Generator
  70. # Copyright 2000-2011 Kitware, Inc., Insight Software Consortium
  71. # All rights reserved.
  72. #
  73. # Redistribution and use in source and binary forms, with or without
  74. # modification, are permitted provided that the following conditions
  75. # are met:
  76. #
  77. # * Redistributions of source code must retain the above copyright
  78. # notice, this list of conditions and the following disclaimer.
  79. #
  80. # * Redistributions in binary form must reproduce the above copyright
  81. # notice, this list of conditions and the following disclaimer in the
  82. # documentation and/or other materials provided with the distribution.
  83. #
  84. # * Neither the names of Kitware, Inc., the Insight Software Consortium,
  85. # nor the names of their contributors may be used to endorse or promote
  86. # products derived from this software without specific prior written
  87. # permission.
  88. #
  89. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  90. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  91. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  92. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  93. # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  94. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  95. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  96. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  97. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  98. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  99. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  100. # The functions defined in this file depend on the fixup_bundle function
  101. # (and others) found in BundleUtilities.cmake
  102. include(BundleUtilities)
  103. set(DeployQt5_cmake_dir "${CMAKE_CURRENT_LIST_DIR}")
  104. set(DeployQt5_apple_plugins_dir "PlugIns")
  105. function(write_qt5_conf qt_conf_dir qt_conf_contents)
  106. set(qt_conf_path "${qt_conf_dir}/qt.conf")
  107. message(STATUS "Writing ${qt_conf_path}")
  108. file(WRITE "${qt_conf_path}" "${qt_conf_contents}")
  109. endfunction()
  110. function(resolve_qt5_paths paths_var)
  111. set(executable_path ${ARGV1})
  112. set(paths_resolved)
  113. foreach(path ${${paths_var}})
  114. if(EXISTS "${path}")
  115. list(APPEND paths_resolved "${path}")
  116. else()
  117. if(${executable_path})
  118. list(APPEND paths_resolved
  119. "${executable_path}/${path}")
  120. else()
  121. list(APPEND paths_resolved
  122. "\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${path}")
  123. endif()
  124. endif()
  125. endforeach()
  126. set(${paths_var} ${paths_resolved} PARENT_SCOPE)
  127. endfunction()
  128. function(fixup_qt5_executable executable)
  129. set(qtplugins ${ARGV1})
  130. set(libs ${ARGV2})
  131. set(dirs ${ARGV3})
  132. set(plugins_dir ${ARGV4})
  133. set(request_qt_conf ${ARGV5})
  134. message(STATUS "fixup_qt5_executable")
  135. message(STATUS " executable='${executable}'")
  136. message(STATUS " qtplugins='${qtplugins}'")
  137. message(STATUS " libs='${libs}'")
  138. message(STATUS " dirs='${dirs}'")
  139. message(STATUS " plugins_dir='${plugins_dir}'")
  140. message(STATUS " request_qt_conf='${request_qt_conf}'")
  141. if(QT_LIBRARY_DIR)
  142. list(APPEND dirs "${QT_LIBRARY_DIR}")
  143. endif()
  144. if(QT_BINARY_DIR)
  145. list(APPEND dirs "${QT_BINARY_DIR}")
  146. endif()
  147. if(APPLE)
  148. set(qt_conf_dir "${executable}/Contents/Resources")
  149. set(executable_path "${executable}")
  150. set(write_qt_conf TRUE)
  151. if(NOT plugins_dir)
  152. set(plugins_dir "${DeployQt5_apple_plugins_dir}")
  153. endif()
  154. else()
  155. get_filename_component(executable_path "${executable}" PATH)
  156. if(NOT executable_path)
  157. set(executable_path ".")
  158. endif()
  159. set(qt_conf_dir "${executable_path}")
  160. set(write_qt_conf ${request_qt_conf})
  161. endif()
  162. foreach(plugin ${qtplugins})
  163. set(installed_plugin_path "")
  164. install_qt5_plugin("${plugin}" "${executable}" 1
  165. installed_plugin_path)
  166. list(APPEND libs ${installed_plugin_path})
  167. endforeach()
  168. foreach(lib ${libs})
  169. if(NOT EXISTS "${lib}")
  170. message(FATAL_ERROR "Library does not exist: ${lib}")
  171. endif()
  172. endforeach()
  173. resolve_qt5_paths(libs "${executable_path}")
  174. if(write_qt_conf)
  175. set(qt_conf_contents "[Paths]\nPlugins = ${plugins_dir}")
  176. write_qt5_conf("${qt_conf_dir}" "${qt_conf_contents}")
  177. endif()
  178. fixup_bundle("${executable}" "${libs}" "${dirs}")
  179. endfunction()
  180. function(install_qt5_plugin_path plugin executable copy
  181. installed_plugin_path_var)
  182. set(plugins_dir ${ARGV4})
  183. set(component ${ARGV5})
  184. set(configurations ${ARGV6})
  185. if(EXISTS "${plugin}")
  186. if(APPLE)
  187. if(NOT plugins_dir)
  188. set(plugins_dir
  189. "${DeployQt5_apple_plugins_dir}")
  190. endif()
  191. set(plugins_path
  192. "${executable}/Contents/${plugins_dir}")
  193. else()
  194. get_filename_component(plugins_path "${executable}"
  195. PATH)
  196. if(NOT plugins_path)
  197. set(plugins_path ".")
  198. endif()
  199. if(plugins_dir)
  200. set(plugins_path
  201. "${plugins_path}/${plugins_dir}")
  202. endif()
  203. endif()
  204. set(plugin_group "")
  205. get_filename_component(plugin_path "${plugin}" PATH)
  206. get_filename_component(plugin_parent_path "${plugin_path}" PATH)
  207. get_filename_component(plugin_parent_dir_name
  208. "${plugin_parent_path}" NAME)
  209. get_filename_component(plugin_name "${plugin}" NAME)
  210. string(TOLOWER "${plugin_parent_dir_name}"
  211. plugin_parent_dir_name)
  212. if("${plugin_parent_dir_name}" STREQUAL "plugins")
  213. get_filename_component(plugin_group "${plugin_path}"
  214. NAME)
  215. set(${plugin_group_var} "${plugin_group}")
  216. endif()
  217. set(plugins_path "${plugins_path}/${plugin_group}")
  218. if(${copy})
  219. file(MAKE_DIRECTORY "${plugins_path}")
  220. file(COPY "${plugin}" DESTINATION "${plugins_path}")
  221. else()
  222. if(configurations AND (CMAKE_CONFIGURATION_TYPES OR
  223. CMAKE_BUILD_TYPE))
  224. set(configurations CONFIGURATIONS
  225. ${configurations})
  226. else()
  227. unset(configurations)
  228. endif()
  229. install(FILES "${plugin}" DESTINATION "${plugins_path}"
  230. ${configurations} ${component})
  231. endif()
  232. set(${installed_plugin_path_var}
  233. "${plugins_path}/${plugin_name}" PARENT_SCOPE)
  234. endif()
  235. endfunction()
  236. function(install_qt5_plugin plugin executable copy installed_plugin_path_var)
  237. set(plugins_dir ${ARGV4})
  238. set(component ${ARGV5})
  239. if(EXISTS "${plugin}")
  240. install_qt5_plugin_path("${plugin}" "${executable}" "${copy}"
  241. "${installed_plugin_path_var}" "${plugins_dir}" "${component}")
  242. else()
  243. #string(TOUPPER "QT_${plugin}_PLUGIN" plugin_var)
  244. set(plugin_release)
  245. set(plugin_debug)
  246. set(plugin_tmp_path)
  247. set(plugin_find_path "${Qt5Core_DIR}/../../../plugins/")
  248. get_filename_component(plugin_find_path "${plugin_find_path}"
  249. REALPATH)
  250. set(plugin_find_release_filename "lib${plugin}.dylib")
  251. set(plugin_find_debug_filename "lib${plugin}_debug.dylib")
  252. file(GLOB_RECURSE pluginlist "${plugin_find_path}"
  253. "${plugin_find_path}/*/lib*.dylib")
  254. foreach(found_plugin ${pluginlist})
  255. get_filename_component(curname "${found_plugin}" NAME)
  256. if("${curname}" STREQUAL "${plugin_find_release_filename}")
  257. set(plugin_tmp_release_path "${found_plugin}")
  258. endif()
  259. if("${curname}" STREQUAL "${plugin_find_debug_filename}")
  260. set(plugin_tmp_debug_path "${found_plugin}")
  261. endif()
  262. endforeach()
  263. if((NOT DEFINED plugin_tmp_release_path OR NOT EXISTS
  264. "${plugin_tmp_release_path}") AND (NOT DEFINED plugin_tmp_debug_PATH OR NOT
  265. EXISTS "${plugin_tmp_debug_path}"))
  266. message(WARNING "Qt plugin \"${plugin}\" not recognized
  267. or found.")
  268. endif()
  269. if(EXISTS "${plugin_tmp_release_path}")
  270. set(plugin_release "${plugin_tmp_release_path}")
  271. elseif(EXISTS "${plugin_tmp_debug_path}")
  272. set(plugin_release "${plugin_tmp_debug_path}")
  273. endif()
  274. if(EXISTS "${plugin_tmp_debug_path}")
  275. set(plugin_debug "${plugin_tmp_debug_path}")
  276. elseif(EXISTS "${plugin_tmp_release_path}")
  277. set(plugin_debug "${plugin_tmp_release_path}")
  278. endif()
  279. if(CMAKE_CONFIGURATION_TYPES OR CMAKE_BUILD_TYPE)
  280. install_qt5_plugin_path("${plugin_release}"
  281. "${executable}" "${copy}" "${installed_plugin_path_var}_release"
  282. "${plugins_dir}" "${component}" "Release|RelWithDebInfo|MinSizeRel")
  283. install_qt5_plugin_path("${plugin_debug}"
  284. "${executable}" "${copy}" "${installed_plugin_path_var}_debug" "${plugins_dir}"
  285. "${component}" "Debug")
  286. if(CMAKE_BUILD_TYPE MATCHES "^Debug$")
  287. set(${installed_plugin_path_var}
  288. ${${installed_plugin_path_var}_debug})
  289. else()
  290. set(${installed_plugin_path_var}
  291. ${${installed_plugin_path_var}_release})
  292. endif()
  293. else()
  294. install_qt5_plugin_path("${plugin_release}"
  295. "${executable}" "${copy}" "${installed_plugin_path_var}" "${plugins_dir}"
  296. "${component}")
  297. endif()
  298. endif()
  299. set(${installed_plugin_path_var} ${${installed_plugin_path_var}}
  300. PARENT_SCOPE)
  301. endfunction()
  302. function(install_qt5_executable executable)
  303. set(qtplugins ${ARGV1})
  304. set(libs ${ARGV2})
  305. set(dirs ${ARGV3})
  306. set(plugins_dir ${ARGV4})
  307. set(request_qt_conf ${ARGV5})
  308. set(component ${ARGV6})
  309. if(QT_LIBRARY_DIR)
  310. list(APPEND dirs "${QT_LIBRARY_DIR}")
  311. endif()
  312. if(QT_BINARY_DIR)
  313. list(APPEND dirs "${QT_BINARY_DIR}")
  314. endif()
  315. if(component)
  316. set(component COMPONENT ${component})
  317. else()
  318. unset(component)
  319. endif()
  320. get_filename_component(executable_absolute "${executable}" ABSOLUTE)
  321. if(EXISTS "${QT_QTCORE_LIBRARY_RELEASE}")
  322. gp_file_type("${executable_absolute}"
  323. "${QT_QTCORE_LIBRARY_RELEASE}" qtcore_type)
  324. elseif(EXISTS "${QT_QTCORE_LIBRARY_DEBUG}")
  325. gp_file_type("${executable_absolute}" "${QT_QTCORE_LIBRARY_DEBUG}"
  326. qtcore_type)
  327. endif()
  328. if(qtcore_type STREQUAL "system")
  329. set(qt_plugins_dir "")
  330. endif()
  331. if(QT_IS_STATIC)
  332. message(WARNING "Qt built statically: not installing plugins.")
  333. else()
  334. foreach(plugin ${qtplugins})
  335. message(STATUS "trying to install plugin ${plugin}")
  336. set(installed_plugin_paths "")
  337. install_qt5_plugin("${plugin}" "${executable}" 0
  338. installed_plugin_paths "${plugins_dir}" "${component}")
  339. list(APPEND libs ${installed_plugin_paths})
  340. endforeach()
  341. endif()
  342. resolve_qt5_paths(libs "")
  343. install(CODE
  344. "include(\"${DeployQt5_cmake_dir}/DeployQt5.cmake\")
  345. set(BU_CHMOD_BUNDLE_ITEMS TRUE)
  346. FIXUP_QT5_EXECUTABLE(\"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${executable}\"
  347. \"\" \"${libs}\" \"${dirs}\" \"${plugins_dir}\" \"${request_qt_conf}\")"
  348. ${component}
  349. )
  350. endfunction()