DeployQt4.cmake 13 KB

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