diff --git a/Lib/distutils/tests/test_unixccompiler.py b/Lib/distutils/tests/test_unixccompiler.py index a3484d4f94cd92..8f67d85f42ceba 100644 --- a/Lib/distutils/tests/test_unixccompiler.py +++ b/Lib/distutils/tests/test_unixccompiler.py @@ -90,7 +90,7 @@ def gcv(v): sys.platform = 'bar' def gcv(v): if v == 'CC': - return 'cc' + return 'icc' elif v == 'GNULD': return 'yes' sysconfig.get_config_var = gcv @@ -100,7 +100,7 @@ def gcv(v): sys.platform = 'bar' def gcv(v): if v == 'CC': - return 'cc' + return 'icc' elif v == 'GNULD': return 'no' sysconfig.get_config_var = gcv diff --git a/Lib/distutils/unixccompiler.py b/Lib/distutils/unixccompiler.py index d00c48981eb6d6..b14ce8454e23fb 100644 --- a/Lib/distutils/unixccompiler.py +++ b/Lib/distutils/unixccompiler.py @@ -13,7 +13,7 @@ * link shared library handled by 'cc -shared' """ -import os, sys, re +import os, sys, re, shutil from distutils import sysconfig from distutils.dep_util import newer @@ -216,7 +216,27 @@ def library_dir_option(self, dir): def _is_gcc(self, compiler_name): # clang uses same syntax for rpath as gcc - return any(name in compiler_name for name in ("gcc", "g++", "clang")) + valid_compiler_names = ("gcc", "g++", "clang") + is_gcc = any(name in compiler_name for name in valid_compiler_names) + # On Linux systems, the compiler name may be, e.g., "cc -pthread". + # The executable "cc" is in this case a symlink to the true compiler. + if not is_gcc and "cc" in compiler_name: + # We need to make sure that this is not another compiler with "cc" + # at the end of its name, like "icc". For this, it is checked + # whether "cc" is the first word, or separated by a space or path + # delimiter before the "cc" substring. + cc_string_location = compiler_name.find("cc") + if cc_string_location == 0 \ + or compiler_name[cc_string_location - 1] == ' ' \ + or compiler_name[cc_string_location - 1] == '/' \ + or compiler_name[cc_string_location - 1] == '\\': + cc_path = shutil.which("cc") + if cc_path is not None: + real_compiler_path = os.path.realpath(cc_path) + is_gcc = any( + name in real_compiler_path \ + for name in valid_compiler_names) + return is_gcc def runtime_library_dir_option(self, dir): # XXX Hackish, at the very least. See Python bug #445902: diff --git a/Misc/NEWS.d/next/Build/2022-10-12-15-35-37.gh-issue-96488.s7h7up.rst b/Misc/NEWS.d/next/Build/2022-10-12-15-35-37.gh-issue-96488.s7h7up.rst new file mode 100644 index 00000000000000..ed7ea3b88d9da9 --- /dev/null +++ b/Misc/NEWS.d/next/Build/2022-10-12-15-35-37.gh-issue-96488.s7h7up.rst @@ -0,0 +1 @@ +Fix build failures with older versions of GCC when using runtime_library_dirs with distutils. Older versions of GCC only support the flag "-Wl,-R" instead of "-R". distutils now also handles this correctly with the compiler alias "cc".