blob: c2f841ff67d048169ad1314a68b43f5943d1134f [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 Wardd1517112000-05-30 01:56:44 +000024from distutils.ccompiler import \
25 CCompiler, gen_preprocess_options, gen_lib_options, \
26 CompileError, LibError, LinkError
27from distutils.errors import DistutilsExecError
Greg Ward170bdc01999-07-10 02:04:22 +000028
29# XXX Things not currently handled:
30# * optimization/debug/warning flags; we just use whatever's in Python's
31# Makefile and live with it. Is this adequate? If not, we might
32# have to have a bunch of subclasses GNUCCompiler, SGICCompiler,
33# SunCCompiler, and I suspect down that road lies madness.
34# * even if we don't know a warning flag from an optimization flag,
35# we need some way for outsiders to feed preprocessor/compiler/linker
36# flags in to us -- eg. a sysadmin might want to mandate certain flags
37# via a site config file, or a user might want to set something for
38# compiling this module distribution only via the setup.py command
39# line, whatever. As long as these options come from something on the
40# current system, they can be as system-dependent as they like, and we
41# should just happily stuff them into the preprocessor/compiler/linker
42# options and carry on.
43
44
45class UnixCCompiler (CCompiler):
46
Greg Ward5e717441999-08-14 23:53:53 +000047 # XXX perhaps there should really be *three* kinds of include
48 # directories: those built in to the preprocessor, those from Python's
49 # Makefiles, and those supplied to {add,set}_include_dirs(). Currently
50 # we make no distinction between the latter two at this point; it's all
51 # up to the client class to select the include directories to use above
52 # and beyond the compiler's defaults. That is, both the Python include
53 # directories and any module- or package-specific include directories
54 # are specified via {add,set}_include_dirs(), and there's no way to
55 # distinguish them. This might be a bug.
Greg Ward65f4a3b1999-08-29 18:23:32 +000056
Greg Ward0e3530b1999-09-29 12:22:50 +000057 compiler_type = 'unix'
58
Greg Ward32c4a8a2000-03-06 03:40:29 +000059 # Needed for the filename generation methods provided by the
60 # base class, CCompiler.
61 src_extensions = [".c",".C",".cc",".cxx",".cpp"]
62 obj_extension = ".o"
63 static_lib_extension = ".a"
Greg Ward1c793302000-04-14 00:48:15 +000064 shared_lib_extension = sysconfig.SO
Greg Ward32c4a8a2000-03-06 03:40:29 +000065 static_lib_format = shared_lib_format = "lib%s%s"
Greg Wardc9f31872000-01-09 22:47:53 +000066
67 # Command to create a static library: seems to be pretty consistent
68 # across the major Unices. Might have to move down into the
69 # constructor if we need platform-specific guesswork.
Greg Ward1c793302000-04-14 00:48:15 +000070 archiver = sysconfig.AR
Greg Wardc9f31872000-01-09 22:47:53 +000071 archiver_options = "-cr"
Greg Ward1c793302000-04-14 00:48:15 +000072 ranlib = sysconfig.RANLIB
Greg Wardc9f31872000-01-09 22:47:53 +000073
74
Greg Ward5e717441999-08-14 23:53:53 +000075 def __init__ (self,
76 verbose=0,
Greg Ward4fecfce1999-10-03 20:45:33 +000077 dry_run=0,
78 force=0):
Greg Ward170bdc01999-07-10 02:04:22 +000079
Greg Ward4fecfce1999-10-03 20:45:33 +000080 CCompiler.__init__ (self, verbose, dry_run, force)
Greg Ward170bdc01999-07-10 02:04:22 +000081
82 self.preprocess_options = None
83 self.compile_options = None
84
Greg Ward5e717441999-08-14 23:53:53 +000085 # Munge CC and OPT together in case there are flags stuck in CC.
86 # Note that using these variables from sysconfig immediately makes
87 # this module specific to building Python extensions and
88 # inappropriate as a general-purpose C compiler front-end. So sue
89 # me. Note also that we use OPT rather than CFLAGS, because CFLAGS
90 # is the flags used to compile Python itself -- not only are there
91 # -I options in there, they are the *wrong* -I options. We'll
92 # leave selection of include directories up to the class using
93 # UnixCCompiler!
94
Greg Ward170bdc01999-07-10 02:04:22 +000095 (self.cc, self.ccflags) = \
Greg Ward1c793302000-04-14 00:48:15 +000096 _split_command (sysconfig.CC + ' ' + sysconfig.OPT)
97 self.ccflags_shared = string.split (sysconfig.CCSHARED)
Greg Ward170bdc01999-07-10 02:04:22 +000098
99 (self.ld_shared, self.ldflags_shared) = \
Greg Ward1c793302000-04-14 00:48:15 +0000100 _split_command (sysconfig.LDSHARED)
Greg Ward170bdc01999-07-10 02:04:22 +0000101
Greg Wardc9f31872000-01-09 22:47:53 +0000102 self.ld_exec = self.cc
103
Greg Ward32c4a8a2000-03-06 03:40:29 +0000104 # __init__ ()
105
Greg Ward170bdc01999-07-10 02:04:22 +0000106
107 def compile (self,
108 sources,
Greg Ward8037cb11999-09-13 03:12:53 +0000109 output_dir=None,
Greg Ward5e717441999-08-14 23:53:53 +0000110 macros=None,
Greg Ward04d78321999-12-12 16:57:47 +0000111 include_dirs=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000112 debug=0,
Greg Ward0e3530b1999-09-29 12:22:50 +0000113 extra_preargs=None,
114 extra_postargs=None):
Greg Ward5e717441999-08-14 23:53:53 +0000115
Greg Ward32c4a8a2000-03-06 03:40:29 +0000116 (output_dir, macros, include_dirs) = \
117 self._fix_compile_args (output_dir, macros, include_dirs)
118 (objects, skip_sources) = self._prep_compile (sources, output_dir)
Greg Ward170bdc01999-07-10 02:04:22 +0000119
Greg Ward32c4a8a2000-03-06 03:40:29 +0000120 # Figure out the options for the compiler command line.
121 pp_opts = gen_preprocess_options (macros, include_dirs)
Greg Wardef6f5152000-02-03 23:07:19 +0000122 cc_args = ['-c'] + pp_opts + self.ccflags + self.ccflags_shared
Greg Wardba233fb2000-02-09 02:17:00 +0000123 if debug:
124 cc_args[:0] = ['-g']
Greg Wardef6f5152000-02-03 23:07:19 +0000125 if extra_preargs:
126 cc_args[:0] = extra_preargs
127 if extra_postargs is None:
128 extra_postargs = []
Greg Ward8037cb11999-09-13 03:12:53 +0000129
Greg Ward32c4a8a2000-03-06 03:40:29 +0000130 # Compile all source files that weren't eliminated by
131 # '_prep_compile()'.
132 for i in range (len (sources)):
133 src = sources[i] ; obj = objects[i]
134 if skip_sources[src]:
135 self.announce ("skipping %s (%s up-to-date)" % (src, obj))
136 else:
137 self.mkpath (os.path.dirname (obj))
Greg Wardd1517112000-05-30 01:56:44 +0000138 try:
139 self.spawn ([self.cc] + cc_args +
140 [src, '-o', obj] +
141 extra_postargs)
142 except DistutilsExecError, msg:
143 raise CompileError, msg
Greg Ward8037cb11999-09-13 03:12:53 +0000144
Greg Ward32c4a8a2000-03-06 03:40:29 +0000145 # Return *all* object filenames, not just the ones we just built.
146 return objects
Greg Ward170bdc01999-07-10 02:04:22 +0000147
Greg Ward32c4a8a2000-03-06 03:40:29 +0000148 # compile ()
149
Greg Wardc9f31872000-01-09 22:47:53 +0000150
Greg Ward036c8052000-03-10 01:48:32 +0000151 def create_static_lib (self,
152 objects,
153 output_libname,
154 output_dir=None,
155 debug=0):
Greg Wardc9f31872000-01-09 22:47:53 +0000156
Greg Warde21dabe2000-03-26 21:40:19 +0000157 (objects, output_dir) = self._fix_object_args (objects, output_dir)
Greg Wardc9f31872000-01-09 22:47:53 +0000158
Greg Ward32c4a8a2000-03-06 03:40:29 +0000159 output_filename = \
160 self.library_filename (output_libname, output_dir=output_dir)
Greg Wardc9f31872000-01-09 22:47:53 +0000161
Greg Ward32c4a8a2000-03-06 03:40:29 +0000162 if self._need_link (objects, output_filename):
Greg Wardf1146572000-03-01 14:43:49 +0000163 self.mkpath (os.path.dirname (output_filename))
Greg Wardc9f31872000-01-09 22:47:53 +0000164 self.spawn ([self.archiver,
165 self.archiver_options,
166 output_filename] +
Greg Ward32c4a8a2000-03-06 03:40:29 +0000167 objects + self.objects)
Greg Ward1c793302000-04-14 00:48:15 +0000168
Greg Ward8eef5832000-04-14 13:53:34 +0000169 # Not many Unices required ranlib anymore -- SunOS 4.x is, I
170 # think the only major Unix that does. Maybe we need some
171 # platform intelligence here to skip ranlib if it's not
172 # needed -- or maybe Python's configure script took care of
173 # it for us, hence the check for leading colon.
174 if self.ranlib[0] != ':':
Greg Wardd1517112000-05-30 01:56:44 +0000175 try:
176 self.spawn ([self.ranlib, output_filename])
177 except DistutilsExecError, msg:
178 raise LibError, msg
Greg Wardc9f31872000-01-09 22:47:53 +0000179 else:
180 self.announce ("skipping %s (up-to-date)" % output_filename)
181
Greg Ward036c8052000-03-10 01:48:32 +0000182 # create_static_lib ()
Greg Wardc9f31872000-01-09 22:47:53 +0000183
Greg Ward170bdc01999-07-10 02:04:22 +0000184
185 def link_shared_lib (self,
186 objects,
187 output_libname,
Greg Ward8037cb11999-09-13 03:12:53 +0000188 output_dir=None,
Greg Ward170bdc01999-07-10 02:04:22 +0000189 libraries=None,
Greg Ward65f4a3b1999-08-29 18:23:32 +0000190 library_dirs=None,
Greg Warde21dabe2000-03-26 21:40:19 +0000191 runtime_library_dirs=None,
Greg Ward5299b6a2000-05-20 13:23:21 +0000192 export_symbols=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000193 debug=0,
Greg Ward0e3530b1999-09-29 12:22:50 +0000194 extra_preargs=None,
195 extra_postargs=None):
Greg Ward8037cb11999-09-13 03:12:53 +0000196 self.link_shared_object (
197 objects,
Greg Ward32c4a8a2000-03-06 03:40:29 +0000198 self.shared_library_filename (output_libname),
Greg Ward8037cb11999-09-13 03:12:53 +0000199 output_dir,
200 libraries,
201 library_dirs,
Greg Warde21dabe2000-03-26 21:40:19 +0000202 runtime_library_dirs,
Greg Ward5299b6a2000-05-20 13:23:21 +0000203 export_symbols,
Greg Wardba233fb2000-02-09 02:17:00 +0000204 debug,
Greg Ward0e3530b1999-09-29 12:22:50 +0000205 extra_preargs,
206 extra_postargs)
Greg Ward8037cb11999-09-13 03:12:53 +0000207
Greg Ward170bdc01999-07-10 02:04:22 +0000208
209 def link_shared_object (self,
210 objects,
211 output_filename,
Greg Ward8037cb11999-09-13 03:12:53 +0000212 output_dir=None,
Greg Ward5e717441999-08-14 23:53:53 +0000213 libraries=None,
Greg Ward65f4a3b1999-08-29 18:23:32 +0000214 library_dirs=None,
Greg Warde21dabe2000-03-26 21:40:19 +0000215 runtime_library_dirs=None,
Greg Ward5299b6a2000-05-20 13:23:21 +0000216 export_symbols=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000217 debug=0,
Greg Ward0e3530b1999-09-29 12:22:50 +0000218 extra_preargs=None,
219 extra_postargs=None):
Greg Ward5e717441999-08-14 23:53:53 +0000220
Greg Warde21dabe2000-03-26 21:40:19 +0000221 (objects, output_dir) = self._fix_object_args (objects, output_dir)
222 (libraries, library_dirs, runtime_library_dirs) = \
223 self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
Greg Ward04d78321999-12-12 16:57:47 +0000224
Greg Wardd03f88a2000-03-18 15:19:51 +0000225 lib_opts = gen_lib_options (self,
Greg Warde21dabe2000-03-26 21:40:19 +0000226 library_dirs, runtime_library_dirs,
Greg Wardd03f88a2000-03-18 15:19:51 +0000227 libraries)
Greg Ward10ca82b2000-02-10 02:51:32 +0000228 if type (output_dir) not in (StringType, NoneType):
229 raise TypeError, "'output_dir' must be a string or None"
Greg Ward8037cb11999-09-13 03:12:53 +0000230 if output_dir is not None:
231 output_filename = os.path.join (output_dir, output_filename)
Greg Ward170bdc01999-07-10 02:04:22 +0000232
Greg Ward32c4a8a2000-03-06 03:40:29 +0000233 if self._need_link (objects, output_filename):
234 ld_args = (self.ldflags_shared + objects + self.objects +
235 lib_opts + ['-o', output_filename])
Greg Wardba233fb2000-02-09 02:17:00 +0000236 if debug:
237 ld_args[:0] = ['-g']
Greg Ward0e3530b1999-09-29 12:22:50 +0000238 if extra_preargs:
239 ld_args[:0] = extra_preargs
240 if extra_postargs:
241 ld_args.extend (extra_postargs)
Greg Wardf1146572000-03-01 14:43:49 +0000242 self.mkpath (os.path.dirname (output_filename))
Greg Wardd1517112000-05-30 01:56:44 +0000243 try:
244 self.spawn ([self.ld_shared] + ld_args)
245 except DistutilsExecError, msg:
246 raise LinkError, msg
Greg Ward8037cb11999-09-13 03:12:53 +0000247 else:
248 self.announce ("skipping %s (up-to-date)" % output_filename)
Greg Ward5e717441999-08-14 23:53:53 +0000249
Greg Ward4fecfce1999-10-03 20:45:33 +0000250 # link_shared_object ()
251
252
Greg Wardc9f31872000-01-09 22:47:53 +0000253 def link_executable (self,
254 objects,
255 output_progname,
256 output_dir=None,
257 libraries=None,
258 library_dirs=None,
Greg Warde21dabe2000-03-26 21:40:19 +0000259 runtime_library_dirs=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000260 debug=0,
Greg Wardc9f31872000-01-09 22:47:53 +0000261 extra_preargs=None,
262 extra_postargs=None):
263
Greg Warde21dabe2000-03-26 21:40:19 +0000264 (objects, output_dir) = self._fix_object_args (objects, output_dir)
265 (libraries, library_dirs, runtime_library_dirs) = \
266 self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
Greg Wardc9f31872000-01-09 22:47:53 +0000267
Greg Wardd03f88a2000-03-18 15:19:51 +0000268 lib_opts = gen_lib_options (self,
Greg Warde21dabe2000-03-26 21:40:19 +0000269 library_dirs, runtime_library_dirs,
Greg Wardd03f88a2000-03-18 15:19:51 +0000270 libraries)
Greg Wardc9f31872000-01-09 22:47:53 +0000271 output_filename = output_progname # Unix-ism!
272 if output_dir is not None:
273 output_filename = os.path.join (output_dir, output_filename)
274
Greg Ward32c4a8a2000-03-06 03:40:29 +0000275 if self._need_link (objects, output_filename):
276 ld_args = objects + self.objects + lib_opts + ['-o', output_filename]
Greg Wardba233fb2000-02-09 02:17:00 +0000277 if debug:
278 ld_args[:0] = ['-g']
Greg Wardc9f31872000-01-09 22:47:53 +0000279 if extra_preargs:
280 ld_args[:0] = extra_preargs
281 if extra_postargs:
282 ld_args.extend (extra_postargs)
Greg Wardf1146572000-03-01 14:43:49 +0000283 self.mkpath (os.path.dirname (output_filename))
Greg Wardd1517112000-05-30 01:56:44 +0000284 try:
285 self.spawn ([self.ld_exec] + ld_args)
286 except DistutilsExecError, msg:
287 raise LinkError, msg
Greg Wardc9f31872000-01-09 22:47:53 +0000288 else:
289 self.announce ("skipping %s (up-to-date)" % output_filename)
290
291 # link_executable ()
292
293
Greg Ward32c4a8a2000-03-06 03:40:29 +0000294 # -- Miscellaneous methods -----------------------------------------
295 # These are all used by the 'gen_lib_options() function, in
296 # ccompiler.py.
297
Greg Ward4fecfce1999-10-03 20:45:33 +0000298 def library_dir_option (self, dir):
299 return "-L" + dir
300
Greg Wardd03f88a2000-03-18 15:19:51 +0000301 def runtime_library_dir_option (self, dir):
302 return "-R" + dir
303
Greg Ward4fecfce1999-10-03 20:45:33 +0000304 def library_option (self, lib):
305 return "-l" + lib
306
307
308 def find_library_file (self, dirs, lib):
309
310 for dir in dirs:
311 shared = os.path.join (dir, self.shared_library_filename (lib))
312 static = os.path.join (dir, self.library_filename (lib))
313
314 # We're second-guessing the linker here, with not much hard
315 # data to go on: GCC seems to prefer the shared library, so I'm
316 # assuming that *all* Unix C compilers do. And of course I'm
317 # ignoring even GCC's "-static" option. So sue me.
318 if os.path.exists (shared):
319 return shared
320 elif os.path.exists (static):
321 return static
322
323 else:
324 # Oops, didn't find it in *any* of 'dirs'
325 return None
326
327 # find_library_file ()
Greg Ward65f4a3b1999-08-29 18:23:32 +0000328
Greg Ward170bdc01999-07-10 02:04:22 +0000329# class UnixCCompiler
330
331
332def _split_command (cmd):
333 """Split a command string up into the progam to run (a string) and
334 the list of arguments; return them as (cmd, arglist)."""
335 args = string.split (cmd)
336 return (args[0], args[1:])