Author: Aurélien COUDERC <coucouf@debian.org>
Forwarded: not-needed
Last-update: 2024-07-23
Description: Make Wayland optional
 .
 This allows to build libkscreen on architectures without qt6-wayland and wayland support

---
 CMakeLists.txt             |   21 ++++++++++++---------
 b/autotests/CMakeLists.txt |   62 +++++++++++++++++++++++++++++++++++++-------------------------
 backends/CMakeLists.txt    |    4 +++-
 tests/CMakeLists.txt       |    4 +++-
 4 files changed, 55 insertions(+), 36 deletions(-)

--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -29,21 +29,26 @@
 include(KDEGitCommitHooks)
 include(ECMDeprecationSettings)
 
-find_package(Qt6 ${QT_MIN_VERSION} CONFIG REQUIRED Core DBus Gui Test WaylandClient)
+find_package(Qt6 ${QT_MIN_VERSION} CONFIG REQUIRED Core DBus Gui Test)
 
 if (Qt6Gui_VERSION VERSION_GREATER_EQUAL "6.10.0")
     find_package(Qt6GuiPrivate ${REQUIRED_QT_VERSION} REQUIRED NO_MODULE)
 endif()
 
-find_package(WaylandScanner)
-
-find_package(PlasmaWaylandProtocols 1.19 CONFIG)
-set_package_properties(PlasmaWaylandProtocols PROPERTIES TYPE REQUIRED)
-
-find_package(Wayland 1.15 COMPONENTS Client)
-set_package_properties(Wayland PROPERTIES
-                       TYPE REQUIRED
-                      )
+find_package(Qt6WaylandClient ${QT_MIN_VERSION} CONFIG)
+set(HAVE_WAYLAND ${Qt6WaylandClient_FOUND})
+message(STATUS "HAVE_WAYLAND: ${HAVE_WAYLAND}")
+if(${HAVE_WAYLAND})
+    find_package(WaylandScanner)
+
+    find_package(PlasmaWaylandProtocols 1.19 CONFIG)
+    set_package_properties(PlasmaWaylandProtocols PROPERTIES TYPE REQUIRED)
+
+    find_package(Wayland 1.15 COMPONENTS Client)
+    set_package_properties(Wayland PROPERTIES
+                           TYPE REQUIRED
+                          )
+endif()
 
 # xrandr backend
 
--- a/backends/CMakeLists.txt
+++ b/backends/CMakeLists.txt
@@ -1,5 +1,7 @@
 add_subdirectory(fake)
-add_subdirectory(kwayland)
+if(${HAVE_WAYLAND})
+    add_subdirectory(kwayland)
+endif()
 
 if(${XCB_RANDR_FOUND})
     message(STATUS "Will build xrandr backend.")
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -1,4 +1,6 @@
 add_executable(printconfig testplugandplay.cpp testpnp.cpp testpnp.h)
 target_link_libraries(printconfig Qt::Gui KF6::Screen)
 
-add_subdirectory(kwayland)
+if(${HAVE_WAYLAND})
+    add_subdirectory(kwayland)
+endif()
--- a/autotests/CMakeLists.txt
+++ b/autotests/CMakeLists.txt
@@ -1,13 +1,23 @@
 add_definitions(-DTEST_DATA="${CMAKE_CURRENT_SOURCE_DIR}/configs/")
 
-include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR}/tests/kwayland/)
+include_directories(${CMAKE_CURRENT_BINARY_DIR})
+if(${HAVE_WAYLAND)
+    include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR}/tests/kwayland/)
+endif()
 
 macro(KSCREEN_ADD_TEST)
     foreach(_testname ${ARGN})
-        set(test_SRCS ${_testname}.cpp ${KSCREEN_WAYLAND_SRCS})
+        set(test_SRCS ${_testname}.cpp)
+        if(${HAVE_WAYLAND)
+            set(test_SRCS ${test_SRCS} ${KSCREEN_WAYLAND_SRCS})
+        endif()
         qt_add_dbus_interface(test_SRCS ${CMAKE_SOURCE_DIR}/interfaces/org.kde.KScreen.FakeBackend.xml fakebackendinterface)
         add_executable(${_testname} ${test_SRCS})
-        target_link_libraries(${_testname} Qt::Core Qt::Gui Qt::Test Qt::DBus KF6::Screen ${KSCREEN_WAYLAND_LIBS})
+        if(${HAVE_WAYLAND)
+            target_link_libraries(${_testname} Qt::Core Qt::Gui Qt::Test Qt::DBus KF6::Screen ${KSCREEN_WAYLAND_LIBS})
+        else()
+            target_link_libraries(${_testname} Qt::Core Qt::Gui Qt::Test Qt::DBus KF6::Screen)
+        endif()
         add_test(NAME kscreen-${_testname}
                  COMMAND dbus-launch $<TARGET_FILE:${_testname}>
         )
@@ -25,28 +35,30 @@
 kscreen_add_test(testmodelistchange)
 kscreen_add_test(testedid)
 
-# FIXME KWaylandServer is not a thing in KF6
-if (NOT TARGET KF6::WaylandServer)
-    message(WARNING "Skipping KF6::WaylandServer based unit tests!")
-    return()
-endif()
-
-set(KSCREEN_WAYLAND_LIBS
-    KF6::WaylandServer KF6::WaylandClient
-)
-
-# For WaylandConfigReader and TestServer
-set(KSCREEN_WAYLAND_SRCS
-    ${CMAKE_SOURCE_DIR}/tests/kwayland/waylandconfigreader.cpp
-    ${CMAKE_SOURCE_DIR}/tests/kwayland/waylandtestserver.cpp
-)
-include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../backends/kwayland)
+if(${HAVE_WAYLAND)
+    # FIXME KWaylandServer is not a thing in KF6
+    if (NOT TARGET KF6::WaylandServer)
+        message(WARNING "Skipping KF6::WaylandServer based unit tests!")
+        return()
+    endif()
+
+    set(KSCREEN_WAYLAND_LIBS
+        KF6::WaylandServer KF6::WaylandClient
+    )
+
+    # For WaylandConfigReader and TestServer
+    set(KSCREEN_WAYLAND_SRCS
+        ${CMAKE_SOURCE_DIR}/tests/kwayland/waylandconfigreader.cpp
+        ${CMAKE_SOURCE_DIR}/tests/kwayland/waylandtestserver.cpp
+    )
+    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../backends/kwayland)
 
-kscreen_add_test(testkwaylandbackend)
-kscreen_add_test(testkwaylandconfig)
+    kscreen_add_test(testkwaylandbackend)
+    kscreen_add_test(testkwaylandconfig)
 
-set(KSCREEN_WAYLAND_LIBS "")
-set(KSCREEN_WAYLAND_SRCS "")
+    set(KSCREEN_WAYLAND_LIBS "")
+    set(KSCREEN_WAYLAND_SRCS "")
+endif()
 
 
 if (ENABLE_XRANDR_TESTS)
--- a/src/libdpms/CMakeLists.txt
+++ b/src/libdpms/CMakeLists.txt
@@ -1,21 +1,31 @@
 # SPDX-License-Identifier: BSD-3-Clause
 # SPDX-FileCopyrightText: 2022 Aleix Pol Gonzalez <aleixpol@kde.org>
 
+configure_file(dpms-config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/dpms-config.h)
+
 add_library(KF6ScreenDpms SHARED)
-target_sources(KF6ScreenDpms PRIVATE dpms.cpp dpms.h abstractdpmshelper.cpp waylanddpmshelper.cpp xcbdpmshelper.cpp waylanddpmshelper.cpp)
-target_link_libraries(KF6ScreenDpms PUBLIC Qt::Gui
-                                    PRIVATE XCB::XCB XCB::DPMS XCB::RANDR
-                                            Qt::GuiPrivate Qt::WaylandClient Wayland::Client
-)
+if(${HAVE_WAYLAND})
+    target_sources(KF6ScreenDpms PRIVATE dpms.cpp dpms.h abstractdpmshelper.cpp waylanddpmshelper.cpp xcbdpmshelper.cpp waylanddpmshelper.cpp)
+    target_link_libraries(KF6ScreenDpms PUBLIC Qt::Gui
+                                        PRIVATE XCB::XCB XCB::DPMS XCB::RANDR
+                                                Qt::GuiPrivate Qt::WaylandClient Wayland::Client
+    )
 
-if (Qt6_VERSION VERSION_GREATER_EQUAL "6.8.0")
-    set(private_code_option  "PRIVATE_CODE")
-endif()
-qt6_generate_wayland_protocol_client_sources(KF6ScreenDpms
-    ${private_code_option}
-    FILES
-        ${PLASMA_WAYLAND_PROTOCOLS_DIR}/dpms.xml
-)
+    if (Qt6_VERSION VERSION_GREATER_EQUAL "6.8.0")
+        set(private_code_option  "PRIVATE_CODE")
+    endif()
+    qt6_generate_wayland_protocol_client_sources(KF6ScreenDpms
+        ${private_code_option}
+        FILES
+            ${PLASMA_WAYLAND_PROTOCOLS_DIR}/dpms.xml
+    )
+else()
+    target_sources(KF6ScreenDpms PRIVATE dpms.cpp abstractdpmshelper.cpp xcbdpmshelper.cpp)
+    target_link_libraries(KF6ScreenDpms PUBLIC Qt::Gui
+                                        PRIVATE XCB::XCB XCB::DPMS XCB::RANDR
+                                                Qt::GuiPrivate
+    )
+endif()
 
 set_target_properties(KF6ScreenDpms PROPERTIES
     VERSION "${KSCREEN_VERSION}"
--- /dev/null
+++ b/src/libdpms/dpms-config.h.cmake
@@ -0,0 +1 @@
+#cmakedefine01 HAVE_WAYLAND
--- a/src/libdpms/dpms.cpp
+++ b/src/libdpms/dpms.cpp
@@ -2,9 +2,14 @@
 //
 // SPDX-License-Identifier: LGPL-2.1-or-later
 
+#include "dpms-config.h"
 #include "dpms.h"
 #include "kscreendpms_debug.h"
+
+#if HAVE_WAYLAND
 #include "waylanddpmshelper_p.h"
+#endif
+
 #include "xcbdpmshelper_p.h"
 
 #include <QGuiApplication>
@@ -15,8 +20,10 @@
 {
     if (QX11Info::isPlatformX11()) {
         m_helper.reset(new XcbDpmsHelper);
+#if HAVE_WAYLAND
     } else if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) {
         m_helper.reset(new WaylandDpmsHelper);
+#endif
     } else {
         qCWarning(KSCREEN_DPMS) << "dpms unsupported on this system";
         return;
