blob: 01493131108a402a7d3f5028b1dcdb701bc8c0ff [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
56 _obj_ext = '.o'
57 _exe_ext = ''
58 _shared_lib_ext = SO
59 _static_lib_ext = '.a'
Greg Ward5e717441999-08-14 23:53:53 +000060
61 def __init__ (self,
62 verbose=0,
63 dry_run=0):
Greg Ward170bdc01999-07-10 02:04:22 +000064
Greg Ward5e717441999-08-14 23:53:53 +000065 CCompiler.__init__ (self, verbose, dry_run)
Greg Ward170bdc01999-07-10 02:04:22 +000066
67 self.preprocess_options = None
68 self.compile_options = None
69
Greg Ward5e717441999-08-14 23:53:53 +000070 # Munge CC and OPT together in case there are flags stuck in CC.
71 # Note that using these variables from sysconfig immediately makes
72 # this module specific to building Python extensions and
73 # inappropriate as a general-purpose C compiler front-end. So sue
74 # me. Note also that we use OPT rather than CFLAGS, because CFLAGS
75 # is the flags used to compile Python itself -- not only are there
76 # -I options in there, they are the *wrong* -I options. We'll
77 # leave selection of include directories up to the class using
78 # UnixCCompiler!
79
Greg Ward170bdc01999-07-10 02:04:22 +000080 (self.cc, self.ccflags) = \
81 _split_command (CC + ' ' + OPT)
82 self.ccflags_shared = string.split (CCSHARED)
83
84 (self.ld_shared, self.ldflags_shared) = \
85 _split_command (LDSHARED)
86
87
88 def compile (self,
89 sources,
Greg Ward8037cb11999-09-13 03:12:53 +000090 output_dir=None,
Greg Ward5e717441999-08-14 23:53:53 +000091 macros=None,
92 includes=None):
93
Greg Ward8037cb11999-09-13 03:12:53 +000094 if output_dir is None:
95 output_dir = self.output_dir
Greg Ward5e717441999-08-14 23:53:53 +000096 if macros is None:
97 macros = []
98 if includes is None:
99 includes = []
Greg Ward170bdc01999-07-10 02:04:22 +0000100
101 if type (macros) is not ListType:
102 raise TypeError, \
103 "'macros' (if supplied) must be a list of tuples"
104 if type (includes) is not ListType:
105 raise TypeError, \
106 "'includes' (if supplied) must be a list of strings"
107
Greg Wardc2941131999-09-08 02:32:19 +0000108 pp_opts = gen_preprocess_options (self.macros + macros,
109 self.include_dirs + includes)
Greg Ward170bdc01999-07-10 02:04:22 +0000110
Greg Ward8037cb11999-09-13 03:12:53 +0000111 # So we can mangle 'sources' without hurting the caller's data
112 orig_sources = sources
113 sources = copy (sources)
Greg Ward170bdc01999-07-10 02:04:22 +0000114
Greg Ward8037cb11999-09-13 03:12:53 +0000115 # Get the list of expected output (object) files and drop files we
116 # don't have to recompile. (Simplistic check -- we just compare the
117 # source and object file, no deep dependency checking involving
118 # header files. Hmmm.)
119 objects = self.object_filenames (sources, output_dir)
120 skipped = newer_pairwise (sources, objects)
121 for skipped_pair in skipped:
122 self.announce ("skipping %s (%s up-to-date)" % skipped_pair)
123
124 # If anything left to compile, compile it
125 if sources:
126 # XXX use of ccflags_shared means we're blithely assuming
127 # that we're compiling for inclusion in a shared object!
128 # (will have to fix this when I add the ability to build a
129 # new Python)
130 cc_args = ['-c'] + pp_opts + \
131 self.ccflags + self.ccflags_shared + \
132 sources
133 self.spawn ([self.cc] + cc_args)
134
135
136 # Note that compiling multiple source files in the same go like
137 # we've just done drops the .o file in the current directory, which
138 # may not be what the caller wants (depending on the 'output_dir'
139 # parameter). So, if necessary, fix that now by moving the .o
140 # files into the desired output directory. (The alternative, of
141 # course, is to compile one-at-a-time with a -o option. 6 of one,
142 # 12/2 of the other...)
143
144 if output_dir:
145 for i in range (len (objects)):
146 src = os.path.basename (objects[i])
147 objects[i] = self.move_file (src, output_dir)
148
149 # Have to re-fetch list of object filenames, because we want to
150 # return *all* of them, including those that weren't recompiled on
151 # this call!
152 return self.object_filenames (orig_sources, output_dir)
Greg Wardc2941131999-09-08 02:32:19 +0000153
Greg Ward170bdc01999-07-10 02:04:22 +0000154
155 # XXX punting on 'link_static_lib()' for now -- it might be better for
156 # CCompiler to mandate just 'link_binary()' or some such to build a new
157 # Python binary; it would then take care of linking in everything
158 # needed for the new Python without messing with an intermediate static
159 # library.
160
161 def link_shared_lib (self,
162 objects,
163 output_libname,
Greg Ward8037cb11999-09-13 03:12:53 +0000164 output_dir=None,
Greg Ward170bdc01999-07-10 02:04:22 +0000165 libraries=None,
Greg Ward65f4a3b1999-08-29 18:23:32 +0000166 library_dirs=None,
167 build_info=None):
Greg Ward170bdc01999-07-10 02:04:22 +0000168 # XXX should we sanity check the library name? (eg. no
169 # slashes)
Greg Ward8037cb11999-09-13 03:12:53 +0000170 self.link_shared_object (
171 objects,
172 "lib%s%s" % (output_libname, self._shared_lib_ext),
173 output_dir,
174 libraries,
175 library_dirs,
176 build_info)
177
Greg Ward170bdc01999-07-10 02:04:22 +0000178
179 def link_shared_object (self,
180 objects,
181 output_filename,
Greg Ward8037cb11999-09-13 03:12:53 +0000182 output_dir=None,
Greg Ward5e717441999-08-14 23:53:53 +0000183 libraries=None,
Greg Ward65f4a3b1999-08-29 18:23:32 +0000184 library_dirs=None,
185 build_info=None):
Greg Ward5e717441999-08-14 23:53:53 +0000186
Greg Ward8037cb11999-09-13 03:12:53 +0000187 if output_dir is None:
188 output_dir = self.output_dir
Greg Ward5e717441999-08-14 23:53:53 +0000189 if libraries is None:
190 libraries = []
191 if library_dirs is None:
192 library_dirs = []
Greg Ward65f4a3b1999-08-29 18:23:32 +0000193 if build_info is None:
194 build_info = {}
195
Greg Wardc2941131999-09-08 02:32:19 +0000196 lib_opts = gen_lib_options (self.libraries + libraries,
197 self.library_dirs + library_dirs,
198 "-l%s", "-L%s")
Greg Ward8037cb11999-09-13 03:12:53 +0000199 if output_dir is not None:
200 output_filename = os.path.join (output_dir, output_filename)
Greg Ward170bdc01999-07-10 02:04:22 +0000201
Greg Ward8037cb11999-09-13 03:12:53 +0000202 # If any of the input object files are newer than the output shared
203 # object, relink. Again, this is a simplistic dependency check:
204 # doesn't look at any of the libraries we might be linking with.
205 if newer_group (objects, output_filename):
206 ld_args = self.ldflags_shared + lib_opts + \
207 objects + ['-o', output_filename]
208
209 self.spawn ([self.ld_shared] + ld_args)
210 else:
211 self.announce ("skipping %s (up-to-date)" % output_filename)
Greg Ward5e717441999-08-14 23:53:53 +0000212
213
Greg Ward8037cb11999-09-13 03:12:53 +0000214 def object_filenames (self, source_filenames, output_dir=None):
Greg Ward5e717441999-08-14 23:53:53 +0000215 outnames = []
216 for inname in source_filenames:
Greg Ward8037cb11999-09-13 03:12:53 +0000217 outname = re.sub (r'\.(c|C|cc|cxx|cpp)$', self._obj_ext, inname)
218 outname = os.path.basename (outname)
219 if output_dir is not None:
220 outname = os.path.join (output_dir, outname)
221 outnames.append (outname)
Greg Ward5e717441999-08-14 23:53:53 +0000222 return outnames
223
Greg Ward8037cb11999-09-13 03:12:53 +0000224 def shared_object_filename (self, source_filename, output_dir=None):
225 outname = re.sub (r'\.(c|C|cc|cxx|cpp)$', self._shared_lib_ext)
226 outname = os.path.basename (outname)
227 if output_dir is not None:
228 outname = os.path.join (output_dir, outname)
229 return outname
Greg Ward5e717441999-08-14 23:53:53 +0000230
231 def library_filename (self, libname):
Greg Ward65f4a3b1999-08-29 18:23:32 +0000232 return "lib%s%s" % (libname, self._static_lib_ext )
Greg Ward5e717441999-08-14 23:53:53 +0000233
234 def shared_library_filename (self, libname):
Greg Ward65f4a3b1999-08-29 18:23:32 +0000235 return "lib%s%s" % (libname, self._shared_lib_ext )
236
Greg Ward170bdc01999-07-10 02:04:22 +0000237# class UnixCCompiler
238
239
240def _split_command (cmd):
241 """Split a command string up into the progam to run (a string) and
242 the list of arguments; return them as (cmd, arglist)."""
243 args = string.split (cmd)
244 return (args[0], args[1:])