Deployqt.cmake 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Deployqt
  2. # --------
  3. #
  4. # Define a function which can find the Qt dependent library and copy them to the bin directory
  5. #
  6. # Usage
  7. # ^^^^^^^^^^^^^^^^
  8. #
  9. # deployqt(target dir)
  10. #
  11. #-----------------------------------------------------------------------------
  12. # 获取windeployqt程序路径WINDEPLOYQT_EXECUTABLE
  13. #-----------------------------------------------------------------------------
  14. if(NOT WINDEPLOYQT_EXECUTABLE)
  15. get_target_property(_qmake_executable Qt5::qmake IMPORTED_LOCATION)
  16. get_filename_component(_qt_bin_dir "${_qmake_executable}" DIRECTORY)
  17. find_program(WINDEPLOYQT_EXECUTABLE windeployqt HINTS "${_qt_bin_dir}")
  18. endif()
  19. #-----------------------------------------------------------------------------
  20. # 函数定义deployqt(target dir)
  21. #-----------------------------------------------------------------------------
  22. function(deployqt target directory)
  23. # 在构建完执行命令,拷贝文件到构建目录
  24. add_custom_command(TARGET ${target} POST_BUILD
  25. COMMAND "${CMAKE_COMMAND}" -E
  26. env PATH="${_qt_bin_dir}" "${WINDEPLOYQT_EXECUTABLE}"
  27. --verbose 0
  28. --no-compiler-runtime
  29. --release
  30. \"$<TARGET_FILE:${target}>\"
  31. )
  32. # 生成路径保存到文件
  33. file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${target}_$<CONFIG>_path"
  34. CONTENT "$<TARGET_FILE:${target}>"
  35. )
  36. install(CODE
  37. "
  38. file(READ \"${CMAKE_CURRENT_BINARY_DIR}/${target}_${CMAKE_BUILD_TYPE}_path\" _file)
  39. execute_process(
  40. COMMAND \"${CMAKE_COMMAND}\" -E
  41. env PATH=\"${_qt_bin_dir}\" \"${WINDEPLOYQT_EXECUTABLE}\"
  42. --dry-run
  43. --no-compiler-runtime
  44. --release
  45. --list mapping
  46. \${_file}
  47. OUTPUT_VARIABLE _output
  48. OUTPUT_STRIP_TRAILING_WHITESPACE
  49. )
  50. separate_arguments(_files WINDOWS_COMMAND \${_output})
  51. while(_files)
  52. list(GET _files 0 _src)
  53. list(GET _files 1 _dest)
  54. execute_process(
  55. COMMAND \"${CMAKE_COMMAND}\" -E
  56. copy \${_src} \"\${CMAKE_INSTALL_PREFIX}/${directory}/\${_dest}\"
  57. )
  58. list(REMOVE_AT _files 0 1)
  59. endwhile()
  60. "
  61. )
  62. # windeployqt doesn't work correctly with the system runtime libraries,
  63. # so we fall back to one of CMake's own modules for copying them over
  64. set(CMAKE_INSTALL_UCRT_LIBRARIES TRUE)
  65. include(InstallRequiredSystemLibraries)
  66. foreach(lib ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS})
  67. get_filename_component(filename "${lib}" NAME)
  68. add_custom_command(TARGET ${target} POST_BUILD
  69. COMMAND "${CMAKE_COMMAND}" -E
  70. copy_if_different "${lib}" \"$<TARGET_FILE_DIR:${target}>\"
  71. )
  72. endforeach()
  73. endfunction()
  74. mark_as_advanced(WINDEPLOYQT_EXECUTABLE)