11import itertools
2+ import os
23import pathlib
34import sys
45import sysconfig
@@ -27,6 +28,46 @@ def get_extra_flags(compiler_flags: str, compiler_py_flags_nodist: str) -> List[
2728 return f"{ flags } { py_flags_nodist } " .split ()
2829
2930
31+ def fixup_build_ext (cmd ):
32+ """Function needed to make build_ext tests pass.
33+
34+ When Python was built with --enable-shared on Unix, -L. is not enough to
35+ find libpython<blah>.so, because regrtest runs in a tempdir, not in the
36+ source directory where the .so lives.
37+
38+ When Python was built with in debug mode on Windows, build_ext commands
39+ need their debug attribute set, and it is not done automatically for
40+ some reason.
41+
42+ This function handles both of these things. Example use:
43+
44+ cmd = build_ext(dist)
45+ support.fixup_build_ext(cmd)
46+ cmd.ensure_finalized()
47+
48+ Unlike most other Unix platforms, Mac OS X embeds absolute paths
49+ to shared libraries into executables, so the fixup is not needed there.
50+
51+ Taken from distutils (was part of the CPython stdlib until Python 3.11)
52+ """
53+ if os .name == 'nt' :
54+ cmd .debug = sys .executable .endswith ('_d.exe' )
55+ elif sysconfig .get_config_var ('Py_ENABLE_SHARED' ):
56+ # To further add to the shared builds fun on Unix, we can't just add
57+ # library_dirs to the Extension() instance because that doesn't get
58+ # plumbed through to the final compiler command.
59+ runshared = sysconfig .get_config_var ('RUNSHARED' )
60+ if runshared is None :
61+ cmd .library_dirs = ['.' ]
62+ else :
63+ if sys .platform == 'darwin' :
64+ cmd .library_dirs = []
65+ else :
66+ name , equals , value = runshared .partition ('=' )
67+ cmd .library_dirs = [d for d in value .split (os .pathsep ) if d ]
68+
69+
70+
3071def compile_c_extension (
3172 generated_source_path : str ,
3273 build_dir : Optional [str ] = None ,
@@ -49,16 +90,15 @@ def compile_c_extension(
4990 static library of the common parser sources (this is useful in case you are
5091 creating multiple extensions).
5192 """
52- import distutils .log
53- from distutils .core import Distribution , Extension
54- from distutils .tests .support import fixup_build_ext # type: ignore
93+ import setuptools .logging
5594
56- from distutils .ccompiler import new_compiler
57- from distutils .dep_util import newer_group
58- from distutils .sysconfig import customize_compiler
95+ from setuptools import Extension , Distribution
96+ from setuptools ._distutils .dep_util import newer_group
97+ from setuptools ._distutils .ccompiler import new_compiler
98+ from setuptools ._distutils .sysconfig import customize_compiler
5999
60100 if verbose :
61- distutils . log .set_threshold (distutils . log .DEBUG )
101+ setuptools . logging .set_threshold (setuptools . logging . logging .DEBUG )
62102
63103 source_file_path = pathlib .Path (generated_source_path )
64104 extension_name = source_file_path .stem
0 commit comments