blob: a868451462dd060045eba2d2babd16168f5338a0 [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
Greg Wardc2941131999-09-08 02:32:19 +000024from ccompiler import CCompiler, gen_preprocess_options, gen_lib_options
Greg Ward170bdc01999-07-10 02:04:22 +000025
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
Greg Wardc2941131999-09-08 02:32:19 +0000104 pp_opts = gen_preprocess_options (self.macros + macros,
105 self.include_dirs + includes)
Greg Ward170bdc01999-07-10 02:04:22 +0000106
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
Greg Ward5e717441999-08-14 23:53:53 +0000114 self.spawn ([self.cc] + cc_args)
Greg Wardc2941131999-09-08 02:32:19 +0000115 return self.object_filenames (sources)
116
Greg Ward170bdc01999-07-10 02:04:22 +0000117
118 # XXX punting on 'link_static_lib()' for now -- it might be better for
119 # CCompiler to mandate just 'link_binary()' or some such to build a new
120 # Python binary; it would then take care of linking in everything
121 # needed for the new Python without messing with an intermediate static
122 # library.
123
124 def link_shared_lib (self,
125 objects,
126 output_libname,
127 libraries=None,
Greg Ward65f4a3b1999-08-29 18:23:32 +0000128 library_dirs=None,
129 build_info=None):
Greg Ward170bdc01999-07-10 02:04:22 +0000130 # XXX should we sanity check the library name? (eg. no
131 # slashes)
Greg Ward65f4a3b1999-08-29 18:23:32 +0000132 self.link_shared_object (objects, "lib%s%s" % \
133 (output_libname, self._shared_lib_ext),
134 build_info=build_info)
Greg Ward170bdc01999-07-10 02:04:22 +0000135
136
137 def link_shared_object (self,
138 objects,
139 output_filename,
Greg Ward5e717441999-08-14 23:53:53 +0000140 libraries=None,
Greg Ward65f4a3b1999-08-29 18:23:32 +0000141 library_dirs=None,
142 build_info=None):
Greg Ward5e717441999-08-14 23:53:53 +0000143
144 if libraries is None:
145 libraries = []
146 if library_dirs is None:
147 library_dirs = []
Greg Ward65f4a3b1999-08-29 18:23:32 +0000148 if build_info is None:
149 build_info = {}
150
Greg Wardc2941131999-09-08 02:32:19 +0000151 lib_opts = gen_lib_options (self.libraries + libraries,
152 self.library_dirs + library_dirs,
153 "-l%s", "-L%s")
Greg Ward170bdc01999-07-10 02:04:22 +0000154 ld_args = self.ldflags_shared + lib_opts + \
155 objects + ['-o', output_filename]
156
Greg Ward5e717441999-08-14 23:53:53 +0000157 self.spawn ([self.ld_shared] + ld_args)
158
159
160 def object_filenames (self, source_filenames):
161 outnames = []
162 for inname in source_filenames:
Greg Ward65f4a3b1999-08-29 18:23:32 +0000163 outnames.append ( re.sub (r'\.(c|C|cc|cxx|cpp)$',
164 self._obj_ext, inname))
Greg Ward5e717441999-08-14 23:53:53 +0000165 return outnames
166
167 def shared_object_filename (self, source_filename):
Greg Ward65f4a3b1999-08-29 18:23:32 +0000168 return re.sub (r'\.(c|C|cc|cxx|cpp)$', self._shared_lib_ext)
Greg Ward5e717441999-08-14 23:53:53 +0000169
170 def library_filename (self, libname):
Greg Ward65f4a3b1999-08-29 18:23:32 +0000171 return "lib%s%s" % (libname, self._static_lib_ext )
Greg Ward5e717441999-08-14 23:53:53 +0000172
173 def shared_library_filename (self, libname):
Greg Ward65f4a3b1999-08-29 18:23:32 +0000174 return "lib%s%s" % (libname, self._shared_lib_ext )
175
Greg Ward170bdc01999-07-10 02:04:22 +0000176# class UnixCCompiler
177
178
179def _split_command (cmd):
180 """Split a command string up into the progam to run (a string) and
181 the list of arguments; return them as (cmd, arglist)."""
182 args = string.split (cmd)
183 return (args[0], args[1:])