Commit 69ecdad5a494957afcf2447cc6edac6934f2b1b6

Edward Thomson 2019-05-19T10:09:55

regex: use system PCRE if available Attempt to locate a system-installed version of PCRE and use its POSIX compatibility layer, if possible.

diff --git a/cmake/Modules/FindPCRE.cmake b/cmake/Modules/FindPCRE.cmake
new file mode 100644
index 0000000..74ed61e
--- /dev/null
+++ b/cmake/Modules/FindPCRE.cmake
@@ -0,0 +1,38 @@
+# Copyright (C) 2007-2009 LuaDist.
+# Created by Peter Kapec <kapecp@gmail.com>
+# Redistribution and use of this file is allowed according to the terms of the MIT license.
+# For details see the COPYRIGHT file distributed with LuaDist.
+#	Note:
+#		Searching headers and libraries is very simple and is NOT as powerful as scripts
+#		distributed with CMake, because LuaDist defines directories to search for.
+#		Everyone is encouraged to contact the author with improvements. Maybe this file
+#		becomes part of CMake distribution sometimes.
+
+# - Find pcre
+# Find the native PCRE headers and libraries.
+#
+# PCRE_INCLUDE_DIRS	- where to find pcre.h, etc.
+# PCRE_LIBRARIES	- List of libraries when using pcre.
+# PCRE_FOUND	- True if pcre found.
+
+# Look for the header file.
+FIND_PATH(PCRE_INCLUDE_DIR NAMES pcreposix.h)
+
+# Look for the library.
+FIND_LIBRARY(PCRE_LIBRARY NAMES pcre)
+FIND_LIBRARY(PCRE_POSIX_LIBRARY NAMES pcreposix)
+
+# Handle the QUIETLY and REQUIRED arguments and set PCRE_FOUND to TRUE if all listed variables are TRUE.
+INCLUDE(FindPackageHandleStandardArgs)
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(PCRE DEFAULT_MSG PCRE_LIBRARY PCRE_POSIX_LIBRARY PCRE_INCLUDE_DIR)
+
+# Copy the results to the output variables.
+IF(PCRE_FOUND)
+	SET(PCRE_LIBRARIES ${PCRE_LIBRARY} ${PCRE_POSIX_LIBRARY})
+	SET(PCRE_INCLUDE_DIRS ${PCRE_INCLUDE_DIR})
+ELSE(PCRE_FOUND)
+	SET(PCRE_LIBRARIES)
+	SET(PCRE_INCLUDE_DIRS)
+ENDIF(PCRE_FOUND)
+
+MARK_AS_ADVANCED(PCRE_INCLUDE_DIRS PCRE_LIBRARIES)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 79d7b55..852a2af 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -290,13 +290,14 @@ ELSE()
 ENDIF()
 
 # Specify regular expression implementation
+FIND_PACKAGE(PCRE)
+
 IF(REGEX STREQUAL "")
 	CHECK_SYMBOL_EXISTS(regcomp_l "regex.h;xlocale.h" HAVE_REGCOMP_L)
-	CHECK_SYMBOL_EXISTS(pcre_regcomp "pcreposix.h" HAVE_PCRE)
 
 	IF(HAVE_REGCOMP_L)
 		SET(REGEX "regcomp_l")
-	ELSEIF(HAVE_PCRE)
+	ELSEIF(PCRE_FOUND)
 		SET(REGEX "pcre")
 	ELSE()
 		SET(REGEX "builtin")
@@ -309,6 +310,9 @@ IF(REGEX STREQUAL "regcomp_l")
 ELSEIF(REGEX STREQUAL "pcre")
 	ADD_FEATURE_INFO(regex ON "using system PCRE")
 	SET(GIT_REGEX_PCRE 1)
+
+	LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${PCRE_INCLUDE_DIRS})
+	LIST(APPEND LIBGIT2_LIBS ${PCRE_LIBRARIES})
 ELSEIF(REGEX STREQUAL "regcomp")
 	ADD_FEATURE_INFO(regex ON "using system regcomp")
 	SET(GIT_REGEX_REGCOMP 1)
diff --git a/src/posix_regex.h b/src/posix_regex.h
index a0482c9..e76f69c 100644
--- a/src/posix_regex.h
+++ b/src/posix_regex.h
@@ -30,10 +30,18 @@
 # define p_regexec pcre_regexec
 # define p_regfree pcre_regfree
 
-/* Otherwise, use regcomp_l if available, or regcomp if not. */
+/*
+ * Use the system-provided `regex` routines, whether that's via the
+ * PCRE emulation layer, or libc, preferring `regcomp_l` it's available.
+ */
+
 #else
 
-# include <regex.h>
+# ifdef GIT_REGEX_PCRE
+#  include <pcreposix.h>
+# else
+#  include <regex.h>
+# endif
 
 # define P_REG_EXTENDED REG_EXTENDED
 # define P_REG_ICASE REG_ICASE