Skip to content

Instantly share code, notes, and snippets.

@maz-1
Created March 3, 2022 10:29
Show Gist options
  • Save maz-1/199e624c5ae3d3947881727d43437034 to your computer and use it in GitHub Desktop.
Save maz-1/199e624c5ae3d3947881727d43437034 to your computer and use it in GitHub Desktop.
windeployqt.cmake
# find windeployqt
if(Qt5_FOUND AND WIN32 AND TARGET Qt5::qmake AND NOT TARGET Qt5::windeployqt)
get_target_property(_qt5_qmake_location Qt5::qmake IMPORTED_LOCATION)
execute_process(
COMMAND "${_qt5_qmake_location}" -query QT_INSTALL_PREFIX
RESULT_VARIABLE return_code
OUTPUT_VARIABLE qt5_install_prefix
OUTPUT_STRIP_TRAILING_WHITESPACE
)
set(imported_location "${qt5_install_prefix}/bin/windeployqt.exe")
if(NOT EXISTS ${imported_location})
get_filename_component(_qt5_qmake_bin_dir "${_qt5_qmake_location}" DIRECTORY)
set(imported_location "${_qt5_qmake_bin_dir}/windeployqt.exe")
endif()
if(EXISTS ${imported_location})
add_executable(Qt5::windeployqt IMPORTED)
set_target_properties(Qt5::windeployqt PROPERTIES
IMPORTED_LOCATION ${imported_location}
)
endif()
endif()
if(TARGET Qt5::windeployqt)
# execute windeployqt in a tmp directory after build then copy
add_custom_command(TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E remove_directory "${CMAKE_CURRENT_BINARY_DIR}/windeployqt"
COMMAND set PATH=%PATH%$<SEMICOLON>${qt5_install_prefix}/bin
COMMAND Qt5::windeployqt --dir "${CMAKE_CURRENT_BINARY_DIR}/windeployqt" "$<TARGET_FILE_DIR:${PROJECT_NAME}>/$<TARGET_FILE_NAME:${PROJECT_NAME}>" || dir >nul:
#COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_BINARY_DIR}/windeployqt/*" "$<TARGET_FILE_DIR:${PROJECT_NAME}>/"
)
endif()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment