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 | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 20 | import string, re |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 21 | from types import * |
| 22 | from sysconfig import \ |
| 23 | CC, CCSHARED, CFLAGS, OPT, LDSHARED, LDFLAGS, RANLIB, AR, SO |
Greg Ward | c294113 | 1999-09-08 02:32:19 +0000 | [diff] [blame] | 24 | from ccompiler import CCompiler, gen_preprocess_options, gen_lib_options |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 25 | |
| 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 | |
| 55 | _obj_ext = '.o' |
| 56 | _exe_ext = '' |
| 57 | _shared_lib_ext = SO |
| 58 | _static_lib_ext = '.a' |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 59 | |
| 60 | def __init__ (self, |
| 61 | verbose=0, |
| 62 | dry_run=0): |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 63 | |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 64 | CCompiler.__init__ (self, verbose, dry_run) |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 65 | |
| 66 | self.preprocess_options = None |
| 67 | self.compile_options = None |
| 68 | |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 69 | # Munge CC and OPT together in case there are flags stuck in CC. |
| 70 | # Note that using these variables from sysconfig immediately makes |
| 71 | # this module specific to building Python extensions and |
| 72 | # inappropriate as a general-purpose C compiler front-end. So sue |
| 73 | # me. Note also that we use OPT rather than CFLAGS, because CFLAGS |
| 74 | # is the flags used to compile Python itself -- not only are there |
| 75 | # -I options in there, they are the *wrong* -I options. We'll |
| 76 | # leave selection of include directories up to the class using |
| 77 | # UnixCCompiler! |
| 78 | |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 79 | (self.cc, self.ccflags) = \ |
| 80 | _split_command (CC + ' ' + OPT) |
| 81 | self.ccflags_shared = string.split (CCSHARED) |
| 82 | |
| 83 | (self.ld_shared, self.ldflags_shared) = \ |
| 84 | _split_command (LDSHARED) |
| 85 | |
| 86 | |
| 87 | def compile (self, |
| 88 | sources, |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 89 | macros=None, |
| 90 | includes=None): |
| 91 | |
| 92 | if macros is None: |
| 93 | macros = [] |
| 94 | if includes is None: |
| 95 | includes = [] |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 96 | |
| 97 | if type (macros) is not ListType: |
| 98 | raise TypeError, \ |
| 99 | "'macros' (if supplied) must be a list of tuples" |
| 100 | if type (includes) is not ListType: |
| 101 | raise TypeError, \ |
| 102 | "'includes' (if supplied) must be a list of strings" |
| 103 | |
Greg Ward | c294113 | 1999-09-08 02:32:19 +0000 | [diff] [blame] | 104 | pp_opts = gen_preprocess_options (self.macros + macros, |
| 105 | self.include_dirs + includes) |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 106 | |
| 107 | # use of ccflags_shared means we're blithely assuming that we're |
| 108 | # compiling for inclusion in a shared object! (will have to fix |
| 109 | # this when I add the ability to build a new Python) |
| 110 | cc_args = ['-c'] + pp_opts + \ |
| 111 | self.ccflags + self.ccflags_shared + \ |
| 112 | sources |
| 113 | |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 114 | self.spawn ([self.cc] + cc_args) |
Greg Ward | c294113 | 1999-09-08 02:32:19 +0000 | [diff] [blame] | 115 | return self.object_filenames (sources) |
| 116 | |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 117 | |
| 118 | # XXX punting on 'link_static_lib()' for now -- it might be better for |
| 119 | # CCompiler to mandate just 'link_binary()' or some such to build a new |
| 120 | # Python binary; it would then take care of linking in everything |
| 121 | # needed for the new Python without messing with an intermediate static |
| 122 | # library. |
| 123 | |
| 124 | def link_shared_lib (self, |
| 125 | objects, |
| 126 | output_libname, |
| 127 | libraries=None, |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 128 | library_dirs=None, |
| 129 | build_info=None): |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 130 | # XXX should we sanity check the library name? (eg. no |
| 131 | # slashes) |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 132 | self.link_shared_object (objects, "lib%s%s" % \ |
| 133 | (output_libname, self._shared_lib_ext), |
| 134 | build_info=build_info) |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 135 | |
| 136 | |
| 137 | def link_shared_object (self, |
| 138 | objects, |
| 139 | output_filename, |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 140 | libraries=None, |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 141 | library_dirs=None, |
| 142 | build_info=None): |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 143 | |
| 144 | if libraries is None: |
| 145 | libraries = [] |
| 146 | if library_dirs is None: |
| 147 | library_dirs = [] |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 148 | if build_info is None: |
| 149 | build_info = {} |
| 150 | |
Greg Ward | c294113 | 1999-09-08 02:32:19 +0000 | [diff] [blame] | 151 | lib_opts = gen_lib_options (self.libraries + libraries, |
| 152 | self.library_dirs + library_dirs, |
| 153 | "-l%s", "-L%s") |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 154 | ld_args = self.ldflags_shared + lib_opts + \ |
| 155 | objects + ['-o', output_filename] |
| 156 | |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 157 | self.spawn ([self.ld_shared] + ld_args) |
| 158 | |
| 159 | |
| 160 | def object_filenames (self, source_filenames): |
| 161 | outnames = [] |
| 162 | for inname in source_filenames: |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 163 | outnames.append ( re.sub (r'\.(c|C|cc|cxx|cpp)$', |
| 164 | self._obj_ext, inname)) |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 165 | return outnames |
| 166 | |
| 167 | def shared_object_filename (self, source_filename): |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 168 | return re.sub (r'\.(c|C|cc|cxx|cpp)$', self._shared_lib_ext) |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 169 | |
| 170 | def library_filename (self, libname): |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 171 | return "lib%s%s" % (libname, self._static_lib_ext ) |
Greg Ward | 5e71744 | 1999-08-14 23:53:53 +0000 | [diff] [blame] | 172 | |
| 173 | def shared_library_filename (self, libname): |
Greg Ward | 65f4a3b | 1999-08-29 18:23:32 +0000 | [diff] [blame] | 174 | return "lib%s%s" % (libname, self._shared_lib_ext ) |
| 175 | |
Greg Ward | 170bdc0 | 1999-07-10 02:04:22 +0000 | [diff] [blame] | 176 | # class UnixCCompiler |
| 177 | |
| 178 | |
| 179 | def _split_command (cmd): |
| 180 | """Split a command string up into the progam to run (a string) and |
| 181 | the list of arguments; return them as (cmd, arglist).""" |
| 182 | args = string.split (cmd) |
| 183 | return (args[0], args[1:]) |