blob: 47d8ad6325e91649a79c403dceb908f4e7236e88 [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 Ward3ff3b032000-06-21 02:58:46 +000024from distutils.dep_util import newer
Greg Wardd1517112000-05-30 01:56:44 +000025from distutils.ccompiler import \
Greg Ward3add77f2000-05-30 02:02:49 +000026 CCompiler, gen_preprocess_options, gen_lib_options
27from distutils.errors import \
28 DistutilsExecError, CompileError, LibError, LinkError
Greg Ward170bdc01999-07-10 02:04:22 +000029
30# XXX Things not currently handled:
31# * optimization/debug/warning flags; we just use whatever's in Python's
32# Makefile and live with it. Is this adequate? If not, we might
33# have to have a bunch of subclasses GNUCCompiler, SGICCompiler,
34# SunCCompiler, and I suspect down that road lies madness.
35# * even if we don't know a warning flag from an optimization flag,
36# we need some way for outsiders to feed preprocessor/compiler/linker
37# flags in to us -- eg. a sysadmin might want to mandate certain flags
38# via a site config file, or a user might want to set something for
39# compiling this module distribution only via the setup.py command
40# line, whatever. As long as these options come from something on the
41# current system, they can be as system-dependent as they like, and we
42# should just happily stuff them into the preprocessor/compiler/linker
43# options and carry on.
44
45
46class UnixCCompiler (CCompiler):
47
Greg Ward5e717441999-08-14 23:53:53 +000048 # XXX perhaps there should really be *three* kinds of include
49 # directories: those built in to the preprocessor, those from Python's
50 # Makefiles, and those supplied to {add,set}_include_dirs(). Currently
51 # we make no distinction between the latter two at this point; it's all
52 # up to the client class to select the include directories to use above
53 # and beyond the compiler's defaults. That is, both the Python include
54 # directories and any module- or package-specific include directories
55 # are specified via {add,set}_include_dirs(), and there's no way to
56 # distinguish them. This might be a bug.
Greg Ward65f4a3b1999-08-29 18:23:32 +000057
Greg Ward0e3530b1999-09-29 12:22:50 +000058 compiler_type = 'unix'
59
Greg Ward32c4a8a2000-03-06 03:40:29 +000060 # Needed for the filename generation methods provided by the
61 # base class, CCompiler.
62 src_extensions = [".c",".C",".cc",".cxx",".cpp"]
63 obj_extension = ".o"
64 static_lib_extension = ".a"
Greg Ward1c793302000-04-14 00:48:15 +000065 shared_lib_extension = sysconfig.SO
Greg Ward32c4a8a2000-03-06 03:40:29 +000066 static_lib_format = shared_lib_format = "lib%s%s"
Greg Wardc9f31872000-01-09 22:47:53 +000067
68 # Command to create a static library: seems to be pretty consistent
69 # across the major Unices. Might have to move down into the
70 # constructor if we need platform-specific guesswork.
Greg Ward1c793302000-04-14 00:48:15 +000071 archiver = sysconfig.AR
Greg Wardc9f31872000-01-09 22:47:53 +000072 archiver_options = "-cr"
Greg Ward1c793302000-04-14 00:48:15 +000073 ranlib = sysconfig.RANLIB
Greg Wardc9f31872000-01-09 22:47:53 +000074
75
Greg Ward5e717441999-08-14 23:53:53 +000076 def __init__ (self,
77 verbose=0,
Greg Ward4fecfce1999-10-03 20:45:33 +000078 dry_run=0,
79 force=0):
Greg Ward170bdc01999-07-10 02:04:22 +000080
Greg Ward4fecfce1999-10-03 20:45:33 +000081 CCompiler.__init__ (self, verbose, dry_run, force)
Greg Ward170bdc01999-07-10 02:04:22 +000082
83 self.preprocess_options = None
84 self.compile_options = None
85
Greg Ward5e717441999-08-14 23:53:53 +000086 # Munge CC and OPT together in case there are flags stuck in CC.
87 # Note that using these variables from sysconfig immediately makes
88 # this module specific to building Python extensions and
89 # inappropriate as a general-purpose C compiler front-end. So sue
90 # me. Note also that we use OPT rather than CFLAGS, because CFLAGS
91 # is the flags used to compile Python itself -- not only are there
92 # -I options in there, they are the *wrong* -I options. We'll
93 # leave selection of include directories up to the class using
94 # UnixCCompiler!
95
Greg Ward170bdc01999-07-10 02:04:22 +000096 (self.cc, self.ccflags) = \
Greg Ward1c793302000-04-14 00:48:15 +000097 _split_command (sysconfig.CC + ' ' + sysconfig.OPT)
98 self.ccflags_shared = string.split (sysconfig.CCSHARED)
Greg Ward170bdc01999-07-10 02:04:22 +000099
100 (self.ld_shared, self.ldflags_shared) = \
Greg Ward1c793302000-04-14 00:48:15 +0000101 _split_command (sysconfig.LDSHARED)
Greg Ward170bdc01999-07-10 02:04:22 +0000102
Greg Wardc9f31872000-01-09 22:47:53 +0000103 self.ld_exec = self.cc
104
Greg Ward32c4a8a2000-03-06 03:40:29 +0000105 # __init__ ()
106
Greg Ward170bdc01999-07-10 02:04:22 +0000107
Greg Ward3ff3b032000-06-21 02:58:46 +0000108 def preprocess (self,
109 source,
110 output_file=None,
111 macros=None,
112 include_dirs=None,
113 extra_preargs=None,
114 extra_postargs=None):
115
116 (_, macros, include_dirs) = \
117 self._fix_compile_args (None, macros, include_dirs)
118 pp_opts = gen_preprocess_options (macros, include_dirs)
119 cc_args = ['-E'] + pp_opts
120 if output_file:
121 cc_args.extend(['-o', output_file])
122 if extra_preargs:
123 cc_args[:0] = extra_preargs
124 if extra_postargs:
125 extra_postargs.extend(extra_postargs)
126
127 # We need to preprocess: either we're being forced to, or the
128 # source file is newer than the target (or the target doesn't
129 # exist).
130 if self.force or (output_file and newer(source, output_file)):
131 if output_file:
132 self.mkpath(os.path.dirname(output_file))
133 try:
134 self.spawn ([self.cc] + cc_args)
135 except DistutilsExecError, msg:
136 raise CompileError, msg
137
138
Greg Ward170bdc01999-07-10 02:04:22 +0000139 def compile (self,
140 sources,
Greg Ward8037cb11999-09-13 03:12:53 +0000141 output_dir=None,
Greg Ward5e717441999-08-14 23:53:53 +0000142 macros=None,
Greg Ward04d78321999-12-12 16:57:47 +0000143 include_dirs=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000144 debug=0,
Greg Ward0e3530b1999-09-29 12:22:50 +0000145 extra_preargs=None,
146 extra_postargs=None):
Greg Ward5e717441999-08-14 23:53:53 +0000147
Greg Ward32c4a8a2000-03-06 03:40:29 +0000148 (output_dir, macros, include_dirs) = \
149 self._fix_compile_args (output_dir, macros, include_dirs)
150 (objects, skip_sources) = self._prep_compile (sources, output_dir)
Greg Ward170bdc01999-07-10 02:04:22 +0000151
Greg Ward32c4a8a2000-03-06 03:40:29 +0000152 # Figure out the options for the compiler command line.
153 pp_opts = gen_preprocess_options (macros, include_dirs)
Greg Wardef6f5152000-02-03 23:07:19 +0000154 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 Ward32c4a8a2000-03-06 03:40:29 +0000162 # Compile all source files that weren't eliminated by
163 # '_prep_compile()'.
164 for i in range (len (sources)):
165 src = sources[i] ; obj = objects[i]
166 if skip_sources[src]:
167 self.announce ("skipping %s (%s up-to-date)" % (src, obj))
168 else:
169 self.mkpath (os.path.dirname (obj))
Greg Wardd1517112000-05-30 01:56:44 +0000170 try:
171 self.spawn ([self.cc] + cc_args +
172 [src, '-o', obj] +
173 extra_postargs)
174 except DistutilsExecError, msg:
175 raise CompileError, msg
Greg Ward8037cb11999-09-13 03:12:53 +0000176
Greg Ward32c4a8a2000-03-06 03:40:29 +0000177 # Return *all* object filenames, not just the ones we just built.
178 return objects
Greg Ward170bdc01999-07-10 02:04:22 +0000179
Greg Ward32c4a8a2000-03-06 03:40:29 +0000180 # compile ()
181
Greg Wardc9f31872000-01-09 22:47:53 +0000182
Greg Ward036c8052000-03-10 01:48:32 +0000183 def create_static_lib (self,
184 objects,
185 output_libname,
186 output_dir=None,
187 debug=0):
Greg Wardc9f31872000-01-09 22:47:53 +0000188
Greg Warde21dabe2000-03-26 21:40:19 +0000189 (objects, output_dir) = self._fix_object_args (objects, output_dir)
Greg Wardc9f31872000-01-09 22:47:53 +0000190
Greg Ward32c4a8a2000-03-06 03:40:29 +0000191 output_filename = \
192 self.library_filename (output_libname, output_dir=output_dir)
Greg Wardc9f31872000-01-09 22:47:53 +0000193
Greg Ward32c4a8a2000-03-06 03:40:29 +0000194 if self._need_link (objects, output_filename):
Greg Wardf1146572000-03-01 14:43:49 +0000195 self.mkpath (os.path.dirname (output_filename))
Greg Wardc9f31872000-01-09 22:47:53 +0000196 self.spawn ([self.archiver,
197 self.archiver_options,
198 output_filename] +
Greg Ward32c4a8a2000-03-06 03:40:29 +0000199 objects + self.objects)
Greg Ward1c793302000-04-14 00:48:15 +0000200
Greg Ward8eef5832000-04-14 13:53:34 +0000201 # Not many Unices required ranlib anymore -- SunOS 4.x is, I
202 # think the only major Unix that does. Maybe we need some
203 # platform intelligence here to skip ranlib if it's not
204 # needed -- or maybe Python's configure script took care of
205 # it for us, hence the check for leading colon.
206 if self.ranlib[0] != ':':
Greg Wardd1517112000-05-30 01:56:44 +0000207 try:
208 self.spawn ([self.ranlib, output_filename])
209 except DistutilsExecError, msg:
210 raise LibError, msg
Greg Wardc9f31872000-01-09 22:47:53 +0000211 else:
212 self.announce ("skipping %s (up-to-date)" % output_filename)
213
Greg Ward036c8052000-03-10 01:48:32 +0000214 # create_static_lib ()
Greg Wardc9f31872000-01-09 22:47:53 +0000215
Greg Ward170bdc01999-07-10 02:04:22 +0000216
217 def link_shared_lib (self,
218 objects,
219 output_libname,
Greg Ward8037cb11999-09-13 03:12:53 +0000220 output_dir=None,
Greg Ward170bdc01999-07-10 02:04:22 +0000221 libraries=None,
Greg Ward65f4a3b1999-08-29 18:23:32 +0000222 library_dirs=None,
Greg Warde21dabe2000-03-26 21:40:19 +0000223 runtime_library_dirs=None,
Greg Ward5299b6a2000-05-20 13:23:21 +0000224 export_symbols=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000225 debug=0,
Greg Ward0e3530b1999-09-29 12:22:50 +0000226 extra_preargs=None,
227 extra_postargs=None):
Greg Ward8037cb11999-09-13 03:12:53 +0000228 self.link_shared_object (
229 objects,
Greg Ward32c4a8a2000-03-06 03:40:29 +0000230 self.shared_library_filename (output_libname),
Greg Ward8037cb11999-09-13 03:12:53 +0000231 output_dir,
232 libraries,
233 library_dirs,
Greg Warde21dabe2000-03-26 21:40:19 +0000234 runtime_library_dirs,
Greg Ward5299b6a2000-05-20 13:23:21 +0000235 export_symbols,
Greg Wardba233fb2000-02-09 02:17:00 +0000236 debug,
Greg Ward0e3530b1999-09-29 12:22:50 +0000237 extra_preargs,
238 extra_postargs)
Greg Ward8037cb11999-09-13 03:12:53 +0000239
Greg Ward170bdc01999-07-10 02:04:22 +0000240
241 def link_shared_object (self,
242 objects,
243 output_filename,
Greg Ward8037cb11999-09-13 03:12:53 +0000244 output_dir=None,
Greg Ward5e717441999-08-14 23:53:53 +0000245 libraries=None,
Greg Ward65f4a3b1999-08-29 18:23:32 +0000246 library_dirs=None,
Greg Warde21dabe2000-03-26 21:40:19 +0000247 runtime_library_dirs=None,
Greg Ward5299b6a2000-05-20 13:23:21 +0000248 export_symbols=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000249 debug=0,
Greg Ward0e3530b1999-09-29 12:22:50 +0000250 extra_preargs=None,
251 extra_postargs=None):
Greg Ward5e717441999-08-14 23:53:53 +0000252
Greg Warde21dabe2000-03-26 21:40:19 +0000253 (objects, output_dir) = self._fix_object_args (objects, output_dir)
254 (libraries, library_dirs, runtime_library_dirs) = \
255 self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
Greg Ward04d78321999-12-12 16:57:47 +0000256
Greg Wardd03f88a2000-03-18 15:19:51 +0000257 lib_opts = gen_lib_options (self,
Greg Warde21dabe2000-03-26 21:40:19 +0000258 library_dirs, runtime_library_dirs,
Greg Wardd03f88a2000-03-18 15:19:51 +0000259 libraries)
Greg Ward10ca82b2000-02-10 02:51:32 +0000260 if type (output_dir) not in (StringType, NoneType):
261 raise TypeError, "'output_dir' must be a string or None"
Greg Ward8037cb11999-09-13 03:12:53 +0000262 if output_dir is not None:
263 output_filename = os.path.join (output_dir, output_filename)
Greg Ward170bdc01999-07-10 02:04:22 +0000264
Greg Ward32c4a8a2000-03-06 03:40:29 +0000265 if self._need_link (objects, output_filename):
266 ld_args = (self.ldflags_shared + objects + self.objects +
267 lib_opts + ['-o', output_filename])
Greg Wardba233fb2000-02-09 02:17:00 +0000268 if debug:
269 ld_args[:0] = ['-g']
Greg Ward0e3530b1999-09-29 12:22:50 +0000270 if extra_preargs:
271 ld_args[:0] = extra_preargs
272 if extra_postargs:
273 ld_args.extend (extra_postargs)
Greg Wardf1146572000-03-01 14:43:49 +0000274 self.mkpath (os.path.dirname (output_filename))
Greg Wardd1517112000-05-30 01:56:44 +0000275 try:
276 self.spawn ([self.ld_shared] + ld_args)
277 except DistutilsExecError, msg:
278 raise LinkError, msg
Greg Ward8037cb11999-09-13 03:12:53 +0000279 else:
280 self.announce ("skipping %s (up-to-date)" % output_filename)
Greg Ward5e717441999-08-14 23:53:53 +0000281
Greg Ward4fecfce1999-10-03 20:45:33 +0000282 # link_shared_object ()
283
284
Greg Wardc9f31872000-01-09 22:47:53 +0000285 def link_executable (self,
286 objects,
287 output_progname,
288 output_dir=None,
289 libraries=None,
290 library_dirs=None,
Greg Warde21dabe2000-03-26 21:40:19 +0000291 runtime_library_dirs=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000292 debug=0,
Greg Wardc9f31872000-01-09 22:47:53 +0000293 extra_preargs=None,
294 extra_postargs=None):
295
Greg Warde21dabe2000-03-26 21:40:19 +0000296 (objects, output_dir) = self._fix_object_args (objects, output_dir)
297 (libraries, library_dirs, runtime_library_dirs) = \
298 self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
Greg Wardc9f31872000-01-09 22:47:53 +0000299
Greg Wardd03f88a2000-03-18 15:19:51 +0000300 lib_opts = gen_lib_options (self,
Greg Warde21dabe2000-03-26 21:40:19 +0000301 library_dirs, runtime_library_dirs,
Greg Wardd03f88a2000-03-18 15:19:51 +0000302 libraries)
Greg Wardc9f31872000-01-09 22:47:53 +0000303 output_filename = output_progname # Unix-ism!
304 if output_dir is not None:
305 output_filename = os.path.join (output_dir, output_filename)
306
Greg Ward32c4a8a2000-03-06 03:40:29 +0000307 if self._need_link (objects, output_filename):
308 ld_args = objects + self.objects + lib_opts + ['-o', output_filename]
Greg Wardba233fb2000-02-09 02:17:00 +0000309 if debug:
310 ld_args[:0] = ['-g']
Greg Wardc9f31872000-01-09 22:47:53 +0000311 if extra_preargs:
312 ld_args[:0] = extra_preargs
313 if extra_postargs:
314 ld_args.extend (extra_postargs)
Greg Wardf1146572000-03-01 14:43:49 +0000315 self.mkpath (os.path.dirname (output_filename))
Greg Wardd1517112000-05-30 01:56:44 +0000316 try:
317 self.spawn ([self.ld_exec] + ld_args)
318 except DistutilsExecError, msg:
319 raise LinkError, msg
Greg Wardc9f31872000-01-09 22:47:53 +0000320 else:
321 self.announce ("skipping %s (up-to-date)" % output_filename)
322
323 # link_executable ()
324
325
Greg Ward32c4a8a2000-03-06 03:40:29 +0000326 # -- Miscellaneous methods -----------------------------------------
327 # These are all used by the 'gen_lib_options() function, in
328 # ccompiler.py.
329
Greg Ward4fecfce1999-10-03 20:45:33 +0000330 def library_dir_option (self, dir):
331 return "-L" + dir
332
Greg Wardd03f88a2000-03-18 15:19:51 +0000333 def runtime_library_dir_option (self, dir):
334 return "-R" + dir
335
Greg Ward4fecfce1999-10-03 20:45:33 +0000336 def library_option (self, lib):
337 return "-l" + lib
338
339
340 def find_library_file (self, dirs, lib):
341
342 for dir in dirs:
343 shared = os.path.join (dir, self.shared_library_filename (lib))
344 static = os.path.join (dir, self.library_filename (lib))
345
346 # We're second-guessing the linker here, with not much hard
347 # data to go on: GCC seems to prefer the shared library, so I'm
348 # assuming that *all* Unix C compilers do. And of course I'm
349 # ignoring even GCC's "-static" option. So sue me.
350 if os.path.exists (shared):
351 return shared
352 elif os.path.exists (static):
353 return static
354
355 else:
356 # Oops, didn't find it in *any* of 'dirs'
357 return None
358
359 # find_library_file ()
Greg Ward65f4a3b1999-08-29 18:23:32 +0000360
Greg Ward170bdc01999-07-10 02:04:22 +0000361# class UnixCCompiler
362
363
364def _split_command (cmd):
365 """Split a command string up into the progam to run (a string) and
366 the list of arguments; return them as (cmd, arglist)."""
367 args = string.split (cmd)
368 return (args[0], args[1:])