Deployqt.cmake 2.3 KB

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