From de10fbc2386dcac3ab810c49b6977b2ee01bf426 Mon Sep 17 00:00:00 2001
From: Khem Raj <raj.khem@gmail.com>
Date: Wed, 17 Feb 2021 13:30:23 -0800
Subject: [PATCH] setup.py: Do not mix C and C++ compiler options

EXTRA_ENV_COMPILE_ARGS is used both with CC and CXX
so using -std=c++11 or -std=gnu99 together will cause
build time errors espcially with clang

Keep '-std=c++11' to fix native build error
with old gcc (such as gcc 5.4.0 on ubuntu 16.04), for clang
we will remove them through GRPC_PYTHON_CFLAGS at do_compile
in bb recipe.

While export CC="gcc ", cc_args is None, it will
cause subprocess.Popen always return 1. On centos 8, if you don't
install package libatomic, there will be a native build error
`cannot find /usr/lib64/libatomic.so.1.2.0'.

Add no harm '-g' to cc_args if cc_args is empty.

Upstream-Status: Inappropriate [oe specific]

Signed-off-by: Khem Raj <raj.khem@gmail.com>
Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
---
 setup.py                      | 9 ++++++---
 src/python/grpcio/commands.py | 5 ++++-
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/setup.py b/setup.py
index ec27fe4..b2d2d1a 100644
--- a/setup.py
+++ b/setup.py
@@ -206,8 +206,11 @@ def check_linker_need_libatomic():
     """Test if linker on system needs libatomic."""
     code_test = (b'#include <atomic>\n' +
                  b'int main() { return std::atomic<int64_t>{}; }')
-    cxx = shlex.split(os.environ.get('CXX', 'c++'))
-    cpp_test = subprocess.Popen(cxx + ['-x', 'c++', '-std=c++14', '-'],
+    cxx, cxx_args = os.environ.get('CXX').split(' ', 1) or 'c++'
+    if not cxx_args:
+      cxx_args = "-g"
+
+    cpp_test = subprocess.Popen([cxx, cxx_args, '-x', 'c++', '-std=c++14', '-'],
                                 stdin=PIPE,
                                 stdout=PIPE,
                                 stderr=PIPE)
@@ -217,7 +220,7 @@ def check_linker_need_libatomic():
     # Double-check to see if -latomic actually can solve the problem.
     # https://github.com/grpc/grpc/issues/22491
     cpp_test = subprocess.Popen(
-        [cxx, '-x', 'c++', '-std=c++14', '-', '-latomic'],
+        [cxx, cxx_args, '-x', 'c++', '-std=c++14', '-', '-latomic'],
         stdin=PIPE,
         stdout=PIPE,
         stderr=PIPE)
diff --git a/src/python/grpcio/commands.py b/src/python/grpcio/commands.py
index d93b6c7..a8c4a51 100644
--- a/src/python/grpcio/commands.py
+++ b/src/python/grpcio/commands.py
@@ -228,7 +228,10 @@ class BuildExt(build_ext.build_ext):
             """
             try:
                 # TODO(lidiz) Remove the generated a.out for success tests.
-                cc_test = subprocess.Popen(['cc', '-x', 'c', '-std=c++14', '-'],
+                cc_test, cc_args = os.environ.get('CC').split(' ', 1) or 'gcc'
+                if not cc_args:
+                    cc_args = "-g"
+                cc_test = subprocess.Popen([cc_test, cc_args, '-x', 'c', '-std=c++14', '-'],
                                            stdin=subprocess.PIPE,
                                            stdout=subprocess.PIPE,
                                            stderr=subprocess.PIPE)
-- 
2.25.1

