Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 1 | """distutils.unixccompiler |
| 2 | |
| 3 | Contains the UnixCCompiler class, a subclass of CCompiler that handles |
| 4 | the "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 Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 20 | import string, re, os |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 21 | from types import * |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 22 | from copy import copy |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 23 | from sysconfig import \ |
| 24 | CC, CCSHARED, CFLAGS, OPT, LDSHARED, LDFLAGS, RANLIB, AR, SO |
Greg Ward | c294113 | 1999-09-08 02:32:19 +0000 | [diff] [blame] | 25 | from ccompiler import CCompiler, gen_preprocess_options, gen_lib_options |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 26 | from util import move_file, newer_pairwise, newer_group |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 27 | |
| 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 | |
| 44 | class UnixCCompiler (CCompiler): |
| 45 | |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 46 | # 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 Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 55 | |
Greg Ward | 0e3530b | 1999-09-29 12:22:50 +0000 | [diff] [blame] | 56 | compiler_type = 'unix' |
| 57 | |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 58 | _obj_ext = '.o' |
| 59 | _exe_ext = '' |
| 60 | _shared_lib_ext = SO |
| 61 | _static_lib_ext = '.a' |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 62 | |
| 63 | def __init__ (self, |
| 64 | verbose=0, |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 65 | dry_run=0, |
| 66 | force=0): |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 67 | |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 68 | CCompiler.__init__ (self, verbose, dry_run, force) |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 69 | |
| 70 | self.preprocess_options = None |
| 71 | self.compile_options = None |
| 72 | |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 73 | # Munge CC and OPT together in case there are flags stuck in CC. |
| 74 | # Note that using these variables from sysconfig immediately makes |
| 75 | # this module specific to building Python extensions and |
| 76 | # inappropriate as a general-purpose C compiler front-end. So sue |
| 77 | # me. Note also that we use OPT rather than CFLAGS, because CFLAGS |
| 78 | # is the flags used to compile Python itself -- not only are there |
| 79 | # -I options in there, they are the *wrong* -I options. We'll |
| 80 | # leave selection of include directories up to the class using |
| 81 | # UnixCCompiler! |
| 82 | |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 83 | (self.cc, self.ccflags) = \ |
| 84 | _split_command (CC + ' ' + OPT) |
| 85 | self.ccflags_shared = string.split (CCSHARED) |
| 86 | |
| 87 | (self.ld_shared, self.ldflags_shared) = \ |
| 88 | _split_command (LDSHARED) |
| 89 | |
| 90 | |
| 91 | def compile (self, |
| 92 | sources, |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 93 | output_dir=None, |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 94 | macros=None, |
Greg Ward | 0e3530b | 1999-09-29 12:22:50 +0000 | [diff] [blame] | 95 | includes=None, |
| 96 | extra_preargs=None, |
| 97 | extra_postargs=None): |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 98 | |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 99 | if output_dir is None: |
| 100 | output_dir = self.output_dir |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 101 | if macros is None: |
| 102 | macros = [] |
| 103 | if includes is None: |
| 104 | includes = [] |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 105 | |
| 106 | if type (macros) is not ListType: |
| 107 | raise TypeError, \ |
| 108 | "'macros' (if supplied) must be a list of tuples" |
| 109 | if type (includes) is not ListType: |
| 110 | raise TypeError, \ |
| 111 | "'includes' (if supplied) must be a list of strings" |
| 112 | |
Greg Ward | c294113 | 1999-09-08 02:32:19 +0000 | [diff] [blame] | 113 | pp_opts = gen_preprocess_options (self.macros + macros, |
| 114 | self.include_dirs + includes) |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 115 | |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 116 | # So we can mangle 'sources' without hurting the caller's data |
| 117 | orig_sources = sources |
| 118 | sources = copy (sources) |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 119 | |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 120 | # Get the list of expected output (object) files and drop files we |
| 121 | # don't have to recompile. (Simplistic check -- we just compare the |
| 122 | # source and object file, no deep dependency checking involving |
| 123 | # header files. Hmmm.) |
| 124 | objects = self.object_filenames (sources, output_dir) |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 125 | if not self.force: |
| 126 | skipped = newer_pairwise (sources, objects) |
| 127 | for skipped_pair in skipped: |
| 128 | self.announce ("skipping %s (%s up-to-date)" % skipped_pair) |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 129 | |
| 130 | # If anything left to compile, compile it |
| 131 | if sources: |
| 132 | # XXX use of ccflags_shared means we're blithely assuming |
| 133 | # that we're compiling for inclusion in a shared object! |
| 134 | # (will have to fix this when I add the ability to build a |
| 135 | # new Python) |
| 136 | cc_args = ['-c'] + pp_opts + \ |
| 137 | self.ccflags + self.ccflags_shared + \ |
| 138 | sources |
Greg Ward | 0e3530b | 1999-09-29 12:22:50 +0000 | [diff] [blame] | 139 | if extra_preargs: |
| 140 | cc_args[:0] = extra_preargs |
| 141 | if extra_postargs: |
| 142 | cc_args.extend (extra_postargs) |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 143 | self.spawn ([self.cc] + cc_args) |
| 144 | |
| 145 | |
| 146 | # Note that compiling multiple source files in the same go like |
| 147 | # we've just done drops the .o file in the current directory, which |
| 148 | # may not be what the caller wants (depending on the 'output_dir' |
| 149 | # parameter). So, if necessary, fix that now by moving the .o |
| 150 | # files into the desired output directory. (The alternative, of |
| 151 | # course, is to compile one-at-a-time with a -o option. 6 of one, |
| 152 | # 12/2 of the other...) |
| 153 | |
| 154 | if output_dir: |
| 155 | for i in range (len (objects)): |
| 156 | src = os.path.basename (objects[i]) |
| 157 | objects[i] = self.move_file (src, output_dir) |
| 158 | |
| 159 | # Have to re-fetch list of object filenames, because we want to |
| 160 | # return *all* of them, including those that weren't recompiled on |
| 161 | # this call! |
| 162 | return self.object_filenames (orig_sources, output_dir) |
Greg Ward | c294113 | 1999-09-08 02:32:19 +0000 | [diff] [blame] | 163 | |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 164 | |
| 165 | # XXX punting on 'link_static_lib()' for now -- it might be better for |
| 166 | # CCompiler to mandate just 'link_binary()' or some such to build a new |
| 167 | # Python binary; it would then take care of linking in everything |
| 168 | # needed for the new Python without messing with an intermediate static |
| 169 | # library. |
| 170 | |
| 171 | def link_shared_lib (self, |
| 172 | objects, |
| 173 | output_libname, |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 174 | output_dir=None, |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 175 | libraries=None, |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 176 | library_dirs=None, |
Greg Ward | 0e3530b | 1999-09-29 12:22:50 +0000 | [diff] [blame] | 177 | extra_preargs=None, |
| 178 | extra_postargs=None): |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 179 | # XXX should we sanity check the library name? (eg. no |
| 180 | # slashes) |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 181 | self.link_shared_object ( |
| 182 | objects, |
| 183 | "lib%s%s" % (output_libname, self._shared_lib_ext), |
| 184 | output_dir, |
| 185 | libraries, |
| 186 | library_dirs, |
Greg Ward | 0e3530b | 1999-09-29 12:22:50 +0000 | [diff] [blame] | 187 | extra_preargs, |
| 188 | extra_postargs) |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 189 | |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 190 | |
| 191 | def link_shared_object (self, |
| 192 | objects, |
| 193 | output_filename, |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 194 | output_dir=None, |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 195 | libraries=None, |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 196 | library_dirs=None, |
Greg Ward | 0e3530b | 1999-09-29 12:22:50 +0000 | [diff] [blame] | 197 | extra_preargs=None, |
| 198 | extra_postargs=None): |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 199 | |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 200 | if output_dir is None: |
| 201 | output_dir = self.output_dir |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 202 | if libraries is None: |
| 203 | libraries = [] |
| 204 | if library_dirs is None: |
| 205 | library_dirs = [] |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 206 | |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 207 | lib_opts = gen_lib_options (self, |
| 208 | self.library_dirs + library_dirs, |
| 209 | self.libraries + libraries) |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 210 | if output_dir is not None: |
| 211 | output_filename = os.path.join (output_dir, output_filename) |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 212 | |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 213 | # If any of the input object files are newer than the output shared |
| 214 | # object, relink. Again, this is a simplistic dependency check: |
| 215 | # doesn't look at any of the libraries we might be linking with. |
Greg Ward | b116e45 | 1999-09-21 18:36:15 +0000 | [diff] [blame] | 216 | # Note that we have to dance around errors comparing timestamps if |
| 217 | # we're in dry-run mode (yuck). |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 218 | if not self.force: |
| 219 | try: |
| 220 | newer = newer_group (objects, output_filename) |
| 221 | except OSError: |
| 222 | if self.dry_run: |
| 223 | newer = 1 |
| 224 | else: |
| 225 | raise |
Greg Ward | b116e45 | 1999-09-21 18:36:15 +0000 | [diff] [blame] | 226 | |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 227 | if self.force or newer: |
| 228 | ld_args = self.ldflags_shared + objects + \ |
| 229 | lib_opts + ['-o', output_filename] |
Greg Ward | 0e3530b | 1999-09-29 12:22:50 +0000 | [diff] [blame] | 230 | if extra_preargs: |
| 231 | ld_args[:0] = extra_preargs |
| 232 | if extra_postargs: |
| 233 | ld_args.extend (extra_postargs) |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 234 | self.spawn ([self.ld_shared] + ld_args) |
| 235 | else: |
| 236 | self.announce ("skipping %s (up-to-date)" % output_filename) |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 237 | |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 238 | # link_shared_object () |
| 239 | |
| 240 | |
| 241 | # -- Filename-mangling (etc.) methods ------------------------------ |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 242 | |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 243 | def object_filenames (self, source_filenames, output_dir=None): |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 244 | outnames = [] |
| 245 | for inname in source_filenames: |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 246 | outname = re.sub (r'\.(c|C|cc|cxx|cpp)$', self._obj_ext, inname) |
| 247 | outname = os.path.basename (outname) |
| 248 | if output_dir is not None: |
| 249 | outname = os.path.join (output_dir, outname) |
| 250 | outnames.append (outname) |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 251 | return outnames |
| 252 | |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 253 | def shared_object_filename (self, source_filename, output_dir=None): |
| 254 | outname = re.sub (r'\.(c|C|cc|cxx|cpp)$', self._shared_lib_ext) |
| 255 | outname = os.path.basename (outname) |
| 256 | if output_dir is not None: |
| 257 | outname = os.path.join (output_dir, outname) |
| 258 | return outname |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 259 | |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 260 | |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 261 | def library_filename (self, libname): |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 262 | return "lib%s%s" % (libname, self._static_lib_ext) |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 263 | |
| 264 | def shared_library_filename (self, libname): |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 265 | return "lib%s%s" % (libname, self._shared_lib_ext) |
| 266 | |
| 267 | |
| 268 | def library_dir_option (self, dir): |
| 269 | return "-L" + dir |
| 270 | |
| 271 | def library_option (self, lib): |
| 272 | return "-l" + lib |
| 273 | |
| 274 | |
| 275 | def find_library_file (self, dirs, lib): |
| 276 | |
| 277 | for dir in dirs: |
| 278 | shared = os.path.join (dir, self.shared_library_filename (lib)) |
| 279 | static = os.path.join (dir, self.library_filename (lib)) |
| 280 | |
| 281 | # We're second-guessing the linker here, with not much hard |
| 282 | # data to go on: GCC seems to prefer the shared library, so I'm |
| 283 | # assuming that *all* Unix C compilers do. And of course I'm |
| 284 | # ignoring even GCC's "-static" option. So sue me. |
| 285 | if os.path.exists (shared): |
| 286 | return shared |
| 287 | elif os.path.exists (static): |
| 288 | return static |
| 289 | |
| 290 | else: |
| 291 | # Oops, didn't find it in *any* of 'dirs' |
| 292 | return None |
| 293 | |
| 294 | # find_library_file () |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 295 | |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 296 | # class UnixCCompiler |
| 297 | |
| 298 | |
| 299 | def _split_command (cmd): |
| 300 | """Split a command string up into the progam to run (a string) and |
| 301 | the list of arguments; return them as (cmd, arglist).""" |
| 302 | args = string.split (cmd) |
| 303 | return (args[0], args[1:]) |