blob: 7765faf88bdc0b7d5ab59985fe184b2af121e1fc [file] [log] [blame]
Greg Ward170bdc01999-07-10 02:04:22 +00001"""distutils.unixccompiler
2
3Contains the UnixCCompiler class, a subclass of CCompiler that handles
4the "typical" Unix-style command-line C compiler:
5 * macros defined with -Dname[=value]
6 * macros undefined with -Uname
7 * include search directories specified with -Idir
8 * libraries specified with -lllib
9 * library search directories specified with -Ldir
10 * compile handled by 'cc' (or similar) executable with -c option:
11 compiles .c to .o
12 * link static library handled by 'ar' command (possibly with 'ranlib')
13 * link shared library handled by 'cc -shared'
14"""
15
16# created 1999/07/05, Greg Ward
17
Greg Ward3ce77fd2000-03-02 01:49:45 +000018__revision__ = "$Id$"
Greg Ward170bdc01999-07-10 02:04:22 +000019
Greg Ward8037cb11999-09-13 03:12:53 +000020import string, re, os
Greg Ward170bdc01999-07-10 02:04:22 +000021from types import *
Greg Ward8037cb11999-09-13 03:12:53 +000022from copy import copy
Greg Ward170bdc01999-07-10 02:04:22 +000023from sysconfig import \
24 CC, CCSHARED, CFLAGS, OPT, LDSHARED, LDFLAGS, RANLIB, AR, SO
Greg Wardc2941131999-09-08 02:32:19 +000025from ccompiler import CCompiler, gen_preprocess_options, gen_lib_options
Greg Ward8037cb11999-09-13 03:12:53 +000026from util import move_file, newer_pairwise, newer_group
Greg Ward170bdc01999-07-10 02:04:22 +000027
28# XXX Things not currently handled:
29# * optimization/debug/warning flags; we just use whatever's in Python's
30# Makefile and live with it. Is this adequate? If not, we might
31# have to have a bunch of subclasses GNUCCompiler, SGICCompiler,
32# SunCCompiler, and I suspect down that road lies madness.
33# * even if we don't know a warning flag from an optimization flag,
34# we need some way for outsiders to feed preprocessor/compiler/linker
35# flags in to us -- eg. a sysadmin might want to mandate certain flags
36# via a site config file, or a user might want to set something for
37# compiling this module distribution only via the setup.py command
38# line, whatever. As long as these options come from something on the
39# current system, they can be as system-dependent as they like, and we
40# should just happily stuff them into the preprocessor/compiler/linker
41# options and carry on.
42
43
44class UnixCCompiler (CCompiler):
45
Greg Ward5e717441999-08-14 23:53:53 +000046 # XXX perhaps there should really be *three* kinds of include
47 # directories: those built in to the preprocessor, those from Python's
48 # Makefiles, and those supplied to {add,set}_include_dirs(). Currently
49 # we make no distinction between the latter two at this point; it's all
50 # up to the client class to select the include directories to use above
51 # and beyond the compiler's defaults. That is, both the Python include
52 # directories and any module- or package-specific include directories
53 # are specified via {add,set}_include_dirs(), and there's no way to
54 # distinguish them. This might be a bug.
Greg Ward65f4a3b1999-08-29 18:23:32 +000055
Greg Ward0e3530b1999-09-29 12:22:50 +000056 compiler_type = 'unix'
57
Greg Ward65f4a3b1999-08-29 18:23:32 +000058 _obj_ext = '.o'
59 _exe_ext = ''
60 _shared_lib_ext = SO
61 _static_lib_ext = '.a'
Greg Wardc9f31872000-01-09 22:47:53 +000062
63 # Command to create a static library: seems to be pretty consistent
64 # across the major Unices. Might have to move down into the
65 # constructor if we need platform-specific guesswork.
66 archiver = "ar"
67 archiver_options = "-cr"
68
69
Greg Ward5e717441999-08-14 23:53:53 +000070 def __init__ (self,
71 verbose=0,
Greg Ward4fecfce1999-10-03 20:45:33 +000072 dry_run=0,
73 force=0):
Greg Ward170bdc01999-07-10 02:04:22 +000074
Greg Ward4fecfce1999-10-03 20:45:33 +000075 CCompiler.__init__ (self, verbose, dry_run, force)
Greg Ward170bdc01999-07-10 02:04:22 +000076
77 self.preprocess_options = None
78 self.compile_options = None
79
Greg Ward5e717441999-08-14 23:53:53 +000080 # Munge CC and OPT together in case there are flags stuck in CC.
81 # Note that using these variables from sysconfig immediately makes
82 # this module specific to building Python extensions and
83 # inappropriate as a general-purpose C compiler front-end. So sue
84 # me. Note also that we use OPT rather than CFLAGS, because CFLAGS
85 # is the flags used to compile Python itself -- not only are there
86 # -I options in there, they are the *wrong* -I options. We'll
87 # leave selection of include directories up to the class using
88 # UnixCCompiler!
89
Greg Ward170bdc01999-07-10 02:04:22 +000090 (self.cc, self.ccflags) = \
91 _split_command (CC + ' ' + OPT)
92 self.ccflags_shared = string.split (CCSHARED)
93
94 (self.ld_shared, self.ldflags_shared) = \
95 _split_command (LDSHARED)
96
Greg Wardc9f31872000-01-09 22:47:53 +000097 self.ld_exec = self.cc
98
Greg Ward170bdc01999-07-10 02:04:22 +000099
100 def compile (self,
101 sources,
Greg Ward8037cb11999-09-13 03:12:53 +0000102 output_dir=None,
Greg Ward49ffce12000-03-02 01:21:54 +0000103 keep_dir=0,
Greg Ward5e717441999-08-14 23:53:53 +0000104 macros=None,
Greg Ward04d78321999-12-12 16:57:47 +0000105 include_dirs=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000106 debug=0,
Greg Ward0e3530b1999-09-29 12:22:50 +0000107 extra_preargs=None,
108 extra_postargs=None):
Greg Ward5e717441999-08-14 23:53:53 +0000109
Greg Ward10ca82b2000-02-10 02:51:32 +0000110 if type (output_dir) not in (StringType, NoneType):
111 raise TypeError, "'output_dir' must be a string or None"
Greg Ward8037cb11999-09-13 03:12:53 +0000112 if output_dir is None:
113 output_dir = self.output_dir
Greg Ward5e717441999-08-14 23:53:53 +0000114 if macros is None:
115 macros = []
Greg Ward04d78321999-12-12 16:57:47 +0000116 if include_dirs is None:
117 include_dirs = []
Greg Ward170bdc01999-07-10 02:04:22 +0000118
119 if type (macros) is not ListType:
120 raise TypeError, \
121 "'macros' (if supplied) must be a list of tuples"
Greg Ward04d78321999-12-12 16:57:47 +0000122 if type (include_dirs) not in (ListType, TupleType):
Greg Ward170bdc01999-07-10 02:04:22 +0000123 raise TypeError, \
Greg Ward04d78321999-12-12 16:57:47 +0000124 "'include_dirs' (if supplied) must be a list of strings"
125 include_dirs = list (include_dirs)
Greg Ward170bdc01999-07-10 02:04:22 +0000126
Greg Wardc2941131999-09-08 02:32:19 +0000127 pp_opts = gen_preprocess_options (self.macros + macros,
Greg Ward04d78321999-12-12 16:57:47 +0000128 self.include_dirs + include_dirs)
Greg Ward170bdc01999-07-10 02:04:22 +0000129
Greg Ward8037cb11999-09-13 03:12:53 +0000130 # So we can mangle 'sources' without hurting the caller's data
131 orig_sources = sources
132 sources = copy (sources)
Greg Ward170bdc01999-07-10 02:04:22 +0000133
Greg Ward8037cb11999-09-13 03:12:53 +0000134 # Get the list of expected output (object) files and drop files we
135 # don't have to recompile. (Simplistic check -- we just compare the
136 # source and object file, no deep dependency checking involving
137 # header files. Hmmm.)
Greg Ward49ffce12000-03-02 01:21:54 +0000138 objects = self.object_filenames (sources,
139 output_dir=output_dir,
140 keep_dir=keep_dir)
141 all_objects = copy (objects) # preserve full list to return
142
Greg Ward4fecfce1999-10-03 20:45:33 +0000143 if not self.force:
144 skipped = newer_pairwise (sources, objects)
145 for skipped_pair in skipped:
146 self.announce ("skipping %s (%s up-to-date)" % skipped_pair)
Greg Ward8037cb11999-09-13 03:12:53 +0000147
Greg Wardef6f5152000-02-03 23:07:19 +0000148 # Build list of (source,object) tuples for convenience
149 srcobj = []
150 for i in range (len (sources)):
151 srcobj.append ((sources[i], objects[i]))
Greg Ward8037cb11999-09-13 03:12:53 +0000152
Greg Wardef6f5152000-02-03 23:07:19 +0000153 # Compile all source files that weren't eliminated by
154 # 'newer_pairwise()'.
155 # XXX use of ccflags_shared means we're blithely assuming
156 # that we're compiling for inclusion in a shared object!
157 # (will have to fix this when I add the ability to build a
158 # new Python)
159 cc_args = ['-c'] + pp_opts + self.ccflags + self.ccflags_shared
Greg Wardba233fb2000-02-09 02:17:00 +0000160 if debug:
161 cc_args[:0] = ['-g']
Greg Wardef6f5152000-02-03 23:07:19 +0000162 if extra_preargs:
163 cc_args[:0] = extra_preargs
164 if extra_postargs is None:
165 extra_postargs = []
Greg Ward8037cb11999-09-13 03:12:53 +0000166
Greg Wardf1146572000-03-01 14:43:49 +0000167 if output_dir is not None:
168 self.mkpath (output_dir)
Greg Wardef6f5152000-02-03 23:07:19 +0000169 for (source,object) in srcobj:
170 self.spawn ([self.cc] + cc_args +
171 [source, '-o', object] +
172 extra_postargs)
Greg Ward8037cb11999-09-13 03:12:53 +0000173
174 # Have to re-fetch list of object filenames, because we want to
175 # return *all* of them, including those that weren't recompiled on
176 # this call!
Greg Ward49ffce12000-03-02 01:21:54 +0000177 return all_objects
Greg Ward170bdc01999-07-10 02:04:22 +0000178
Greg Wardc9f31872000-01-09 22:47:53 +0000179
180 def _fix_link_args (self, output_dir, libraries, library_dirs):
181 """Fixes up the arguments supplied to the 'link_*' methods:
182 if output_dir is None, use self.output_dir; ensure that
183 libraries and library_dirs are both lists (could be None or
184 tuples on input -- both are converted to lists). Return
185 a tuple of the three input arguments."""
186
187 if output_dir is None:
188 output_dir = self.output_dir
189 if libraries is None:
190 libraries = []
191 if library_dirs is None:
192 library_dirs = []
193
194 if type (libraries) not in (ListType, TupleType):
195 raise TypeError, \
196 "'libraries' (if supplied) must be a list of strings"
197 if type (library_dirs) not in (ListType, TupleType):
198 raise TypeError, \
199 "'library_dirs' (if supplied) must be a list of strings"
200 libraries = list (libraries)
201 library_dirs = list (library_dirs)
202
203 return (output_dir, libraries, library_dirs)
204
205
206 def link_static_lib (self,
207 objects,
208 output_libname,
Greg Wardba233fb2000-02-09 02:17:00 +0000209 output_dir=None,
210 debug=0):
Greg Wardc9f31872000-01-09 22:47:53 +0000211
212 if type (objects) not in (ListType, TupleType):
213 raise TypeError, \
214 "'objects' must be a list or tuple of strings"
215 objects = list (objects)
216
Greg Ward10ca82b2000-02-10 02:51:32 +0000217 if type (output_dir) not in (StringType, NoneType):
218 raise TypeError, "'output_dir' must be a string or None"
Greg Wardc9f31872000-01-09 22:47:53 +0000219 if output_dir is None:
220 output_dir = self.output_dir
221
222 output_filename = self.library_filename (output_libname)
223 if output_dir is not None:
224 output_filename = os.path.join (output_dir, output_filename)
225
226 # Check timestamps: if any of the object files are newer than
227 # the library file, *or* if "force" is true, then we'll
228 # recreate the library.
229 if not self.force:
230 if self.dry_run:
231 newer = newer_group (objects, output_filename, missing='newer')
232 else:
233 newer = newer_group (objects, output_filename)
234
235 if self.force or newer:
Greg Wardf1146572000-03-01 14:43:49 +0000236 self.mkpath (os.path.dirname (output_filename))
Greg Wardc9f31872000-01-09 22:47:53 +0000237 self.spawn ([self.archiver,
238 self.archiver_options,
239 output_filename] +
240 objects)
241 else:
242 self.announce ("skipping %s (up-to-date)" % output_filename)
243
244 # link_static_lib ()
245
Greg Ward170bdc01999-07-10 02:04:22 +0000246
247 def link_shared_lib (self,
248 objects,
249 output_libname,
Greg Ward8037cb11999-09-13 03:12:53 +0000250 output_dir=None,
Greg Ward170bdc01999-07-10 02:04:22 +0000251 libraries=None,
Greg Ward65f4a3b1999-08-29 18:23:32 +0000252 library_dirs=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000253 debug=0,
Greg Ward0e3530b1999-09-29 12:22:50 +0000254 extra_preargs=None,
255 extra_postargs=None):
Greg Ward170bdc01999-07-10 02:04:22 +0000256 # XXX should we sanity check the library name? (eg. no
257 # slashes)
Greg Ward8037cb11999-09-13 03:12:53 +0000258 self.link_shared_object (
259 objects,
260 "lib%s%s" % (output_libname, self._shared_lib_ext),
261 output_dir,
262 libraries,
263 library_dirs,
Greg Wardba233fb2000-02-09 02:17:00 +0000264 debug,
Greg Ward0e3530b1999-09-29 12:22:50 +0000265 extra_preargs,
266 extra_postargs)
Greg Ward8037cb11999-09-13 03:12:53 +0000267
Greg Ward170bdc01999-07-10 02:04:22 +0000268
269 def link_shared_object (self,
270 objects,
271 output_filename,
Greg Ward8037cb11999-09-13 03:12:53 +0000272 output_dir=None,
Greg Ward5e717441999-08-14 23:53:53 +0000273 libraries=None,
Greg Ward65f4a3b1999-08-29 18:23:32 +0000274 library_dirs=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000275 debug=0,
Greg Ward0e3530b1999-09-29 12:22:50 +0000276 extra_preargs=None,
277 extra_postargs=None):
Greg Ward5e717441999-08-14 23:53:53 +0000278
Greg Wardc9f31872000-01-09 22:47:53 +0000279 (output_dir, libraries, library_dirs) = \
280 self._fix_link_args (output_dir, libraries, library_dirs)
Greg Ward04d78321999-12-12 16:57:47 +0000281
Greg Ward4fecfce1999-10-03 20:45:33 +0000282 lib_opts = gen_lib_options (self,
283 self.library_dirs + library_dirs,
284 self.libraries + libraries)
Greg Ward10ca82b2000-02-10 02:51:32 +0000285 if type (output_dir) not in (StringType, NoneType):
286 raise TypeError, "'output_dir' must be a string or None"
Greg Ward8037cb11999-09-13 03:12:53 +0000287 if output_dir is not None:
288 output_filename = os.path.join (output_dir, output_filename)
Greg Ward170bdc01999-07-10 02:04:22 +0000289
Greg Ward8037cb11999-09-13 03:12:53 +0000290 # If any of the input object files are newer than the output shared
291 # object, relink. Again, this is a simplistic dependency check:
292 # doesn't look at any of the libraries we might be linking with.
Greg Wardc9f31872000-01-09 22:47:53 +0000293
Greg Ward4fecfce1999-10-03 20:45:33 +0000294 if not self.force:
Greg Wardc9f31872000-01-09 22:47:53 +0000295 if self.dry_run:
296 newer = newer_group (objects, output_filename, missing='newer')
297 else:
Greg Ward4fecfce1999-10-03 20:45:33 +0000298 newer = newer_group (objects, output_filename)
Greg Wardb116e451999-09-21 18:36:15 +0000299
Greg Ward4fecfce1999-10-03 20:45:33 +0000300 if self.force or newer:
301 ld_args = self.ldflags_shared + objects + \
302 lib_opts + ['-o', output_filename]
Greg Wardba233fb2000-02-09 02:17:00 +0000303 if debug:
304 ld_args[:0] = ['-g']
Greg Ward0e3530b1999-09-29 12:22:50 +0000305 if extra_preargs:
306 ld_args[:0] = extra_preargs
307 if extra_postargs:
308 ld_args.extend (extra_postargs)
Greg Wardf1146572000-03-01 14:43:49 +0000309 self.mkpath (os.path.dirname (output_filename))
Greg Ward8037cb11999-09-13 03:12:53 +0000310 self.spawn ([self.ld_shared] + ld_args)
311 else:
312 self.announce ("skipping %s (up-to-date)" % output_filename)
Greg Ward5e717441999-08-14 23:53:53 +0000313
Greg Ward4fecfce1999-10-03 20:45:33 +0000314 # link_shared_object ()
315
316
Greg Wardc9f31872000-01-09 22:47:53 +0000317 def link_executable (self,
318 objects,
319 output_progname,
320 output_dir=None,
321 libraries=None,
322 library_dirs=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000323 debug=0,
Greg Wardc9f31872000-01-09 22:47:53 +0000324 extra_preargs=None,
325 extra_postargs=None):
326
327 (output_dir, libraries, library_dirs) = \
328 self._fix_link_args (output_dir, libraries, library_dirs)
329
330 lib_opts = gen_lib_options (self,
331 self.library_dirs + library_dirs,
332 self.libraries + libraries)
333 output_filename = output_progname # Unix-ism!
334 if output_dir is not None:
335 output_filename = os.path.join (output_dir, output_filename)
336
337 # Same ol' simplistic-but-still-useful dependency check.
338 if not self.force:
339 if self.dry_run:
340 newer = newer_group (objects, output_filename, missing='newer')
341 else:
342 newer = newer_group (objects, output_filename)
343
344 if self.force or newer:
345 ld_args = objects + lib_opts + ['-o', output_filename]
Greg Wardba233fb2000-02-09 02:17:00 +0000346 if debug:
347 ld_args[:0] = ['-g']
Greg Wardc9f31872000-01-09 22:47:53 +0000348 if extra_preargs:
349 ld_args[:0] = extra_preargs
350 if extra_postargs:
351 ld_args.extend (extra_postargs)
Greg Wardf1146572000-03-01 14:43:49 +0000352 self.mkpath (os.path.dirname (output_filename))
Greg Wardc9f31872000-01-09 22:47:53 +0000353 self.spawn ([self.ld_exec] + ld_args)
354 else:
355 self.announce ("skipping %s (up-to-date)" % output_filename)
356
357 # link_executable ()
358
359
Greg Ward4fecfce1999-10-03 20:45:33 +0000360 # -- Filename-mangling (etc.) methods ------------------------------
Greg Ward5e717441999-08-14 23:53:53 +0000361
Greg Wardc9f31872000-01-09 22:47:53 +0000362 def object_filenames (self, source_filenames,
363 keep_dir=0, output_dir=None):
Greg Ward5e717441999-08-14 23:53:53 +0000364 outnames = []
365 for inname in source_filenames:
Greg Ward8037cb11999-09-13 03:12:53 +0000366 outname = re.sub (r'\.(c|C|cc|cxx|cpp)$', self._obj_ext, inname)
Greg Wardc9f31872000-01-09 22:47:53 +0000367 if not keep_dir:
368 outname = os.path.basename (outname)
Greg Ward8037cb11999-09-13 03:12:53 +0000369 if output_dir is not None:
370 outname = os.path.join (output_dir, outname)
371 outnames.append (outname)
Greg Ward5e717441999-08-14 23:53:53 +0000372 return outnames
373
Greg Wardc9f31872000-01-09 22:47:53 +0000374 def shared_object_filename (self, source_filename,
375 keep_dir=0, output_dir=None):
Greg Ward8037cb11999-09-13 03:12:53 +0000376 outname = re.sub (r'\.(c|C|cc|cxx|cpp)$', self._shared_lib_ext)
Greg Wardc9f31872000-01-09 22:47:53 +0000377 if not keep_dir:
378 outname = os.path.basename (outname)
Greg Ward8037cb11999-09-13 03:12:53 +0000379 if output_dir is not None:
380 outname = os.path.join (output_dir, outname)
381 return outname
Greg Ward5e717441999-08-14 23:53:53 +0000382
Greg Ward4fecfce1999-10-03 20:45:33 +0000383
Greg Ward5e717441999-08-14 23:53:53 +0000384 def library_filename (self, libname):
Greg Wardc9f31872000-01-09 22:47:53 +0000385 (dirname, basename) = os.path.split (libname)
386 return os.path.join (dirname,
387 "lib%s%s" % (basename, self._static_lib_ext))
Greg Ward5e717441999-08-14 23:53:53 +0000388
389 def shared_library_filename (self, libname):
Greg Wardc9f31872000-01-09 22:47:53 +0000390 (dirname, basename) = os.path.split (libname)
391 return os.path.join (dirname,
392 "lib%s%s" % (basename, self._shared_lib_ext))
Greg Ward4fecfce1999-10-03 20:45:33 +0000393
394
395 def library_dir_option (self, dir):
396 return "-L" + dir
397
398 def library_option (self, lib):
399 return "-l" + lib
400
401
402 def find_library_file (self, dirs, lib):
403
404 for dir in dirs:
405 shared = os.path.join (dir, self.shared_library_filename (lib))
406 static = os.path.join (dir, self.library_filename (lib))
407
408 # We're second-guessing the linker here, with not much hard
409 # data to go on: GCC seems to prefer the shared library, so I'm
410 # assuming that *all* Unix C compilers do. And of course I'm
411 # ignoring even GCC's "-static" option. So sue me.
412 if os.path.exists (shared):
413 return shared
414 elif os.path.exists (static):
415 return static
416
417 else:
418 # Oops, didn't find it in *any* of 'dirs'
419 return None
420
421 # find_library_file ()
Greg Ward65f4a3b1999-08-29 18:23:32 +0000422
Greg Ward170bdc01999-07-10 02:04:22 +0000423# class UnixCCompiler
424
425
426def _split_command (cmd):
427 """Split a command string up into the progam to run (a string) and
428 the list of arguments; return them as (cmd, arglist)."""
429 args = string.split (cmd)
430 return (args[0], args[1:])