blob: fb58269cc1a9ced2064bcd4c1db77d27bb051659 [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 Ward0e3530b1999-09-29 12:22:50 +0000105 extra_preargs=None,
106 extra_postargs=None):
Greg Ward5e717441999-08-14 23:53:53 +0000107
Greg Ward8037cb11999-09-13 03:12:53 +0000108 if output_dir is None:
109 output_dir = self.output_dir
Greg Ward5e717441999-08-14 23:53:53 +0000110 if macros is None:
111 macros = []
Greg Ward04d78321999-12-12 16:57:47 +0000112 if include_dirs is None:
113 include_dirs = []
Greg Ward170bdc01999-07-10 02:04:22 +0000114
115 if type (macros) is not ListType:
116 raise TypeError, \
117 "'macros' (if supplied) must be a list of tuples"
Greg Ward04d78321999-12-12 16:57:47 +0000118 if type (include_dirs) not in (ListType, TupleType):
Greg Ward170bdc01999-07-10 02:04:22 +0000119 raise TypeError, \
Greg Ward04d78321999-12-12 16:57:47 +0000120 "'include_dirs' (if supplied) must be a list of strings"
121 include_dirs = list (include_dirs)
Greg Ward170bdc01999-07-10 02:04:22 +0000122
Greg Wardc2941131999-09-08 02:32:19 +0000123 pp_opts = gen_preprocess_options (self.macros + macros,
Greg Ward04d78321999-12-12 16:57:47 +0000124 self.include_dirs + include_dirs)
Greg Ward170bdc01999-07-10 02:04:22 +0000125
Greg Ward8037cb11999-09-13 03:12:53 +0000126 # So we can mangle 'sources' without hurting the caller's data
127 orig_sources = sources
128 sources = copy (sources)
Greg Ward170bdc01999-07-10 02:04:22 +0000129
Greg Ward8037cb11999-09-13 03:12:53 +0000130 # Get the list of expected output (object) files and drop files we
131 # don't have to recompile. (Simplistic check -- we just compare the
132 # source and object file, no deep dependency checking involving
133 # header files. Hmmm.)
Greg Wardc9f31872000-01-09 22:47:53 +0000134 objects = self.object_filenames (sources, output_dir=output_dir)
Greg Ward4fecfce1999-10-03 20:45:33 +0000135 if not self.force:
136 skipped = newer_pairwise (sources, objects)
137 for skipped_pair in skipped:
138 self.announce ("skipping %s (%s up-to-date)" % skipped_pair)
Greg Ward8037cb11999-09-13 03:12:53 +0000139
140 # If anything left to compile, compile it
141 if sources:
142 # XXX use of ccflags_shared means we're blithely assuming
143 # that we're compiling for inclusion in a shared object!
144 # (will have to fix this when I add the ability to build a
145 # new Python)
146 cc_args = ['-c'] + pp_opts + \
147 self.ccflags + self.ccflags_shared + \
148 sources
Greg Ward0e3530b1999-09-29 12:22:50 +0000149 if extra_preargs:
150 cc_args[:0] = extra_preargs
151 if extra_postargs:
152 cc_args.extend (extra_postargs)
Greg Ward8037cb11999-09-13 03:12:53 +0000153 self.spawn ([self.cc] + cc_args)
154
155
156 # Note that compiling multiple source files in the same go like
157 # we've just done drops the .o file in the current directory, which
158 # may not be what the caller wants (depending on the 'output_dir'
159 # parameter). So, if necessary, fix that now by moving the .o
160 # files into the desired output directory. (The alternative, of
161 # course, is to compile one-at-a-time with a -o option. 6 of one,
162 # 12/2 of the other...)
163
164 if output_dir:
165 for i in range (len (objects)):
166 src = os.path.basename (objects[i])
167 objects[i] = self.move_file (src, output_dir)
168
169 # Have to re-fetch list of object filenames, because we want to
170 # return *all* of them, including those that weren't recompiled on
171 # this call!
172 return self.object_filenames (orig_sources, output_dir)
Greg Ward170bdc01999-07-10 02:04:22 +0000173
Greg Wardc9f31872000-01-09 22:47:53 +0000174
175 def _fix_link_args (self, output_dir, libraries, library_dirs):
176 """Fixes up the arguments supplied to the 'link_*' methods:
177 if output_dir is None, use self.output_dir; ensure that
178 libraries and library_dirs are both lists (could be None or
179 tuples on input -- both are converted to lists). Return
180 a tuple of the three input arguments."""
181
182 if output_dir is None:
183 output_dir = self.output_dir
184 if libraries is None:
185 libraries = []
186 if library_dirs is None:
187 library_dirs = []
188
189 if type (libraries) not in (ListType, TupleType):
190 raise TypeError, \
191 "'libraries' (if supplied) must be a list of strings"
192 if type (library_dirs) not in (ListType, TupleType):
193 raise TypeError, \
194 "'library_dirs' (if supplied) must be a list of strings"
195 libraries = list (libraries)
196 library_dirs = list (library_dirs)
197
198 return (output_dir, libraries, library_dirs)
199
200
201 def link_static_lib (self,
202 objects,
203 output_libname,
204 output_dir=None):
205
206 if type (objects) not in (ListType, TupleType):
207 raise TypeError, \
208 "'objects' must be a list or tuple of strings"
209 objects = list (objects)
210
211 if output_dir is None:
212 output_dir = self.output_dir
213
214 output_filename = self.library_filename (output_libname)
215 if output_dir is not None:
216 output_filename = os.path.join (output_dir, output_filename)
217
218 # Check timestamps: if any of the object files are newer than
219 # the library file, *or* if "force" is true, then we'll
220 # recreate the library.
221 if not self.force:
222 if self.dry_run:
223 newer = newer_group (objects, output_filename, missing='newer')
224 else:
225 newer = newer_group (objects, output_filename)
226
227 if self.force or newer:
228 self.spawn ([self.archiver,
229 self.archiver_options,
230 output_filename] +
231 objects)
232 else:
233 self.announce ("skipping %s (up-to-date)" % output_filename)
234
235 # link_static_lib ()
236
Greg Ward170bdc01999-07-10 02:04:22 +0000237
238 def link_shared_lib (self,
239 objects,
240 output_libname,
Greg Ward8037cb11999-09-13 03:12:53 +0000241 output_dir=None,
Greg Ward170bdc01999-07-10 02:04:22 +0000242 libraries=None,
Greg Ward65f4a3b1999-08-29 18:23:32 +0000243 library_dirs=None,
Greg Ward0e3530b1999-09-29 12:22:50 +0000244 extra_preargs=None,
245 extra_postargs=None):
Greg Ward170bdc01999-07-10 02:04:22 +0000246 # XXX should we sanity check the library name? (eg. no
247 # slashes)
Greg Ward8037cb11999-09-13 03:12:53 +0000248 self.link_shared_object (
249 objects,
250 "lib%s%s" % (output_libname, self._shared_lib_ext),
251 output_dir,
252 libraries,
253 library_dirs,
Greg Ward0e3530b1999-09-29 12:22:50 +0000254 extra_preargs,
255 extra_postargs)
Greg Ward8037cb11999-09-13 03:12:53 +0000256
Greg Ward170bdc01999-07-10 02:04:22 +0000257
258 def link_shared_object (self,
259 objects,
260 output_filename,
Greg Ward8037cb11999-09-13 03:12:53 +0000261 output_dir=None,
Greg Ward5e717441999-08-14 23:53:53 +0000262 libraries=None,
Greg Ward65f4a3b1999-08-29 18:23:32 +0000263 library_dirs=None,
Greg Ward0e3530b1999-09-29 12:22:50 +0000264 extra_preargs=None,
265 extra_postargs=None):
Greg Ward5e717441999-08-14 23:53:53 +0000266
Greg Wardc9f31872000-01-09 22:47:53 +0000267 (output_dir, libraries, library_dirs) = \
268 self._fix_link_args (output_dir, libraries, library_dirs)
Greg Ward04d78321999-12-12 16:57:47 +0000269
Greg Ward4fecfce1999-10-03 20:45:33 +0000270 lib_opts = gen_lib_options (self,
271 self.library_dirs + library_dirs,
272 self.libraries + libraries)
Greg Ward8037cb11999-09-13 03:12:53 +0000273 if output_dir is not None:
274 output_filename = os.path.join (output_dir, output_filename)
Greg Ward170bdc01999-07-10 02:04:22 +0000275
Greg Ward8037cb11999-09-13 03:12:53 +0000276 # If any of the input object files are newer than the output shared
277 # object, relink. Again, this is a simplistic dependency check:
278 # doesn't look at any of the libraries we might be linking with.
Greg Wardc9f31872000-01-09 22:47:53 +0000279
Greg Ward4fecfce1999-10-03 20:45:33 +0000280 if not self.force:
Greg Wardc9f31872000-01-09 22:47:53 +0000281 if self.dry_run:
282 newer = newer_group (objects, output_filename, missing='newer')
283 else:
Greg Ward4fecfce1999-10-03 20:45:33 +0000284 newer = newer_group (objects, output_filename)
Greg Wardb116e451999-09-21 18:36:15 +0000285
Greg Ward4fecfce1999-10-03 20:45:33 +0000286 if self.force or newer:
287 ld_args = self.ldflags_shared + objects + \
288 lib_opts + ['-o', output_filename]
Greg Ward0e3530b1999-09-29 12:22:50 +0000289 if extra_preargs:
290 ld_args[:0] = extra_preargs
291 if extra_postargs:
292 ld_args.extend (extra_postargs)
Greg Ward8037cb11999-09-13 03:12:53 +0000293 self.spawn ([self.ld_shared] + ld_args)
294 else:
295 self.announce ("skipping %s (up-to-date)" % output_filename)
Greg Ward5e717441999-08-14 23:53:53 +0000296
Greg Ward4fecfce1999-10-03 20:45:33 +0000297 # link_shared_object ()
298
299
Greg Wardc9f31872000-01-09 22:47:53 +0000300 def link_executable (self,
301 objects,
302 output_progname,
303 output_dir=None,
304 libraries=None,
305 library_dirs=None,
306 extra_preargs=None,
307 extra_postargs=None):
308
309 (output_dir, libraries, library_dirs) = \
310 self._fix_link_args (output_dir, libraries, library_dirs)
311
312 lib_opts = gen_lib_options (self,
313 self.library_dirs + library_dirs,
314 self.libraries + libraries)
315 output_filename = output_progname # Unix-ism!
316 if output_dir is not None:
317 output_filename = os.path.join (output_dir, output_filename)
318
319 # Same ol' simplistic-but-still-useful dependency check.
320 if not self.force:
321 if self.dry_run:
322 newer = newer_group (objects, output_filename, missing='newer')
323 else:
324 newer = newer_group (objects, output_filename)
325
326 if self.force or newer:
327 ld_args = objects + lib_opts + ['-o', output_filename]
328 if extra_preargs:
329 ld_args[:0] = extra_preargs
330 if extra_postargs:
331 ld_args.extend (extra_postargs)
332 self.spawn ([self.ld_exec] + ld_args)
333 else:
334 self.announce ("skipping %s (up-to-date)" % output_filename)
335
336 # link_executable ()
337
338
Greg Ward4fecfce1999-10-03 20:45:33 +0000339 # -- Filename-mangling (etc.) methods ------------------------------
Greg Ward5e717441999-08-14 23:53:53 +0000340
Greg Wardc9f31872000-01-09 22:47:53 +0000341 def object_filenames (self, source_filenames,
342 keep_dir=0, output_dir=None):
Greg Ward5e717441999-08-14 23:53:53 +0000343 outnames = []
344 for inname in source_filenames:
Greg Ward8037cb11999-09-13 03:12:53 +0000345 outname = re.sub (r'\.(c|C|cc|cxx|cpp)$', self._obj_ext, inname)
Greg Wardc9f31872000-01-09 22:47:53 +0000346 if not keep_dir:
347 outname = os.path.basename (outname)
Greg Ward8037cb11999-09-13 03:12:53 +0000348 if output_dir is not None:
349 outname = os.path.join (output_dir, outname)
350 outnames.append (outname)
Greg Ward5e717441999-08-14 23:53:53 +0000351 return outnames
352
Greg Wardc9f31872000-01-09 22:47:53 +0000353 def shared_object_filename (self, source_filename,
354 keep_dir=0, output_dir=None):
Greg Ward8037cb11999-09-13 03:12:53 +0000355 outname = re.sub (r'\.(c|C|cc|cxx|cpp)$', self._shared_lib_ext)
Greg Wardc9f31872000-01-09 22:47:53 +0000356 if not keep_dir:
357 outname = os.path.basename (outname)
Greg Ward8037cb11999-09-13 03:12:53 +0000358 if output_dir is not None:
359 outname = os.path.join (output_dir, outname)
360 return outname
Greg Ward5e717441999-08-14 23:53:53 +0000361
Greg Ward4fecfce1999-10-03 20:45:33 +0000362
Greg Ward5e717441999-08-14 23:53:53 +0000363 def library_filename (self, libname):
Greg Wardc9f31872000-01-09 22:47:53 +0000364 (dirname, basename) = os.path.split (libname)
365 return os.path.join (dirname,
366 "lib%s%s" % (basename, self._static_lib_ext))
Greg Ward5e717441999-08-14 23:53:53 +0000367
368 def shared_library_filename (self, libname):
Greg Wardc9f31872000-01-09 22:47:53 +0000369 (dirname, basename) = os.path.split (libname)
370 return os.path.join (dirname,
371 "lib%s%s" % (basename, self._shared_lib_ext))
Greg Ward4fecfce1999-10-03 20:45:33 +0000372
373
374 def library_dir_option (self, dir):
375 return "-L" + dir
376
377 def library_option (self, lib):
378 return "-l" + lib
379
380
381 def find_library_file (self, dirs, lib):
382
383 for dir in dirs:
384 shared = os.path.join (dir, self.shared_library_filename (lib))
385 static = os.path.join (dir, self.library_filename (lib))
386
387 # We're second-guessing the linker here, with not much hard
388 # data to go on: GCC seems to prefer the shared library, so I'm
389 # assuming that *all* Unix C compilers do. And of course I'm
390 # ignoring even GCC's "-static" option. So sue me.
391 if os.path.exists (shared):
392 return shared
393 elif os.path.exists (static):
394 return static
395
396 else:
397 # Oops, didn't find it in *any* of 'dirs'
398 return None
399
400 # find_library_file ()
Greg Ward65f4a3b1999-08-29 18:23:32 +0000401
Greg Ward170bdc01999-07-10 02:04:22 +0000402# class UnixCCompiler
403
404
405def _split_command (cmd):
406 """Split a command string up into the progam to run (a string) and
407 the list of arguments; return them as (cmd, arglist)."""
408 args = string.split (cmd)
409 return (args[0], args[1:])