Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync with distutils #4790

Merged
merged 16 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/4790.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Synced with pypa/distutils@ff11eed0c including bugfix for duplicate CFLAGS and adaption to support Python 3.13 is_abs in the C compiler (#4669).
39 changes: 23 additions & 16 deletions setuptools/_distutils/ccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
for the Distutils compiler abstraction model."""

import os
import pathlib
import re
import sys
import types
Expand Down Expand Up @@ -969,27 +970,33 @@ def out_extensions(self):
return dict.fromkeys(self.src_extensions, self.obj_extension)

def _make_out_path(self, output_dir, strip_dir, src_name):
base, ext = os.path.splitext(src_name)
base = self._make_relative(base)
return self._make_out_path_exts(
output_dir, strip_dir, src_name, self.out_extensions
)

@classmethod
def _make_out_path_exts(cls, output_dir, strip_dir, src_name, extensions):
r"""
>>> exts = {'.c': '.o'}
>>> CCompiler._make_out_path_exts('.', False, '/foo/bar.c', exts).replace('\\', '/')
'./foo/bar.o'
>>> CCompiler._make_out_path_exts('.', True, '/foo/bar.c', exts).replace('\\', '/')
'./bar.o'
"""
src = pathlib.PurePath(src_name)
# Ensure base is relative to honor output_dir (python/cpython#37775).
base = cls._make_relative(src)
try:
new_ext = self.out_extensions[ext]
new_ext = extensions[src.suffix]
except LookupError:
raise UnknownFileError(f"unknown file type '{ext}' (from '{src_name}')")
raise UnknownFileError(f"unknown file type '{src.suffix}' (from '{src}')")
if strip_dir:
base = os.path.basename(base)
return os.path.join(output_dir, base + new_ext)
base = pathlib.PurePath(base.name)
return os.path.join(output_dir, base.with_suffix(new_ext))

@staticmethod
def _make_relative(base):
"""
In order to ensure that a filename always honors the
indicated output_dir, make sure it's relative.
Ref python/cpython#37775.
"""
# Chop off the drive
no_drive = os.path.splitdrive(base)[1]
# If abs, chop off leading /
return no_drive[os.path.isabs(no_drive) :]
def _make_relative(base: pathlib.Path):
return base.relative_to(base.anchor)

def shared_object_filename(self, basename, strip_dir=False, output_dir=''):
assert output_dir is not None
Expand Down
1 change: 0 additions & 1 deletion setuptools/_distutils/sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ def customize_compiler(compiler):
ldshared = _add_flags(ldshared, 'LD')
ldcxxshared = _add_flags(ldcxxshared, 'LD')
cflags = os.environ.get('CFLAGS', cflags)
cflags = _add_flags(cflags, 'C')
ldshared = _add_flags(ldshared, 'C')
cxxflags = os.environ.get('CXXFLAGS', cxxflags)
ldcxxshared = _add_flags(ldcxxshared, 'CXX')
Expand Down
2 changes: 2 additions & 0 deletions setuptools/_distutils/tests/test_ccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import pytest

pytestmark = pytest.mark.usefixtures('suppress_path_mangle')


def _make_strs(paths):
"""
Expand Down
6 changes: 2 additions & 4 deletions setuptools/_distutils/tests/test_sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,9 @@ def test_customize_compiler(self):
comp = self.customize_compiler()
assert comp.exes['archiver'] == 'env_ar --env-arflags'
assert comp.exes['preprocessor'] == 'env_cpp --env-cppflags'
assert (
comp.exes['compiler'] == 'env_cc --env-cflags --env-cflags --env-cppflags'
)
assert comp.exes['compiler'] == 'env_cc --env-cflags --env-cppflags'
assert comp.exes['compiler_so'] == (
'env_cc --env-cflags --env-cflags --env-cppflags --sc-ccshared'
'env_cc --env-cflags --env-cppflags --sc-ccshared'
)
assert (
comp.exes['compiler_cxx']
Expand Down
Loading