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