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 | 1c79330 | 2000-04-14 00:48:15 +0000 | [diff] [blame] | 23 | from distutils import sysconfig |
Greg Ward | 3ff3b03 | 2000-06-21 02:58:46 +0000 | [diff] [blame] | 24 | from distutils.dep_util import newer |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 25 | from distutils.ccompiler import \ |
Greg Ward | 3add77f | 2000-05-30 02:02:49 +0000 | [diff] [blame] | 26 | CCompiler, gen_preprocess_options, gen_lib_options |
| 27 | from distutils.errors import \ |
| 28 | DistutilsExecError, CompileError, LibError, LinkError |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 29 | |
| 30 | # XXX Things not currently handled: |
| 31 | # * optimization/debug/warning flags; we just use whatever's in Python's |
| 32 | # Makefile and live with it. Is this adequate? If not, we might |
| 33 | # have to have a bunch of subclasses GNUCCompiler, SGICCompiler, |
| 34 | # SunCCompiler, and I suspect down that road lies madness. |
| 35 | # * even if we don't know a warning flag from an optimization flag, |
| 36 | # we need some way for outsiders to feed preprocessor/compiler/linker |
| 37 | # flags in to us -- eg. a sysadmin might want to mandate certain flags |
| 38 | # via a site config file, or a user might want to set something for |
| 39 | # compiling this module distribution only via the setup.py command |
| 40 | # line, whatever. As long as these options come from something on the |
| 41 | # current system, they can be as system-dependent as they like, and we |
| 42 | # should just happily stuff them into the preprocessor/compiler/linker |
| 43 | # options and carry on. |
| 44 | |
| 45 | |
| 46 | class UnixCCompiler (CCompiler): |
| 47 | |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 48 | # XXX perhaps there should really be *three* kinds of include |
| 49 | # directories: those built in to the preprocessor, those from Python's |
| 50 | # Makefiles, and those supplied to {add,set}_include_dirs(). Currently |
| 51 | # we make no distinction between the latter two at this point; it's all |
| 52 | # up to the client class to select the include directories to use above |
| 53 | # and beyond the compiler's defaults. That is, both the Python include |
| 54 | # directories and any module- or package-specific include directories |
| 55 | # are specified via {add,set}_include_dirs(), and there's no way to |
| 56 | # distinguish them. This might be a bug. |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 57 | |
Greg Ward | 0e3530b | 1999-09-29 12:22:50 +0000 | [diff] [blame] | 58 | compiler_type = 'unix' |
| 59 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 60 | # Needed for the filename generation methods provided by the |
| 61 | # base class, CCompiler. |
| 62 | src_extensions = [".c",".C",".cc",".cxx",".cpp"] |
| 63 | obj_extension = ".o" |
| 64 | static_lib_extension = ".a" |
Greg Ward | 1c79330 | 2000-04-14 00:48:15 +0000 | [diff] [blame] | 65 | shared_lib_extension = sysconfig.SO |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 66 | static_lib_format = shared_lib_format = "lib%s%s" |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 67 | |
| 68 | # Command to create a static library: seems to be pretty consistent |
| 69 | # across the major Unices. Might have to move down into the |
| 70 | # constructor if we need platform-specific guesswork. |
Greg Ward | 1c79330 | 2000-04-14 00:48:15 +0000 | [diff] [blame] | 71 | archiver = sysconfig.AR |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 72 | archiver_options = "-cr" |
Greg Ward | 1c79330 | 2000-04-14 00:48:15 +0000 | [diff] [blame] | 73 | ranlib = sysconfig.RANLIB |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 74 | |
| 75 | |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 76 | def __init__ (self, |
| 77 | verbose=0, |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 78 | dry_run=0, |
| 79 | force=0): |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 80 | |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 81 | CCompiler.__init__ (self, verbose, dry_run, force) |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 82 | |
| 83 | self.preprocess_options = None |
| 84 | self.compile_options = None |
| 85 | |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 86 | # Munge CC and OPT together in case there are flags stuck in CC. |
| 87 | # Note that using these variables from sysconfig immediately makes |
| 88 | # this module specific to building Python extensions and |
| 89 | # inappropriate as a general-purpose C compiler front-end. So sue |
| 90 | # me. Note also that we use OPT rather than CFLAGS, because CFLAGS |
| 91 | # is the flags used to compile Python itself -- not only are there |
| 92 | # -I options in there, they are the *wrong* -I options. We'll |
| 93 | # leave selection of include directories up to the class using |
| 94 | # UnixCCompiler! |
| 95 | |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 96 | (self.cc, self.ccflags) = \ |
Greg Ward | 1c79330 | 2000-04-14 00:48:15 +0000 | [diff] [blame] | 97 | _split_command (sysconfig.CC + ' ' + sysconfig.OPT) |
| 98 | self.ccflags_shared = string.split (sysconfig.CCSHARED) |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 99 | |
| 100 | (self.ld_shared, self.ldflags_shared) = \ |
Greg Ward | 1c79330 | 2000-04-14 00:48:15 +0000 | [diff] [blame] | 101 | _split_command (sysconfig.LDSHARED) |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 102 | |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 103 | self.ld_exec = self.cc |
| 104 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 105 | # __init__ () |
| 106 | |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 107 | |
Greg Ward | 3ff3b03 | 2000-06-21 02:58:46 +0000 | [diff] [blame] | 108 | def preprocess (self, |
| 109 | source, |
| 110 | output_file=None, |
| 111 | macros=None, |
| 112 | include_dirs=None, |
| 113 | extra_preargs=None, |
| 114 | extra_postargs=None): |
| 115 | |
| 116 | (_, macros, include_dirs) = \ |
| 117 | self._fix_compile_args (None, macros, include_dirs) |
| 118 | pp_opts = gen_preprocess_options (macros, include_dirs) |
| 119 | cc_args = ['-E'] + pp_opts |
| 120 | if output_file: |
| 121 | cc_args.extend(['-o', output_file]) |
| 122 | if extra_preargs: |
| 123 | cc_args[:0] = extra_preargs |
| 124 | if extra_postargs: |
| 125 | extra_postargs.extend(extra_postargs) |
| 126 | |
| 127 | # We need to preprocess: either we're being forced to, or the |
| 128 | # source file is newer than the target (or the target doesn't |
| 129 | # exist). |
| 130 | if self.force or (output_file and newer(source, output_file)): |
| 131 | if output_file: |
| 132 | self.mkpath(os.path.dirname(output_file)) |
| 133 | try: |
| 134 | self.spawn ([self.cc] + cc_args) |
| 135 | except DistutilsExecError, msg: |
| 136 | raise CompileError, msg |
| 137 | |
| 138 | |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 139 | def compile (self, |
| 140 | sources, |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 141 | output_dir=None, |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 142 | macros=None, |
Greg Ward | 04d7832 | 1999-12-12 16:57:47 +0000 | [diff] [blame] | 143 | include_dirs=None, |
Greg Ward | ba233fb | 2000-02-09 02:17:00 +0000 | [diff] [blame] | 144 | debug=0, |
Greg Ward | 0e3530b | 1999-09-29 12:22:50 +0000 | [diff] [blame] | 145 | extra_preargs=None, |
| 146 | extra_postargs=None): |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 147 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 148 | (output_dir, macros, include_dirs) = \ |
| 149 | self._fix_compile_args (output_dir, macros, include_dirs) |
| 150 | (objects, skip_sources) = self._prep_compile (sources, output_dir) |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 151 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 152 | # Figure out the options for the compiler command line. |
| 153 | pp_opts = gen_preprocess_options (macros, include_dirs) |
Greg Ward | ef6f515 | 2000-02-03 23:07:19 +0000 | [diff] [blame] | 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 | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 162 | # Compile all source files that weren't eliminated by |
| 163 | # '_prep_compile()'. |
| 164 | for i in range (len (sources)): |
| 165 | src = sources[i] ; obj = objects[i] |
| 166 | if skip_sources[src]: |
| 167 | self.announce ("skipping %s (%s up-to-date)" % (src, obj)) |
| 168 | else: |
| 169 | self.mkpath (os.path.dirname (obj)) |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 170 | try: |
| 171 | self.spawn ([self.cc] + cc_args + |
| 172 | [src, '-o', obj] + |
| 173 | extra_postargs) |
| 174 | except DistutilsExecError, msg: |
| 175 | raise CompileError, msg |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 176 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 177 | # Return *all* object filenames, not just the ones we just built. |
| 178 | return objects |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 179 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 180 | # compile () |
| 181 | |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 182 | |
Greg Ward | 036c805 | 2000-03-10 01:48:32 +0000 | [diff] [blame] | 183 | def create_static_lib (self, |
| 184 | objects, |
| 185 | output_libname, |
| 186 | output_dir=None, |
| 187 | debug=0): |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 188 | |
Greg Ward | e21dabe | 2000-03-26 21:40:19 +0000 | [diff] [blame] | 189 | (objects, output_dir) = self._fix_object_args (objects, output_dir) |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 190 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 191 | output_filename = \ |
| 192 | self.library_filename (output_libname, output_dir=output_dir) |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 193 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 194 | if self._need_link (objects, output_filename): |
Greg Ward | f114657 | 2000-03-01 14:43:49 +0000 | [diff] [blame] | 195 | self.mkpath (os.path.dirname (output_filename)) |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 196 | self.spawn ([self.archiver, |
| 197 | self.archiver_options, |
| 198 | output_filename] + |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 199 | objects + self.objects) |
Greg Ward | 1c79330 | 2000-04-14 00:48:15 +0000 | [diff] [blame] | 200 | |
Greg Ward | 8eef583 | 2000-04-14 13:53:34 +0000 | [diff] [blame] | 201 | # Not many Unices required ranlib anymore -- SunOS 4.x is, I |
| 202 | # think the only major Unix that does. Maybe we need some |
| 203 | # platform intelligence here to skip ranlib if it's not |
| 204 | # needed -- or maybe Python's configure script took care of |
| 205 | # it for us, hence the check for leading colon. |
| 206 | if self.ranlib[0] != ':': |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 207 | try: |
| 208 | self.spawn ([self.ranlib, output_filename]) |
| 209 | except DistutilsExecError, msg: |
| 210 | raise LibError, msg |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 211 | else: |
| 212 | self.announce ("skipping %s (up-to-date)" % output_filename) |
| 213 | |
Greg Ward | 036c805 | 2000-03-10 01:48:32 +0000 | [diff] [blame] | 214 | # create_static_lib () |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 215 | |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 216 | |
| 217 | def link_shared_lib (self, |
| 218 | objects, |
| 219 | output_libname, |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 220 | output_dir=None, |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 221 | libraries=None, |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 222 | library_dirs=None, |
Greg Ward | e21dabe | 2000-03-26 21:40:19 +0000 | [diff] [blame] | 223 | runtime_library_dirs=None, |
Greg Ward | 5299b6a | 2000-05-20 13:23:21 +0000 | [diff] [blame] | 224 | export_symbols=None, |
Greg Ward | ba233fb | 2000-02-09 02:17:00 +0000 | [diff] [blame] | 225 | debug=0, |
Greg Ward | 0e3530b | 1999-09-29 12:22:50 +0000 | [diff] [blame] | 226 | extra_preargs=None, |
| 227 | extra_postargs=None): |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 228 | self.link_shared_object ( |
| 229 | objects, |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 230 | self.shared_library_filename (output_libname), |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 231 | output_dir, |
| 232 | libraries, |
| 233 | library_dirs, |
Greg Ward | e21dabe | 2000-03-26 21:40:19 +0000 | [diff] [blame] | 234 | runtime_library_dirs, |
Greg Ward | 5299b6a | 2000-05-20 13:23:21 +0000 | [diff] [blame] | 235 | export_symbols, |
Greg Ward | ba233fb | 2000-02-09 02:17:00 +0000 | [diff] [blame] | 236 | debug, |
Greg Ward | 0e3530b | 1999-09-29 12:22:50 +0000 | [diff] [blame] | 237 | extra_preargs, |
| 238 | extra_postargs) |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 239 | |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 240 | |
| 241 | def link_shared_object (self, |
| 242 | objects, |
| 243 | output_filename, |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 244 | output_dir=None, |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 245 | libraries=None, |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 246 | library_dirs=None, |
Greg Ward | e21dabe | 2000-03-26 21:40:19 +0000 | [diff] [blame] | 247 | runtime_library_dirs=None, |
Greg Ward | 5299b6a | 2000-05-20 13:23:21 +0000 | [diff] [blame] | 248 | export_symbols=None, |
Greg Ward | ba233fb | 2000-02-09 02:17:00 +0000 | [diff] [blame] | 249 | debug=0, |
Greg Ward | 0e3530b | 1999-09-29 12:22:50 +0000 | [diff] [blame] | 250 | extra_preargs=None, |
| 251 | extra_postargs=None): |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 252 | |
Greg Ward | e21dabe | 2000-03-26 21:40:19 +0000 | [diff] [blame] | 253 | (objects, output_dir) = self._fix_object_args (objects, output_dir) |
| 254 | (libraries, library_dirs, runtime_library_dirs) = \ |
| 255 | self._fix_lib_args (libraries, library_dirs, runtime_library_dirs) |
Greg Ward | 04d7832 | 1999-12-12 16:57:47 +0000 | [diff] [blame] | 256 | |
Greg Ward | d03f88a | 2000-03-18 15:19:51 +0000 | [diff] [blame] | 257 | lib_opts = gen_lib_options (self, |
Greg Ward | e21dabe | 2000-03-26 21:40:19 +0000 | [diff] [blame] | 258 | library_dirs, runtime_library_dirs, |
Greg Ward | d03f88a | 2000-03-18 15:19:51 +0000 | [diff] [blame] | 259 | libraries) |
Greg Ward | 10ca82b | 2000-02-10 02:51:32 +0000 | [diff] [blame] | 260 | if type (output_dir) not in (StringType, NoneType): |
| 261 | raise TypeError, "'output_dir' must be a string or None" |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 262 | if output_dir is not None: |
| 263 | output_filename = os.path.join (output_dir, output_filename) |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 264 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 265 | if self._need_link (objects, output_filename): |
| 266 | ld_args = (self.ldflags_shared + objects + self.objects + |
| 267 | lib_opts + ['-o', output_filename]) |
Greg Ward | ba233fb | 2000-02-09 02:17:00 +0000 | [diff] [blame] | 268 | if debug: |
| 269 | ld_args[:0] = ['-g'] |
Greg Ward | 0e3530b | 1999-09-29 12:22:50 +0000 | [diff] [blame] | 270 | if extra_preargs: |
| 271 | ld_args[:0] = extra_preargs |
| 272 | if extra_postargs: |
| 273 | ld_args.extend (extra_postargs) |
Greg Ward | f114657 | 2000-03-01 14:43:49 +0000 | [diff] [blame] | 274 | self.mkpath (os.path.dirname (output_filename)) |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 275 | try: |
| 276 | self.spawn ([self.ld_shared] + ld_args) |
| 277 | except DistutilsExecError, msg: |
| 278 | raise LinkError, msg |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame] | 279 | else: |
| 280 | self.announce ("skipping %s (up-to-date)" % output_filename) |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 281 | |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 282 | # link_shared_object () |
| 283 | |
| 284 | |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 285 | def link_executable (self, |
| 286 | objects, |
| 287 | output_progname, |
| 288 | output_dir=None, |
| 289 | libraries=None, |
| 290 | library_dirs=None, |
Greg Ward | e21dabe | 2000-03-26 21:40:19 +0000 | [diff] [blame] | 291 | runtime_library_dirs=None, |
Greg Ward | ba233fb | 2000-02-09 02:17:00 +0000 | [diff] [blame] | 292 | debug=0, |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 293 | extra_preargs=None, |
| 294 | extra_postargs=None): |
| 295 | |
Greg Ward | e21dabe | 2000-03-26 21:40:19 +0000 | [diff] [blame] | 296 | (objects, output_dir) = self._fix_object_args (objects, output_dir) |
| 297 | (libraries, library_dirs, runtime_library_dirs) = \ |
| 298 | self._fix_lib_args (libraries, library_dirs, runtime_library_dirs) |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 299 | |
Greg Ward | d03f88a | 2000-03-18 15:19:51 +0000 | [diff] [blame] | 300 | lib_opts = gen_lib_options (self, |
Greg Ward | e21dabe | 2000-03-26 21:40:19 +0000 | [diff] [blame] | 301 | library_dirs, runtime_library_dirs, |
Greg Ward | d03f88a | 2000-03-18 15:19:51 +0000 | [diff] [blame] | 302 | libraries) |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 303 | output_filename = output_progname # Unix-ism! |
| 304 | if output_dir is not None: |
| 305 | output_filename = os.path.join (output_dir, output_filename) |
| 306 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 307 | if self._need_link (objects, output_filename): |
| 308 | ld_args = objects + self.objects + lib_opts + ['-o', output_filename] |
Greg Ward | ba233fb | 2000-02-09 02:17:00 +0000 | [diff] [blame] | 309 | if debug: |
| 310 | ld_args[:0] = ['-g'] |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 311 | if extra_preargs: |
| 312 | ld_args[:0] = extra_preargs |
| 313 | if extra_postargs: |
| 314 | ld_args.extend (extra_postargs) |
Greg Ward | f114657 | 2000-03-01 14:43:49 +0000 | [diff] [blame] | 315 | self.mkpath (os.path.dirname (output_filename)) |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 316 | try: |
| 317 | self.spawn ([self.ld_exec] + ld_args) |
| 318 | except DistutilsExecError, msg: |
| 319 | raise LinkError, msg |
Greg Ward | c9f3187 | 2000-01-09 22:47:53 +0000 | [diff] [blame] | 320 | else: |
| 321 | self.announce ("skipping %s (up-to-date)" % output_filename) |
| 322 | |
| 323 | # link_executable () |
| 324 | |
| 325 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 326 | # -- Miscellaneous methods ----------------------------------------- |
| 327 | # These are all used by the 'gen_lib_options() function, in |
| 328 | # ccompiler.py. |
| 329 | |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 330 | def library_dir_option (self, dir): |
| 331 | return "-L" + dir |
| 332 | |
Greg Ward | d03f88a | 2000-03-18 15:19:51 +0000 | [diff] [blame] | 333 | def runtime_library_dir_option (self, dir): |
| 334 | return "-R" + dir |
| 335 | |
Greg Ward | 4fecfce | 1999-10-03 20:45:33 +0000 | [diff] [blame] | 336 | def library_option (self, lib): |
| 337 | return "-l" + lib |
| 338 | |
| 339 | |
| 340 | def find_library_file (self, dirs, lib): |
| 341 | |
| 342 | for dir in dirs: |
| 343 | shared = os.path.join (dir, self.shared_library_filename (lib)) |
| 344 | static = os.path.join (dir, self.library_filename (lib)) |
| 345 | |
| 346 | # We're second-guessing the linker here, with not much hard |
| 347 | # data to go on: GCC seems to prefer the shared library, so I'm |
| 348 | # assuming that *all* Unix C compilers do. And of course I'm |
| 349 | # ignoring even GCC's "-static" option. So sue me. |
| 350 | if os.path.exists (shared): |
| 351 | return shared |
| 352 | elif os.path.exists (static): |
| 353 | return static |
| 354 | |
| 355 | else: |
| 356 | # Oops, didn't find it in *any* of 'dirs' |
| 357 | return None |
| 358 | |
| 359 | # find_library_file () |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 360 | |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 361 | # class UnixCCompiler |
| 362 | |
| 363 | |
| 364 | def _split_command (cmd): |
| 365 | """Split a command string up into the progam to run (a string) and |
| 366 | the list of arguments; return them as (cmd, arglist).""" |
| 367 | args = string.split (cmd) |
| 368 | return (args[0], args[1:]) |