]> jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/setuptools/_distutils/command/build_ext.py
init: venv aand flask
[dlqueue.git] / venv / lib / python3.11 / site-packages / setuptools / _distutils / command / build_ext.py
1 """distutils.command.build_ext
2
3 Implements the Distutils 'build_ext' command, for building extension
4 modules (currently limited to C extensions, should accommodate C++
5 extensions ASAP)."""
6
7 import contextlib
8 import os
9 import re
10 import sys
11 from ..core import Command
12 from ..errors import (
13 DistutilsOptionError,
14 DistutilsSetupError,
15 CCompilerError,
16 DistutilsError,
17 CompileError,
18 DistutilsPlatformError,
19 )
20 from ..sysconfig import customize_compiler, get_python_version
21 from ..sysconfig import get_config_h_filename
22 from ..dep_util import newer_group
23 from ..extension import Extension
24 from ..util import get_platform
25 from distutils._log import log
26 from . import py37compat
27
28 from site import USER_BASE
29
30 # An extension name is just a dot-separated list of Python NAMEs (ie.
31 # the same as a fully-qualified module name).
32 extension_name_re = re.compile(r'^[a-zA-Z_][a-zA-Z_0-9]*(\.[a-zA-Z_][a-zA-Z_0-9]*)*$')
33
34
35 def show_compilers():
36 from ..ccompiler import show_compilers
37
38 show_compilers()
39
40
41 class build_ext(Command):
42 description = "build C/C++ extensions (compile/link to build directory)"
43
44 # XXX thoughts on how to deal with complex command-line options like
45 # these, i.e. how to make it so fancy_getopt can suck them off the
46 # command line and make it look like setup.py defined the appropriate
47 # lists of tuples of what-have-you.
48 # - each command needs a callback to process its command-line options
49 # - Command.__init__() needs access to its share of the whole
50 # command line (must ultimately come from
51 # Distribution.parse_command_line())
52 # - it then calls the current command class' option-parsing
53 # callback to deal with weird options like -D, which have to
54 # parse the option text and churn out some custom data
55 # structure
56 # - that data structure (in this case, a list of 2-tuples)
57 # will then be present in the command object by the time
58 # we get to finalize_options() (i.e. the constructor
59 # takes care of both command-line and client options
60 # in between initialize_options() and finalize_options())
61
62 sep_by = " (separated by '%s')" % os.pathsep
63 user_options = [
64 ('build-lib=', 'b', "directory for compiled extension modules"),
65 ('build-temp=', 't', "directory for temporary files (build by-products)"),
66 (
67 'plat-name=',
68 'p',
69 "platform name to cross-compile for, if supported "
70 "(default: %s)" % get_platform(),
71 ),
72 (
73 'inplace',
74 'i',
75 "ignore build-lib and put compiled extensions into the source "
76 + "directory alongside your pure Python modules",
77 ),
78 (
79 'include-dirs=',
80 'I',
81 "list of directories to search for header files" + sep_by,
82 ),
83 ('define=', 'D', "C preprocessor macros to define"),
84 ('undef=', 'U', "C preprocessor macros to undefine"),
85 ('libraries=', 'l', "external C libraries to link with"),
86 (
87 'library-dirs=',
88 'L',
89 "directories to search for external C libraries" + sep_by,
90 ),
91 ('rpath=', 'R', "directories to search for shared C libraries at runtime"),
92 ('link-objects=', 'O', "extra explicit link objects to include in the link"),
93 ('debug', 'g', "compile/link with debugging information"),
94 ('force', 'f', "forcibly build everything (ignore file timestamps)"),
95 ('compiler=', 'c', "specify the compiler type"),
96 ('parallel=', 'j', "number of parallel build jobs"),
97 ('swig-cpp', None, "make SWIG create C++ files (default is C)"),
98 ('swig-opts=', None, "list of SWIG command line options"),
99 ('swig=', None, "path to the SWIG executable"),
100 ('user', None, "add user include, library and rpath"),
101 ]
102
103 boolean_options = ['inplace', 'debug', 'force', 'swig-cpp', 'user']
104
105 help_options = [
106 ('help-compiler', None, "list available compilers", show_compilers),
107 ]
108
109 def initialize_options(self):
110 self.extensions = None
111 self.build_lib = None
112 self.plat_name = None
113 self.build_temp = None
114 self.inplace = 0
115 self.package = None
116
117 self.include_dirs = None
118 self.define = None
119 self.undef = None
120 self.libraries = None
121 self.library_dirs = None
122 self.rpath = None
123 self.link_objects = None
124 self.debug = None
125 self.force = None
126 self.compiler = None
127 self.swig = None
128 self.swig_cpp = None
129 self.swig_opts = None
130 self.user = None
131 self.parallel = None
132
133 def finalize_options(self): # noqa: C901
134 from distutils import sysconfig
135
136 self.set_undefined_options(
137 'build',
138 ('build_lib', 'build_lib'),
139 ('build_temp', 'build_temp'),
140 ('compiler', 'compiler'),
141 ('debug', 'debug'),
142 ('force', 'force'),
143 ('parallel', 'parallel'),
144 ('plat_name', 'plat_name'),
145 )
146
147 if self.package is None:
148 self.package = self.distribution.ext_package
149
150 self.extensions = self.distribution.ext_modules
151
152 # Make sure Python's include directories (for Python.h, pyconfig.h,
153 # etc.) are in the include search path.
154 py_include = sysconfig.get_python_inc()
155 plat_py_include = sysconfig.get_python_inc(plat_specific=1)
156 if self.include_dirs is None:
157 self.include_dirs = self.distribution.include_dirs or []
158 if isinstance(self.include_dirs, str):
159 self.include_dirs = self.include_dirs.split(os.pathsep)
160
161 # If in a virtualenv, add its include directory
162 # Issue 16116
163 if sys.exec_prefix != sys.base_exec_prefix:
164 self.include_dirs.append(os.path.join(sys.exec_prefix, 'include'))
165
166 # Put the Python "system" include dir at the end, so that
167 # any local include dirs take precedence.
168 self.include_dirs.extend(py_include.split(os.path.pathsep))
169 if plat_py_include != py_include:
170 self.include_dirs.extend(plat_py_include.split(os.path.pathsep))
171
172 self.ensure_string_list('libraries')
173 self.ensure_string_list('link_objects')
174
175 # Life is easier if we're not forever checking for None, so
176 # simplify these options to empty lists if unset
177 if self.libraries is None:
178 self.libraries = []
179 if self.library_dirs is None:
180 self.library_dirs = []
181 elif isinstance(self.library_dirs, str):
182 self.library_dirs = self.library_dirs.split(os.pathsep)
183
184 if self.rpath is None:
185 self.rpath = []
186 elif isinstance(self.rpath, str):
187 self.rpath = self.rpath.split(os.pathsep)
188
189 # for extensions under windows use different directories
190 # for Release and Debug builds.
191 # also Python's library directory must be appended to library_dirs
192 if os.name == 'nt':
193 # the 'libs' directory is for binary installs - we assume that
194 # must be the *native* platform. But we don't really support
195 # cross-compiling via a binary install anyway, so we let it go.
196 self.library_dirs.append(os.path.join(sys.exec_prefix, 'libs'))
197 if sys.base_exec_prefix != sys.prefix: # Issue 16116
198 self.library_dirs.append(os.path.join(sys.base_exec_prefix, 'libs'))
199 if self.debug:
200 self.build_temp = os.path.join(self.build_temp, "Debug")
201 else:
202 self.build_temp = os.path.join(self.build_temp, "Release")
203
204 # Append the source distribution include and library directories,
205 # this allows distutils on windows to work in the source tree
206 self.include_dirs.append(os.path.dirname(get_config_h_filename()))
207 self.library_dirs.append(sys.base_exec_prefix)
208
209 # Use the .lib files for the correct architecture
210 if self.plat_name == 'win32':
211 suffix = 'win32'
212 else:
213 # win-amd64
214 suffix = self.plat_name[4:]
215 new_lib = os.path.join(sys.exec_prefix, 'PCbuild')
216 if suffix:
217 new_lib = os.path.join(new_lib, suffix)
218 self.library_dirs.append(new_lib)
219
220 # For extensions under Cygwin, Python's library directory must be
221 # appended to library_dirs
222 if sys.platform[:6] == 'cygwin':
223 if not sysconfig.python_build:
224 # building third party extensions
225 self.library_dirs.append(
226 os.path.join(
227 sys.prefix, "lib", "python" + get_python_version(), "config"
228 )
229 )
230 else:
231 # building python standard extensions
232 self.library_dirs.append('.')
233
234 # For building extensions with a shared Python library,
235 # Python's library directory must be appended to library_dirs
236 # See Issues: #1600860, #4366
237 if sysconfig.get_config_var('Py_ENABLE_SHARED'):
238 if not sysconfig.python_build:
239 # building third party extensions
240 self.library_dirs.append(sysconfig.get_config_var('LIBDIR'))
241 else:
242 # building python standard extensions
243 self.library_dirs.append('.')
244
245 # The argument parsing will result in self.define being a string, but
246 # it has to be a list of 2-tuples. All the preprocessor symbols
247 # specified by the 'define' option will be set to '1'. Multiple
248 # symbols can be separated with commas.
249
250 if self.define:
251 defines = self.define.split(',')
252 self.define = [(symbol, '1') for symbol in defines]
253
254 # The option for macros to undefine is also a string from the
255 # option parsing, but has to be a list. Multiple symbols can also
256 # be separated with commas here.
257 if self.undef:
258 self.undef = self.undef.split(',')
259
260 if self.swig_opts is None:
261 self.swig_opts = []
262 else:
263 self.swig_opts = self.swig_opts.split(' ')
264
265 # Finally add the user include and library directories if requested
266 if self.user:
267 user_include = os.path.join(USER_BASE, "include")
268 user_lib = os.path.join(USER_BASE, "lib")
269 if os.path.isdir(user_include):
270 self.include_dirs.append(user_include)
271 if os.path.isdir(user_lib):
272 self.library_dirs.append(user_lib)
273 self.rpath.append(user_lib)
274
275 if isinstance(self.parallel, str):
276 try:
277 self.parallel = int(self.parallel)
278 except ValueError:
279 raise DistutilsOptionError("parallel should be an integer")
280
281 def run(self): # noqa: C901
282 from ..ccompiler import new_compiler
283
284 # 'self.extensions', as supplied by setup.py, is a list of
285 # Extension instances. See the documentation for Extension (in
286 # distutils.extension) for details.
287 #
288 # For backwards compatibility with Distutils 0.8.2 and earlier, we
289 # also allow the 'extensions' list to be a list of tuples:
290 # (ext_name, build_info)
291 # where build_info is a dictionary containing everything that
292 # Extension instances do except the name, with a few things being
293 # differently named. We convert these 2-tuples to Extension
294 # instances as needed.
295
296 if not self.extensions:
297 return
298
299 # If we were asked to build any C/C++ libraries, make sure that the
300 # directory where we put them is in the library search path for
301 # linking extensions.
302 if self.distribution.has_c_libraries():
303 build_clib = self.get_finalized_command('build_clib')
304 self.libraries.extend(build_clib.get_library_names() or [])
305 self.library_dirs.append(build_clib.build_clib)
306
307 # Setup the CCompiler object that we'll use to do all the
308 # compiling and linking
309 self.compiler = new_compiler(
310 compiler=self.compiler,
311 verbose=self.verbose,
312 dry_run=self.dry_run,
313 force=self.force,
314 )
315 customize_compiler(self.compiler)
316 # If we are cross-compiling, init the compiler now (if we are not
317 # cross-compiling, init would not hurt, but people may rely on
318 # late initialization of compiler even if they shouldn't...)
319 if os.name == 'nt' and self.plat_name != get_platform():
320 self.compiler.initialize(self.plat_name)
321
322 # And make sure that any compile/link-related options (which might
323 # come from the command-line or from the setup script) are set in
324 # that CCompiler object -- that way, they automatically apply to
325 # all compiling and linking done here.
326 if self.include_dirs is not None:
327 self.compiler.set_include_dirs(self.include_dirs)
328 if self.define is not None:
329 # 'define' option is a list of (name,value) tuples
330 for name, value in self.define:
331 self.compiler.define_macro(name, value)
332 if self.undef is not None:
333 for macro in self.undef:
334 self.compiler.undefine_macro(macro)
335 if self.libraries is not None:
336 self.compiler.set_libraries(self.libraries)
337 if self.library_dirs is not None:
338 self.compiler.set_library_dirs(self.library_dirs)
339 if self.rpath is not None:
340 self.compiler.set_runtime_library_dirs(self.rpath)
341 if self.link_objects is not None:
342 self.compiler.set_link_objects(self.link_objects)
343
344 # Now actually compile and link everything.
345 self.build_extensions()
346
347 def check_extensions_list(self, extensions): # noqa: C901
348 """Ensure that the list of extensions (presumably provided as a
349 command option 'extensions') is valid, i.e. it is a list of
350 Extension objects. We also support the old-style list of 2-tuples,
351 where the tuples are (ext_name, build_info), which are converted to
352 Extension instances here.
353
354 Raise DistutilsSetupError if the structure is invalid anywhere;
355 just returns otherwise.
356 """
357 if not isinstance(extensions, list):
358 raise DistutilsSetupError(
359 "'ext_modules' option must be a list of Extension instances"
360 )
361
362 for i, ext in enumerate(extensions):
363 if isinstance(ext, Extension):
364 continue # OK! (assume type-checking done
365 # by Extension constructor)
366
367 if not isinstance(ext, tuple) or len(ext) != 2:
368 raise DistutilsSetupError(
369 "each element of 'ext_modules' option must be an "
370 "Extension instance or 2-tuple"
371 )
372
373 ext_name, build_info = ext
374
375 log.warning(
376 "old-style (ext_name, build_info) tuple found in "
377 "ext_modules for extension '%s' "
378 "-- please convert to Extension instance",
379 ext_name,
380 )
381
382 if not (isinstance(ext_name, str) and extension_name_re.match(ext_name)):
383 raise DistutilsSetupError(
384 "first element of each tuple in 'ext_modules' "
385 "must be the extension name (a string)"
386 )
387
388 if not isinstance(build_info, dict):
389 raise DistutilsSetupError(
390 "second element of each tuple in 'ext_modules' "
391 "must be a dictionary (build info)"
392 )
393
394 # OK, the (ext_name, build_info) dict is type-safe: convert it
395 # to an Extension instance.
396 ext = Extension(ext_name, build_info['sources'])
397
398 # Easy stuff: one-to-one mapping from dict elements to
399 # instance attributes.
400 for key in (
401 'include_dirs',
402 'library_dirs',
403 'libraries',
404 'extra_objects',
405 'extra_compile_args',
406 'extra_link_args',
407 ):
408 val = build_info.get(key)
409 if val is not None:
410 setattr(ext, key, val)
411
412 # Medium-easy stuff: same syntax/semantics, different names.
413 ext.runtime_library_dirs = build_info.get('rpath')
414 if 'def_file' in build_info:
415 log.warning(
416 "'def_file' element of build info dict " "no longer supported"
417 )
418
419 # Non-trivial stuff: 'macros' split into 'define_macros'
420 # and 'undef_macros'.
421 macros = build_info.get('macros')
422 if macros:
423 ext.define_macros = []
424 ext.undef_macros = []
425 for macro in macros:
426 if not (isinstance(macro, tuple) and len(macro) in (1, 2)):
427 raise DistutilsSetupError(
428 "'macros' element of build info dict "
429 "must be 1- or 2-tuple"
430 )
431 if len(macro) == 1:
432 ext.undef_macros.append(macro[0])
433 elif len(macro) == 2:
434 ext.define_macros.append(macro)
435
436 extensions[i] = ext
437
438 def get_source_files(self):
439 self.check_extensions_list(self.extensions)
440 filenames = []
441
442 # Wouldn't it be neat if we knew the names of header files too...
443 for ext in self.extensions:
444 filenames.extend(ext.sources)
445 return filenames
446
447 def get_outputs(self):
448 # Sanity check the 'extensions' list -- can't assume this is being
449 # done in the same run as a 'build_extensions()' call (in fact, we
450 # can probably assume that it *isn't*!).
451 self.check_extensions_list(self.extensions)
452
453 # And build the list of output (built) filenames. Note that this
454 # ignores the 'inplace' flag, and assumes everything goes in the
455 # "build" tree.
456 outputs = []
457 for ext in self.extensions:
458 outputs.append(self.get_ext_fullpath(ext.name))
459 return outputs
460
461 def build_extensions(self):
462 # First, sanity-check the 'extensions' list
463 self.check_extensions_list(self.extensions)
464 if self.parallel:
465 self._build_extensions_parallel()
466 else:
467 self._build_extensions_serial()
468
469 def _build_extensions_parallel(self):
470 workers = self.parallel
471 if self.parallel is True:
472 workers = os.cpu_count() # may return None
473 try:
474 from concurrent.futures import ThreadPoolExecutor
475 except ImportError:
476 workers = None
477
478 if workers is None:
479 self._build_extensions_serial()
480 return
481
482 with ThreadPoolExecutor(max_workers=workers) as executor:
483 futures = [
484 executor.submit(self.build_extension, ext) for ext in self.extensions
485 ]
486 for ext, fut in zip(self.extensions, futures):
487 with self._filter_build_errors(ext):
488 fut.result()
489
490 def _build_extensions_serial(self):
491 for ext in self.extensions:
492 with self._filter_build_errors(ext):
493 self.build_extension(ext)
494
495 @contextlib.contextmanager
496 def _filter_build_errors(self, ext):
497 try:
498 yield
499 except (CCompilerError, DistutilsError, CompileError) as e:
500 if not ext.optional:
501 raise
502 self.warn('building extension "{}" failed: {}'.format(ext.name, e))
503
504 def build_extension(self, ext):
505 sources = ext.sources
506 if sources is None or not isinstance(sources, (list, tuple)):
507 raise DistutilsSetupError(
508 "in 'ext_modules' option (extension '%s'), "
509 "'sources' must be present and must be "
510 "a list of source filenames" % ext.name
511 )
512 # sort to make the resulting .so file build reproducible
513 sources = sorted(sources)
514
515 ext_path = self.get_ext_fullpath(ext.name)
516 depends = sources + ext.depends
517 if not (self.force or newer_group(depends, ext_path, 'newer')):
518 log.debug("skipping '%s' extension (up-to-date)", ext.name)
519 return
520 else:
521 log.info("building '%s' extension", ext.name)
522
523 # First, scan the sources for SWIG definition files (.i), run
524 # SWIG on 'em to create .c files, and modify the sources list
525 # accordingly.
526 sources = self.swig_sources(sources, ext)
527
528 # Next, compile the source code to object files.
529
530 # XXX not honouring 'define_macros' or 'undef_macros' -- the
531 # CCompiler API needs to change to accommodate this, and I
532 # want to do one thing at a time!
533
534 # Two possible sources for extra compiler arguments:
535 # - 'extra_compile_args' in Extension object
536 # - CFLAGS environment variable (not particularly
537 # elegant, but people seem to expect it and I
538 # guess it's useful)
539 # The environment variable should take precedence, and
540 # any sensible compiler will give precedence to later
541 # command line args. Hence we combine them in order:
542 extra_args = ext.extra_compile_args or []
543
544 macros = ext.define_macros[:]
545 for undef in ext.undef_macros:
546 macros.append((undef,))
547
548 objects = self.compiler.compile(
549 sources,
550 output_dir=self.build_temp,
551 macros=macros,
552 include_dirs=ext.include_dirs,
553 debug=self.debug,
554 extra_postargs=extra_args,
555 depends=ext.depends,
556 )
557
558 # XXX outdated variable, kept here in case third-part code
559 # needs it.
560 self._built_objects = objects[:]
561
562 # Now link the object files together into a "shared object" --
563 # of course, first we have to figure out all the other things
564 # that go into the mix.
565 if ext.extra_objects:
566 objects.extend(ext.extra_objects)
567 extra_args = ext.extra_link_args or []
568
569 # Detect target language, if not provided
570 language = ext.language or self.compiler.detect_language(sources)
571
572 self.compiler.link_shared_object(
573 objects,
574 ext_path,
575 libraries=self.get_libraries(ext),
576 library_dirs=ext.library_dirs,
577 runtime_library_dirs=ext.runtime_library_dirs,
578 extra_postargs=extra_args,
579 export_symbols=self.get_export_symbols(ext),
580 debug=self.debug,
581 build_temp=self.build_temp,
582 target_lang=language,
583 )
584
585 def swig_sources(self, sources, extension):
586 """Walk the list of source files in 'sources', looking for SWIG
587 interface (.i) files. Run SWIG on all that are found, and
588 return a modified 'sources' list with SWIG source files replaced
589 by the generated C (or C++) files.
590 """
591 new_sources = []
592 swig_sources = []
593 swig_targets = {}
594
595 # XXX this drops generated C/C++ files into the source tree, which
596 # is fine for developers who want to distribute the generated
597 # source -- but there should be an option to put SWIG output in
598 # the temp dir.
599
600 if self.swig_cpp:
601 log.warning("--swig-cpp is deprecated - use --swig-opts=-c++")
602
603 if (
604 self.swig_cpp
605 or ('-c++' in self.swig_opts)
606 or ('-c++' in extension.swig_opts)
607 ):
608 target_ext = '.cpp'
609 else:
610 target_ext = '.c'
611
612 for source in sources:
613 (base, ext) = os.path.splitext(source)
614 if ext == ".i": # SWIG interface file
615 new_sources.append(base + '_wrap' + target_ext)
616 swig_sources.append(source)
617 swig_targets[source] = new_sources[-1]
618 else:
619 new_sources.append(source)
620
621 if not swig_sources:
622 return new_sources
623
624 swig = self.swig or self.find_swig()
625 swig_cmd = [swig, "-python"]
626 swig_cmd.extend(self.swig_opts)
627 if self.swig_cpp:
628 swig_cmd.append("-c++")
629
630 # Do not override commandline arguments
631 if not self.swig_opts:
632 for o in extension.swig_opts:
633 swig_cmd.append(o)
634
635 for source in swig_sources:
636 target = swig_targets[source]
637 log.info("swigging %s to %s", source, target)
638 self.spawn(swig_cmd + ["-o", target, source])
639
640 return new_sources
641
642 def find_swig(self):
643 """Return the name of the SWIG executable. On Unix, this is
644 just "swig" -- it should be in the PATH. Tries a bit harder on
645 Windows.
646 """
647 if os.name == "posix":
648 return "swig"
649 elif os.name == "nt":
650 # Look for SWIG in its standard installation directory on
651 # Windows (or so I presume!). If we find it there, great;
652 # if not, act like Unix and assume it's in the PATH.
653 for vers in ("1.3", "1.2", "1.1"):
654 fn = os.path.join("c:\\swig%s" % vers, "swig.exe")
655 if os.path.isfile(fn):
656 return fn
657 else:
658 return "swig.exe"
659 else:
660 raise DistutilsPlatformError(
661 "I don't know how to find (much less run) SWIG "
662 "on platform '%s'" % os.name
663 )
664
665 # -- Name generators -----------------------------------------------
666 # (extension names, filenames, whatever)
667 def get_ext_fullpath(self, ext_name):
668 """Returns the path of the filename for a given extension.
669
670 The file is located in `build_lib` or directly in the package
671 (inplace option).
672 """
673 fullname = self.get_ext_fullname(ext_name)
674 modpath = fullname.split('.')
675 filename = self.get_ext_filename(modpath[-1])
676
677 if not self.inplace:
678 # no further work needed
679 # returning :
680 # build_dir/package/path/filename
681 filename = os.path.join(*modpath[:-1] + [filename])
682 return os.path.join(self.build_lib, filename)
683
684 # the inplace option requires to find the package directory
685 # using the build_py command for that
686 package = '.'.join(modpath[0:-1])
687 build_py = self.get_finalized_command('build_py')
688 package_dir = os.path.abspath(build_py.get_package_dir(package))
689
690 # returning
691 # package_dir/filename
692 return os.path.join(package_dir, filename)
693
694 def get_ext_fullname(self, ext_name):
695 """Returns the fullname of a given extension name.
696
697 Adds the `package.` prefix"""
698 if self.package is None:
699 return ext_name
700 else:
701 return self.package + '.' + ext_name
702
703 def get_ext_filename(self, ext_name):
704 r"""Convert the name of an extension (eg. "foo.bar") into the name
705 of the file from which it will be loaded (eg. "foo/bar.so", or
706 "foo\bar.pyd").
707 """
708 from ..sysconfig import get_config_var
709
710 ext_path = ext_name.split('.')
711 ext_suffix = get_config_var('EXT_SUFFIX')
712 return os.path.join(*ext_path) + ext_suffix
713
714 def get_export_symbols(self, ext):
715 """Return the list of symbols that a shared extension has to
716 export. This either uses 'ext.export_symbols' or, if it's not
717 provided, "PyInit_" + module_name. Only relevant on Windows, where
718 the .pyd file (DLL) must export the module "PyInit_" function.
719 """
720 name = ext.name.split('.')[-1]
721 try:
722 # Unicode module name support as defined in PEP-489
723 # https://peps.python.org/pep-0489/#export-hook-name
724 name.encode('ascii')
725 except UnicodeEncodeError:
726 suffix = 'U_' + name.encode('punycode').replace(b'-', b'_').decode('ascii')
727 else:
728 suffix = "_" + name
729
730 initfunc_name = "PyInit" + suffix
731 if initfunc_name not in ext.export_symbols:
732 ext.export_symbols.append(initfunc_name)
733 return ext.export_symbols
734
735 def get_libraries(self, ext): # noqa: C901
736 """Return the list of libraries to link against when building a
737 shared extension. On most platforms, this is just 'ext.libraries';
738 on Windows, we add the Python library (eg. python20.dll).
739 """
740 # The python library is always needed on Windows. For MSVC, this
741 # is redundant, since the library is mentioned in a pragma in
742 # pyconfig.h that MSVC groks. The other Windows compilers all seem
743 # to need it mentioned explicitly, though, so that's what we do.
744 # Append '_d' to the python import library on debug builds.
745 if sys.platform == "win32":
746 from .._msvccompiler import MSVCCompiler
747
748 if not isinstance(self.compiler, MSVCCompiler):
749 template = "python%d%d"
750 if self.debug:
751 template = template + '_d'
752 pythonlib = template % (
753 sys.hexversion >> 24,
754 (sys.hexversion >> 16) & 0xFF,
755 )
756 # don't extend ext.libraries, it may be shared with other
757 # extensions, it is a reference to the original list
758 return ext.libraries + [pythonlib]
759 else:
760 # On Android only the main executable and LD_PRELOADs are considered
761 # to be RTLD_GLOBAL, all the dependencies of the main executable
762 # remain RTLD_LOCAL and so the shared libraries must be linked with
763 # libpython when python is built with a shared python library (issue
764 # bpo-21536).
765 # On Cygwin (and if required, other POSIX-like platforms based on
766 # Windows like MinGW) it is simply necessary that all symbols in
767 # shared libraries are resolved at link time.
768 from ..sysconfig import get_config_var
769
770 link_libpython = False
771 if get_config_var('Py_ENABLE_SHARED'):
772 # A native build on an Android device or on Cygwin
773 if hasattr(sys, 'getandroidapilevel'):
774 link_libpython = True
775 elif sys.platform == 'cygwin':
776 link_libpython = True
777 elif '_PYTHON_HOST_PLATFORM' in os.environ:
778 # We are cross-compiling for one of the relevant platforms
779 if get_config_var('ANDROID_API_LEVEL') != 0:
780 link_libpython = True
781 elif get_config_var('MACHDEP') == 'cygwin':
782 link_libpython = True
783
784 if link_libpython:
785 ldversion = get_config_var('LDVERSION')
786 return ext.libraries + ['python' + ldversion]
787
788 return ext.libraries + py37compat.pythonlib()