DeployQt4.cmake 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 <mike@mikemcquaid.com>
  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. unset(executable_path)
  125. if(ARGC GREATER 1)
  126. set(executable_path ${ARGV1})
  127. endif()
  128. set(paths_resolved)
  129. foreach(path ${${paths_var}})
  130. if(EXISTS "${path}")
  131. list(APPEND paths_resolved "${path}")
  132. else()
  133. if(${executable_path})
  134. list(APPEND paths_resolved "${executable_path}/${path}")
  135. else()
  136. list(APPEND paths_resolved "\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${path}")
  137. endif()
  138. endif()
  139. endforeach()
  140. set(${paths_var} ${paths_resolved} PARENT_SCOPE)
  141. endfunction()
  142. function(fixup_qt4_executable executable)
  143. unset(qtplugins)
  144. if(ARGC GREATER 1)
  145. set(qtplugins ${ARGV1})
  146. endif()
  147. unset(libs)
  148. if(ARGC GREATER 2)
  149. set(libs ${ARGV2})
  150. endif()
  151. unset(dirs)
  152. if(ARGC GREATER 3)
  153. set(dirs ${ARGV3})
  154. endif()
  155. unset(plugins_dir)
  156. if(ARGC GREATER 4)
  157. set(plugins_dir ${ARGV4})
  158. endif()
  159. unset(request_qt_conf)
  160. if(ARGC GREATER 5)
  161. set(request_qt_conf ${ARGV5})
  162. endif()
  163. message(STATUS "fixup_qt4_executable")
  164. message(STATUS " executable='${executable}'")
  165. message(STATUS " qtplugins='${qtplugins}'")
  166. message(STATUS " libs='${libs}'")
  167. message(STATUS " dirs='${dirs}'")
  168. message(STATUS " plugins_dir='${plugins_dir}'")
  169. message(STATUS " request_qt_conf='${request_qt_conf}'")
  170. if(QT_LIBRARY_DIR)
  171. list(APPEND dirs "${QT_LIBRARY_DIR}")
  172. endif()
  173. if(QT_BINARY_DIR)
  174. list(APPEND dirs "${QT_BINARY_DIR}")
  175. endif()
  176. if(APPLE)
  177. set(qt_conf_dir "${executable}/Contents/Resources")
  178. set(executable_path "${executable}")
  179. set(write_qt_conf TRUE)
  180. if(NOT DEFINED plugins_dir)
  181. set(plugins_dir "${DeployQt4_apple_plugins_dir}")
  182. endif()
  183. else()
  184. get_filename_component(executable_path "${executable}" PATH)
  185. if(NOT executable_path)
  186. set(executable_path ".")
  187. endif()
  188. set(qt_conf_dir "${executable_path}")
  189. set(write_qt_conf ${request_qt_conf})
  190. endif()
  191. foreach(plugin ${qtplugins})
  192. set(installed_plugin_path "")
  193. install_qt4_plugin("${plugin}" "${executable}" 1 installed_plugin_path)
  194. list(APPEND libs ${installed_plugin_path})
  195. endforeach()
  196. foreach(lib ${libs})
  197. if(NOT EXISTS "${lib}")
  198. message(FATAL_ERROR "Library does not exist: ${lib}")
  199. endif()
  200. endforeach()
  201. resolve_qt4_paths(libs "${executable_path}")
  202. if(write_qt_conf)
  203. set(qt_conf_contents "[Paths]\nPlugins = ${plugins_dir}")
  204. write_qt4_conf("${qt_conf_dir}" "${qt_conf_contents}")
  205. endif()
  206. fixup_bundle("${executable}" "${libs}" "${dirs}")
  207. endfunction()
  208. function(install_qt4_plugin_path plugin executable copy installed_plugin_path_var)
  209. unset(plugins_dir)
  210. if(ARGC GREATER 4)
  211. set(plugins_dir ${ARGV4})
  212. endif()
  213. unset(component)
  214. if(ARGC GREATER 5)
  215. set(component ${ARGV5})
  216. endif()
  217. unset(configurations)
  218. if(ARGC GREATER 6)
  219. set(configurations ${ARGV6})
  220. endif()
  221. if(EXISTS "${plugin}")
  222. if(APPLE)
  223. if(NOT plugins_dir)
  224. set(plugins_dir "${DeployQt4_apple_plugins_dir}")
  225. endif()
  226. set(plugins_path "${executable}/Contents/${plugins_dir}")
  227. else()
  228. get_filename_component(plugins_path "${executable}" PATH)
  229. if(NOT plugins_path)
  230. set(plugins_path ".")
  231. endif()
  232. if(plugins_dir)
  233. set(plugins_path "${plugins_path}/${plugins_dir}")
  234. endif()
  235. endif()
  236. set(plugin_group "")
  237. get_filename_component(plugin_path "${plugin}" PATH)
  238. get_filename_component(plugin_parent_path "${plugin_path}" PATH)
  239. get_filename_component(plugin_parent_dir_name "${plugin_parent_path}" NAME)
  240. get_filename_component(plugin_name "${plugin}" NAME)
  241. string(TOLOWER "${plugin_parent_dir_name}" plugin_parent_dir_name)
  242. if("${plugin_parent_dir_name}" STREQUAL "plugins")
  243. get_filename_component(plugin_group "${plugin_path}" NAME)
  244. set(${plugin_group_var} "${plugin_group}")
  245. endif()
  246. set(plugins_path "${plugins_path}/${plugin_group}")
  247. if(${copy})
  248. file(MAKE_DIRECTORY "${plugins_path}")
  249. file(COPY "${plugin}" DESTINATION "${plugins_path}")
  250. else()
  251. if(configurations AND (CMAKE_CONFIGURATION_TYPES OR CMAKE_BUILD_TYPE))
  252. set(configurations CONFIGURATIONS ${configurations})
  253. else()
  254. unset(configurations)
  255. endif()
  256. install(FILES "${plugin}" DESTINATION "${plugins_path}" ${configurations} ${component})
  257. endif()
  258. set(${installed_plugin_path_var} "${plugins_path}/${plugin_name}" PARENT_SCOPE)
  259. endif()
  260. endfunction()
  261. function(install_qt4_plugin plugin executable copy installed_plugin_path_var)
  262. unset(plugins_dir)
  263. if(ARGC GREATER 4)
  264. set(plugins_dir ${ARGV4})
  265. endif()
  266. unset(component)
  267. if(ARGC GREATER 5)
  268. set(component ${ARGV5})
  269. endif()
  270. if(EXISTS "${plugin}")
  271. install_qt4_plugin_path("${plugin}" "${executable}" "${copy}" "${installed_plugin_path_var}" "${plugins_dir}" "${component}")
  272. else()
  273. string(TOUPPER "QT_${plugin}_PLUGIN" plugin_var)
  274. set(plugin_release_var "${plugin_var}_RELEASE")
  275. set(plugin_debug_var "${plugin_var}_DEBUG")
  276. set(plugin_release "${${plugin_release_var}}")
  277. set(plugin_debug "${${plugin_debug_var}}")
  278. if(DEFINED "${plugin_release_var}" AND DEFINED "${plugin_debug_var}" AND NOT EXISTS "${plugin_release}" AND NOT EXISTS "${plugin_debug}")
  279. message(WARNING "Qt plugin \"${plugin}\" not recognized or found.")
  280. endif()
  281. if(NOT EXISTS "${${plugin_debug_var}}")
  282. set(plugin_debug "${plugin_release}")
  283. endif()
  284. if(CMAKE_CONFIGURATION_TYPES OR CMAKE_BUILD_TYPE)
  285. install_qt4_plugin_path("${plugin_release}" "${executable}" "${copy}" "${installed_plugin_path_var}_release" "${plugins_dir}" "${component}" "Release|RelWithDebInfo|MinSizeRel")
  286. install_qt4_plugin_path("${plugin_debug}" "${executable}" "${copy}" "${installed_plugin_path_var}_debug" "${plugins_dir}" "${component}" "Debug")
  287. if(CMAKE_BUILD_TYPE MATCHES "^Debug$")
  288. set(${installed_plugin_path_var} ${${installed_plugin_path_var}_debug})
  289. else()
  290. set(${installed_plugin_path_var} ${${installed_plugin_path_var}_release})
  291. endif()
  292. else()
  293. install_qt4_plugin_path("${plugin_release}" "${executable}" "${copy}" "${installed_plugin_path_var}" "${plugins_dir}" "${component}")
  294. endif()
  295. endif()
  296. set(${installed_plugin_path_var} ${${installed_plugin_path_var}} PARENT_SCOPE)
  297. endfunction()
  298. function(install_qt4_executable executable)
  299. unset(qtplugins)
  300. if(ARGC GREATER 1)
  301. set(qtplugins ${ARGV1})
  302. endif()
  303. unset(libs)
  304. if(ARGC GREATER 2)
  305. set(libs ${ARGV2})
  306. endif()
  307. unset(dirs)
  308. if(ARGC GREATER 3)
  309. set(dirs ${ARGV3})
  310. endif()
  311. unset(plugins_dir)
  312. if(ARGC GREATER 4)
  313. set(plugins_dir ${ARGV4})
  314. endif()
  315. unset(request_qt_conf)
  316. if(ARGC GREATER 5)
  317. set(request_qt_conf ${ARGV5})
  318. endif()
  319. unset(component)
  320. if(ARGC GREATER 6)
  321. set(component ${ARGV6})
  322. endif()
  323. if(QT_LIBRARY_DIR)
  324. list(APPEND dirs "${QT_LIBRARY_DIR}")
  325. endif()
  326. if(QT_BINARY_DIR)
  327. list(APPEND dirs "${QT_BINARY_DIR}")
  328. endif()
  329. if(component)
  330. set(component COMPONENT ${component})
  331. else()
  332. unset(component)
  333. endif()
  334. get_filename_component(executable_absolute "${executable}" ABSOLUTE)
  335. if(EXISTS "${QT_QTCORE_LIBRARY_RELEASE}")
  336. gp_file_type("${executable_absolute}" "${QT_QTCORE_LIBRARY_RELEASE}" qtcore_type)
  337. elseif(EXISTS "${QT_QTCORE_LIBRARY_DEBUG}")
  338. gp_file_type("${executable_absolute}" "${QT_QTCORE_LIBRARY_DEBUG}" qtcore_type)
  339. endif()
  340. if(qtcore_type STREQUAL "system")
  341. set(qt_plugins_dir "")
  342. endif()
  343. if(QT_IS_STATIC)
  344. message(WARNING "Qt built statically: not installing plugins.")
  345. else()
  346. foreach(plugin ${qtplugins})
  347. set(installed_plugin_paths "")
  348. install_qt4_plugin("${plugin}" "${executable}" 0 installed_plugin_paths "${plugins_dir}" "${component}")
  349. list(APPEND libs ${installed_plugin_paths})
  350. endforeach()
  351. endif()
  352. resolve_qt4_paths(libs "")
  353. install(CODE
  354. "include(\"${DeployQt4_cmake_dir}/DeployQt4.cmake\")
  355. set(BU_CHMOD_BUNDLE_ITEMS TRUE)
  356. FIXUP_QT4_EXECUTABLE(\"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${executable}\" \"\" \"${libs}\" \"${dirs}\" \"${plugins_dir}\" \"${request_qt_conf}\")"
  357. ${component}
  358. )
  359. endfunction()