DeployQt4.cmake 15 KB

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