From f8e7d124500e869ffc5643228c34d3745d64a3d7 Mon Sep 17 00:00:00 2001
From: Nicholas Wilson <nicholas@nicholaswilson.me.uk>
Date: Sat, 25 Oct 2025 12:28:05 +0100
Subject: [PATCH] Use correctly-cased CMake variables for better compatibility
 (#825)

* Use the correct casing for <PackageName>_FOUND
* Add a CI test with CMake very latest (currently 4.1.2). This job also sets DCMAKE_POLICY_VERSION_MINIMUM to verify that we don't break any future policies, greater than our minimum version 3.15.
* Tidy the ncurses support for libreadline to use READLINE_LIBRARIES
---
 .github/workflows/dev.yml | 140 +++++++++++++++++++++++++++++++++-----
 CMakeLists.txt            |  50 ++++++--------
 cmake/FindEditline.cmake  |   5 ++
 cmake/FindReadline.cmake  |  17 ++++-
 4 files changed, 164 insertions(+), 48 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index c97099601..ac3f8d001 100644
--- CMakeLists.txt
+++ CMakeLists.txt
@@ -146,7 +146,7 @@ endif()
 # setting CMAKE_MODULE_PATH on the command line.
 list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
 
-# external packages
+# External packages
 find_package(BZip2)
 find_package(ZLIB)
 find_package(Readline)
@@ -446,7 +446,7 @@ if(HAVE_VSCRIPT)
 endif()
 
 # bzip2 lib
-if(BZIP2_FOUND)
+if(BZip2_FOUND)
   option(PCRE2_SUPPORT_LIBBZ2 "Enable support for linking pcre2grep with libbz2." ON)
 endif()
 
@@ -456,12 +456,12 @@ if(ZLIB_FOUND)
 endif()
 
 # editline lib
-if(EDITLINE_FOUND)
+if(Editline_FOUND)
   option(PCRE2_SUPPORT_LIBEDIT "Enable support for linking pcre2test with libedit." OFF)
 endif()
 
 # readline lib
-if(READLINE_FOUND)
+if(Readline_FOUND)
   option(PCRE2_SUPPORT_LIBREADLINE "Enable support for linking pcre2test with libreadline." ON)
 endif()
 
@@ -498,12 +498,12 @@ if(PCRE2_BUILD_PCRE2GREP AND NOT PCRE2_BUILD_PCRE2_8)
 endif()
 
 if(PCRE2_SUPPORT_LIBBZ2)
-  if(BZIP2_FOUND)
-    include_directories(${BZIP2_INCLUDE_DIR})
+  if(BZip2_FOUND)
+    include_directories(${BZIP2_INCLUDE_DIRS})
   else()
     message(
       FATAL_ERROR
-      " libbz2 not found. Set BZIP2_INCLUDE_DIR to a compatible header\n"
+      " libbz2 not found. Set BZIP2_INCLUDE_DIRS to a compatible header\n"
       " or set BZip2_ROOT to a full bzip2 installed tree, as needed."
     )
   endif()
@@ -511,42 +511,42 @@ endif()
 
 if(PCRE2_SUPPORT_LIBZ)
   if(ZLIB_FOUND)
-    include_directories(${ZLIB_INCLUDE_DIR})
+    include_directories(${ZLIB_INCLUDE_DIRS})
   else()
     message(
       FATAL_ERROR
-      " zlib not found. Set ZLIB_INCLUDE_DIR to a compatible header\n"
+      " zlib not found. Set ZLIB_INCLUDE_DIRS to a compatible header\n"
       " or set ZLIB_ROOT to a full zlib installed tree, as needed."
     )
   endif()
 endif()
 
 if(PCRE2_SUPPORT_LIBEDIT)
-  if(EDITLINE_FOUND)
-    include_directories(${EDITLINE_INCLUDE_DIR})
+  if(Editline_FOUND)
+    include_directories(${EDITLINE_INCLUDE_DIRS})
   else()
     message(
       FATAL_ERROR
-      " libedit not found. Set EDITLINE_INCLUDE_DIR to a compatible header\n"
+      " libedit not found. Set EDITLINE_INCLUDE_DIRS to a compatible header\n"
       " or set Editline_ROOT to a full libedit installed tree, as needed."
     )
   endif()
 endif()
 
 if(PCRE2_SUPPORT_LIBREADLINE)
-  if(READLINE_FOUND)
-    include_directories(${READLINE_INCLUDE_DIR})
+  if(Readline_FOUND)
+    include_directories(${READLINE_INCLUDE_DIRS})
   else()
     message(
       FATAL_ERROR
-      " libreadline not found. Set READLINE_INCLUDE_DIR to a compatible header\n"
+      " libreadline not found. Set READLINE_INCLUDE_DIRS to a compatible header\n"
       " or set Readline_ROOT to a full libreadline installed tree, as needed."
     )
   endif()
 endif()
 
 if(PCRE2_SUPPORT_LIBREADLINE AND PCRE2_SUPPORT_LIBEDIT)
-  if(READLINE_FOUND)
+  if(Readline_FOUND)
     message(
       FATAL_ERROR
       " Only one of the readline compatible libraries can be enabled.\n"
@@ -603,24 +603,16 @@ endif()
 set(PCRE2TEST_LIBS)
 set(PCRE2GREP_LIBS)
 
-# This next one used to reference ${READLINE_LIBRARY})
-# but I was advised to add the NCURSES test as well, along with
-# some modifications to cmake/FindReadline.cmake which should
-# make it possible to override the default if necessary. PH
-
 if(PCRE2_SUPPORT_LIBREADLINE)
   set(SUPPORT_LIBREADLINE 1)
-  list(APPEND PCRE2TEST_LIBS ${READLINE_LIBRARY})
-  if(DEFINED NCURSES_LIBRARY)
-    list(APPEND PCRE2TEST_LIBS ${NCURSES_LIBRARY})
-  endif()
+  list(APPEND PCRE2TEST_LIBS ${READLINE_LIBRARIES})
 endif()
 
 # libedit is a plug-compatible alternative to libreadline
 
 if(PCRE2_SUPPORT_LIBEDIT)
   set(SUPPORT_LIBEDIT 1)
-  list(APPEND PCRE2TEST_LIBS ${EDITLINE_LIBRARY})
+  list(APPEND PCRE2TEST_LIBS ${EDITLINE_LIBRARIES})
 endif()
 
 if(PCRE2_SUPPORT_LIBZ)
@@ -1589,17 +1581,17 @@ if(PCRE2_SHOW_REPORT)
   else()
     message(STATUS "  Link pcre2grep with libz .......... : Library not found")
   endif()
-  if(BZIP2_FOUND)
+  if(BZip2_FOUND)
     message(STATUS "  Link pcre2grep with libbz2 ........ : ${PCRE2_SUPPORT_LIBBZ2}")
   else()
     message(STATUS "  Link pcre2grep with libbz2 ........ : Library not found")
   endif()
-  if(EDITLINE_FOUND)
+  if(Editline_FOUND)
     message(STATUS "  Link pcre2test with libeditline ... : ${PCRE2_SUPPORT_LIBEDIT}")
   else()
     message(STATUS "  Link pcre2test with libeditline ... : Library not found")
   endif()
-  if(READLINE_FOUND)
+  if(Readline_FOUND)
     message(STATUS "  Link pcre2test with libreadline ... : ${PCRE2_SUPPORT_LIBREADLINE}")
   else()
     message(STATUS "  Link pcre2test with libreadline ... : Library not found")
diff --git a/cmake/FindEditline.cmake b/cmake/FindEditline.cmake
index c973c7b64..4784e052c 100644
--- cmake/FindEditline.cmake
+++ cmake/FindEditline.cmake
@@ -8,3 +8,8 @@ mark_as_advanced(EDITLINE_LIBRARY)
 
 include(FindPackageHandleStandardArgs)
 find_package_handle_standard_args(Editline DEFAULT_MSG EDITLINE_LIBRARY EDITLINE_INCLUDE_DIR)
+
+if(Editline_FOUND)
+  set(EDITLINE_LIBRARIES "${EDITLINE_LIBRARY}")
+  set(EDITLINE_INCLUDE_DIRS "${EDITLINE_INCLUDE_DIR}")
+endif()
diff --git a/cmake/FindReadline.cmake b/cmake/FindReadline.cmake
index 399096c37..e55fbcf22 100644
--- cmake/FindReadline.cmake
+++ cmake/FindReadline.cmake
@@ -16,6 +16,11 @@ if(READLINE_INCLUDE_DIR AND READLINE_LIBRARY)
   include(CheckSymbolExists)
   include(CMakePushCheckState)
 
+  set(first_run FALSE)
+  if(NOT DEFINED HAVE_READLINE_FUNC)
+    set(first_run TRUE)
+  endif()
+
   cmake_push_check_state(RESET)
   set(CMAKE_REQUIRED_LIBRARIES "${READLINE_LIBRARY}")
   set(CMAKE_REQUIRED_INCLUDES "${READLINE_INCLUDE_DIR}")
@@ -36,7 +41,9 @@ if(READLINE_INCLUDE_DIR AND READLINE_LIBRARY)
         check_symbol_exists("readline" "stdio.h;readline/readline.h" HAVE_READLINE_FUNC_${lib})
 
         if(HAVE_READLINE_FUNC_${lib})
-          message(STATUS "Looking for readline - readline needs ${lib}")
+          if(first_run)
+            message(STATUS "Looking for readline - readline needs ${lib}")
+          endif()
           set(NCURSES_LIBRARY "${NCURSES_LIBRARY_${lib}}" CACHE FILEPATH "Path to the ncurses library")
           mark_as_advanced(NCURSES_LIBRARY)
           break()
@@ -50,3 +57,11 @@ endif()
 
 include(FindPackageHandleStandardArgs)
 find_package_handle_standard_args(Readline DEFAULT_MSG READLINE_LIBRARY READLINE_INCLUDE_DIR)
+
+if(Readline_FOUND)
+  set(READLINE_LIBRARIES "${READLINE_LIBRARY}")
+  if(DEFINED NCURSES_LIBRARY)
+    list(APPEND READLINE_LIBRARIES "${NCURSES_LIBRARY}")
+  endif()
+  set(READLINE_INCLUDE_DIRS "${READLINE_INCLUDE_DIR}")
+endif()
