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 | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 62 | |
| 63 | # Command to create a static library: seems to be pretty consistent |
| 64 | # across the major Unices. Might have to move down into the |
| 65 | # constructor if we need platform-specific guesswork. |
| 66 | archiver = "ar" |
| 67 | archiver_options = "-cr" |
| 68 | |
| 69 | |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 70 | def __init__ (self, |
| 71 | verbose=0, |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 72 | dry_run=0, |
| 73 | force=0): |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 74 | |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 75 | CCompiler.__init__ (self, verbose, dry_run, force) |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 76 | |
| 77 | self.preprocess_options = None |
| 78 | self.compile_options = None |
| 79 | |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 80 | # Munge CC and OPT together in case there are flags stuck in CC. |
| 81 | # Note that using these variables from sysconfig immediately makes |
| 82 | # this module specific to building Python extensions and |
| 83 | # inappropriate as a general-purpose C compiler front-end. So sue |
| 84 | # me. Note also that we use OPT rather than CFLAGS, because CFLAGS |
| 85 | # is the flags used to compile Python itself -- not only are there |
| 86 | # -I options in there, they are the *wrong* -I options. We'll |
| 87 | # leave selection of include directories up to the class using |
| 88 | # UnixCCompiler! |
| 89 | |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 90 | (self.cc, self.ccflags) = \ |
| 91 | _split_command (CC + ' ' + OPT) |
| 92 | self.ccflags_shared = string.split (CCSHARED) |
| 93 | |
| 94 | (self.ld_shared, self.ldflags_shared) = \ |
| 95 | _split_command (LDSHARED) |
| 96 | |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 97 | self.ld_exec = self.cc |
| 98 | |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 99 | |
| 100 | def compile (self, |
| 101 | sources, |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 102 | output_dir=None, |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 103 | macros=None, |
Greg Ward | 04d7832 | 1999-12-12 16:57:47 +0000 | [diff] [blame] | 104 | include_dirs=None, |
Greg Ward | ba233fb | 2000-02-09 02:17:00 +0000 | [diff] [blame] | 105 | debug=0, |
Greg Ward | 0e3530b | 1999-09-29 12:22:50 +0000 | [diff] [blame] | 106 | extra_preargs=None, |
| 107 | extra_postargs=None): |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 108 | |
Greg Ward | 10ca82b | 2000-02-10 02:51:32 +0000 | [diff] [blame] | 109 | if type (output_dir) not in (StringType, NoneType): |
| 110 | raise TypeError, "'output_dir' must be a string or None" |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 111 | if output_dir is None: |
| 112 | output_dir = self.output_dir |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 113 | if macros is None: |
| 114 | macros = [] |
Greg Ward | 04d7832 | 1999-12-12 16:57:47 +0000 | [diff] [blame] | 115 | if include_dirs is None: |
| 116 | include_dirs = [] |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 117 | |
| 118 | if type (macros) is not ListType: |
| 119 | raise TypeError, \ |
| 120 | "'macros' (if supplied) must be a list of tuples" |
Greg Ward | 04d7832 | 1999-12-12 16:57:47 +0000 | [diff] [blame] | 121 | if type (include_dirs) not in (ListType, TupleType): |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 122 | raise TypeError, \ |
Greg Ward | 04d7832 | 1999-12-12 16:57:47 +0000 | [diff] [blame] | 123 | "'include_dirs' (if supplied) must be a list of strings" |
| 124 | include_dirs = list (include_dirs) |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 125 | |
Greg Ward | c294113 | 1999-09-08 02:32:19 +0000 | [diff] [blame] | 126 | pp_opts = gen_preprocess_options (self.macros + macros, |
Greg Ward | 04d7832 | 1999-12-12 16:57:47 +0000 | [diff] [blame] | 127 | self.include_dirs + include_dirs) |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 128 | |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 129 | # So we can mangle 'sources' without hurting the caller's data |
| 130 | orig_sources = sources |
| 131 | sources = copy (sources) |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 132 | |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 133 | # Get the list of expected output (object) files and drop files we |
| 134 | # don't have to recompile. (Simplistic check -- we just compare the |
| 135 | # source and object file, no deep dependency checking involving |
| 136 | # header files. Hmmm.) |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 137 | objects = self.object_filenames (sources, output_dir=output_dir) |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 138 | if not self.force: |
| 139 | skipped = newer_pairwise (sources, objects) |
| 140 | for skipped_pair in skipped: |
| 141 | self.announce ("skipping %s (%s up-to-date)" % skipped_pair) |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 142 | |
Greg Ward | ef6f515 | 2000-02-03 23:07:19 +0000 | [diff] [blame] | 143 | # Build list of (source,object) tuples for convenience |
| 144 | srcobj = [] |
| 145 | for i in range (len (sources)): |
| 146 | srcobj.append ((sources[i], objects[i])) |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 147 | |
Greg Ward | ef6f515 | 2000-02-03 23:07:19 +0000 | [diff] [blame] | 148 | # Compile all source files that weren't eliminated by |
| 149 | # 'newer_pairwise()'. |
| 150 | # XXX use of ccflags_shared means we're blithely assuming |
| 151 | # that we're compiling for inclusion in a shared object! |
| 152 | # (will have to fix this when I add the ability to build a |
| 153 | # new Python) |
| 154 | cc_args = ['-c'] + pp_opts + self.ccflags + self.ccflags_shared |
Greg Ward | ba233fb | 2000-02-09 02:17:00 +0000 | [diff] [blame] | 155 | if debug: |
| 156 | cc_args[:0] = ['-g'] |
Greg Ward | ef6f515 | 2000-02-03 23:07:19 +0000 | [diff] [blame] | 157 | if extra_preargs: |
| 158 | cc_args[:0] = extra_preargs |
| 159 | if extra_postargs is None: |
| 160 | extra_postargs = [] |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 161 | |
Greg Ward | f114657 | 2000-03-01 14:43:49 +0000 | [diff] [blame^] | 162 | if output_dir is not None: |
| 163 | self.mkpath (output_dir) |
Greg Ward | ef6f515 | 2000-02-03 23:07:19 +0000 | [diff] [blame] | 164 | for (source,object) in srcobj: |
| 165 | self.spawn ([self.cc] + cc_args + |
| 166 | [source, '-o', object] + |
| 167 | extra_postargs) |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 168 | |
| 169 | # Have to re-fetch list of object filenames, because we want to |
| 170 | # return *all* of them, including those that weren't recompiled on |
| 171 | # this call! |
Greg Ward | f114657 | 2000-03-01 14:43:49 +0000 | [diff] [blame^] | 172 | return self.object_filenames (orig_sources, output_dir=output_dir) |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 173 | |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 174 | |
| 175 | def _fix_link_args (self, output_dir, libraries, library_dirs): |
| 176 | """Fixes up the arguments supplied to the 'link_*' methods: |
| 177 | if output_dir is None, use self.output_dir; ensure that |
| 178 | libraries and library_dirs are both lists (could be None or |
| 179 | tuples on input -- both are converted to lists). Return |
| 180 | a tuple of the three input arguments.""" |
| 181 | |
| 182 | if output_dir is None: |
| 183 | output_dir = self.output_dir |
| 184 | if libraries is None: |
| 185 | libraries = [] |
| 186 | if library_dirs is None: |
| 187 | library_dirs = [] |
| 188 | |
| 189 | if type (libraries) not in (ListType, TupleType): |
| 190 | raise TypeError, \ |
| 191 | "'libraries' (if supplied) must be a list of strings" |
| 192 | if type (library_dirs) not in (ListType, TupleType): |
| 193 | raise TypeError, \ |
| 194 | "'library_dirs' (if supplied) must be a list of strings" |
| 195 | libraries = list (libraries) |
| 196 | library_dirs = list (library_dirs) |
| 197 | |
| 198 | return (output_dir, libraries, library_dirs) |
| 199 | |
| 200 | |
| 201 | def link_static_lib (self, |
| 202 | objects, |
| 203 | output_libname, |
Greg Ward | ba233fb | 2000-02-09 02:17:00 +0000 | [diff] [blame] | 204 | output_dir=None, |
| 205 | debug=0): |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 206 | |
| 207 | if type (objects) not in (ListType, TupleType): |
| 208 | raise TypeError, \ |
| 209 | "'objects' must be a list or tuple of strings" |
| 210 | objects = list (objects) |
| 211 | |
Greg Ward | 10ca82b | 2000-02-10 02:51:32 +0000 | [diff] [blame] | 212 | if type (output_dir) not in (StringType, NoneType): |
| 213 | raise TypeError, "'output_dir' must be a string or None" |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 214 | if output_dir is None: |
| 215 | output_dir = self.output_dir |
| 216 | |
| 217 | output_filename = self.library_filename (output_libname) |
| 218 | if output_dir is not None: |
| 219 | output_filename = os.path.join (output_dir, output_filename) |
| 220 | |
| 221 | # Check timestamps: if any of the object files are newer than |
| 222 | # the library file, *or* if "force" is true, then we'll |
| 223 | # recreate the library. |
| 224 | if not self.force: |
| 225 | if self.dry_run: |
| 226 | newer = newer_group (objects, output_filename, missing='newer') |
| 227 | else: |
| 228 | newer = newer_group (objects, output_filename) |
| 229 | |
| 230 | if self.force or newer: |
Greg Ward | f114657 | 2000-03-01 14:43:49 +0000 | [diff] [blame^] | 231 | self.mkpath (os.path.dirname (output_filename)) |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 232 | self.spawn ([self.archiver, |
| 233 | self.archiver_options, |
| 234 | output_filename] + |
| 235 | objects) |
| 236 | else: |
| 237 | self.announce ("skipping %s (up-to-date)" % output_filename) |
| 238 | |
| 239 | # link_static_lib () |
| 240 | |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 241 | |
| 242 | def link_shared_lib (self, |
| 243 | objects, |
| 244 | output_libname, |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 245 | output_dir=None, |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 246 | libraries=None, |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 247 | library_dirs=None, |
Greg Ward | ba233fb | 2000-02-09 02:17:00 +0000 | [diff] [blame] | 248 | debug=0, |
Greg Ward | 0e3530b | 1999-09-29 12:22:50 +0000 | [diff] [blame] | 249 | extra_preargs=None, |
| 250 | extra_postargs=None): |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 251 | # XXX should we sanity check the library name? (eg. no |
| 252 | # slashes) |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 253 | self.link_shared_object ( |
| 254 | objects, |
| 255 | "lib%s%s" % (output_libname, self._shared_lib_ext), |
| 256 | output_dir, |
| 257 | libraries, |
| 258 | library_dirs, |
Greg Ward | ba233fb | 2000-02-09 02:17:00 +0000 | [diff] [blame] | 259 | debug, |
Greg Ward | 0e3530b | 1999-09-29 12:22:50 +0000 | [diff] [blame] | 260 | extra_preargs, |
| 261 | extra_postargs) |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 262 | |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 263 | |
| 264 | def link_shared_object (self, |
| 265 | objects, |
| 266 | output_filename, |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 267 | output_dir=None, |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 268 | libraries=None, |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 269 | library_dirs=None, |
Greg Ward | ba233fb | 2000-02-09 02:17:00 +0000 | [diff] [blame] | 270 | debug=0, |
Greg Ward | 0e3530b | 1999-09-29 12:22:50 +0000 | [diff] [blame] | 271 | extra_preargs=None, |
| 272 | extra_postargs=None): |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 273 | |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 274 | (output_dir, libraries, library_dirs) = \ |
| 275 | self._fix_link_args (output_dir, libraries, library_dirs) |
Greg Ward | 04d7832 | 1999-12-12 16:57:47 +0000 | [diff] [blame] | 276 | |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 277 | lib_opts = gen_lib_options (self, |
| 278 | self.library_dirs + library_dirs, |
| 279 | self.libraries + libraries) |
Greg Ward | 10ca82b | 2000-02-10 02:51:32 +0000 | [diff] [blame] | 280 | if type (output_dir) not in (StringType, NoneType): |
| 281 | raise TypeError, "'output_dir' must be a string or None" |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 282 | if output_dir is not None: |
| 283 | output_filename = os.path.join (output_dir, output_filename) |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 284 | |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 285 | # If any of the input object files are newer than the output shared |
| 286 | # object, relink. Again, this is a simplistic dependency check: |
| 287 | # doesn't look at any of the libraries we might be linking with. |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 288 | |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 289 | if not self.force: |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 290 | if self.dry_run: |
| 291 | newer = newer_group (objects, output_filename, missing='newer') |
| 292 | else: |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 293 | newer = newer_group (objects, output_filename) |
Greg Ward | b116e45 | 1999-09-21 18:36:15 +0000 | [diff] [blame] | 294 | |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 295 | if self.force or newer: |
| 296 | ld_args = self.ldflags_shared + objects + \ |
| 297 | lib_opts + ['-o', output_filename] |
Greg Ward | ba233fb | 2000-02-09 02:17:00 +0000 | [diff] [blame] | 298 | if debug: |
| 299 | ld_args[:0] = ['-g'] |
Greg Ward | 0e3530b | 1999-09-29 12:22:50 +0000 | [diff] [blame] | 300 | if extra_preargs: |
| 301 | ld_args[:0] = extra_preargs |
| 302 | if extra_postargs: |
| 303 | ld_args.extend (extra_postargs) |
Greg Ward | f114657 | 2000-03-01 14:43:49 +0000 | [diff] [blame^] | 304 | self.mkpath (os.path.dirname (output_filename)) |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 305 | self.spawn ([self.ld_shared] + ld_args) |
| 306 | else: |
| 307 | self.announce ("skipping %s (up-to-date)" % output_filename) |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 308 | |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 309 | # link_shared_object () |
| 310 | |
| 311 | |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 312 | def link_executable (self, |
| 313 | objects, |
| 314 | output_progname, |
| 315 | output_dir=None, |
| 316 | libraries=None, |
| 317 | library_dirs=None, |
Greg Ward | ba233fb | 2000-02-09 02:17:00 +0000 | [diff] [blame] | 318 | debug=0, |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 319 | extra_preargs=None, |
| 320 | extra_postargs=None): |
| 321 | |
| 322 | (output_dir, libraries, library_dirs) = \ |
| 323 | self._fix_link_args (output_dir, libraries, library_dirs) |
| 324 | |
| 325 | lib_opts = gen_lib_options (self, |
| 326 | self.library_dirs + library_dirs, |
| 327 | self.libraries + libraries) |
| 328 | output_filename = output_progname # Unix-ism! |
| 329 | if output_dir is not None: |
| 330 | output_filename = os.path.join (output_dir, output_filename) |
| 331 | |
| 332 | # Same ol' simplistic-but-still-useful dependency check. |
| 333 | if not self.force: |
| 334 | if self.dry_run: |
| 335 | newer = newer_group (objects, output_filename, missing='newer') |
| 336 | else: |
| 337 | newer = newer_group (objects, output_filename) |
| 338 | |
| 339 | if self.force or newer: |
| 340 | ld_args = objects + lib_opts + ['-o', output_filename] |
Greg Ward | ba233fb | 2000-02-09 02:17:00 +0000 | [diff] [blame] | 341 | if debug: |
| 342 | ld_args[:0] = ['-g'] |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 343 | if extra_preargs: |
| 344 | ld_args[:0] = extra_preargs |
| 345 | if extra_postargs: |
| 346 | ld_args.extend (extra_postargs) |
Greg Ward | f114657 | 2000-03-01 14:43:49 +0000 | [diff] [blame^] | 347 | self.mkpath (os.path.dirname (output_filename)) |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 348 | self.spawn ([self.ld_exec] + ld_args) |
| 349 | else: |
| 350 | self.announce ("skipping %s (up-to-date)" % output_filename) |
| 351 | |
| 352 | # link_executable () |
| 353 | |
| 354 | |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 355 | # -- Filename-mangling (etc.) methods ------------------------------ |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 356 | |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 357 | def object_filenames (self, source_filenames, |
| 358 | keep_dir=0, output_dir=None): |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 359 | outnames = [] |
| 360 | for inname in source_filenames: |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 361 | outname = re.sub (r'\.(c|C|cc|cxx|cpp)$', self._obj_ext, inname) |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 362 | if not keep_dir: |
| 363 | outname = os.path.basename (outname) |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 364 | if output_dir is not None: |
| 365 | outname = os.path.join (output_dir, outname) |
| 366 | outnames.append (outname) |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 367 | return outnames |
| 368 | |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 369 | def shared_object_filename (self, source_filename, |
| 370 | keep_dir=0, output_dir=None): |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 371 | outname = re.sub (r'\.(c|C|cc|cxx|cpp)$', self._shared_lib_ext) |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 372 | if not keep_dir: |
| 373 | outname = os.path.basename (outname) |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 374 | if output_dir is not None: |
| 375 | outname = os.path.join (output_dir, outname) |
| 376 | return outname |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 377 | |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 378 | |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 379 | def library_filename (self, libname): |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 380 | (dirname, basename) = os.path.split (libname) |
| 381 | return os.path.join (dirname, |
| 382 | "lib%s%s" % (basename, self._static_lib_ext)) |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 383 | |
| 384 | def shared_library_filename (self, libname): |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 385 | (dirname, basename) = os.path.split (libname) |
| 386 | return os.path.join (dirname, |
| 387 | "lib%s%s" % (basename, self._shared_lib_ext)) |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 388 | |
| 389 | |
| 390 | def library_dir_option (self, dir): |
| 391 | return "-L" + dir |
| 392 | |
| 393 | def library_option (self, lib): |
| 394 | return "-l" + lib |
| 395 | |
| 396 | |
| 397 | def find_library_file (self, dirs, lib): |
| 398 | |
| 399 | for dir in dirs: |
| 400 | shared = os.path.join (dir, self.shared_library_filename (lib)) |
| 401 | static = os.path.join (dir, self.library_filename (lib)) |
| 402 | |
| 403 | # We're second-guessing the linker here, with not much hard |
| 404 | # data to go on: GCC seems to prefer the shared library, so I'm |
| 405 | # assuming that *all* Unix C compilers do. And of course I'm |
| 406 | # ignoring even GCC's "-static" option. So sue me. |
| 407 | if os.path.exists (shared): |
| 408 | return shared |
| 409 | elif os.path.exists (static): |
| 410 | return static |
| 411 | |
| 412 | else: |
| 413 | # Oops, didn't find it in *any* of 'dirs' |
| 414 | return None |
| 415 | |
| 416 | # find_library_file () |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 417 | |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 418 | # class UnixCCompiler |
| 419 | |
| 420 | |
| 421 | def _split_command (cmd): |
| 422 | """Split a command string up into the progam to run (a string) and |
| 423 | the list of arguments; return them as (cmd, arglist).""" |
| 424 | args = string.split (cmd) |
| 425 | return (args[0], args[1:]) |