blob: fb5ed6677ca07f107cde196b338ba79ad1b88d27 [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
20import string
21from 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
45 # XXX any -I and -D options that we get from Makefile (via sysconfig)
46 # are preserved, but not treated specially: that is, they are not put
47 # in the self.include_dirs and self.macros, etc. lists that we inherit
48 # from CCompiler. I'm not sure if this is right, wrong or indifferent,
49 # but it should probably be a documented part of the CCompiler API:
50 # ie. there are *three* kinds of include directories, those from the
51 # compiler, those from Python's Makefiles, and those supplied to
52 # {add,set}_include_dirs() -- and 'set_include_dirs()' only overrides
53 # the last kind! I suspect the same applies to libraries and library
54 # directories -- anything else?
55
56 def __init__ (self):
57
58 CCompiler.__init__ (self)
59
60 self.preprocess_options = None
61 self.compile_options = None
62
63 # munge CC and OPT together in case there are flags stuck in CC
64 (self.cc, self.ccflags) = \
65 _split_command (CC + ' ' + OPT)
66 self.ccflags_shared = string.split (CCSHARED)
67
68 (self.ld_shared, self.ldflags_shared) = \
69 _split_command (LDSHARED)
70
71
72 def compile (self,
73 sources,
74 macros=[],
75 includes=[]):
76
77 if type (macros) is not ListType:
78 raise TypeError, \
79 "'macros' (if supplied) must be a list of tuples"
80 if type (includes) is not ListType:
81 raise TypeError, \
82 "'includes' (if supplied) must be a list of strings"
83
84 pp_opts = _gen_preprocess_options (self.macros + macros,
85 self.include_dirs + includes)
86
87 # use of ccflags_shared means we're blithely assuming that we're
88 # compiling for inclusion in a shared object! (will have to fix
89 # this when I add the ability to build a new Python)
90 cc_args = ['-c'] + pp_opts + \
91 self.ccflags + self.ccflags_shared + \
92 sources
93
94 # this will change to 'spawn' when I have it!
95 print string.join ([self.cc] + cc_args, ' ')
96
97
98 # XXX punting on 'link_static_lib()' for now -- it might be better for
99 # CCompiler to mandate just 'link_binary()' or some such to build a new
100 # Python binary; it would then take care of linking in everything
101 # needed for the new Python without messing with an intermediate static
102 # library.
103
104 def link_shared_lib (self,
105 objects,
106 output_libname,
107 libraries=None,
108 library_dirs=None):
109 # XXX should we sanity check the library name? (eg. no
110 # slashes)
111 self.link_shared_object (objects, "lib%s%s" % (output_libname, SO))
112
113
114 def link_shared_object (self,
115 objects,
116 output_filename,
117 libraries=[],
118 library_dirs=[]):
119
120 lib_opts = _gen_lib_options (self.libraries + libraries,
121 self.library_dirs + library_dirs)
122 ld_args = self.ldflags_shared + lib_opts + \
123 objects + ['-o', output_filename]
124
125 print string.join ([self.ld_shared] + ld_args, ' ')
126
127
128# class UnixCCompiler
129
130
131def _split_command (cmd):
132 """Split a command string up into the progam to run (a string) and
133 the list of arguments; return them as (cmd, arglist)."""
134 args = string.split (cmd)
135 return (args[0], args[1:])
136
137
138def _gen_preprocess_options (macros, includes):
139
140 # XXX it would be nice (mainly aesthetic, and so we don't generate
141 # stupid-looking command lines) to go over 'macros' and eliminate
142 # redundant definitions/undefinitions (ie. ensure that only the
143 # latest mention of a particular macro winds up on the command
144 # line). I don't think it's essential, though, since most (all?)
145 # Unix C compilers only pay attention to the latest -D or -U
146 # mention of a macro on their command line. Similar situation for
147 # 'includes'. I'm punting on both for now. Anyways, weeding out
148 # redundancies like this should probably be the province of
149 # CCompiler, since the data structures used are inherited from it
150 # and therefore common to all CCompiler classes.
151
152
153 pp_opts = []
154 for macro in macros:
155 if len (macro) == 1: # undefine this macro
156 pp_opts.append ("-U%s" % macro[0])
157 elif len (macro) == 2:
158 if macro[1] is None: # define with no explicit value
159 pp_opts.append ("-D%s" % macro[0])
160 else:
161 # XXX *don't* need to be clever about quoting the
162 # macro value here, because we're going to avoid the
163 # shell at all costs when we spawn the command!
164 pp_opts.append ("-D%s=%s" % macro)
165
166 for dir in includes:
167 pp_opts.append ("-I%s" % dir)
168
169 return pp_opts
170
171# _gen_preprocess_options ()
172
173
174def _gen_lib_options (libraries, library_dirs):
175
176 lib_opts = []
177
178 for dir in library_dirs:
179 lib_opts.append ("-L%s" % dir)
180
181 # XXX it's important that we *not* remove redundant library mentions!
182 # sometimes you really do have to say "-lfoo -lbar -lfoo" in order to
183 # resolve all symbols. I just hope we never have to say "-lfoo obj.o
184 # -lbar" to get things to work -- that's certainly a possibility, but a
185 # pretty nasty way to arrange your C code.
186
187 for lib in libraries:
188 lib_opts.append ("-l%s" % lib)
189
190 return lib_opts
191
192# _gen_lib_options ()