build_ext.py 32 KB

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