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