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 | |
| 56 | _obj_ext = '.o' |
| 57 | _exe_ext = '' |
| 58 | _shared_lib_ext = SO |
| 59 | _static_lib_ext = '.a' |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 60 | |
| 61 | def __init__ (self, |
| 62 | verbose=0, |
| 63 | dry_run=0): |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 64 | |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 65 | CCompiler.__init__ (self, verbose, dry_run) |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 66 | |
| 67 | self.preprocess_options = None |
| 68 | self.compile_options = None |
| 69 | |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 70 | # Munge CC and OPT together in case there are flags stuck in CC. |
| 71 | # Note that using these variables from sysconfig immediately makes |
| 72 | # this module specific to building Python extensions and |
| 73 | # inappropriate as a general-purpose C compiler front-end. So sue |
| 74 | # me. Note also that we use OPT rather than CFLAGS, because CFLAGS |
| 75 | # is the flags used to compile Python itself -- not only are there |
| 76 | # -I options in there, they are the *wrong* -I options. We'll |
| 77 | # leave selection of include directories up to the class using |
| 78 | # UnixCCompiler! |
| 79 | |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 80 | (self.cc, self.ccflags) = \ |
| 81 | _split_command (CC + ' ' + OPT) |
| 82 | self.ccflags_shared = string.split (CCSHARED) |
| 83 | |
| 84 | (self.ld_shared, self.ldflags_shared) = \ |
| 85 | _split_command (LDSHARED) |
| 86 | |
| 87 | |
| 88 | def compile (self, |
| 89 | sources, |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame^] | 90 | output_dir=None, |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 91 | macros=None, |
| 92 | includes=None): |
| 93 | |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame^] | 94 | if output_dir is None: |
| 95 | output_dir = self.output_dir |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 96 | if macros is None: |
| 97 | macros = [] |
| 98 | if includes is None: |
| 99 | includes = [] |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 100 | |
| 101 | if type (macros) is not ListType: |
| 102 | raise TypeError, \ |
| 103 | "'macros' (if supplied) must be a list of tuples" |
| 104 | if type (includes) is not ListType: |
| 105 | raise TypeError, \ |
| 106 | "'includes' (if supplied) must be a list of strings" |
| 107 | |
Greg Ward | c294113 | 1999-09-08 02:32:19 +0000 | [diff] [blame] | 108 | pp_opts = gen_preprocess_options (self.macros + macros, |
| 109 | self.include_dirs + includes) |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 110 | |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame^] | 111 | # So we can mangle 'sources' without hurting the caller's data |
| 112 | orig_sources = sources |
| 113 | sources = copy (sources) |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 114 | |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame^] | 115 | # Get the list of expected output (object) files and drop files we |
| 116 | # don't have to recompile. (Simplistic check -- we just compare the |
| 117 | # source and object file, no deep dependency checking involving |
| 118 | # header files. Hmmm.) |
| 119 | objects = self.object_filenames (sources, output_dir) |
| 120 | skipped = newer_pairwise (sources, objects) |
| 121 | for skipped_pair in skipped: |
| 122 | self.announce ("skipping %s (%s up-to-date)" % skipped_pair) |
| 123 | |
| 124 | # If anything left to compile, compile it |
| 125 | if sources: |
| 126 | # XXX use of ccflags_shared means we're blithely assuming |
| 127 | # that we're compiling for inclusion in a shared object! |
| 128 | # (will have to fix this when I add the ability to build a |
| 129 | # new Python) |
| 130 | cc_args = ['-c'] + pp_opts + \ |
| 131 | self.ccflags + self.ccflags_shared + \ |
| 132 | sources |
| 133 | self.spawn ([self.cc] + cc_args) |
| 134 | |
| 135 | |
| 136 | # Note that compiling multiple source files in the same go like |
| 137 | # we've just done drops the .o file in the current directory, which |
| 138 | # may not be what the caller wants (depending on the 'output_dir' |
| 139 | # parameter). So, if necessary, fix that now by moving the .o |
| 140 | # files into the desired output directory. (The alternative, of |
| 141 | # course, is to compile one-at-a-time with a -o option. 6 of one, |
| 142 | # 12/2 of the other...) |
| 143 | |
| 144 | if output_dir: |
| 145 | for i in range (len (objects)): |
| 146 | src = os.path.basename (objects[i]) |
| 147 | objects[i] = self.move_file (src, output_dir) |
| 148 | |
| 149 | # Have to re-fetch list of object filenames, because we want to |
| 150 | # return *all* of them, including those that weren't recompiled on |
| 151 | # this call! |
| 152 | return self.object_filenames (orig_sources, output_dir) |
Greg Ward | c294113 | 1999-09-08 02:32:19 +0000 | [diff] [blame] | 153 | |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 154 | |
| 155 | # XXX punting on 'link_static_lib()' for now -- it might be better for |
| 156 | # CCompiler to mandate just 'link_binary()' or some such to build a new |
| 157 | # Python binary; it would then take care of linking in everything |
| 158 | # needed for the new Python without messing with an intermediate static |
| 159 | # library. |
| 160 | |
| 161 | def link_shared_lib (self, |
| 162 | objects, |
| 163 | output_libname, |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame^] | 164 | output_dir=None, |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 165 | libraries=None, |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 166 | library_dirs=None, |
| 167 | build_info=None): |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 168 | # XXX should we sanity check the library name? (eg. no |
| 169 | # slashes) |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame^] | 170 | self.link_shared_object ( |
| 171 | objects, |
| 172 | "lib%s%s" % (output_libname, self._shared_lib_ext), |
| 173 | output_dir, |
| 174 | libraries, |
| 175 | library_dirs, |
| 176 | build_info) |
| 177 | |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 178 | |
| 179 | def link_shared_object (self, |
| 180 | objects, |
| 181 | output_filename, |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame^] | 182 | output_dir=None, |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 183 | libraries=None, |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 184 | library_dirs=None, |
| 185 | build_info=None): |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 186 | |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame^] | 187 | if output_dir is None: |
| 188 | output_dir = self.output_dir |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 189 | if libraries is None: |
| 190 | libraries = [] |
| 191 | if library_dirs is None: |
| 192 | library_dirs = [] |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 193 | if build_info is None: |
| 194 | build_info = {} |
| 195 | |
Greg Ward | c294113 | 1999-09-08 02:32:19 +0000 | [diff] [blame] | 196 | lib_opts = gen_lib_options (self.libraries + libraries, |
| 197 | self.library_dirs + library_dirs, |
| 198 | "-l%s", "-L%s") |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame^] | 199 | if output_dir is not None: |
| 200 | output_filename = os.path.join (output_dir, output_filename) |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 201 | |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame^] | 202 | # If any of the input object files are newer than the output shared |
| 203 | # object, relink. Again, this is a simplistic dependency check: |
| 204 | # doesn't look at any of the libraries we might be linking with. |
| 205 | if newer_group (objects, output_filename): |
| 206 | ld_args = self.ldflags_shared + lib_opts + \ |
| 207 | objects + ['-o', output_filename] |
| 208 | |
| 209 | self.spawn ([self.ld_shared] + ld_args) |
| 210 | else: |
| 211 | self.announce ("skipping %s (up-to-date)" % output_filename) |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 212 | |
| 213 | |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame^] | 214 | def object_filenames (self, source_filenames, output_dir=None): |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 215 | outnames = [] |
| 216 | for inname in source_filenames: |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame^] | 217 | outname = re.sub (r'\.(c|C|cc|cxx|cpp)$', self._obj_ext, inname) |
| 218 | outname = os.path.basename (outname) |
| 219 | if output_dir is not None: |
| 220 | outname = os.path.join (output_dir, outname) |
| 221 | outnames.append (outname) |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 222 | return outnames |
| 223 | |
Greg Ward | 8037cb1 | 1999-09-13 03:12:53 +0000 | [diff] [blame^] | 224 | def shared_object_filename (self, source_filename, output_dir=None): |
| 225 | outname = re.sub (r'\.(c|C|cc|cxx|cpp)$', self._shared_lib_ext) |
| 226 | outname = os.path.basename (outname) |
| 227 | if output_dir is not None: |
| 228 | outname = os.path.join (output_dir, outname) |
| 229 | return outname |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 230 | |
| 231 | def library_filename (self, libname): |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 232 | return "lib%s%s" % (libname, self._static_lib_ext ) |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 233 | |
| 234 | def shared_library_filename (self, libname): |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 235 | return "lib%s%s" % (libname, self._shared_lib_ext ) |
| 236 | |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 237 | # class UnixCCompiler |
| 238 | |
| 239 | |
| 240 | def _split_command (cmd): |
| 241 | """Split a command string up into the progam to run (a string) and |
| 242 | the list of arguments; return them as (cmd, arglist).""" |
| 243 | args = string.split (cmd) |
| 244 | return (args[0], args[1:]) |