qt5查找优化

This commit is contained in:
2026-08-03 10:56:46 +08:00
parent 7edbf0717e
commit 0b3e9b1b38
3 changed files with 100 additions and 0 deletions
+98
View File
@@ -0,0 +1,98 @@
set(Qt5_ROOT "" CACHE PATH "Qt5 install prefix")
set(_qt5_components ${Qt5_FIND_COMPONENTS})
set(_qt5_prefix_candidates)
foreach (_qt5_prefix IN ITEMS
"${Qt5_ROOT}"
"$ENV{Qt5_ROOT}"
"$ENV{QTDIR}"
"${CMAKE_CURRENT_LIST_DIR}/../../Qt/Qt5.14.2/5.14.2/msvc2017_64"
"C:/Qt/Qt5.14.2/5.14.2/msvc2017_64"
"D:/Qt/Qt5.14.2/5.14.2/msvc2017_64"
"C:/Qt/5.14.2/msvc2017_64"
"D:/Qt/5.14.2/msvc2017_64"
)
if (NOT "${_qt5_prefix}" STREQUAL "")
list(APPEND _qt5_prefix_candidates "${_qt5_prefix}")
endif ()
endforeach ()
if (Qt5_DIR AND NOT EXISTS "${Qt5_DIR}/Qt5Config.cmake")
message(STATUS "Qt5_DIR ignored: ${Qt5_DIR}")
unset(Qt5_DIR CACHE)
unset(Qt5_DIR)
endif ()
if (NOT Qt5_DIR)
unset(_qt5_config_dir CACHE)
find_path(_qt5_config_dir
NAMES Qt5Config.cmake
PATHS ${_qt5_prefix_candidates}
PATH_SUFFIXES lib/cmake/Qt5 lib64/cmake/Qt5 cmake/Qt5
NO_DEFAULT_PATH
)
if (NOT _qt5_config_dir)
find_path(_qt5_config_dir
NAMES Qt5Config.cmake
PATH_SUFFIXES lib/cmake/Qt5 lib64/cmake/Qt5 cmake/Qt5
)
endif ()
if (_qt5_config_dir)
set(Qt5_DIR "${_qt5_config_dir}" CACHE PATH "Qt5 cmake config dir" FORCE)
endif ()
endif ()
if (Qt5_DIR)
get_filename_component(Qt5_BaseDIR "${Qt5_DIR}/../../.." ABSOLUTE)
set(Qt5_BaseDIR "${Qt5_BaseDIR}" CACHE PATH "Qt5 base dir" FORCE)
set(Qt5_Plugin_DIR "${Qt5_BaseDIR}/plugins" CACHE PATH "Qt5 plugin dir" FORCE)
endif ()
find_package(Qt5 CONFIG QUIET COMPONENTS ${_qt5_components})
function(_qt5_print_target_property target property)
if (NOT TARGET ${target})
return()
endif ()
get_target_property(_qt5_value ${target} ${property})
if (_qt5_value)
message(STATUS "${target} ${property}=${_qt5_value}")
endif ()
endfunction()
function(_qt5_print_status)
message(STATUS "Qt5_FOUND=${Qt5_FOUND}")
message(STATUS "Qt5_VERSION=${Qt5_VERSION}")
message(STATUS "Qt5_DIR=${Qt5_DIR}")
message(STATUS "Qt5_BaseDIR=${Qt5_BaseDIR}")
message(STATUS "Qt5_Plugin_DIR=${Qt5_Plugin_DIR}")
message(STATUS "Qt5 requested components=${_qt5_components}")
foreach (_qt5_component ${_qt5_components})
message(STATUS "Qt5${_qt5_component}_FOUND=${Qt5${_qt5_component}_FOUND}")
if (TARGET Qt5::${_qt5_component})
message(STATUS "Qt5 target found: Qt5::${_qt5_component}")
_qt5_print_target_property(Qt5::${_qt5_component} IMPORTED_LOCATION_DEBUG)
_qt5_print_target_property(Qt5::${_qt5_component} IMPORTED_LOCATION_RELEASE)
_qt5_print_target_property(Qt5::${_qt5_component} IMPORTED_IMPLIB_DEBUG)
_qt5_print_target_property(Qt5::${_qt5_component} IMPORTED_IMPLIB_RELEASE)
_qt5_print_target_property(Qt5::${_qt5_component} INTERFACE_INCLUDE_DIRECTORIES)
else ()
message(STATUS "Qt5 target missing: Qt5::${_qt5_component}")
endif ()
endforeach ()
endfunction()
function(_qt5_fail message_text)
if (Qt5_FIND_REQUIRED)
message(FATAL_ERROR "${message_text}")
else ()
message(STATUS "${message_text}")
endif ()
endfunction()
#_qt5_print_status()
if (NOT Qt5_FOUND)
set(_qt5_missing_components)
foreach (_qt5_component ${_qt5_components})
if (NOT Qt5${_qt5_component}_FOUND)
list(APPEND _qt5_missing_components ${_qt5_component})
endif ()
endforeach ()
if (_qt5_missing_components)
_qt5_fail("Could NOT find Qt5 components: ${_qt5_missing_components}, Qt5_DIR=${Qt5_DIR}")
return()
endif ()
_qt5_fail("Could NOT find Qt5, Qt5_DIR=${Qt5_DIR}")
return()
endif ()