build_ext.py 31 KB

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