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