From 6251637a67cc4695e1ca76b6189906e13163fd66 Mon Sep 17 00:00:00 2001
From: Isuru Fernando <isuruf@gmail.com>
Date: Thu, 16 Sep 2021 15:46:09 -0500
Subject: [PATCH 07/25] bpo-22699: Allow compiling on debian/ubuntu with a
 different compiler

This PR fixes one issue mentioned in the bpo
https://bugs.python.org/issue22699#msg364685 with a slightly better
patch than given
---
 setup.py | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/setup.py b/setup.py
index 87f76ad41b..95ee953e0e 100644
--- a/setup.py
+++ b/setup.py
@@ -687,9 +687,30 @@ def check_extension_import(self, ext):
     def add_multiarch_paths(self):
         # Debian/Ubuntu multiarch support.
         # https://wiki.ubuntu.com/MultiarchSpec
-        tmpfile = os.path.join(self.build_temp, 'multiarch')
         if not os.path.exists(self.build_temp):
             os.makedirs(self.build_temp)
+
+        tmpfile_sysroot = os.path.join(self.build_temp, 'sysroot')
+        ret_sysroot = run_command(
+            '%s -print-sysroot > %s 2> /dev/null' % (CC, tmpfile_sysroot))
+
+        try:
+            if ret_sysroot == 0:
+                with open(tmpfile_sysroot) as fp:
+                    sysroot = fp.readline().strip()
+                    # if the sysroot is not /, then we are not using
+                    # the compiler from debian/ubuntu
+                    if sysroot not in ['', '/']:
+                        add_dir_to_list(self.compiler.library_dirs,
+                            sysroot + '/usr/lib/')
+                        add_dir_to_list(self.compiler.include_dirs,
+                            sysroot + '/usr/include/')
+                        return
+        finally:
+            os.unlink(tmpfile_sysroot)
+
+        tmpfile = os.path.join(self.build_temp, 'multiarch')
+
         ret = run_command(
             '%s -print-multiarch > %s 2> /dev/null' % (CC, tmpfile))
         multiarch_path_component = ''
