blob: 6594043c732355c6d5571d541bc9b2644f23da00 [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 Ward5e717441999-08-14 23:53:53 +000020import string, re
Greg Ward170bdc01999-07-10 02:04:22 +000021from types import *
22from sysconfig import \
23 CC, CCSHARED, CFLAGS, OPT, LDSHARED, LDFLAGS, RANLIB, AR, SO
24from ccompiler import CCompiler
25
26
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
55 _obj_ext = '.o'
56 _exe_ext = ''
57 _shared_lib_ext = SO
58 _static_lib_ext = '.a'
Greg Ward5e717441999-08-14 23:53:53 +000059
60 def __init__ (self,
61 verbose=0,
62 dry_run=0):
Greg Ward170bdc01999-07-10 02:04:22 +000063
Greg Ward5e717441999-08-14 23:53:53 +000064 CCompiler.__init__ (self, verbose, dry_run)
Greg Ward170bdc01999-07-10 02:04:22 +000065
66 self.preprocess_options = None
67 self.compile_options = None
68
Greg Ward5e717441999-08-14 23:53:53 +000069 # Munge CC and OPT together in case there are flags stuck in CC.
70 # Note that using these variables from sysconfig immediately makes
71 # this module specific to building Python extensions and
72 # inappropriate as a general-purpose C compiler front-end. So sue
73 # me. Note also that we use OPT rather than CFLAGS, because CFLAGS
74 # is the flags used to compile Python itself -- not only are there
75 # -I options in there, they are the *wrong* -I options. We'll
76 # leave selection of include directories up to the class using
77 # UnixCCompiler!
78
Greg Ward170bdc01999-07-10 02:04:22 +000079 (self.cc, self.ccflags) = \
80 _split_command (CC + ' ' + OPT)
81 self.ccflags_shared = string.split (CCSHARED)
82
83 (self.ld_shared, self.ldflags_shared) = \
84 _split_command (LDSHARED)
85
86
87 def compile (self,
88 sources,
Greg Ward5e717441999-08-14 23:53:53 +000089 macros=None,
90 includes=None):
91
92 if macros is None:
93 macros = []
94 if includes is None:
95 includes = []
Greg Ward170bdc01999-07-10 02:04:22 +000096
97 if type (macros) is not ListType:
98 raise TypeError, \
99 "'macros' (if supplied) must be a list of tuples"
100 if type (includes) is not ListType:
101 raise TypeError, \
102 "'includes' (if supplied) must be a list of strings"
103
104 pp_opts = _gen_preprocess_options (self.macros + macros,
105 self.include_dirs + includes)
106
107 # use of ccflags_shared means we're blithely assuming that we're
108 # compiling for inclusion in a shared object! (will have to fix
109 # this when I add the ability to build a new Python)
110 cc_args = ['-c'] + pp_opts + \
111 self.ccflags + self.ccflags_shared + \
112 sources
113
114 # this will change to 'spawn' when I have it!
Greg Ward5e717441999-08-14 23:53:53 +0000115 #print string.join ([self.cc] + cc_args, ' ')
116 self.spawn ([self.cc] + cc_args)
Greg Ward170bdc01999-07-10 02:04:22 +0000117
118
119 # XXX punting on 'link_static_lib()' for now -- it might be better for
120 # CCompiler to mandate just 'link_binary()' or some such to build a new
121 # Python binary; it would then take care of linking in everything
122 # needed for the new Python without messing with an intermediate static
123 # library.
124
125 def link_shared_lib (self,
126 objects,
127 output_libname,
128 libraries=None,
Greg Ward65f4a3b1999-08-29 18:23:32 +0000129 library_dirs=None,
130 build_info=None):
Greg Ward170bdc01999-07-10 02:04:22 +0000131 # XXX should we sanity check the library name? (eg. no
132 # slashes)
Greg Ward65f4a3b1999-08-29 18:23:32 +0000133 self.link_shared_object (objects, "lib%s%s" % \
134 (output_libname, self._shared_lib_ext),
135 build_info=build_info)
Greg Ward170bdc01999-07-10 02:04:22 +0000136
137
138 def link_shared_object (self,
139 objects,
140 output_filename,
Greg Ward5e717441999-08-14 23:53:53 +0000141 libraries=None,
Greg Ward65f4a3b1999-08-29 18:23:32 +0000142 library_dirs=None,
143 build_info=None):
Greg Ward5e717441999-08-14 23:53:53 +0000144
145 if libraries is None:
146 libraries = []
147 if library_dirs is None:
148 library_dirs = []
Greg Ward65f4a3b1999-08-29 18:23:32 +0000149 if build_info is None:
150 build_info = {}
151
Greg Ward170bdc01999-07-10 02:04:22 +0000152 lib_opts = _gen_lib_options (self.libraries + libraries,
153 self.library_dirs + library_dirs)
154 ld_args = self.ldflags_shared + lib_opts + \
155 objects + ['-o', output_filename]
156
Greg Ward5e717441999-08-14 23:53:53 +0000157 #print string.join ([self.ld_shared] + ld_args, ' ')
158 self.spawn ([self.ld_shared] + ld_args)
159
160
161 def object_filenames (self, source_filenames):
162 outnames = []
163 for inname in source_filenames:
Greg Ward65f4a3b1999-08-29 18:23:32 +0000164 outnames.append ( re.sub (r'\.(c|C|cc|cxx|cpp)$',
165 self._obj_ext, inname))
Greg Ward5e717441999-08-14 23:53:53 +0000166 return outnames
167
168 def shared_object_filename (self, source_filename):
Greg Ward65f4a3b1999-08-29 18:23:32 +0000169 return re.sub (r'\.(c|C|cc|cxx|cpp)$', self._shared_lib_ext)
Greg Ward5e717441999-08-14 23:53:53 +0000170
171 def library_filename (self, libname):
Greg Ward65f4a3b1999-08-29 18:23:32 +0000172 return "lib%s%s" % (libname, self._static_lib_ext )
Greg Ward5e717441999-08-14 23:53:53 +0000173
174 def shared_library_filename (self, libname):
Greg Ward65f4a3b1999-08-29 18:23:32 +0000175 return "lib%s%s" % (libname, self._shared_lib_ext )
176
Greg Ward5e717441999-08-14 23:53:53 +0000177
178
Greg Ward170bdc01999-07-10 02:04:22 +0000179# class UnixCCompiler
180
181
182def _split_command (cmd):
183 """Split a command string up into the progam to run (a string) and
184 the list of arguments; return them as (cmd, arglist)."""
185 args = string.split (cmd)
186 return (args[0], args[1:])
187
188
189def _gen_preprocess_options (macros, includes):
190
191 # XXX it would be nice (mainly aesthetic, and so we don't generate
192 # stupid-looking command lines) to go over 'macros' and eliminate
193 # redundant definitions/undefinitions (ie. ensure that only the
194 # latest mention of a particular macro winds up on the command
195 # line). I don't think it's essential, though, since most (all?)
196 # Unix C compilers only pay attention to the latest -D or -U
197 # mention of a macro on their command line. Similar situation for
198 # 'includes'. I'm punting on both for now. Anyways, weeding out
199 # redundancies like this should probably be the province of
200 # CCompiler, since the data structures used are inherited from it
201 # and therefore common to all CCompiler classes.
202
203
204 pp_opts = []
205 for macro in macros:
206 if len (macro) == 1: # undefine this macro
207 pp_opts.append ("-U%s" % macro[0])
208 elif len (macro) == 2:
209 if macro[1] is None: # define with no explicit value
210 pp_opts.append ("-D%s" % macro[0])
211 else:
212 # XXX *don't* need to be clever about quoting the
213 # macro value here, because we're going to avoid the
214 # shell at all costs when we spawn the command!
215 pp_opts.append ("-D%s=%s" % macro)
216
217 for dir in includes:
218 pp_opts.append ("-I%s" % dir)
219
220 return pp_opts
221
222# _gen_preprocess_options ()
223
224
225def _gen_lib_options (libraries, library_dirs):
226
227 lib_opts = []
228
229 for dir in library_dirs:
230 lib_opts.append ("-L%s" % dir)
231
232 # XXX it's important that we *not* remove redundant library mentions!
233 # sometimes you really do have to say "-lfoo -lbar -lfoo" in order to
234 # resolve all symbols. I just hope we never have to say "-lfoo obj.o
235 # -lbar" to get things to work -- that's certainly a possibility, but a
236 # pretty nasty way to arrange your C code.
237
238 for lib in libraries:
239 lib_opts.append ("-l%s" % lib)
240
241 return lib_opts
242
243# _gen_lib_options ()