From 3b10bdd8590ae788ff7ae3d8fe570480a8dcbda9 Mon Sep 17 00:00:00 2001
From: Jordan Williams <jordan@jwillikers.com>
Date: Fri, 3 Nov 2023 08:51:04 -0500
Subject: [PATCH] Wayland: Remove extra-cmake-modules dependency

Fixes #1774.
---
 CMakeLists.txt     | 11 ++++----
 src/CMakeLists.txt | 67 +++++++++++++++++++++++++++++-----------------
 2 files changed, 48 insertions(+), 30 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 35835833..964c12aa 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -249,12 +249,13 @@ endif()
 # Use Wayland for window creation
 #--------------------------------------------------------------------
 if (_GLFW_WAYLAND)
-    find_package(ECM REQUIRED NO_MODULE)
-    list(APPEND CMAKE_MODULE_PATH "${ECM_MODULE_PATH}")
 
-    find_package(Wayland REQUIRED Client Cursor Egl)
-    find_package(WaylandScanner REQUIRED)
-    find_package(WaylandProtocols 1.15 REQUIRED)
+    include(FindPkgConfig)
+    pkg_check_modules(Wayland REQUIRED
+        wayland-client>=0.2.7
+        wayland-cursor>=0.2.7
+        wayland-egl>=0.2.7
+        xkbcommon)
 
     list(APPEND glfw_PKG_DEPS "wayland-client")
 
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index b6dd86c5..066f4d98 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -39,31 +39,6 @@ elseif (_GLFW_WAYLAND)
     set(glfw_SOURCES ${common_SOURCES} wl_init.c wl_monitor.c wl_window.c
                      posix_time.c posix_thread.c xkb_unicode.c
                      egl_context.c osmesa_context.c)
-
-    ecm_add_wayland_client_protocol(glfw_SOURCES
-        PROTOCOL
-        "${WAYLAND_PROTOCOLS_PKGDATADIR}/stable/xdg-shell/xdg-shell.xml"
-        BASENAME xdg-shell)
-    ecm_add_wayland_client_protocol(glfw_SOURCES
-        PROTOCOL
-        "${WAYLAND_PROTOCOLS_PKGDATADIR}/unstable/xdg-decoration/xdg-decoration-unstable-v1.xml"
-        BASENAME xdg-decoration)
-    ecm_add_wayland_client_protocol(glfw_SOURCES
-        PROTOCOL
-        "${WAYLAND_PROTOCOLS_PKGDATADIR}/stable/viewporter/viewporter.xml"
-        BASENAME viewporter)
-    ecm_add_wayland_client_protocol(glfw_SOURCES
-        PROTOCOL
-        "${WAYLAND_PROTOCOLS_PKGDATADIR}/unstable/relative-pointer/relative-pointer-unstable-v1.xml"
-        BASENAME relative-pointer-unstable-v1)
-    ecm_add_wayland_client_protocol(glfw_SOURCES
-        PROTOCOL
-        "${WAYLAND_PROTOCOLS_PKGDATADIR}/unstable/pointer-constraints/pointer-constraints-unstable-v1.xml"
-        BASENAME pointer-constraints-unstable-v1)
-    ecm_add_wayland_client_protocol(glfw_SOURCES
-        PROTOCOL
-        "${WAYLAND_PROTOCOLS_PKGDATADIR}/unstable/idle-inhibit/idle-inhibit-unstable-v1.xml"
-        BASENAME idle-inhibit-unstable-v1)
 elseif (_GLFW_OSMESA)
     set(glfw_HEADERS ${common_HEADERS} null_platform.h null_joystick.h
                      posix_time.h posix_thread.h osmesa_context.h)
@@ -71,6 +46,48 @@ elseif (_GLFW_OSMESA)
                      null_joystick.c posix_time.c posix_thread.c osmesa_context.c)
 endif()
 
+if (_GLFW_WAYLAND)
+    find_program(WAYLAND_SCANNER_EXECUTABLE NAMES wayland-scanner)
+    pkg_check_modules(WAYLAND_PROTOCOLS REQUIRED wayland-protocols>=1.15)
+    pkg_get_variable(WAYLAND_PROTOCOLS_BASE wayland-protocols pkgdatadir)
+
+    macro(wayland_generate protocol_file output_file)
+          add_custom_command(OUTPUT ${output_file}.h
+              COMMAND ${WAYLAND_SCANNER_EXECUTABLE} client-header
+              < ${protocol_file} > ${output_file}.h
+              DEPENDS ${protocol_file})
+          list(APPEND GLFW_WAYLAND_PROTOCOL_SOURCES ${output_file}.h)
+
+          add_custom_command(OUTPUT ${output_file}.c
+              COMMAND ${WAYLAND_SCANNER_EXECUTABLE} private-code
+              < ${protocol_file} > ${output_file}.c
+              DEPENDS ${protocol_file})
+          list(APPEND GLFW_WAYLAND_PROTOCOL_SOURCES ${output_file}.c)
+    endmacro()
+
+    set(GLFW_WAYLAND_PROTOCOL_SOURCES)
+    wayland_generate(
+        ${WAYLAND_PROTOCOLS_BASE}/stable/xdg-shell/xdg-shell.xml
+        ${GLFW_BINARY_DIR}/src/wayland-xdg-shell-client-protocol)
+    wayland_generate(
+        ${WAYLAND_PROTOCOLS_BASE}/unstable/xdg-decoration/xdg-decoration-unstable-v1.xml
+        ${GLFW_BINARY_DIR}/src/wayland-xdg-decoration-client-protocol)
+    wayland_generate(
+        ${WAYLAND_PROTOCOLS_BASE}/stable/viewporter/viewporter.xml
+        ${GLFW_BINARY_DIR}/src/wayland-viewporter-client-protocol)
+    wayland_generate(
+        ${WAYLAND_PROTOCOLS_BASE}/unstable/relative-pointer/relative-pointer-unstable-v1.xml
+        ${GLFW_BINARY_DIR}/src/wayland-relative-pointer-unstable-v1-client-protocol)
+    wayland_generate(
+        ${WAYLAND_PROTOCOLS_BASE}/unstable/pointer-constraints/pointer-constraints-unstable-v1.xml
+        ${GLFW_BINARY_DIR}/src/wayland-pointer-constraints-unstable-v1-client-protocol)
+    wayland_generate(
+        ${WAYLAND_PROTOCOLS_BASE}/unstable/idle-inhibit/idle-inhibit-unstable-v1.xml
+        ${GLFW_BINARY_DIR}/src/wayland-idle-inhibit-unstable-v1-client-protocol)
+
+    list(APPEND glfw_SOURCES ${GLFW_WAYLAND_PROTOCOL_SOURCES})
+endif()
+
 if (_GLFW_X11 OR _GLFW_WAYLAND)
     if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
         set(glfw_HEADERS ${glfw_HEADERS} linux_joystick.h)
-- 
2.41.0

