DeployQt4.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. # - Functions to help assemble a standalone Qt4 executable.
  2. # A collection of CMake utility functions useful for deploying
  3. # Qt4 executables.
  4. #
  5. # The following functions are provided by this module:
  6. # write_qt4_conf
  7. # resolve_qt4_paths
  8. # fixup_qt4_executable
  9. # install_qt4_plugin_path
  10. # install_qt4_plugin
  11. # install_qt4_executable
  12. # Requires CMake 2.6 or greater because it uses function and
  13. # PARENT_SCOPE. Also depends on BundleUtilities.cmake.
  14. #
  15. # WRITE_QT4_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_QT4_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_QT4_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. # Qt4 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_QT4_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_QT4_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_QT4_PLUGIN_PATH.
  58. #
  59. # INSTALL_QT4_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 Qt4 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_QT4_BUNDLE.
  66. #=============================================================================
  67. # Copyright 2011 Mike McQuaid <[email protected]>
  68. #
  69. # Distributed under the OSI-approved BSD License (the "License");
  70. # see accompanying file Copyright.txt for details.
  71. #
  72. # This software is distributed WITHOUT ANY WARRANTY; without even the
  73. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  74. # See the License for more information.
  75. #=============================================================================
  76. # (To distribute this file outside of CMake, substitute the full
  77. # License text for the above reference.)
  78. # The functions defined in this file depend on the fixup_bundle function
  79. # (and others) found in BundleUtilities.cmake
  80. include(BundleUtilities)
  81. set(DeployQt4_cmake_dir "${CMAKE_CURRENT_LIST_DIR}")
  82. set(DeployQt4_apple_plugins_dir "PlugIns")
  83. function(write_qt4_conf qt_conf_dir qt_conf_contents)
  84. set(qt_conf_path "${qt_conf_dir}/qt.conf")
  85. message(STATUS "Writing ${qt_conf_path}")
  86. file(WRITE "${qt_conf_path}" "${qt_conf_contents}")
  87. endfunction()
  88. function(resolve_qt4_paths paths_var)
  89. set(executable_path ${ARGV1})
  90. set(paths_resolved)
  91. foreach(path ${${paths_var}})
  92. if(EXISTS "${path}")
  93. list(APPEND paths_resolved "${path}")
  94. else()
  95. if(${executable_path})
  96. list(APPEND paths_resolved "${executable_path}/${path}")
  97. else()
  98. list(APPEND paths_resolved "\${CMAKE_INSTALL_PREFIX}/${path}")
  99. endif()
  100. endif()
  101. endforeach()
  102. set(${paths_var} ${paths_resolved} PARENT_SCOPE)
  103. endfunction()
  104. function(fixup_qt4_executable executable)
  105. set(qtplugins ${ARGV1})
  106. set(libs ${ARGV2})
  107. set(dirs ${ARGV3})
  108. set(plugins_dir ${ARGV4})
  109. set(request_qt_conf ${ARGV5})
  110. message(STATUS "fixup_qt4_executable")
  111. message(STATUS " executable='${executable}'")
  112. message(STATUS " qtplugins='${qtplugins}'")
  113. message(STATUS " libs='${libs}'")
  114. message(STATUS " dirs='${dirs}'")
  115. message(STATUS " plugins_dir='${plugins_dir}'")
  116. message(STATUS " request_qt_conf='${request_qt_conf}'")
  117. if(QT_LIBRARY_DIR)
  118. list(APPEND dirs "${QT_LIBRARY_DIR}")
  119. endif()
  120. if(QT_BINARY_DIR)
  121. list(APPEND dirs "${QT_BINARY_DIR}")
  122. endif()
  123. if(APPLE)
  124. set(qt_conf_dir "${executable}/Contents/Resources")
  125. set(executable_path "${executable}")
  126. set(write_qt_conf TRUE)
  127. if(NOT plugins_dir)
  128. set(plugins_dir "${DeployQt4_apple_plugins_dir}")
  129. endif()
  130. else()
  131. get_filename_component(executable_path "${executable}" PATH)
  132. if(NOT executable_path)
  133. set(executable_path ".")
  134. endif()
  135. set(qt_conf_dir "${executable_path}")
  136. set(write_qt_conf ${request_qt_conf})
  137. endif()
  138. foreach(plugin ${qtplugins})
  139. set(installed_plugin_path "")
  140. install_qt4_plugin("${plugin}" "${executable}" 1 installed_plugin_path)
  141. list(APPEND libs ${installed_plugin_path})
  142. endforeach()
  143. foreach(lib ${libs})
  144. if(NOT EXISTS "${lib}")
  145. message(FATAL_ERROR "Library does not exist: ${lib}")
  146. endif()
  147. endforeach()
  148. resolve_qt4_paths(libs "${executable_path}")
  149. if(write_qt_conf)
  150. set(qt_conf_contents "[Paths]\nPlugins = ${plugins_dir}")
  151. write_qt4_conf("${qt_conf_dir}" "${qt_conf_contents}")
  152. endif()
  153. fixup_bundle("${executable}" "${libs}" "${dirs}")
  154. endfunction()
  155. function(install_qt4_plugin_path plugin executable copy installed_plugin_path_var)
  156. set(plugins_dir ${ARGV4})
  157. set(component ${ARGV5})
  158. set(configurations ${ARGV6})
  159. if(EXISTS "${plugin}")
  160. if(APPLE)
  161. if(NOT plugins_dir)
  162. set(plugins_dir "${DeployQt4_apple_plugins_dir}")
  163. endif()
  164. set(plugins_path "${executable}/Contents/${plugins_dir}")
  165. else()
  166. get_filename_component(plugins_path "${executable}" PATH)
  167. if(NOT plugins_path)
  168. set(plugins_path ".")
  169. endif()
  170. if(plugins_dir)
  171. set(plugins_path "${plugins_path}/${plugins_dir}")
  172. endif()
  173. endif()
  174. set(plugin_group "")
  175. get_filename_component(plugin_path "${plugin}" PATH)
  176. get_filename_component(plugin_parent_path "${plugin_path}" PATH)
  177. get_filename_component(plugin_parent_dir_name "${plugin_parent_path}" NAME)
  178. get_filename_component(plugin_name "${plugin}" NAME)
  179. string(TOLOWER "${plugin_parent_dir_name}" plugin_parent_dir_name)
  180. if("${plugin_parent_dir_name}" STREQUAL "plugins")
  181. get_filename_component(plugin_group "${plugin_path}" NAME)
  182. set(${plugin_group_var} "${plugin_group}")
  183. endif()
  184. set(plugins_path "${plugins_path}/${plugin_group}")
  185. if(${copy})
  186. file(MAKE_DIRECTORY "${plugins_path}")
  187. file(COPY "${plugin}" DESTINATION "${plugins_path}")
  188. else()
  189. if(configurations AND (CMAKE_CONFIGURATION_TYPES OR CMAKE_BUILD_TYPE))
  190. set(configurations CONFIGURATIONS ${configurations})
  191. else()
  192. unset(configurations)
  193. endif()
  194. install(FILES "${plugin}" DESTINATION "${plugins_path}" ${configurations} ${component})
  195. endif()
  196. set(${installed_plugin_path_var} "${plugins_path}/${plugin_name}" PARENT_SCOPE)
  197. endif()
  198. endfunction()
  199. function(install_qt4_plugin plugin executable copy installed_plugin_path_var)
  200. set(plugins_dir ${ARGV4})
  201. set(component ${ARGV5})
  202. if(EXISTS "${plugin}")
  203. install_qt4_plugin_path("${plugin}" "${executable}" "${copy}" "${installed_plugin_path_var}" "${plugins_dir}" "${component}")
  204. else()
  205. string(TOUPPER "QT_${plugin}_PLUGIN" plugin_var)
  206. set(plugin_release_var "${plugin_var}_RELEASE")
  207. set(plugin_debug_var "${plugin_var}_DEBUG")
  208. set(plugin_release "${${plugin_release_var}}")
  209. set(plugin_debug "${${plugin_debug_var}}")
  210. if(DEFINED "${plugin_release_var}" AND DEFINED "${plugin_debug_var}" AND NOT EXISTS "${plugin_release}" AND NOT EXISTS "${plugin_debug}")
  211. message(WARNING "Qt plugin \"${plugin}\" not recognized or found.")
  212. endif()
  213. if(NOT EXISTS "${${plugin_debug_var}}")
  214. set(plugin_debug "${plugin_release}")
  215. endif()
  216. if(CMAKE_CONFIGURATION_TYPES OR CMAKE_BUILD_TYPE)
  217. install_qt4_plugin_path("${plugin_release}" "${executable}" "${copy}" "${installed_plugin_path_var}_release" "${plugins_dir}" "${component}" "Release|RelWithDebInfo|MinSizeRel")
  218. install_qt4_plugin_path("${plugin_debug}" "${executable}" "${copy}" "${installed_plugin_path_var}_debug" "${plugins_dir}" "${component}" "Debug")
  219. if(CMAKE_BUILD_TYPE MATCHES "^Debug$")
  220. set(${installed_plugin_path_var} ${${installed_plugin_path_var}_debug})
  221. else()
  222. set(${installed_plugin_path_var} ${${installed_plugin_path_var}_release})
  223. endif()
  224. else()
  225. install_qt4_plugin_path("${plugin_release}" "${executable}" "${copy}" "${installed_plugin_path_var}" "${plugins_dir}" "${component}")
  226. endif()
  227. endif()
  228. set(${installed_plugin_path_var} ${${installed_plugin_path_var}} PARENT_SCOPE)
  229. endfunction()
  230. function(install_qt4_executable executable)
  231. set(qtplugins ${ARGV1})
  232. set(libs ${ARGV2})
  233. set(dirs ${ARGV3})
  234. set(plugins_dir ${ARGV4})
  235. set(request_qt_conf ${ARGV5})
  236. set(component ${ARGV6})
  237. if(QT_LIBRARY_DIR)
  238. list(APPEND dirs "${QT_LIBRARY_DIR}")
  239. endif()
  240. if(QT_BINARY_DIR)
  241. list(APPEND dirs "${QT_BINARY_DIR}")
  242. endif()
  243. if(component)
  244. set(component COMPONENT ${component})
  245. else()
  246. unset(component)
  247. endif()
  248. get_filename_component(executable_absolute "${executable}" ABSOLUTE)
  249. if(EXISTS "${QT_QTCORE_LIBRARY_RELEASE}")
  250. gp_file_type("${executable_absolute}" "${QT_QTCORE_LIBRARY_RELEASE}" qtcore_type)
  251. elseif(EXISTS "${QT_QTCORE_LIBRARY_DEBUG}")
  252. gp_file_type("${executable_absolute}" "${QT_QTCORE_LIBRARY_DEBUG}" qtcore_type)
  253. endif()
  254. if(qtcore_type STREQUAL "system")
  255. set(qt_plugins_dir "")
  256. endif()
  257. if(QT_IS_STATIC)
  258. message(WARNING "Qt built statically: not installing plugins.")
  259. else()
  260. foreach(plugin ${qtplugins})
  261. set(installed_plugin_paths "")
  262. install_qt4_plugin("${plugin}" "${executable}" 0 installed_plugin_paths "${plugins_dir}" "${component}")
  263. list(APPEND libs ${installed_plugin_paths})
  264. endforeach()
  265. endif()
  266. resolve_qt4_paths(libs)
  267. install(CODE
  268. "INCLUDE(\"${DeployQt4_cmake_dir}/DeployQt4.cmake\")
  269. SET(BU_CHMOD_BUNDLE_ITEMS TRUE)
  270. FIXUP_QT4_EXECUTABLE(\"\${CMAKE_INSTALL_PREFIX}/${executable}\" \"\" \"${libs}\" \"${dirs}\" \"${plugins_dir}\" \"${request_qt_conf}\")"
  271. ${component}
  272. )
  273. endfunction()