Windeployqt.cmake 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # The MIT License (MIT)
  2. #
  3. # Copyright (c) 2017 Nathan Osman
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. find_package(Qt5Core REQUIRED)
  23. # Retrieve the absolute path to qmake and then use that path to find
  24. # the windeployqt binary
  25. get_target_property(_qmake_executable Qt5::qmake IMPORTED_LOCATION)
  26. get_filename_component(_qt_bin_dir "${_qmake_executable}" DIRECTORY)
  27. find_program(WINDEPLOYQT_EXECUTABLE windeployqt HINTS "${_qt_bin_dir}")
  28. # Running this with MSVC 2015 requires CMake 3.6+
  29. if((MSVC_VERSION VERSION_EQUAL 1900 OR MSVC_VERSION VERSION_GREATER 1900)
  30. AND CMAKE_VERSION VERSION_LESS "3.6")
  31. message(WARNING "Deploying with MSVC 2015+ requires CMake 3.6+")
  32. endif()
  33. # Add commands that copy the Qt runtime to the target's output directory after
  34. # build and install the Qt runtime to the specified directory
  35. function(windeployqt target directory)
  36. # Run windeployqt immediately after build
  37. add_custom_command(TARGET ${target} POST_BUILD
  38. COMMAND "${CMAKE_COMMAND}" -E
  39. env PATH="${_qt_bin_dir}" "${WINDEPLOYQT_EXECUTABLE}"
  40. --verbose 0
  41. --no-compiler-runtime
  42. --no-angle
  43. --no-opengl-sw
  44. \"$<TARGET_FILE:${target}>\"
  45. )
  46. # install(CODE ...) doesn't support generator expressions, but
  47. # file(GENERATE ...) does - store the path in a file
  48. file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${target}_path"
  49. CONTENT "$<TARGET_FILE:${target}>"
  50. )
  51. # Before installation, run a series of commands that copy each of the Qt
  52. # runtime files to the appropriate directory for installation
  53. install(CODE
  54. "
  55. file(READ \"${CMAKE_CURRENT_BINARY_DIR}/${target}_path\" _file)
  56. execute_process(
  57. COMMAND \"${CMAKE_COMMAND}\" -E
  58. env PATH=\"${_qt_bin_dir}\" \"${WINDEPLOYQT_EXECUTABLE}\"
  59. --dry-run
  60. --no-compiler-runtime
  61. --no-angle
  62. --no-opengl-sw
  63. --list mapping
  64. \${_file}
  65. OUTPUT_VARIABLE _output
  66. OUTPUT_STRIP_TRAILING_WHITESPACE
  67. )
  68. separate_arguments(_files WINDOWS_COMMAND \${_output})
  69. while(_files)
  70. list(GET _files 0 _src)
  71. list(GET _files 1 _dest)
  72. execute_process(
  73. COMMAND \"${CMAKE_COMMAND}\" -E
  74. copy \${_src} \"\${CMAKE_INSTALL_PREFIX}/${directory}/\${_dest}\"
  75. )
  76. list(REMOVE_AT _files 0 1)
  77. endwhile()
  78. "
  79. )
  80. # windeployqt doesn't work correctly with the system runtime libraries,
  81. # so we fall back to one of CMake's own modules for copying them over
  82. set(CMAKE_INSTALL_UCRT_LIBRARIES TRUE)
  83. include(InstallRequiredSystemLibraries)
  84. foreach(lib ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS})
  85. get_filename_component(filename "${lib}" NAME)
  86. add_custom_command(TARGET ${target} POST_BUILD
  87. COMMAND "${CMAKE_COMMAND}" -E
  88. copy_if_different "${lib}" \"$<TARGET_FILE_DIR:${target}>\"
  89. )
  90. endforeach()
  91. endfunction()
  92. mark_as_advanced(WINDEPLOYQT_EXECUTABLE)