blob: 5e1524c67c833b6c8e0977dfc6cfd837fa8ee90b [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 Ward1c793302000-04-14 00:48:15 +000023from distutils import sysconfig
Greg Ward32c4a8a2000-03-06 03:40:29 +000024from distutils.ccompiler import CCompiler, gen_preprocess_options, gen_lib_options
Greg Ward170bdc01999-07-10 02:04:22 +000025
26# XXX Things not currently handled:
27# * optimization/debug/warning flags; we just use whatever's in Python's
28# Makefile and live with it. Is this adequate? If not, we might
29# have to have a bunch of subclasses GNUCCompiler, SGICCompiler,
30# SunCCompiler, and I suspect down that road lies madness.
31# * even if we don't know a warning flag from an optimization flag,
32# we need some way for outsiders to feed preprocessor/compiler/linker
33# flags in to us -- eg. a sysadmin might want to mandate certain flags
34# via a site config file, or a user might want to set something for
35# compiling this module distribution only via the setup.py command
36# line, whatever. As long as these options come from something on the
37# current system, they can be as system-dependent as they like, and we
38# should just happily stuff them into the preprocessor/compiler/linker
39# options and carry on.
40
41
42class UnixCCompiler (CCompiler):
43
Greg Ward5e717441999-08-14 23:53:53 +000044 # XXX perhaps there should really be *three* kinds of include
45 # directories: those built in to the preprocessor, those from Python's
46 # Makefiles, and those supplied to {add,set}_include_dirs(). Currently
47 # we make no distinction between the latter two at this point; it's all
48 # up to the client class to select the include directories to use above
49 # and beyond the compiler's defaults. That is, both the Python include
50 # directories and any module- or package-specific include directories
51 # are specified via {add,set}_include_dirs(), and there's no way to
52 # distinguish them. This might be a bug.
Greg Ward65f4a3b1999-08-29 18:23:32 +000053
Greg Ward0e3530b1999-09-29 12:22:50 +000054 compiler_type = 'unix'
55
Greg Ward32c4a8a2000-03-06 03:40:29 +000056 # Needed for the filename generation methods provided by the
57 # base class, CCompiler.
58 src_extensions = [".c",".C",".cc",".cxx",".cpp"]
59 obj_extension = ".o"
60 static_lib_extension = ".a"
Greg Ward1c793302000-04-14 00:48:15 +000061 shared_lib_extension = sysconfig.SO
Greg Ward32c4a8a2000-03-06 03:40:29 +000062 static_lib_format = shared_lib_format = "lib%s%s"
Greg Wardc9f31872000-01-09 22:47:53 +000063
64 # Command to create a static library: seems to be pretty consistent
65 # across the major Unices. Might have to move down into the
66 # constructor if we need platform-specific guesswork.
Greg Ward1c793302000-04-14 00:48:15 +000067 archiver = sysconfig.AR
Greg Wardc9f31872000-01-09 22:47:53 +000068 archiver_options = "-cr"
Greg Ward1c793302000-04-14 00:48:15 +000069 ranlib = sysconfig.RANLIB
Greg Wardc9f31872000-01-09 22:47:53 +000070
71
Greg Ward5e717441999-08-14 23:53:53 +000072 def __init__ (self,
73 verbose=0,
Greg Ward4fecfce1999-10-03 20:45:33 +000074 dry_run=0,
75 force=0):
Greg Ward170bdc01999-07-10 02:04:22 +000076
Greg Ward4fecfce1999-10-03 20:45:33 +000077 CCompiler.__init__ (self, verbose, dry_run, force)
Greg Ward170bdc01999-07-10 02:04:22 +000078
79 self.preprocess_options = None
80 self.compile_options = None
81
Greg Ward5e717441999-08-14 23:53:53 +000082 # Munge CC and OPT together in case there are flags stuck in CC.
83 # Note that using these variables from sysconfig immediately makes
84 # this module specific to building Python extensions and
85 # inappropriate as a general-purpose C compiler front-end. So sue
86 # me. Note also that we use OPT rather than CFLAGS, because CFLAGS
87 # is the flags used to compile Python itself -- not only are there
88 # -I options in there, they are the *wrong* -I options. We'll
89 # leave selection of include directories up to the class using
90 # UnixCCompiler!
91
Greg Ward170bdc01999-07-10 02:04:22 +000092 (self.cc, self.ccflags) = \
Greg Ward1c793302000-04-14 00:48:15 +000093 _split_command (sysconfig.CC + ' ' + sysconfig.OPT)
94 self.ccflags_shared = string.split (sysconfig.CCSHARED)
Greg Ward170bdc01999-07-10 02:04:22 +000095
96 (self.ld_shared, self.ldflags_shared) = \
Greg Ward1c793302000-04-14 00:48:15 +000097 _split_command (sysconfig.LDSHARED)
Greg Ward170bdc01999-07-10 02:04:22 +000098
Greg Wardc9f31872000-01-09 22:47:53 +000099 self.ld_exec = self.cc
100
Greg Ward32c4a8a2000-03-06 03:40:29 +0000101 # __init__ ()
102
Greg Ward170bdc01999-07-10 02:04:22 +0000103
104 def compile (self,
105 sources,
Greg Ward8037cb11999-09-13 03:12:53 +0000106 output_dir=None,
Greg Ward5e717441999-08-14 23:53:53 +0000107 macros=None,
Greg Ward04d78321999-12-12 16:57:47 +0000108 include_dirs=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000109 debug=0,
Greg Ward0e3530b1999-09-29 12:22:50 +0000110 extra_preargs=None,
111 extra_postargs=None):
Greg Ward5e717441999-08-14 23:53:53 +0000112
Greg Ward32c4a8a2000-03-06 03:40:29 +0000113 (output_dir, macros, include_dirs) = \
114 self._fix_compile_args (output_dir, macros, include_dirs)
115 (objects, skip_sources) = self._prep_compile (sources, output_dir)
Greg Ward170bdc01999-07-10 02:04:22 +0000116
Greg Ward32c4a8a2000-03-06 03:40:29 +0000117 # Figure out the options for the compiler command line.
118 pp_opts = gen_preprocess_options (macros, include_dirs)
Greg Wardef6f5152000-02-03 23:07:19 +0000119 cc_args = ['-c'] + pp_opts + self.ccflags + self.ccflags_shared
Greg Wardba233fb2000-02-09 02:17:00 +0000120 if debug:
121 cc_args[:0] = ['-g']
Greg Wardef6f5152000-02-03 23:07:19 +0000122 if extra_preargs:
123 cc_args[:0] = extra_preargs
124 if extra_postargs is None:
125 extra_postargs = []
Greg Ward8037cb11999-09-13 03:12:53 +0000126
Greg Ward32c4a8a2000-03-06 03:40:29 +0000127 # Compile all source files that weren't eliminated by
128 # '_prep_compile()'.
129 for i in range (len (sources)):
130 src = sources[i] ; obj = objects[i]
131 if skip_sources[src]:
132 self.announce ("skipping %s (%s up-to-date)" % (src, obj))
133 else:
134 self.mkpath (os.path.dirname (obj))
135 self.spawn ([self.cc] + cc_args + [src, '-o', obj] + extra_postargs)
Greg Ward8037cb11999-09-13 03:12:53 +0000136
Greg Ward32c4a8a2000-03-06 03:40:29 +0000137 # Return *all* object filenames, not just the ones we just built.
138 return objects
Greg Ward170bdc01999-07-10 02:04:22 +0000139
Greg Ward32c4a8a2000-03-06 03:40:29 +0000140 # compile ()
141
Greg Wardc9f31872000-01-09 22:47:53 +0000142
Greg Ward036c8052000-03-10 01:48:32 +0000143 def create_static_lib (self,
144 objects,
145 output_libname,
146 output_dir=None,
147 debug=0):
Greg Wardc9f31872000-01-09 22:47:53 +0000148
Greg Warde21dabe2000-03-26 21:40:19 +0000149 (objects, output_dir) = self._fix_object_args (objects, output_dir)
Greg Wardc9f31872000-01-09 22:47:53 +0000150
Greg Ward32c4a8a2000-03-06 03:40:29 +0000151 output_filename = \
152 self.library_filename (output_libname, output_dir=output_dir)
Greg Wardc9f31872000-01-09 22:47:53 +0000153
Greg Ward32c4a8a2000-03-06 03:40:29 +0000154 if self._need_link (objects, output_filename):
Greg Wardf1146572000-03-01 14:43:49 +0000155 self.mkpath (os.path.dirname (output_filename))
Greg Wardc9f31872000-01-09 22:47:53 +0000156 self.spawn ([self.archiver,
157 self.archiver_options,
158 output_filename] +
Greg Ward32c4a8a2000-03-06 03:40:29 +0000159 objects + self.objects)
Greg Ward1c793302000-04-14 00:48:15 +0000160
Greg Ward8eef5832000-04-14 13:53:34 +0000161 # Not many Unices required ranlib anymore -- SunOS 4.x is, I
162 # think the only major Unix that does. Maybe we need some
163 # platform intelligence here to skip ranlib if it's not
164 # needed -- or maybe Python's configure script took care of
165 # it for us, hence the check for leading colon.
166 if self.ranlib[0] != ':':
167 self.spawn ([self.ranlib, output_filename])
Greg Wardc9f31872000-01-09 22:47:53 +0000168 else:
169 self.announce ("skipping %s (up-to-date)" % output_filename)
170
Greg Ward036c8052000-03-10 01:48:32 +0000171 # create_static_lib ()
Greg Wardc9f31872000-01-09 22:47:53 +0000172
Greg Ward170bdc01999-07-10 02:04:22 +0000173
174 def link_shared_lib (self,
175 objects,
176 output_libname,
Greg Ward8037cb11999-09-13 03:12:53 +0000177 output_dir=None,
Greg Ward170bdc01999-07-10 02:04:22 +0000178 libraries=None,
Greg Ward65f4a3b1999-08-29 18:23:32 +0000179 library_dirs=None,
Greg Warde21dabe2000-03-26 21:40:19 +0000180 runtime_library_dirs=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000181 debug=0,
Greg Ward0e3530b1999-09-29 12:22:50 +0000182 extra_preargs=None,
183 extra_postargs=None):
Greg Ward8037cb11999-09-13 03:12:53 +0000184 self.link_shared_object (
185 objects,
Greg Ward32c4a8a2000-03-06 03:40:29 +0000186 self.shared_library_filename (output_libname),
Greg Ward8037cb11999-09-13 03:12:53 +0000187 output_dir,
188 libraries,
189 library_dirs,
Greg Warde21dabe2000-03-26 21:40:19 +0000190 runtime_library_dirs,
Greg Wardba233fb2000-02-09 02:17:00 +0000191 debug,
Greg Ward0e3530b1999-09-29 12:22:50 +0000192 extra_preargs,
193 extra_postargs)
Greg Ward8037cb11999-09-13 03:12:53 +0000194
Greg Ward170bdc01999-07-10 02:04:22 +0000195
196 def link_shared_object (self,
197 objects,
198 output_filename,
Greg Ward8037cb11999-09-13 03:12:53 +0000199 output_dir=None,
Greg Ward5e717441999-08-14 23:53:53 +0000200 libraries=None,
Greg Ward65f4a3b1999-08-29 18:23:32 +0000201 library_dirs=None,
Greg Warde21dabe2000-03-26 21:40:19 +0000202 runtime_library_dirs=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000203 debug=0,
Greg Ward0e3530b1999-09-29 12:22:50 +0000204 extra_preargs=None,
205 extra_postargs=None):
Greg Ward5e717441999-08-14 23:53:53 +0000206
Greg Warde21dabe2000-03-26 21:40:19 +0000207 (objects, output_dir) = self._fix_object_args (objects, output_dir)
208 (libraries, library_dirs, runtime_library_dirs) = \
209 self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
Greg Ward04d78321999-12-12 16:57:47 +0000210
Greg Wardd03f88a2000-03-18 15:19:51 +0000211 lib_opts = gen_lib_options (self,
Greg Warde21dabe2000-03-26 21:40:19 +0000212 library_dirs, runtime_library_dirs,
Greg Wardd03f88a2000-03-18 15:19:51 +0000213 libraries)
Greg Ward10ca82b2000-02-10 02:51:32 +0000214 if type (output_dir) not in (StringType, NoneType):
215 raise TypeError, "'output_dir' must be a string or None"
Greg Ward8037cb11999-09-13 03:12:53 +0000216 if output_dir is not None:
217 output_filename = os.path.join (output_dir, output_filename)
Greg Ward170bdc01999-07-10 02:04:22 +0000218
Greg Ward32c4a8a2000-03-06 03:40:29 +0000219 if self._need_link (objects, output_filename):
220 ld_args = (self.ldflags_shared + objects + self.objects +
221 lib_opts + ['-o', output_filename])
Greg Wardba233fb2000-02-09 02:17:00 +0000222 if debug:
223 ld_args[:0] = ['-g']
Greg Ward0e3530b1999-09-29 12:22:50 +0000224 if extra_preargs:
225 ld_args[:0] = extra_preargs
226 if extra_postargs:
227 ld_args.extend (extra_postargs)
Greg Wardf1146572000-03-01 14:43:49 +0000228 self.mkpath (os.path.dirname (output_filename))
Greg Ward8037cb11999-09-13 03:12:53 +0000229 self.spawn ([self.ld_shared] + ld_args)
230 else:
231 self.announce ("skipping %s (up-to-date)" % output_filename)
Greg Ward5e717441999-08-14 23:53:53 +0000232
Greg Ward4fecfce1999-10-03 20:45:33 +0000233 # link_shared_object ()
234
235
Greg Wardc9f31872000-01-09 22:47:53 +0000236 def link_executable (self,
237 objects,
238 output_progname,
239 output_dir=None,
240 libraries=None,
241 library_dirs=None,
Greg Warde21dabe2000-03-26 21:40:19 +0000242 runtime_library_dirs=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000243 debug=0,
Greg Wardc9f31872000-01-09 22:47:53 +0000244 extra_preargs=None,
245 extra_postargs=None):
246
Greg Warde21dabe2000-03-26 21:40:19 +0000247 (objects, output_dir) = self._fix_object_args (objects, output_dir)
248 (libraries, library_dirs, runtime_library_dirs) = \
249 self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
Greg Wardc9f31872000-01-09 22:47:53 +0000250
Greg Wardd03f88a2000-03-18 15:19:51 +0000251 lib_opts = gen_lib_options (self,
Greg Warde21dabe2000-03-26 21:40:19 +0000252 library_dirs, runtime_library_dirs,
Greg Wardd03f88a2000-03-18 15:19:51 +0000253 libraries)
Greg Wardc9f31872000-01-09 22:47:53 +0000254 output_filename = output_progname # Unix-ism!
255 if output_dir is not None:
256 output_filename = os.path.join (output_dir, output_filename)
257
Greg Ward32c4a8a2000-03-06 03:40:29 +0000258 if self._need_link (objects, output_filename):
259 ld_args = objects + self.objects + lib_opts + ['-o', output_filename]
Greg Wardba233fb2000-02-09 02:17:00 +0000260 if debug:
261 ld_args[:0] = ['-g']
Greg Wardc9f31872000-01-09 22:47:53 +0000262 if extra_preargs:
263 ld_args[:0] = extra_preargs
264 if extra_postargs:
265 ld_args.extend (extra_postargs)
Greg Wardf1146572000-03-01 14:43:49 +0000266 self.mkpath (os.path.dirname (output_filename))
Greg Wardc9f31872000-01-09 22:47:53 +0000267 self.spawn ([self.ld_exec] + ld_args)
268 else:
269 self.announce ("skipping %s (up-to-date)" % output_filename)
270
271 # link_executable ()
272
273
Greg Ward32c4a8a2000-03-06 03:40:29 +0000274 # -- Miscellaneous methods -----------------------------------------
275 # These are all used by the 'gen_lib_options() function, in
276 # ccompiler.py.
277
Greg Ward4fecfce1999-10-03 20:45:33 +0000278 def library_dir_option (self, dir):
279 return "-L" + dir
280
Greg Wardd03f88a2000-03-18 15:19:51 +0000281 def runtime_library_dir_option (self, dir):
282 return "-R" + dir
283
Greg Ward4fecfce1999-10-03 20:45:33 +0000284 def library_option (self, lib):
285 return "-l" + lib
286
287
288 def find_library_file (self, dirs, lib):
289
290 for dir in dirs:
291 shared = os.path.join (dir, self.shared_library_filename (lib))
292 static = os.path.join (dir, self.library_filename (lib))
293
294 # We're second-guessing the linker here, with not much hard
295 # data to go on: GCC seems to prefer the shared library, so I'm
296 # assuming that *all* Unix C compilers do. And of course I'm
297 # ignoring even GCC's "-static" option. So sue me.
298 if os.path.exists (shared):
299 return shared
300 elif os.path.exists (static):
301 return static
302
303 else:
304 # Oops, didn't find it in *any* of 'dirs'
305 return None
306
307 # find_library_file ()
Greg Ward65f4a3b1999-08-29 18:23:32 +0000308
Greg Ward170bdc01999-07-10 02:04:22 +0000309# class UnixCCompiler
310
311
312def _split_command (cmd):
313 """Split a command string up into the progam to run (a string) and
314 the list of arguments; return them as (cmd, arglist)."""
315 args = string.split (cmd)
316 return (args[0], args[1:])