CMake: tidy & fix use of cache variables under policy CMP0126
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f35b5ed..86d58fe 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -111,8 +111,13 @@
 
 message(STATUS "Using CMake version ${CMAKE_VERSION} (${CMAKE_COMMAND})")
 
-# Increased minimum to 3.15 to allow use of string(REPEAT).
-cmake_minimum_required(VERSION 3.15 FATAL_ERROR)
+# Increased minimum to 3.15 to allow use of string(REPEAT). Increase policy
+# version to 3.31 to obtain better defaults such as CMP0126 and CMP0137.
+#
+# Note that for all policies between 3.15 and 3.31, we must therefore support
+# both the old and new behaviour.
+cmake_minimum_required(VERSION 3.15...3.31 FATAL_ERROR)
+
 project(PCRE2 C)
 if("c_std_11" IN_LIST CMAKE_C_COMPILE_FEATURES)
   set(CMAKE_C_STANDARD 11)
@@ -152,12 +157,6 @@
   add_compile_definitions(_CRT_SECURE_NO_DEPRECATE _CRT_SECURE_NO_WARNINGS)
 endif()
 
-# The following policies have been set in the PCRE2 CMake file in the past.
-# Since we specify a minimum of CMake 3.15, these are no longer required.
-# cmake_policy(SET CMP0063 NEW)
-# cmake_policy(SET CMP0026 OLD)
-# cmake_policy(SET CMP0074 NEW)
-
 # For our modules in cmake/. This uses list(APPEND) rather than set() to allow
 # setting CMAKE_MODULE_PATH on the command line.
 list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
@@ -342,7 +341,7 @@
 # Note: CMakeSetup displays these in alphabetical order, regardless of
 # the order we use here.
 
-set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libraries.")
+option(BUILD_SHARED_LIBS "Build shared libraries." OFF)
 
 option(BUILD_STATIC_LIBS "Build static libraries." ON)
 
@@ -359,20 +358,18 @@
 
 option(PCRE2_DISABLE_PERCENT_ZT "Disable the use of %zu and %td (rarely needed)" OFF)
 
-set(
+option(
   PCRE2_EBCDIC
-  OFF
-  CACHE BOOL
   "Use EBCDIC coding instead of ASCII. (This is rarely used outside of mainframe systems.)"
+  OFF
 )
 
-set(PCRE2_EBCDIC_NL25 OFF CACHE BOOL "Use 0x25 as EBCDIC NL character instead of 0x15; implies EBCDIC.")
+option(PCRE2_EBCDIC_NL25 "Use 0x25 as EBCDIC NL character instead of 0x15; implies EBCDIC." OFF)
 
-set(
+option(
   PCRE2_EBCDIC_IGNORING_COMPILER
-  OFF
-  CACHE BOOL
   "Force EBCDIC 1047 using numeric literals rather than C character literals; implies EBCDIC."
+  OFF
 )
 
 option(PCRE2_REBUILD_CHARTABLES "Rebuild char tables" OFF)
@@ -482,29 +479,28 @@
 endif()
 
 if(${CMAKE_SYSTEM_NAME} MATCHES Linux|NetBSD)
-  set(PCRE2_SUPPORT_JIT_SEALLOC OFF CACHE BOOL "Enable SELinux compatible execmem allocator in JIT (experimental).")
+  option(PCRE2_SUPPORT_JIT_SEALLOC "Enable SELinux compatible execmem allocator in JIT (experimental)." OFF)
 else()
   set(PCRE2_SUPPORT_JIT_SEALLOC IGNORE)
 endif()
 
-set(PCRE2GREP_SUPPORT_JIT ON CACHE BOOL "Enable use of Just-in-time compiling in pcre2grep.")
+option(PCRE2GREP_SUPPORT_JIT "Enable use of Just-in-time compiling in pcre2grep." ON)
 
-set(PCRE2GREP_SUPPORT_CALLOUT ON CACHE BOOL "Enable callout string support in pcre2grep.")
+option(PCRE2GREP_SUPPORT_CALLOUT "Enable callout string support in pcre2grep." ON)
 
-set(PCRE2GREP_SUPPORT_CALLOUT_FORK ON CACHE BOOL "Enable callout string fork support in pcre2grep.")
+option(PCRE2GREP_SUPPORT_CALLOUT_FORK "Enable callout string fork support in pcre2grep." ON)
 
-set(PCRE2_SUPPORT_UNICODE ON CACHE BOOL "Enable support for Unicode and UTF-8/UTF-16/UTF-32 encoding.")
+option(PCRE2_SUPPORT_UNICODE "Enable support for Unicode and UTF-8/UTF-16/UTF-32 encoding." ON)
 
-set(
+option(
   PCRE2_SUPPORT_BSR_ANYCRLF
-  OFF
-  CACHE BOOL
   "ON=Backslash-R matches only LF CR and CRLF, OFF=Backslash-R matches all Unicode Linebreaks"
+  OFF
 )
 
-set(PCRE2_NEVER_BACKSLASH_C OFF CACHE BOOL "If ON, backslash-C (upper case C) is locked out.")
+option(PCRE2_NEVER_BACKSLASH_C "If ON, backslash-C (upper case C) is locked out." OFF)
 
-set(PCRE2_SUPPORT_VALGRIND OFF CACHE BOOL "Enable Valgrind support.")
+option(PCRE2_SUPPORT_VALGRIND "Enable Valgrind support." OFF)
 
 option(PCRE2_FUZZ_SUPPORT "Enable fuzzer support" OFF)
 option(PCRE2_DIFF_FUZZ_SUPPORT "Enable differential fuzzer support (requires fuzz support and JIT)" OFF)
diff --git a/cmake/FindReadline.cmake b/cmake/FindReadline.cmake
index e55fbcf..6f01db3 100644
--- a/cmake/FindReadline.cmake
+++ b/cmake/FindReadline.cmake
@@ -10,6 +10,7 @@
 find_library(READLINE_LIBRARY NAMES readline)
 mark_as_advanced(READLINE_LIBRARY)
 
+set(_pcre2_readline_ncurses_library "")
 if(READLINE_INCLUDE_DIR AND READLINE_LIBRARY)
   # Check if we need to link to ncurses as well
 
@@ -44,8 +45,7 @@
           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)
+          set(_pcre2_readline_ncurses_library "${NCURSES_LIBRARY_${lib}}")
           break()
         endif()
       endif()
@@ -60,8 +60,8 @@
 
 if(Readline_FOUND)
   set(READLINE_LIBRARIES "${READLINE_LIBRARY}")
-  if(DEFINED NCURSES_LIBRARY)
-    list(APPEND READLINE_LIBRARIES "${NCURSES_LIBRARY}")
+  if(_pcre2_readline_ncurses_library)
+    list(APPEND READLINE_LIBRARIES "${_pcre2_readline_ncurses_library}")
   endif()
   set(READLINE_INCLUDE_DIRS "${READLINE_INCLUDE_DIR}")
 endif()