blob: 770a543c7568a1e49b206d9df01ad61310b38077 [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
18__rcsid__ = "$Id$"
19
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 Ward5e717441999-08-14 23:53:53 +0000103 macros=None,
Greg Ward04d78321999-12-12 16:57:47 +0000104 include_dirs=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000105 debug=0,
Greg Ward0e3530b1999-09-29 12:22:50 +0000106 extra_preargs=None,
107 extra_postargs=None):
Greg Ward5e717441999-08-14 23:53:53 +0000108
Greg Ward10ca82b2000-02-10 02:51:32 +0000109 if type (output_dir) not in (StringType, NoneType):
110 raise TypeError, "'output_dir' must be a string or None"
Greg Ward8037cb11999-09-13 03:12:53 +0000111 if output_dir is None:
112 output_dir = self.output_dir
Greg Ward5e717441999-08-14 23:53:53 +0000113 if macros is None:
114 macros = []
Greg Ward04d78321999-12-12 16:57:47 +0000115 if include_dirs is None:
116 include_dirs = []
Greg Ward170bdc01999-07-10 02:04:22 +0000117
118 if type (macros) is not ListType:
119 raise TypeError, \
120 "'macros' (if supplied) must be a list of tuples"
Greg Ward04d78321999-12-12 16:57:47 +0000121 if type (include_dirs) not in (ListType, TupleType):
Greg Ward170bdc01999-07-10 02:04:22 +0000122 raise TypeError, \
Greg Ward04d78321999-12-12 16:57:47 +0000123 "'include_dirs' (if supplied) must be a list of strings"
124 include_dirs = list (include_dirs)
Greg Ward170bdc01999-07-10 02:04:22 +0000125
Greg Wardc2941131999-09-08 02:32:19 +0000126 pp_opts = gen_preprocess_options (self.macros + macros,
Greg Ward04d78321999-12-12 16:57:47 +0000127 self.include_dirs + include_dirs)
Greg Ward170bdc01999-07-10 02:04:22 +0000128
Greg Ward8037cb11999-09-13 03:12:53 +0000129 # So we can mangle 'sources' without hurting the caller's data
130 orig_sources = sources
131 sources = copy (sources)
Greg Ward170bdc01999-07-10 02:04:22 +0000132
Greg Ward8037cb11999-09-13 03:12:53 +0000133 # Get the list of expected output (object) files and drop files we
134 # don't have to recompile. (Simplistic check -- we just compare the
135 # source and object file, no deep dependency checking involving
136 # header files. Hmmm.)
Greg Wardc9f31872000-01-09 22:47:53 +0000137 objects = self.object_filenames (sources, output_dir=output_dir)
Greg Ward4fecfce1999-10-03 20:45:33 +0000138 if not self.force:
139 skipped = newer_pairwise (sources, objects)
140 for skipped_pair in skipped:
141 self.announce ("skipping %s (%s up-to-date)" % skipped_pair)
Greg Ward8037cb11999-09-13 03:12:53 +0000142
Greg Wardef6f5152000-02-03 23:07:19 +0000143 # Build list of (source,object) tuples for convenience
144 srcobj = []
145 for i in range (len (sources)):
146 srcobj.append ((sources[i], objects[i]))
Greg Ward8037cb11999-09-13 03:12:53 +0000147
Greg Wardef6f5152000-02-03 23:07:19 +0000148 # Compile all source files that weren't eliminated by
149 # 'newer_pairwise()'.
150 # XXX use of ccflags_shared means we're blithely assuming
151 # that we're compiling for inclusion in a shared object!
152 # (will have to fix this when I add the ability to build a
153 # new Python)
154 cc_args = ['-c'] + pp_opts + self.ccflags + self.ccflags_shared
Greg Wardba233fb2000-02-09 02:17:00 +0000155 if debug:
156 cc_args[:0] = ['-g']
Greg Wardef6f5152000-02-03 23:07:19 +0000157 if extra_preargs:
158 cc_args[:0] = extra_preargs
159 if extra_postargs is None:
160 extra_postargs = []
Greg Ward8037cb11999-09-13 03:12:53 +0000161
Greg Wardef6f5152000-02-03 23:07:19 +0000162 for (source,object) in srcobj:
163 self.spawn ([self.cc] + cc_args +
164 [source, '-o', object] +
165 extra_postargs)
Greg Ward8037cb11999-09-13 03:12:53 +0000166
167 # Have to re-fetch list of object filenames, because we want to
168 # return *all* of them, including those that weren't recompiled on
169 # this call!
170 return self.object_filenames (orig_sources, output_dir)
Greg Ward170bdc01999-07-10 02:04:22 +0000171
Greg Wardc9f31872000-01-09 22:47:53 +0000172
173 def _fix_link_args (self, output_dir, libraries, library_dirs):
174 """Fixes up the arguments supplied to the 'link_*' methods:
175 if output_dir is None, use self.output_dir; ensure that
176 libraries and library_dirs are both lists (could be None or
177 tuples on input -- both are converted to lists). Return
178 a tuple of the three input arguments."""
179
180 if output_dir is None:
181 output_dir = self.output_dir
182 if libraries is None:
183 libraries = []
184 if library_dirs is None:
185 library_dirs = []
186
187 if type (libraries) not in (ListType, TupleType):
188 raise TypeError, \
189 "'libraries' (if supplied) must be a list of strings"
190 if type (library_dirs) not in (ListType, TupleType):
191 raise TypeError, \
192 "'library_dirs' (if supplied) must be a list of strings"
193 libraries = list (libraries)
194 library_dirs = list (library_dirs)
195
196 return (output_dir, libraries, library_dirs)
197
198
199 def link_static_lib (self,
200 objects,
201 output_libname,
Greg Wardba233fb2000-02-09 02:17:00 +0000202 output_dir=None,
203 debug=0):
Greg Wardc9f31872000-01-09 22:47:53 +0000204
205 if type (objects) not in (ListType, TupleType):
206 raise TypeError, \
207 "'objects' must be a list or tuple of strings"
208 objects = list (objects)
209
Greg Ward10ca82b2000-02-10 02:51:32 +0000210 if type (output_dir) not in (StringType, NoneType):
211 raise TypeError, "'output_dir' must be a string or None"
Greg Wardc9f31872000-01-09 22:47:53 +0000212 if output_dir is None:
213 output_dir = self.output_dir
214
215 output_filename = self.library_filename (output_libname)
216 if output_dir is not None:
217 output_filename = os.path.join (output_dir, output_filename)
218
219 # Check timestamps: if any of the object files are newer than
220 # the library file, *or* if "force" is true, then we'll
221 # recreate the library.
222 if not self.force:
223 if self.dry_run:
224 newer = newer_group (objects, output_filename, missing='newer')
225 else:
226 newer = newer_group (objects, output_filename)
227
228 if self.force or newer:
229 self.spawn ([self.archiver,
230 self.archiver_options,
231 output_filename] +
232 objects)
233 else:
234 self.announce ("skipping %s (up-to-date)" % output_filename)
235
236 # link_static_lib ()
237
Greg Ward170bdc01999-07-10 02:04:22 +0000238
239 def link_shared_lib (self,
240 objects,
241 output_libname,
Greg Ward8037cb11999-09-13 03:12:53 +0000242 output_dir=None,
Greg Ward170bdc01999-07-10 02:04:22 +0000243 libraries=None,
Greg Ward65f4a3b1999-08-29 18:23:32 +0000244 library_dirs=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000245 debug=0,
Greg Ward0e3530b1999-09-29 12:22:50 +0000246 extra_preargs=None,
247 extra_postargs=None):
Greg Ward170bdc01999-07-10 02:04:22 +0000248 # XXX should we sanity check the library name? (eg. no
249 # slashes)
Greg Ward8037cb11999-09-13 03:12:53 +0000250 self.link_shared_object (
251 objects,
252 "lib%s%s" % (output_libname, self._shared_lib_ext),
253 output_dir,
254 libraries,
255 library_dirs,
Greg Wardba233fb2000-02-09 02:17:00 +0000256 debug,
Greg Ward0e3530b1999-09-29 12:22:50 +0000257 extra_preargs,
258 extra_postargs)
Greg Ward8037cb11999-09-13 03:12:53 +0000259
Greg Ward170bdc01999-07-10 02:04:22 +0000260
261 def link_shared_object (self,
262 objects,
263 output_filename,
Greg Ward8037cb11999-09-13 03:12:53 +0000264 output_dir=None,
Greg Ward5e717441999-08-14 23:53:53 +0000265 libraries=None,
Greg Ward65f4a3b1999-08-29 18:23:32 +0000266 library_dirs=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000267 debug=0,
Greg Ward0e3530b1999-09-29 12:22:50 +0000268 extra_preargs=None,
269 extra_postargs=None):
Greg Ward5e717441999-08-14 23:53:53 +0000270
Greg Wardc9f31872000-01-09 22:47:53 +0000271 (output_dir, libraries, library_dirs) = \
272 self._fix_link_args (output_dir, libraries, library_dirs)
Greg Ward04d78321999-12-12 16:57:47 +0000273
Greg Ward4fecfce1999-10-03 20:45:33 +0000274 lib_opts = gen_lib_options (self,
275 self.library_dirs + library_dirs,
276 self.libraries + libraries)
Greg Ward10ca82b2000-02-10 02:51:32 +0000277 if type (output_dir) not in (StringType, NoneType):
278 raise TypeError, "'output_dir' must be a string or None"
Greg Ward8037cb11999-09-13 03:12:53 +0000279 if output_dir is not None:
280 output_filename = os.path.join (output_dir, output_filename)
Greg Ward170bdc01999-07-10 02:04:22 +0000281
Greg Ward8037cb11999-09-13 03:12:53 +0000282 # If any of the input object files are newer than the output shared
283 # object, relink. Again, this is a simplistic dependency check:
284 # doesn't look at any of the libraries we might be linking with.
Greg Wardc9f31872000-01-09 22:47:53 +0000285
Greg Ward4fecfce1999-10-03 20:45:33 +0000286 if not self.force:
Greg Wardc9f31872000-01-09 22:47:53 +0000287 if self.dry_run:
288 newer = newer_group (objects, output_filename, missing='newer')
289 else:
Greg Ward4fecfce1999-10-03 20:45:33 +0000290 newer = newer_group (objects, output_filename)
Greg Wardb116e451999-09-21 18:36:15 +0000291
Greg Ward4fecfce1999-10-03 20:45:33 +0000292 if self.force or newer:
293 ld_args = self.ldflags_shared + objects + \
294 lib_opts + ['-o', output_filename]
Greg Wardba233fb2000-02-09 02:17:00 +0000295 if debug:
296 ld_args[:0] = ['-g']
Greg Ward0e3530b1999-09-29 12:22:50 +0000297 if extra_preargs:
298 ld_args[:0] = extra_preargs
299 if extra_postargs:
300 ld_args.extend (extra_postargs)
Greg Ward8037cb11999-09-13 03:12:53 +0000301 self.spawn ([self.ld_shared] + ld_args)
302 else:
303 self.announce ("skipping %s (up-to-date)" % output_filename)
Greg Ward5e717441999-08-14 23:53:53 +0000304
Greg Ward4fecfce1999-10-03 20:45:33 +0000305 # link_shared_object ()
306
307
Greg Wardc9f31872000-01-09 22:47:53 +0000308 def link_executable (self,
309 objects,
310 output_progname,
311 output_dir=None,
312 libraries=None,
313 library_dirs=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000314 debug=0,
Greg Wardc9f31872000-01-09 22:47:53 +0000315 extra_preargs=None,
316 extra_postargs=None):
317
318 (output_dir, libraries, library_dirs) = \
319 self._fix_link_args (output_dir, libraries, library_dirs)
320
321 lib_opts = gen_lib_options (self,
322 self.library_dirs + library_dirs,
323 self.libraries + libraries)
324 output_filename = output_progname # Unix-ism!
325 if output_dir is not None:
326 output_filename = os.path.join (output_dir, output_filename)
327
328 # Same ol' simplistic-but-still-useful dependency check.
329 if not self.force:
330 if self.dry_run:
331 newer = newer_group (objects, output_filename, missing='newer')
332 else:
333 newer = newer_group (objects, output_filename)
334
335 if self.force or newer:
336 ld_args = objects + lib_opts + ['-o', output_filename]
Greg Wardba233fb2000-02-09 02:17:00 +0000337 if debug:
338 ld_args[:0] = ['-g']
Greg Wardc9f31872000-01-09 22:47:53 +0000339 if extra_preargs:
340 ld_args[:0] = extra_preargs
341 if extra_postargs:
342 ld_args.extend (extra_postargs)
343 self.spawn ([self.ld_exec] + ld_args)
344 else:
345 self.announce ("skipping %s (up-to-date)" % output_filename)
346
347 # link_executable ()
348
349
Greg Ward4fecfce1999-10-03 20:45:33 +0000350 # -- Filename-mangling (etc.) methods ------------------------------
Greg Ward5e717441999-08-14 23:53:53 +0000351
Greg Wardc9f31872000-01-09 22:47:53 +0000352 def object_filenames (self, source_filenames,
353 keep_dir=0, output_dir=None):
Greg Ward5e717441999-08-14 23:53:53 +0000354 outnames = []
355 for inname in source_filenames:
Greg Ward8037cb11999-09-13 03:12:53 +0000356 outname = re.sub (r'\.(c|C|cc|cxx|cpp)$', self._obj_ext, inname)
Greg Wardc9f31872000-01-09 22:47:53 +0000357 if not keep_dir:
358 outname = os.path.basename (outname)
Greg Ward8037cb11999-09-13 03:12:53 +0000359 if output_dir is not None:
360 outname = os.path.join (output_dir, outname)
361 outnames.append (outname)
Greg Ward5e717441999-08-14 23:53:53 +0000362 return outnames
363
Greg Wardc9f31872000-01-09 22:47:53 +0000364 def shared_object_filename (self, source_filename,
365 keep_dir=0, output_dir=None):
Greg Ward8037cb11999-09-13 03:12:53 +0000366 outname = re.sub (r'\.(c|C|cc|cxx|cpp)$', self._shared_lib_ext)
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 return outname
Greg Ward5e717441999-08-14 23:53:53 +0000372
Greg Ward4fecfce1999-10-03 20:45:33 +0000373
Greg Ward5e717441999-08-14 23:53:53 +0000374 def library_filename (self, libname):
Greg Wardc9f31872000-01-09 22:47:53 +0000375 (dirname, basename) = os.path.split (libname)
376 return os.path.join (dirname,
377 "lib%s%s" % (basename, self._static_lib_ext))
Greg Ward5e717441999-08-14 23:53:53 +0000378
379 def shared_library_filename (self, libname):
Greg Wardc9f31872000-01-09 22:47:53 +0000380 (dirname, basename) = os.path.split (libname)
381 return os.path.join (dirname,
382 "lib%s%s" % (basename, self._shared_lib_ext))
Greg Ward4fecfce1999-10-03 20:45:33 +0000383
384
385 def library_dir_option (self, dir):
386 return "-L" + dir
387
388 def library_option (self, lib):
389 return "-l" + lib
390
391
392 def find_library_file (self, dirs, lib):
393
394 for dir in dirs:
395 shared = os.path.join (dir, self.shared_library_filename (lib))
396 static = os.path.join (dir, self.library_filename (lib))
397
398 # We're second-guessing the linker here, with not much hard
399 # data to go on: GCC seems to prefer the shared library, so I'm
400 # assuming that *all* Unix C compilers do. And of course I'm
401 # ignoring even GCC's "-static" option. So sue me.
402 if os.path.exists (shared):
403 return shared
404 elif os.path.exists (static):
405 return static
406
407 else:
408 # Oops, didn't find it in *any* of 'dirs'
409 return None
410
411 # find_library_file ()
Greg Ward65f4a3b1999-08-29 18:23:32 +0000412
Greg Ward170bdc01999-07-10 02:04:22 +0000413# class UnixCCompiler
414
415
416def _split_command (cmd):
417 """Split a command string up into the progam to run (a string) and
418 the list of arguments; return them as (cmd, arglist)."""
419 args = string.split (cmd)
420 return (args[0], args[1:])