blob: 35390de0ce1e627541f3b2fcb2006b5d722efb30 [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 Ward32c4a8a2000-03-06 03:40:29 +000023from distutils.sysconfig import \
Greg Ward170bdc01999-07-10 02:04:22 +000024 CC, CCSHARED, CFLAGS, OPT, LDSHARED, LDFLAGS, RANLIB, AR, SO
Greg Ward32c4a8a2000-03-06 03:40:29 +000025from distutils.ccompiler import CCompiler, gen_preprocess_options, gen_lib_options
Greg Ward170bdc01999-07-10 02:04:22 +000026
27# XXX Things not currently handled:
28# * optimization/debug/warning flags; we just use whatever's in Python's
29# Makefile and live with it. Is this adequate? If not, we might
30# have to have a bunch of subclasses GNUCCompiler, SGICCompiler,
31# SunCCompiler, and I suspect down that road lies madness.
32# * even if we don't know a warning flag from an optimization flag,
33# we need some way for outsiders to feed preprocessor/compiler/linker
34# flags in to us -- eg. a sysadmin might want to mandate certain flags
35# via a site config file, or a user might want to set something for
36# compiling this module distribution only via the setup.py command
37# line, whatever. As long as these options come from something on the
38# current system, they can be as system-dependent as they like, and we
39# should just happily stuff them into the preprocessor/compiler/linker
40# options and carry on.
41
42
43class UnixCCompiler (CCompiler):
44
Greg Ward5e717441999-08-14 23:53:53 +000045 # XXX perhaps there should really be *three* kinds of include
46 # directories: those built in to the preprocessor, those from Python's
47 # Makefiles, and those supplied to {add,set}_include_dirs(). Currently
48 # we make no distinction between the latter two at this point; it's all
49 # up to the client class to select the include directories to use above
50 # and beyond the compiler's defaults. That is, both the Python include
51 # directories and any module- or package-specific include directories
52 # are specified via {add,set}_include_dirs(), and there's no way to
53 # distinguish them. This might be a bug.
Greg Ward65f4a3b1999-08-29 18:23:32 +000054
Greg Ward0e3530b1999-09-29 12:22:50 +000055 compiler_type = 'unix'
56
Greg Ward32c4a8a2000-03-06 03:40:29 +000057 # Needed for the filename generation methods provided by the
58 # base class, CCompiler.
59 src_extensions = [".c",".C",".cc",".cxx",".cpp"]
60 obj_extension = ".o"
61 static_lib_extension = ".a"
62 shared_lib_extension = ".so"
63 static_lib_format = shared_lib_format = "lib%s%s"
Greg Wardc9f31872000-01-09 22:47:53 +000064
65 # Command to create a static library: seems to be pretty consistent
66 # across the major Unices. Might have to move down into the
67 # constructor if we need platform-specific guesswork.
68 archiver = "ar"
69 archiver_options = "-cr"
70
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) = \
93 _split_command (CC + ' ' + OPT)
94 self.ccflags_shared = string.split (CCSHARED)
95
96 (self.ld_shared, self.ldflags_shared) = \
97 _split_command (LDSHARED)
98
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
143 def link_static_lib (self,
144 objects,
145 output_libname,
Greg Wardba233fb2000-02-09 02:17:00 +0000146 output_dir=None,
147 debug=0):
Greg Wardc9f31872000-01-09 22:47:53 +0000148
Greg Ward32c4a8a2000-03-06 03:40:29 +0000149 (objects, output_dir) = self._fix_link_args (objects, output_dir, takes_libs=0)
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 Wardc9f31872000-01-09 22:47:53 +0000160 else:
161 self.announce ("skipping %s (up-to-date)" % output_filename)
162
163 # link_static_lib ()
164
Greg Ward170bdc01999-07-10 02:04:22 +0000165
166 def link_shared_lib (self,
167 objects,
168 output_libname,
Greg Ward8037cb11999-09-13 03:12:53 +0000169 output_dir=None,
Greg Ward170bdc01999-07-10 02:04:22 +0000170 libraries=None,
Greg Ward65f4a3b1999-08-29 18:23:32 +0000171 library_dirs=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000172 debug=0,
Greg Ward0e3530b1999-09-29 12:22:50 +0000173 extra_preargs=None,
174 extra_postargs=None):
Greg Ward8037cb11999-09-13 03:12:53 +0000175 self.link_shared_object (
176 objects,
Greg Ward32c4a8a2000-03-06 03:40:29 +0000177 self.shared_library_filename (output_libname),
Greg Ward8037cb11999-09-13 03:12:53 +0000178 output_dir,
179 libraries,
180 library_dirs,
Greg Wardba233fb2000-02-09 02:17:00 +0000181 debug,
Greg Ward0e3530b1999-09-29 12:22:50 +0000182 extra_preargs,
183 extra_postargs)
Greg Ward8037cb11999-09-13 03:12:53 +0000184
Greg Ward170bdc01999-07-10 02:04:22 +0000185
186 def link_shared_object (self,
187 objects,
188 output_filename,
Greg Ward8037cb11999-09-13 03:12:53 +0000189 output_dir=None,
Greg Ward5e717441999-08-14 23:53:53 +0000190 libraries=None,
Greg Ward65f4a3b1999-08-29 18:23:32 +0000191 library_dirs=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000192 debug=0,
Greg Ward0e3530b1999-09-29 12:22:50 +0000193 extra_preargs=None,
194 extra_postargs=None):
Greg Ward5e717441999-08-14 23:53:53 +0000195
Greg Ward32c4a8a2000-03-06 03:40:29 +0000196 (objects, output_dir, libraries, library_dirs) = \
197 self._fix_link_args (objects, output_dir, takes_libs=1,
198 libraries=libraries, library_dirs=library_dirs)
Greg Ward04d78321999-12-12 16:57:47 +0000199
Greg Ward32c4a8a2000-03-06 03:40:29 +0000200 lib_opts = gen_lib_options (self, library_dirs, libraries)
Greg Ward10ca82b2000-02-10 02:51:32 +0000201 if type (output_dir) not in (StringType, NoneType):
202 raise TypeError, "'output_dir' must be a string or None"
Greg Ward8037cb11999-09-13 03:12:53 +0000203 if output_dir is not None:
204 output_filename = os.path.join (output_dir, output_filename)
Greg Ward170bdc01999-07-10 02:04:22 +0000205
Greg Ward32c4a8a2000-03-06 03:40:29 +0000206 if self._need_link (objects, output_filename):
207 ld_args = (self.ldflags_shared + objects + self.objects +
208 lib_opts + ['-o', output_filename])
Greg Wardba233fb2000-02-09 02:17:00 +0000209 if debug:
210 ld_args[:0] = ['-g']
Greg Ward0e3530b1999-09-29 12:22:50 +0000211 if extra_preargs:
212 ld_args[:0] = extra_preargs
213 if extra_postargs:
214 ld_args.extend (extra_postargs)
Greg Wardf1146572000-03-01 14:43:49 +0000215 self.mkpath (os.path.dirname (output_filename))
Greg Ward8037cb11999-09-13 03:12:53 +0000216 self.spawn ([self.ld_shared] + ld_args)
217 else:
218 self.announce ("skipping %s (up-to-date)" % output_filename)
Greg Ward5e717441999-08-14 23:53:53 +0000219
Greg Ward4fecfce1999-10-03 20:45:33 +0000220 # link_shared_object ()
221
222
Greg Wardc9f31872000-01-09 22:47:53 +0000223 def link_executable (self,
224 objects,
225 output_progname,
226 output_dir=None,
227 libraries=None,
228 library_dirs=None,
Greg Wardba233fb2000-02-09 02:17:00 +0000229 debug=0,
Greg Wardc9f31872000-01-09 22:47:53 +0000230 extra_preargs=None,
231 extra_postargs=None):
232
Greg Ward32c4a8a2000-03-06 03:40:29 +0000233 (objects, output_dir, libraries, library_dirs) = \
234 self._fix_link_args (objects, output_dir, takes_libs=1,
235 libraries=libraries, library_dirs=library_dirs)
Greg Wardc9f31872000-01-09 22:47:53 +0000236
Greg Ward32c4a8a2000-03-06 03:40:29 +0000237 lib_opts = gen_lib_options (self, library_dirs, libraries)
Greg Wardc9f31872000-01-09 22:47:53 +0000238 output_filename = output_progname # Unix-ism!
239 if output_dir is not None:
240 output_filename = os.path.join (output_dir, output_filename)
241
Greg Ward32c4a8a2000-03-06 03:40:29 +0000242 if self._need_link (objects, output_filename):
243 ld_args = objects + self.objects + lib_opts + ['-o', output_filename]
Greg Wardba233fb2000-02-09 02:17:00 +0000244 if debug:
245 ld_args[:0] = ['-g']
Greg Wardc9f31872000-01-09 22:47:53 +0000246 if extra_preargs:
247 ld_args[:0] = extra_preargs
248 if extra_postargs:
249 ld_args.extend (extra_postargs)
Greg Wardf1146572000-03-01 14:43:49 +0000250 self.mkpath (os.path.dirname (output_filename))
Greg Wardc9f31872000-01-09 22:47:53 +0000251 self.spawn ([self.ld_exec] + ld_args)
252 else:
253 self.announce ("skipping %s (up-to-date)" % output_filename)
254
255 # link_executable ()
256
257
Greg Ward32c4a8a2000-03-06 03:40:29 +0000258 # -- Miscellaneous methods -----------------------------------------
259 # These are all used by the 'gen_lib_options() function, in
260 # ccompiler.py.
261
Greg Ward4fecfce1999-10-03 20:45:33 +0000262 def library_dir_option (self, dir):
263 return "-L" + dir
264
265 def library_option (self, lib):
266 return "-l" + lib
267
268
269 def find_library_file (self, dirs, lib):
270
271 for dir in dirs:
272 shared = os.path.join (dir, self.shared_library_filename (lib))
273 static = os.path.join (dir, self.library_filename (lib))
274
275 # We're second-guessing the linker here, with not much hard
276 # data to go on: GCC seems to prefer the shared library, so I'm
277 # assuming that *all* Unix C compilers do. And of course I'm
278 # ignoring even GCC's "-static" option. So sue me.
279 if os.path.exists (shared):
280 return shared
281 elif os.path.exists (static):
282 return static
283
284 else:
285 # Oops, didn't find it in *any* of 'dirs'
286 return None
287
288 # find_library_file ()
Greg Ward65f4a3b1999-08-29 18:23:32 +0000289
Greg Ward170bdc01999-07-10 02:04:22 +0000290# class UnixCCompiler
291
292
293def _split_command (cmd):
294 """Split a command string up into the progam to run (a string) and
295 the list of arguments; return them as (cmd, arglist)."""
296 args = string.split (cmd)
297 return (args[0], args[1:])