浏览代码

cmake: Refactor find_qt macro

If QT_VERSION is not defined, it gets define with the AUTO value. And
its definition is moved to the helper file.

find_qt now:
- Check quietly for Qt5 and Qt6
- If QT_VERSION is set to AUTO. It checks firstly if Qt5 was found it
  will use it. If not it do the same for Qt6
- If QT_VERSION is set to 5 or 6, it checks if the choice was found and
  use it. And if not, it falls back to the other if found.
- If neither Qt5 or Qt6 are found, a fatal error is emitted.
- The macro saved the _QT_VERSION in the cache to replace QT_VERSION,
  so the process is not repeated each time that find_qt is used.
- When Qt::Gui is in the Linux component list, Qt::GuiPrivate is added.
  So using the versioned one is no longer required.
tytan652 2 年之前
父节点
当前提交
ed5f8bec60
共有 3 个文件被更改,包括 66 次插入12 次删除
  1. 0 1
      .cmake-format.json
  2. 1 1
      CMakeLists.txt
  3. 65 10
      cmake/ObsPluginHelpers.cmake

+ 0 - 1
.cmake-format.json

@@ -3,7 +3,6 @@
       "find_qt": {
         "flags": [],
         "kwargs": {
-          "VERSION": "+",
           "COMPONENTS": "+",
           "COMPONENTS_WIN": "+",
           "COMPONENTS_MACOS": "+",

+ 1 - 1
CMakeLists.txt

@@ -28,7 +28,7 @@ target_sources(${CMAKE_PROJECT_NAME} PRIVATE src/plugin-main.c)
 find_package(libobs REQUIRED)
 find_package(obs-frontend-api REQUIRED)
 include(cmake/ObsPluginHelpers.cmake)
-find_qt(VERSION ${QT_VERSION} COMPONENTS Widgets Core)
+find_qt(COMPONENTS Widgets Core)
 
 configure_file(src/plugin-macros.h.in
                ${CMAKE_SOURCE_DIR}/src/plugin-macros.generated.h)

+ 65 - 10
cmake/ObsPluginHelpers.cmake

@@ -54,43 +54,98 @@ endif()
 
 if(NOT QT_VERSION)
   set(QT_VERSION
-      "5"
-      CACHE STRING "OBS Qt version [5, 6]" FORCE)
-  set_property(CACHE QT_VERSION PROPERTY STRINGS 5 6)
+      AUTO
+      CACHE STRING "OBS Qt version [AUTO, 5, 6]" FORCE)
+  set_property(CACHE QT_VERSION PROPERTY STRINGS AUTO 5 6)
 endif()
 
 macro(find_qt)
-  set(oneValueArgs VERSION)
   set(multiValueArgs COMPONENTS COMPONENTS_WIN COMPONENTS_MAC COMPONENTS_LINUX)
   cmake_parse_arguments(FIND_QT "" "${oneValueArgs}" "${multiValueArgs}"
                         ${ARGN})
+  set(QT_NO_CREATE_VERSIONLESS_TARGETS ON)
+  find_package(
+    Qt5
+    COMPONENTS Core
+    QUIET)
+  find_package(
+    Qt6
+    COMPONENTS Core
+    QUIET)
+
+  if(NOT _QT_VERSION AND QT_VERSION STREQUAL AUTO)
+    if(TARGET Qt6::Core)
+      set(_QT_VERSION
+          6
+          CACHE INTERNAL "")
+    elseif(TARGET Qt5::Core)
+      set(_QT_VERSION
+          5
+          CACHE INTERNAL "")
+    endif()
+    message(STATUS "Qt version: ${_QT_VERSION}")
+  elseif(NOT _QT_VERSION)
+    if(TARGET Qt${QT_VERSION}::Core)
+      set(_QT_VERSION
+          ${QT_VERSION}
+          CACHE INTERNAL "")
+    else()
+      if(QT_VERSION EQUAL 6)
+        set(FALLBACK_QT_VERSION 5)
+      else()
+        set(FALLBACK_QT_VERSION 6)
+      endif()
+      message(
+        WARNING
+          "Qt${QT_VERSION} was not found, falling back to Qt${FALLBACK_QT_VERSION}"
+      )
+
+      if(TARGET Qt${FALLBACK_QT_VERSION}::Core)
+        set(_QT_VERSION
+            ${FALLBACK_QT_VERSION}
+            CACHE INTERNAL "")
+      endif()
+    endif()
+    message(STATUS "Qt version: ${_QT_VERSION}")
+  endif()
+
+  set(QT_NO_CREATE_VERSIONLESS_TARGETS OFF)
+
+  if(NOT _QT_VERSION)
+    message(FATAL_ERROR "Neither Qt5 or Qt6 were found")
+  endif()
 
   if(OS_WINDOWS)
     find_package(
-      Qt${FIND_QT_VERSION}
+      Qt${_QT_VERSION}
       COMPONENTS ${FIND_QT_COMPONENTS} ${FIND_QT_COMPONENTS_WIN}
       REQUIRED)
   elseif(OS_MACOS)
     find_package(
-      Qt${FIND_QT_VERSION}
+      Qt${_QT_VERSION}
       COMPONENTS ${FIND_QT_COMPONENTS} ${FIND_QT_COMPONENTS_MAC}
       REQUIRED)
   else()
     find_package(
-      Qt${FIND_QT_VERSION}
+      Qt${_QT_VERSION}
       COMPONENTS ${FIND_QT_COMPONENTS} ${FIND_QT_COMPONENTS_LINUX}
       REQUIRED)
   endif()
 
+  list(APPEND FIND_QT_COMPONENTS "Core")
+
+  if("Gui" IN_LIST FIND_QT_COMPONENTS_LINUX)
+    list(APPEND FIND_QT_COMPONENTS_LINUX "GuiPrivate")
+  endif()
+
   foreach(_COMPONENT IN LISTS FIND_QT_COMPONENTS FIND_QT_COMPONENTS_WIN
                               FIND_QT_COMPONENTS_MAC FIND_QT_COMPONENTS_LINUX)
-    if(NOT TARGET Qt::${_COMPONENT} AND TARGET
-                                        Qt${FIND_QT_VERSION}::${_COMPONENT})
+    if(NOT TARGET Qt::${_COMPONENT} AND TARGET Qt${_QT_VERSION}::${_COMPONENT})
 
       add_library(Qt::${_COMPONENT} INTERFACE IMPORTED)
       set_target_properties(
         Qt::${_COMPONENT} PROPERTIES INTERFACE_LINK_LIBRARIES
-                                     "Qt${FIND_QT_VERSION}::${_COMPONENT}")
+                                     "Qt${_QT_VERSION}::${_COMPONENT}")
     endif()
   endforeach()
 endmacro()