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