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