blob: 2ede447fede99641bc905c59142bb4da96839651 [file] [log] [blame]
Greg Warde393ddb1999-08-14 23:57:49 +00001"""distutils.command.build_ext
2
3Implements the Distutils 'build_ext' command, for building extension
Jeremy Hyltona05e2932000-06-28 14:48:01 +00004modules (currently limited to C extensions, should accommodate C++
Greg Warde393ddb1999-08-14 23:57:49 +00005extensions ASAP)."""
6
7# created 1999/08/09, Greg Ward
8
Greg Ward3ce77fd2000-03-02 01:49:45 +00009__revision__ = "$Id$"
Greg Warde393ddb1999-08-14 23:57:49 +000010
11import sys, os, string, re
12from types import *
13from distutils.core import Command
Greg Warde393ddb1999-08-14 23:57:49 +000014from distutils.errors import *
Greg Ward5ca84b82000-06-25 02:10:46 +000015from distutils.sysconfig import customize_compiler
Greg Ward52134002000-05-26 01:31:53 +000016from distutils.dep_util import newer_group
Greg Ward5322f002000-05-31 01:09:52 +000017from distutils.extension import Extension
Greg Warde393ddb1999-08-14 23:57:49 +000018
Greg Warddbb96251999-09-21 18:27:12 +000019# An extension name is just a dot-separated list of Python NAMEs (ie.
20# the same as a fully-qualified module name).
21extension_name_re = re.compile \
22 (r'^[a-zA-Z_][a-zA-Z_0-9]*(\.[a-zA-Z_][a-zA-Z_0-9]*)*$')
Greg Warde393ddb1999-08-14 23:57:49 +000023
24
Greg Ward34593812000-06-24 01:23:37 +000025def show_compilers ():
26 from distutils.ccompiler import show_compilers
27 show_compilers()
28
29
Greg Ward1993f9a2000-02-18 00:13:53 +000030class build_ext (Command):
Greg Warde393ddb1999-08-14 23:57:49 +000031
Greg Ward37bc8152000-01-30 18:34:15 +000032 description = "build C/C++ extensions (compile/link to build directory)"
33
Greg Warde393ddb1999-08-14 23:57:49 +000034 # XXX thoughts on how to deal with complex command-line options like
35 # these, i.e. how to make it so fancy_getopt can suck them off the
36 # command line and make it look like setup.py defined the appropriate
37 # lists of tuples of what-have-you.
38 # - each command needs a callback to process its command-line options
39 # - Command.__init__() needs access to its share of the whole
40 # command line (must ultimately come from
41 # Distribution.parse_command_line())
42 # - it then calls the current command class' option-parsing
43 # callback to deal with weird options like -D, which have to
44 # parse the option text and churn out some custom data
45 # structure
46 # - that data structure (in this case, a list of 2-tuples)
47 # will then be present in the command object by the time
Greg Warde01149c2000-02-18 00:35:22 +000048 # we get to finalize_options() (i.e. the constructor
Greg Warde393ddb1999-08-14 23:57:49 +000049 # takes care of both command-line and client options
Greg Warde01149c2000-02-18 00:35:22 +000050 # in between initialize_options() and finalize_options())
Greg Warde393ddb1999-08-14 23:57:49 +000051
Greg Wardbbeceea2000-02-18 00:25:39 +000052 user_options = [
Greg Warde51d69e2000-03-01 01:43:28 +000053 ('build-lib=', 'b',
Greg Wardbbeceea2000-02-18 00:25:39 +000054 "directory for compiled extension modules"),
Greg Warde51d69e2000-03-01 01:43:28 +000055 ('build-temp=', 't',
56 "directory for temporary files (build by-products)"),
57 ('inplace', 'i',
58 "ignore build-lib and put compiled extensions into the source" +
59 "directory alongside your pure Python modules"),
Greg Wardbbeceea2000-02-18 00:25:39 +000060 ('include-dirs=', 'I',
61 "list of directories to search for header files"),
62 ('define=', 'D',
63 "C preprocessor macros to define"),
64 ('undef=', 'U',
65 "C preprocessor macros to undefine"),
Greg Wardcdb20ba2000-03-26 21:45:14 +000066 ('libraries=', 'l',
Greg Wardbbeceea2000-02-18 00:25:39 +000067 "external C libraries to link with"),
68 ('library-dirs=', 'L',
69 "directories to search for external C libraries"),
70 ('rpath=', 'R',
71 "directories to search for shared C libraries at runtime"),
72 ('link-objects=', 'O',
73 "extra explicit link objects to include in the link"),
74 ('debug', 'g',
75 "compile/link with debugging information"),
Greg Wardc41d6b32000-04-10 00:19:42 +000076 ('force', 'f',
Gregory P. Smith6cd0c432000-05-12 00:34:12 +000077 "forcibly build everything (ignore file timestamps)"),
78 ('compiler=', 'c',
79 "specify the compiler type"),
Greg Wardf3bd7472000-06-27 01:37:10 +000080 ('swig-cpp', None,
81 "make SWIG create C++ files (default is C)"),
Greg Wardbbeceea2000-02-18 00:25:39 +000082 ]
Greg Ward34593812000-06-24 01:23:37 +000083
Greg Ward9d17a7a2000-06-07 03:00:06 +000084 help_options = [
85 ('help-compiler', None,
Greg Ward34593812000-06-24 01:23:37 +000086 "list available compilers", show_compilers),
Greg Ward9d17a7a2000-06-07 03:00:06 +000087 ]
Greg Warde393ddb1999-08-14 23:57:49 +000088
Greg Warde01149c2000-02-18 00:35:22 +000089 def initialize_options (self):
Greg Ward71eb8641999-09-08 02:42:30 +000090 self.extensions = None
Greg Warde51d69e2000-03-01 01:43:28 +000091 self.build_lib = None
92 self.build_temp = None
93 self.inplace = 0
Greg Ward609a5c81999-09-13 13:55:34 +000094 self.package = None
95
Greg Warde393ddb1999-08-14 23:57:49 +000096 self.include_dirs = None
97 self.define = None
98 self.undef = None
Greg Wardcdb20ba2000-03-26 21:45:14 +000099 self.libraries = None
Greg Warde393ddb1999-08-14 23:57:49 +0000100 self.library_dirs = None
101 self.rpath = None
102 self.link_objects = None
Greg Warde8c6ce42000-02-09 02:20:14 +0000103 self.debug = None
Greg Wardc41d6b32000-04-10 00:19:42 +0000104 self.force = None
Gregory P. Smith6cd0c432000-05-12 00:34:12 +0000105 self.compiler = None
Greg Wardf3bd7472000-06-27 01:37:10 +0000106 self.swig_cpp = None
Greg Warde393ddb1999-08-14 23:57:49 +0000107
Greg Warda6cb8ae1999-09-29 12:49:35 +0000108
Greg Warde01149c2000-02-18 00:35:22 +0000109 def finalize_options (self):
Greg Ward37bc8152000-01-30 18:34:15 +0000110 from distutils import sysconfig
111
Greg Warde8c6ce42000-02-09 02:20:14 +0000112 self.set_undefined_options ('build',
Greg Warde51d69e2000-03-01 01:43:28 +0000113 ('build_lib', 'build_lib'),
114 ('build_temp', 'build_temp'),
Gregory P. Smith6cd0c432000-05-12 00:34:12 +0000115 ('compiler', 'compiler'),
Greg Wardc41d6b32000-04-10 00:19:42 +0000116 ('debug', 'debug'),
117 ('force', 'force'))
Greg Warde393ddb1999-08-14 23:57:49 +0000118
Greg Ward609a5c81999-09-13 13:55:34 +0000119 if self.package is None:
Greg Warddbb96251999-09-21 18:27:12 +0000120 self.package = self.distribution.ext_package
121
122 self.extensions = self.distribution.ext_modules
123
Greg Ward609a5c81999-09-13 13:55:34 +0000124
Greg Warde393ddb1999-08-14 23:57:49 +0000125 # Make sure Python's include directories (for Python.h, config.h,
Greg Ward1561ae12000-04-14 00:50:49 +0000126 # etc.) are in the include search path.
127 py_include = sysconfig.get_python_inc()
128 plat_py_include = sysconfig.get_python_inc(plat_specific=1)
Greg Warde393ddb1999-08-14 23:57:49 +0000129 if self.include_dirs is None:
Greg Warddbb96251999-09-21 18:27:12 +0000130 self.include_dirs = self.distribution.include_dirs or []
Greg Warda6cb8ae1999-09-29 12:49:35 +0000131 if type (self.include_dirs) is StringType:
132 self.include_dirs = string.split (self.include_dirs,
133 os.pathsep)
134
Greg Ward1d16a9f2000-03-29 04:13:49 +0000135 # Put the Python "system" include dir at the end, so that
136 # any local include dirs take precedence.
137 self.include_dirs.append (py_include)
Greg Ward1561ae12000-04-14 00:50:49 +0000138 if plat_py_include != py_include:
139 self.include_dirs.append (plat_py_include)
Greg Warde393ddb1999-08-14 23:57:49 +0000140
Greg Wardcdb20ba2000-03-26 21:45:14 +0000141 if type (self.libraries) is StringType:
142 self.libraries = [self.libraries]
Greg Warddedd5b52000-03-18 15:21:03 +0000143
Greg Ward84229642000-03-31 03:50:23 +0000144 # Life is easier if we're not forever checking for None, so
145 # simplify these options to empty lists if unset
146 if self.libraries is None:
147 self.libraries = []
148 if self.library_dirs is None:
149 self.library_dirs = []
150 if self.rpath is None:
151 self.rpath = []
Greg Wardc1854672000-02-05 02:23:16 +0000152
Greg Ward84229642000-03-31 03:50:23 +0000153 # for extensions under windows use different directories
154 # for Release and Debug builds.
155 # also Python's library directory must be appended to library_dirs
156 if os.name == 'nt':
157 self.library_dirs.append (os.path.join(sys.exec_prefix, 'libs'))
Greg Wardb5937932000-06-27 01:43:24 +0000158 self.implib_dir = self.build_temp
Greg Ward84229642000-03-31 03:50:23 +0000159 if self.debug:
160 self.build_temp = os.path.join (self.build_temp, "Debug")
161 else:
162 self.build_temp = os.path.join (self.build_temp, "Release")
Greg Warde01149c2000-02-18 00:35:22 +0000163 # finalize_options ()
Greg Wardc1854672000-02-05 02:23:16 +0000164
Greg Warde393ddb1999-08-14 23:57:49 +0000165
166 def run (self):
167
Greg Ward37bc8152000-01-30 18:34:15 +0000168 from distutils.ccompiler import new_compiler
169
Greg Ward5322f002000-05-31 01:09:52 +0000170 # 'self.extensions', as supplied by setup.py, is a list of
171 # Extension instances. See the documentation for Extension (in
Greg Wardb081e182000-06-17 23:04:31 +0000172 # distutils.extension) for details.
Greg Ward5322f002000-05-31 01:09:52 +0000173 #
174 # For backwards compatibility with Distutils 0.8.2 and earlier, we
175 # also allow the 'extensions' list to be a list of tuples:
Greg Warde393ddb1999-08-14 23:57:49 +0000176 # (ext_name, build_info)
Greg Ward5322f002000-05-31 01:09:52 +0000177 # where build_info is a dictionary containing everything that
178 # Extension instances do except the name, with a few things being
179 # differently named. We convert these 2-tuples to Extension
180 # instances as needed.
Greg Warde393ddb1999-08-14 23:57:49 +0000181
Greg Ward71eb8641999-09-08 02:42:30 +0000182 if not self.extensions:
183 return
Greg Warde393ddb1999-08-14 23:57:49 +0000184
Greg Wardcdb20ba2000-03-26 21:45:14 +0000185 # If we were asked to build any C/C++ libraries, make sure that the
186 # directory where we put them is in the library search path for
187 # linking extensions.
Greg Ward4d16e0a2000-03-29 02:13:09 +0000188 if self.distribution.has_c_libraries():
Greg Ward4fb29e52000-05-27 17:27:23 +0000189 build_clib = self.get_finalized_command ('build_clib')
Greg Wardcdb20ba2000-03-26 21:45:14 +0000190 self.libraries.extend (build_clib.get_library_names() or [])
191 self.library_dirs.append (build_clib.build_clib)
192
Greg Warde393ddb1999-08-14 23:57:49 +0000193 # Setup the CCompiler object that we'll use to do all the
194 # compiling and linking
Greg Warde401e152000-06-25 02:30:15 +0000195 self.compiler = new_compiler (compiler=self.compiler,
Gregory P. Smith6cd0c432000-05-12 00:34:12 +0000196 verbose=self.verbose,
Greg Ward3c6204a1999-10-03 21:08:42 +0000197 dry_run=self.dry_run,
198 force=self.force)
Greg Ward5ca84b82000-06-25 02:10:46 +0000199 customize_compiler(self.compiler)
Greg Wardcdb20ba2000-03-26 21:45:14 +0000200
201 # And make sure that any compile/link-related options (which might
202 # come from the command-line or from the setup script) are set in
203 # that CCompiler object -- that way, they automatically apply to
204 # all compiling and linking done here.
Greg Warde393ddb1999-08-14 23:57:49 +0000205 if self.include_dirs is not None:
206 self.compiler.set_include_dirs (self.include_dirs)
207 if self.define is not None:
208 # 'define' option is a list of (name,value) tuples
209 for (name,value) in self.define:
210 self.compiler.define_macro (name, value)
211 if self.undef is not None:
212 for macro in self.undef:
213 self.compiler.undefine_macro (macro)
Greg Wardcdb20ba2000-03-26 21:45:14 +0000214 if self.libraries is not None:
215 self.compiler.set_libraries (self.libraries)
Greg Warde393ddb1999-08-14 23:57:49 +0000216 if self.library_dirs is not None:
217 self.compiler.set_library_dirs (self.library_dirs)
218 if self.rpath is not None:
219 self.compiler.set_runtime_library_dirs (self.rpath)
220 if self.link_objects is not None:
221 self.compiler.set_link_objects (self.link_objects)
Greg Ward60f64332000-03-02 01:32:21 +0000222
Greg Wardcdb20ba2000-03-26 21:45:14 +0000223 # Now actually compile and link everything.
Greg Ward4d16e0a2000-03-29 02:13:09 +0000224 self.build_extensions ()
Greg Warde393ddb1999-08-14 23:57:49 +0000225
Greg Ward4d16e0a2000-03-29 02:13:09 +0000226 # run ()
227
Greg Warde393ddb1999-08-14 23:57:49 +0000228
229 def check_extensions_list (self, extensions):
Greg Wardc1854672000-02-05 02:23:16 +0000230 """Ensure that the list of extensions (presumably provided as a
Greg Ward5322f002000-05-31 01:09:52 +0000231 command option 'extensions') is valid, i.e. it is a list of
232 Extension objects. We also support the old-style list of 2-tuples,
233 where the tuples are (ext_name, build_info), which are converted to
234 Extension instances here.
Greg Warde393ddb1999-08-14 23:57:49 +0000235
Greg Ward5322f002000-05-31 01:09:52 +0000236 Raise DistutilsSetupError if the structure is invalid anywhere;
237 just returns otherwise.
238 """
239 if type(extensions) is not ListType:
Greg Ward02a1a2b2000-04-15 22:15:07 +0000240 raise DistutilsSetupError, \
Greg Ward5322f002000-05-31 01:09:52 +0000241 "'ext_modules' option must be a list of Extension instances"
Greg Warde393ddb1999-08-14 23:57:49 +0000242
Greg Ward5322f002000-05-31 01:09:52 +0000243 for i in range(len(extensions)):
244 ext = extensions[i]
245 if isinstance(ext, Extension):
246 continue # OK! (assume type-checking done
247 # by Extension constructor)
Greg Warde393ddb1999-08-14 23:57:49 +0000248
Greg Ward5322f002000-05-31 01:09:52 +0000249 (ext_name, build_info) = ext
250 self.warn(("old-style (ext_name, build_info) tuple found in "
251 "ext_modules for extension '%s'"
252 "-- please convert to Extension instance" % ext_name))
253 if type(ext) is not TupleType and len(ext) != 2:
Greg Ward02a1a2b2000-04-15 22:15:07 +0000254 raise DistutilsSetupError, \
Greg Ward5322f002000-05-31 01:09:52 +0000255 ("each element of 'ext_modules' option must be an "
256 "Extension instance or 2-tuple")
Greg Warde393ddb1999-08-14 23:57:49 +0000257
Greg Ward5322f002000-05-31 01:09:52 +0000258 if not (type(ext_name) is StringType and
259 extension_name_re.match(ext_name)):
Greg Ward02a1a2b2000-04-15 22:15:07 +0000260 raise DistutilsSetupError, \
Greg Ward5322f002000-05-31 01:09:52 +0000261 ("first element of each tuple in 'ext_modules' "
262 "must be the extension name (a string)")
Greg Warde393ddb1999-08-14 23:57:49 +0000263
Greg Ward5322f002000-05-31 01:09:52 +0000264 if type(build_info) is not DictionaryType:
265 raise DistutilsSetupError, \
266 ("second element of each tuple in 'ext_modules' "
267 "must be a dictionary (build info)")
268
269 # OK, the (ext_name, build_info) dict is type-safe: convert it
270 # to an Extension instance.
271 ext = Extension(ext_name, build_info['sources'])
272
273 # Easy stuff: one-to-one mapping from dict elements to
274 # instance attributes.
275 for key in ('include_dirs',
276 'library_dirs',
277 'libraries',
278 'extra_objects',
279 'extra_compile_args',
280 'extra_link_args'):
281 setattr(ext, key, build_info.get(key))
282
283 # Medium-easy stuff: same syntax/semantics, different names.
284 ext.runtime_library_dirs = build_info.get('rpath')
285 ext.export_symbol_file = build_info.get('def_file')
286
287 # Non-trivial stuff: 'macros' split into 'define_macros'
288 # and 'undef_macros'.
289 macros = build_info.get('macros')
290 if macros:
291 ext.define_macros = []
292 ext.undef_macros = []
293 for macro in macros:
294 if not (type(macro) is TupleType and
Greg Ward53c1bc32000-07-27 01:21:54 +0000295 1 <= len(macro) <= 2):
Greg Ward5322f002000-05-31 01:09:52 +0000296 raise DistutilsSetupError, \
297 ("'macros' element of build info dict "
298 "must be 1- or 2-tuple")
299 if len(macro) == 1:
300 ext.undef_macros.append(macro[0])
301 elif len(macro) == 2:
302 ext.define_macros.append(macro)
303
304 extensions[i] = ext
305
306 # for extensions
Greg Warde393ddb1999-08-14 23:57:49 +0000307
308 # check_extensions_list ()
309
310
Greg Warda6cb8ae1999-09-29 12:49:35 +0000311 def get_source_files (self):
Andrew M. Kuchling23adc9f2000-07-14 13:35:07 +0000312 self.check_extensions_list(self.extensions)
Greg Warda6cb8ae1999-09-29 12:49:35 +0000313 filenames = []
314
315 # Wouldn't it be neat if we knew the names of header files too...
Greg Ward5322f002000-05-31 01:09:52 +0000316 for ext in self.extensions:
317 filenames.extend (ext.sources)
Greg Warda6cb8ae1999-09-29 12:49:35 +0000318
319 return filenames
320
321
Greg Ward4d16e0a2000-03-29 02:13:09 +0000322 def get_outputs (self):
323
324 # Sanity check the 'extensions' list -- can't assume this is being
325 # done in the same run as a 'build_extensions()' call (in fact, we
326 # can probably assume that it *isn't*!).
327 self.check_extensions_list (self.extensions)
328
329 # And build the list of output (built) filenames. Note that this
330 # ignores the 'inplace' flag, and assumes everything goes in the
331 # "build" tree.
332 outputs = []
Greg Ward5322f002000-05-31 01:09:52 +0000333 for ext in self.extensions:
334 fullname = self.get_ext_fullname (ext.name)
Greg Ward4d16e0a2000-03-29 02:13:09 +0000335 outputs.append (os.path.join (self.build_lib,
336 self.get_ext_filename(fullname)))
337 return outputs
338
339 # get_outputs ()
340
341
Greg Ward71d55832000-03-30 19:47:22 +0000342 def build_extensions (self):
Greg Warde393ddb1999-08-14 23:57:49 +0000343
Greg Ward4d16e0a2000-03-29 02:13:09 +0000344 # First, sanity-check the 'extensions' list
345 self.check_extensions_list (self.extensions)
346
Greg Ward5322f002000-05-31 01:09:52 +0000347 for ext in self.extensions:
348 sources = ext.sources
Greg Ward48697d91999-12-12 17:01:01 +0000349 if sources is None or type (sources) not in (ListType, TupleType):
Greg Ward02a1a2b2000-04-15 22:15:07 +0000350 raise DistutilsSetupError, \
Greg Wardc1854672000-02-05 02:23:16 +0000351 ("in 'ext_modules' option (extension '%s'), " +
Greg Ward7f0fb0b2000-02-03 23:07:54 +0000352 "'sources' must be present and must be " +
Greg Ward5322f002000-05-31 01:09:52 +0000353 "a list of source filenames") % ext.name
Greg Ward48697d91999-12-12 17:01:01 +0000354 sources = list (sources)
Greg Warda6cb8ae1999-09-29 12:49:35 +0000355
Greg Ward5322f002000-05-31 01:09:52 +0000356 fullname = self.get_ext_fullname (ext.name)
Greg Ward52134002000-05-26 01:31:53 +0000357 if self.inplace:
358 # ignore build-lib -- put the compiled extension into
359 # the source tree along with pure Python modules
360
361 modpath = string.split (fullname, '.')
362 package = string.join (modpath[0:-1], '.')
363 base = modpath[-1]
364
Greg Ward4fb29e52000-05-27 17:27:23 +0000365 build_py = self.get_finalized_command ('build_py')
Greg Ward52134002000-05-26 01:31:53 +0000366 package_dir = build_py.get_package_dir (package)
367 ext_filename = os.path.join (package_dir,
368 self.get_ext_filename(base))
369 else:
370 ext_filename = os.path.join (self.build_lib,
371 self.get_ext_filename(fullname))
372
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000373 if not (self.force or newer_group(sources, ext_filename, 'newer')):
374 self.announce ("skipping '%s' extension (up-to-date)" %
Greg Ward5322f002000-05-31 01:09:52 +0000375 ext.name)
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000376 continue # 'for' loop over all extensions
377 else:
378 self.announce ("building '%s' extension" % ext.name)
Greg Ward7f0fb0b2000-02-03 23:07:54 +0000379
Greg Wardffcaf2d2000-06-24 00:19:35 +0000380 # First, scan the sources for SWIG definition files (.i), run
381 # SWIG on 'em to create .c files, and modify the sources list
382 # accordingly.
383 sources = self.swig_sources(sources)
384
385 # Next, compile the source code to object files.
Greg Ward5322f002000-05-31 01:09:52 +0000386
387 # XXX not honouring 'define_macros' or 'undef_macros' -- the
Jeremy Hyltona05e2932000-06-28 14:48:01 +0000388 # CCompiler API needs to change to accommodate this, and I
Greg Ward5322f002000-05-31 01:09:52 +0000389 # want to do one thing at a time!
390
391 # Two possible sources for extra compiler arguments:
392 # - 'extra_compile_args' in Extension object
393 # - CFLAGS environment variable (not particularly
394 # elegant, but people seem to expect it and I
395 # guess it's useful)
396 # The environment variable should take precedence, and
397 # any sensible compiler will give precendence to later
398 # command line args. Hence we combine them in order:
Greg Warde401e152000-06-25 02:30:15 +0000399 extra_args = ext.extra_compile_args or []
Greg Ward5322f002000-05-31 01:09:52 +0000400
401 # XXX and if we support CFLAGS, why not CC (compiler
402 # executable), CPPFLAGS (pre-processor options), and LDFLAGS
403 # (linker options) too?
404 # XXX should we use shlex to properly parse CFLAGS?
405
Gregory P. Smithb5ebe5d2000-05-13 01:52:14 +0000406 if os.environ.has_key('CFLAGS'):
Greg Ward5322f002000-05-31 01:09:52 +0000407 extra_args.extend(string.split(os.environ['CFLAGS']))
Gregory P. Smithb5ebe5d2000-05-13 01:52:14 +0000408
Greg Warde51d69e2000-03-01 01:43:28 +0000409 objects = self.compiler.compile (sources,
410 output_dir=self.build_temp,
Greg Ward5322f002000-05-31 01:09:52 +0000411 #macros=macros,
412 include_dirs=ext.include_dirs,
Greg Ward18856b82000-05-06 13:12:59 +0000413 debug=self.debug,
414 extra_postargs=extra_args)
Greg Warde393ddb1999-08-14 23:57:49 +0000415
Greg Warda6cb8ae1999-09-29 12:49:35 +0000416 # Now link the object files together into a "shared object" --
417 # of course, first we have to figure out all the other things
418 # that go into the mix.
Greg Ward5322f002000-05-31 01:09:52 +0000419 if ext.extra_objects:
420 objects.extend (ext.extra_objects)
Greg Warde401e152000-06-25 02:30:15 +0000421 extra_args = ext.extra_link_args or []
Greg Ward968d8832000-02-10 02:17:06 +0000422
Greg Wardf46a6882000-06-25 02:23:11 +0000423 # Bunch of fixing-up we have to do for Microsoft's linker.
424 if self.compiler.compiler_type == 'msvc':
425 self.msvc_prelink_hack(sources, ext, extra_args)
Greg Warda6cb8ae1999-09-29 12:49:35 +0000426
Greg Ward5322f002000-05-31 01:09:52 +0000427 self.compiler.link_shared_object (
428 objects, ext_filename,
429 libraries=ext.libraries,
430 library_dirs=ext.library_dirs,
431 runtime_library_dirs=ext.runtime_library_dirs,
432 extra_postargs=extra_args,
Greg Wardcc623a22000-06-28 01:29:37 +0000433 debug=self.debug,
434 build_temp=self.build_temp)
Greg Warde393ddb1999-08-14 23:57:49 +0000435
436 # build_extensions ()
437
438
Greg Wardffcaf2d2000-06-24 00:19:35 +0000439 def swig_sources (self, sources):
440
441 """Walk the list of source files in 'sources', looking for SWIG
442 interface (.i) files. Run SWIG on all that are found, and
443 return a modified 'sources' list with SWIG source files replaced
444 by the generated C (or C++) files.
445 """
446
447 new_sources = []
448 swig_sources = []
449 swig_targets = {}
450
Greg Wardf3bd7472000-06-27 01:37:10 +0000451 # XXX this drops generated C/C++ files into the source tree, which
Greg Wardffcaf2d2000-06-24 00:19:35 +0000452 # is fine for developers who want to distribute the generated
453 # source -- but there should be an option to put SWIG output in
454 # the temp dir.
455
Greg Wardf3bd7472000-06-27 01:37:10 +0000456 if self.swig_cpp:
457 target_ext = '.cpp'
458 else:
459 target_ext = '.c'
460
Greg Wardffcaf2d2000-06-24 00:19:35 +0000461 for source in sources:
462 (base, ext) = os.path.splitext(source)
Greg Ward5ca84b82000-06-25 02:10:46 +0000463 if ext == ".i": # SWIG interface file
Greg Wardf3bd7472000-06-27 01:37:10 +0000464 new_sources.append(base + target_ext)
Greg Ward5ca84b82000-06-25 02:10:46 +0000465 swig_sources.append(source)
Greg Wardffcaf2d2000-06-24 00:19:35 +0000466 swig_targets[source] = new_sources[-1]
467 else:
468 new_sources.append(source)
469
Greg Ward5ca84b82000-06-25 02:10:46 +0000470 if not swig_sources:
Greg Wardffcaf2d2000-06-24 00:19:35 +0000471 return new_sources
472
473 swig = self.find_swig()
Greg Wardf3bd7472000-06-27 01:37:10 +0000474 swig_cmd = [swig, "-python", "-dnone", "-ISWIG"]
475 if self.swig_cpp:
476 swig_cmd.append ("-c++")
Greg Wardffcaf2d2000-06-24 00:19:35 +0000477
478 for source in swig_sources:
Greg Wardf3bd7472000-06-27 01:37:10 +0000479 target = swig_targets[source]
480 self.announce ("swigging %s to %s" % (source, target))
481 self.spawn(swig_cmd + ["-o", target, source])
Greg Wardffcaf2d2000-06-24 00:19:35 +0000482
483 return new_sources
484
485 # swig_sources ()
486
487 def find_swig (self):
488 """Return the name of the SWIG executable. On Unix, this is
489 just "swig" -- it should be in the PATH. Tries a bit harder on
490 Windows.
491 """
492
493 if os.name == "posix":
494 return "swig"
495 elif os.name == "nt":
496
497 # Look for SWIG in its standard installation directory on
498 # Windows (or so I presume!). If we find it there, great;
499 # if not, act like Unix and assume it's in the PATH.
500 for vers in ("1.3", "1.2", "1.1"):
501 fn = os.path.join("c:\\swig%s" % vers, "swig.exe")
502 if os.path.isfile (fn):
503 return fn
504 else:
505 return "swig.exe"
506
507 else:
508 raise DistutilsPlatformError, \
509 ("I don't know how to find (much less run) SWIG "
510 "on platform '%s'") % os.name
511
512 # find_swig ()
513
514
Greg Wardf46a6882000-06-25 02:23:11 +0000515 # -- Hooks 'n hacks ------------------------------------------------
Greg Wardb081e182000-06-17 23:04:31 +0000516
Greg Wardf46a6882000-06-25 02:23:11 +0000517 def msvc_prelink_hack (self, sources, ext, extra_args):
Greg Wardb081e182000-06-17 23:04:31 +0000518
519 # XXX this is a kludge! Knowledge of specific compilers or
520 # platforms really doesn't belong here; in an ideal world, the
521 # CCompiler interface would provide access to everything in a
522 # compiler/linker system needs to build Python extensions, and
523 # we would just do everything nicely and cleanly through that
524 # interface. However, this is a not an ideal world and the
525 # CCompiler interface doesn't handle absolutely everything.
526 # Thus, kludges like this slip in occasionally. (This is no
527 # excuse for committing more platform- and compiler-specific
528 # kludges; they are to be avoided if possible!)
Greg Wardb081e182000-06-17 23:04:31 +0000529
Greg Wardf46a6882000-06-25 02:23:11 +0000530 def_file = ext.export_symbol_file
Greg Wardb081e182000-06-17 23:04:31 +0000531
Greg Wardf46a6882000-06-25 02:23:11 +0000532 if def_file is not None:
533 extra_args.append ('/DEF:' + def_file)
534 else:
535 modname = string.split (ext.name, '.')[-1]
536 extra_args.append('/export:init%s' % modname)
Greg Wardb081e182000-06-17 23:04:31 +0000537
Greg Wardf3bd7472000-06-27 01:37:10 +0000538 # The MSVC linker generates .lib and .exp files, which cannot be
539 # suppressed by any linker switches. The .lib files may even be
540 # needed! Make sure they are generated in the temporary build
541 # directory. Since they have different names for debug and release
542 # builds, they can go into the same directory.
Greg Wardf46a6882000-06-25 02:23:11 +0000543 implib_file = os.path.join (
Greg Wardb5937932000-06-27 01:43:24 +0000544 self.implib_dir,
Greg Wardf46a6882000-06-25 02:23:11 +0000545 self.get_ext_libname (ext.name))
546 extra_args.append ('/IMPLIB:' + implib_file)
547 self.mkpath (os.path.dirname (implib_file))
548
549 # msvc_prelink_hack ()
Greg Wardb081e182000-06-17 23:04:31 +0000550
551
552 # -- Name generators -----------------------------------------------
553 # (extension names, filenames, whatever)
554
Greg Warde51d69e2000-03-01 01:43:28 +0000555 def get_ext_fullname (self, ext_name):
556 if self.package is None:
557 return ext_name
558 else:
559 return self.package + '.' + ext_name
560
561 def get_ext_filename (self, ext_name):
Greg Wardb081e182000-06-17 23:04:31 +0000562 """Convert the name of an extension (eg. "foo.bar") into the name
563 of the file from which it will be loaded (eg. "foo/bar.so", or
564 "foo\bar.pyd").
565 """
566
Greg Ward37bc8152000-01-30 18:34:15 +0000567 from distutils import sysconfig
Greg Warddbb96251999-09-21 18:27:12 +0000568 ext_path = string.split (ext_name, '.')
Greg Ward84229642000-03-31 03:50:23 +0000569 # extensions in debug_mode are named 'module_d.pyd' under windows
570 if os.name == 'nt' and self.debug:
571 return apply (os.path.join, ext_path) + '_d' + sysconfig.SO
Greg Ward37bc8152000-01-30 18:34:15 +0000572 return apply (os.path.join, ext_path) + sysconfig.SO
Greg Warde393ddb1999-08-14 23:57:49 +0000573
Greg Ward84229642000-03-31 03:50:23 +0000574 def get_ext_libname (self, ext_name):
575 # create a filename for the (unneeded) lib-file.
576 # extensions in debug_mode are named 'module_d.pyd' under windows
577 ext_path = string.split (ext_name, '.')
578 if os.name == 'nt' and self.debug:
579 return apply (os.path.join, ext_path) + '_d.lib'
580 return apply (os.path.join, ext_path) + '.lib'
581
Greg Wardfcd974e2000-05-25 01:10:04 +0000582# class build_ext