From a9b4f1c020242e6ac1b0a4b06404527d75e43790 Mon Sep 17 00:00:00 2001
From: Ray Donnelly <mingw.android@gmail.com>
Date: Fri, 16 Nov 2018 23:04:09 +0000
Subject: [PATCH 1/2] Monkey-patch distutils.ccompiler.spawn to elide
 -std=c++11 from non-C++ code

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

diff --git a/setup.py b/setup.py
index e950057..1565a39 100644
--- a/setup.py
+++ b/setup.py
@@ -153,6 +153,26 @@ def check_linker_need_libatomic():
   cc_test.communicate(input=code_test)
   return cc_test.returncode != 0
 
+# Patch ccompiler not to pass -std=c++11 for C code.
+# This is neccessary on macOS to avoid:
+# error: invalid argument '-std=c++11' not allowed with 'C/ObjC'
+def monkeypatch_spawn_using_cxx11_for_cxx_only():
+  from distutils import ccompiler
+  def spawn_using_cxx11_for_cxx_only(old_spawn):
+    def spawn(cmd, search_path=1, verbose=0, dry_run=0):
+      is_cxx = any((arg.endswith('.cc') or arg.endswith('.cpp'))
+                    for arg in cmd)
+      cmd2 = []
+      for arg in cmd:
+        if not is_cxx and arg != '-std=c++11':
+          cmd2.append(arg)
+        elif is_cxx and arg not in ('-Wstrict-prototypes', '-std=gnu99'):
+          cmd2.append(arg)
+      return old_spawn(cmd2, search_path, verbose, dry_run)
+    return spawn
+  ccompiler.spawn = spawn_using_cxx11_for_cxx_only(ccompiler.spawn)
+
+
 # There are some situations (like on Windows) where CC, CFLAGS, and LDFLAGS are
 # entirely ignored/dropped/forgotten by distutils and its Cygwin/MinGW support.
 # We use these environment variables to thus get around that without locking
@@ -381,6 +401,8 @@ PACKAGE_DATA = {
 }
 PACKAGES = setuptools.find_packages(PYTHON_STEM)
 
+monkeypatch_spawn_using_cxx11_for_cxx_only()
+
 setuptools.setup(
   name='grpcio',
   version=grpc_version.VERSION,
-- 
2.20.1

