From 88b5952945662bee3d4b250a4d161b36afbf4f44 Mon Sep 17 00:00:00 2001
From: Massimiliano Torromeo <massimiliano.torromeo@gmail.com>
Date: Fri, 16 Oct 2020 20:21:31 +0200
Subject: [PATCH] Allow grpcio to be built against system re2

---
 setup.py | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/setup.py b/setup.py
index 184d5457f1a..f7e1fbf7c56 100644
--- a/setup.py
+++ b/setup.py
@@ -133,6 +133,11 @@
 BUILD_WITH_SYSTEM_CARES = os.environ.get('GRPC_PYTHON_BUILD_SYSTEM_CARES',
                                          False)
 
+# Export this variable to use the system installation of re2. You need to
+# have the header files installed (in /usr/include/re2) and during
+# runtime, the shared library must be installed
+BUILD_WITH_SYSTEM_RE2 = os.environ.get('GRPC_PYTHON_BUILD_SYSTEM_RE2', False)
+
 # For local development use only: This skips building gRPC Core and its
 # dependencies, including protobuf and boringssl. This allows "incremental"
 # compilation by first building gRPC Core using make, then building only the
@@ -258,6 +263,10 @@ def check_linker_need_libatomic():
     CORE_C_FILES = filter(lambda x: 'third_party/cares' not in x, CORE_C_FILES)
     CARES_INCLUDE = (os.path.join('/usr', 'include'),)
 
+if BUILD_WITH_SYSTEM_RE2:
+    CORE_C_FILES = filter(lambda x: 'third_party/re2' not in x, CORE_C_FILES)
+    RE2_INCLUDE = (os.path.join('/usr', 'include', 're2'),)
+
 EXTENSION_INCLUDE_DIRECTORIES = ((PYTHON_STEM,) + CORE_INCLUDE + ABSL_INCLUDE +
                                  ADDRESS_SORTING_INCLUDE + CARES_INCLUDE +
                                  RE2_INCLUDE + SSL_INCLUDE + UPB_INCLUDE +
@@ -284,6 +293,8 @@ def check_linker_need_libatomic():
     EXTENSION_LIBRARIES += ('z',)
 if BUILD_WITH_SYSTEM_CARES:
     EXTENSION_LIBRARIES += ('cares',)
+if BUILD_WITH_SYSTEM_RE2:
+    EXTENSION_LIBRARIES += ('re2',)
 
 DEFINE_MACROS = (('OPENSSL_NO_ASM', 1), ('_WIN32_WINNT', 0x600))
 if not DISABLE_LIBC_COMPATIBILITY:
---
28.1
