| 1 | n/a | """Compiler abstraction model used by packaging. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | An abstract base class is defined in the ccompiler submodule, and |
|---|
| 4 | n/a | concrete implementations suitable for various platforms are defined in |
|---|
| 5 | n/a | the other submodules. The extension module is also placed in this |
|---|
| 6 | n/a | package. |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | In general, code should not instantiate compiler classes directly but |
|---|
| 9 | n/a | use the new_compiler and customize_compiler functions provided in this |
|---|
| 10 | n/a | module. |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | The compiler system has a registration API: get_default_compiler, |
|---|
| 13 | n/a | set_compiler, show_compilers. |
|---|
| 14 | n/a | """ |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | import os |
|---|
| 17 | n/a | import sys |
|---|
| 18 | n/a | import re |
|---|
| 19 | n/a | import sysconfig |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | from packaging.util import resolve_name |
|---|
| 22 | n/a | from packaging.errors import PackagingPlatformError |
|---|
| 23 | n/a | from packaging import logger |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | def customize_compiler(compiler): |
|---|
| 26 | n/a | """Do any platform-specific customization of a CCompiler instance. |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | Mainly needed on Unix, so we can plug in the information that |
|---|
| 29 | n/a | varies across Unices and is stored in Python's Makefile. |
|---|
| 30 | n/a | """ |
|---|
| 31 | n/a | if compiler.name == "unix": |
|---|
| 32 | n/a | cc, cxx, opt, cflags, ccshared, ldshared, so_ext, ar, ar_flags = ( |
|---|
| 33 | n/a | sysconfig.get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS', |
|---|
| 34 | n/a | 'CCSHARED', 'LDSHARED', 'SO', 'AR', |
|---|
| 35 | n/a | 'ARFLAGS')) |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | if 'CC' in os.environ: |
|---|
| 38 | n/a | cc = os.environ['CC'] |
|---|
| 39 | n/a | if 'CXX' in os.environ: |
|---|
| 40 | n/a | cxx = os.environ['CXX'] |
|---|
| 41 | n/a | if 'LDSHARED' in os.environ: |
|---|
| 42 | n/a | ldshared = os.environ['LDSHARED'] |
|---|
| 43 | n/a | if 'CPP' in os.environ: |
|---|
| 44 | n/a | cpp = os.environ['CPP'] |
|---|
| 45 | n/a | else: |
|---|
| 46 | n/a | cpp = cc + " -E" # not always |
|---|
| 47 | n/a | if 'LDFLAGS' in os.environ: |
|---|
| 48 | n/a | ldshared = ldshared + ' ' + os.environ['LDFLAGS'] |
|---|
| 49 | n/a | if 'CFLAGS' in os.environ: |
|---|
| 50 | n/a | cflags = opt + ' ' + os.environ['CFLAGS'] |
|---|
| 51 | n/a | ldshared = ldshared + ' ' + os.environ['CFLAGS'] |
|---|
| 52 | n/a | if 'CPPFLAGS' in os.environ: |
|---|
| 53 | n/a | cpp = cpp + ' ' + os.environ['CPPFLAGS'] |
|---|
| 54 | n/a | cflags = cflags + ' ' + os.environ['CPPFLAGS'] |
|---|
| 55 | n/a | ldshared = ldshared + ' ' + os.environ['CPPFLAGS'] |
|---|
| 56 | n/a | if 'AR' in os.environ: |
|---|
| 57 | n/a | ar = os.environ['AR'] |
|---|
| 58 | n/a | if 'ARFLAGS' in os.environ: |
|---|
| 59 | n/a | archiver = ar + ' ' + os.environ['ARFLAGS'] |
|---|
| 60 | n/a | else: |
|---|
| 61 | n/a | if ar_flags is not None: |
|---|
| 62 | n/a | archiver = ar + ' ' + ar_flags |
|---|
| 63 | n/a | else: |
|---|
| 64 | n/a | # see if its the proper default value |
|---|
| 65 | n/a | # mmm I don't want to backport the makefile |
|---|
| 66 | n/a | archiver = ar + ' rc' |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | cc_cmd = cc + ' ' + cflags |
|---|
| 69 | n/a | compiler.set_executables( |
|---|
| 70 | n/a | preprocessor=cpp, |
|---|
| 71 | n/a | compiler=cc_cmd, |
|---|
| 72 | n/a | compiler_so=cc_cmd + ' ' + ccshared, |
|---|
| 73 | n/a | compiler_cxx=cxx, |
|---|
| 74 | n/a | linker_so=ldshared, |
|---|
| 75 | n/a | linker_exe=cc, |
|---|
| 76 | n/a | archiver=archiver) |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | compiler.shared_lib_extension = so_ext |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | # Map a sys.platform/os.name ('posix', 'nt') to the default compiler |
|---|
| 82 | n/a | # type for that platform. Keys are interpreted as re match |
|---|
| 83 | n/a | # patterns. Order is important; platform mappings are preferred over |
|---|
| 84 | n/a | # OS names. |
|---|
| 85 | n/a | _default_compilers = ( |
|---|
| 86 | n/a | # Platform string mappings |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | # on a cygwin built python we can use gcc like an ordinary UNIXish |
|---|
| 89 | n/a | # compiler |
|---|
| 90 | n/a | ('cygwin.*', 'unix'), |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | # OS name mappings |
|---|
| 93 | n/a | ('posix', 'unix'), |
|---|
| 94 | n/a | ('nt', 'msvc'), |
|---|
| 95 | n/a | ) |
|---|
| 96 | n/a | |
|---|
| 97 | n/a | def get_default_compiler(osname=None, platform=None): |
|---|
| 98 | n/a | """ Determine the default compiler to use for the given platform. |
|---|
| 99 | n/a | |
|---|
| 100 | n/a | osname should be one of the standard Python OS names (i.e. the |
|---|
| 101 | n/a | ones returned by os.name) and platform the common value |
|---|
| 102 | n/a | returned by sys.platform for the platform in question. |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | The default values are os.name and sys.platform in case the |
|---|
| 105 | n/a | parameters are not given. |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | """ |
|---|
| 108 | n/a | if osname is None: |
|---|
| 109 | n/a | osname = os.name |
|---|
| 110 | n/a | if platform is None: |
|---|
| 111 | n/a | platform = sys.platform |
|---|
| 112 | n/a | for pattern, compiler in _default_compilers: |
|---|
| 113 | n/a | if re.match(pattern, platform) is not None or \ |
|---|
| 114 | n/a | re.match(pattern, osname) is not None: |
|---|
| 115 | n/a | return compiler |
|---|
| 116 | n/a | # Defaults to Unix compiler |
|---|
| 117 | n/a | return 'unix' |
|---|
| 118 | n/a | |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | # compiler mapping |
|---|
| 121 | n/a | # XXX useful to expose them? (i.e. get_compiler_names) |
|---|
| 122 | n/a | _COMPILERS = { |
|---|
| 123 | n/a | 'unix': 'packaging.compiler.unixccompiler.UnixCCompiler', |
|---|
| 124 | n/a | 'msvc': 'packaging.compiler.msvccompiler.MSVCCompiler', |
|---|
| 125 | n/a | 'cygwin': 'packaging.compiler.cygwinccompiler.CygwinCCompiler', |
|---|
| 126 | n/a | 'mingw32': 'packaging.compiler.cygwinccompiler.Mingw32CCompiler', |
|---|
| 127 | n/a | 'bcpp': 'packaging.compiler.bcppcompiler.BCPPCompiler', |
|---|
| 128 | n/a | } |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | def set_compiler(location): |
|---|
| 131 | n/a | """Add or change a compiler""" |
|---|
| 132 | n/a | cls = resolve_name(location) |
|---|
| 133 | n/a | # XXX we want to check the class here |
|---|
| 134 | n/a | _COMPILERS[cls.name] = cls |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | |
|---|
| 137 | n/a | def show_compilers(): |
|---|
| 138 | n/a | """Print list of available compilers (used by the "--help-compiler" |
|---|
| 139 | n/a | options to "build", "build_ext", "build_clib"). |
|---|
| 140 | n/a | """ |
|---|
| 141 | n/a | from packaging.fancy_getopt import FancyGetopt |
|---|
| 142 | n/a | compilers = [] |
|---|
| 143 | n/a | |
|---|
| 144 | n/a | for name, cls in _COMPILERS.items(): |
|---|
| 145 | n/a | if isinstance(cls, str): |
|---|
| 146 | n/a | cls = resolve_name(cls) |
|---|
| 147 | n/a | _COMPILERS[name] = cls |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | compilers.append(("compiler=" + name, None, cls.description)) |
|---|
| 150 | n/a | |
|---|
| 151 | n/a | compilers.sort() |
|---|
| 152 | n/a | pretty_printer = FancyGetopt(compilers) |
|---|
| 153 | n/a | pretty_printer.print_help("List of available compilers:") |
|---|
| 154 | n/a | |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | def new_compiler(plat=None, compiler=None, dry_run=False, force=False): |
|---|
| 157 | n/a | """Generate an instance of some CCompiler subclass for the supplied |
|---|
| 158 | n/a | platform/compiler combination. 'plat' defaults to 'os.name' |
|---|
| 159 | n/a | (eg. 'posix', 'nt'), and 'compiler' defaults to the default compiler |
|---|
| 160 | n/a | for that platform. Currently only 'posix' and 'nt' are supported, and |
|---|
| 161 | n/a | the default compilers are "traditional Unix interface" (UnixCCompiler |
|---|
| 162 | n/a | class) and Visual C++ (MSVCCompiler class). Note that it's perfectly |
|---|
| 163 | n/a | possible to ask for a Unix compiler object under Windows, and a |
|---|
| 164 | n/a | Microsoft compiler object under Unix -- if you supply a value for |
|---|
| 165 | n/a | 'compiler', 'plat' is ignored. |
|---|
| 166 | n/a | """ |
|---|
| 167 | n/a | if plat is None: |
|---|
| 168 | n/a | plat = os.name |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | try: |
|---|
| 171 | n/a | if compiler is None: |
|---|
| 172 | n/a | compiler = get_default_compiler(plat) |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | cls = _COMPILERS[compiler] |
|---|
| 175 | n/a | except KeyError: |
|---|
| 176 | n/a | msg = "don't know how to compile C/C++ code on platform '%s'" % plat |
|---|
| 177 | n/a | if compiler is not None: |
|---|
| 178 | n/a | msg = msg + " with '%s' compiler" % compiler |
|---|
| 179 | n/a | raise PackagingPlatformError(msg) |
|---|
| 180 | n/a | |
|---|
| 181 | n/a | if isinstance(cls, str): |
|---|
| 182 | n/a | cls = resolve_name(cls) |
|---|
| 183 | n/a | _COMPILERS[compiler] = cls |
|---|
| 184 | n/a | |
|---|
| 185 | n/a | return cls(dry_run, force) |
|---|
| 186 | n/a | |
|---|
| 187 | n/a | |
|---|
| 188 | n/a | def gen_preprocess_options(macros, include_dirs): |
|---|
| 189 | n/a | """Generate C pre-processor options (-D, -U, -I) as used by at least |
|---|
| 190 | n/a | two types of compilers: the typical Unix compiler and Visual C++. |
|---|
| 191 | n/a | 'macros' is the usual thing, a list of 1- or 2-tuples, where (name,) |
|---|
| 192 | n/a | means undefine (-U) macro 'name', and (name,value) means define (-D) |
|---|
| 193 | n/a | macro 'name' to 'value'. 'include_dirs' is just a list of directory |
|---|
| 194 | n/a | names to be added to the header file search path (-I). Returns a list |
|---|
| 195 | n/a | of command-line options suitable for either Unix compilers or Visual |
|---|
| 196 | n/a | C++. |
|---|
| 197 | n/a | """ |
|---|
| 198 | n/a | # XXX it would be nice (mainly aesthetic, and so we don't generate |
|---|
| 199 | n/a | # stupid-looking command lines) to go over 'macros' and eliminate |
|---|
| 200 | n/a | # redundant definitions/undefinitions (ie. ensure that only the |
|---|
| 201 | n/a | # latest mention of a particular macro winds up on the command |
|---|
| 202 | n/a | # line). I don't think it's essential, though, since most (all?) |
|---|
| 203 | n/a | # Unix C compilers only pay attention to the latest -D or -U |
|---|
| 204 | n/a | # mention of a macro on their command line. Similar situation for |
|---|
| 205 | n/a | # 'include_dirs'. I'm punting on both for now. Anyways, weeding out |
|---|
| 206 | n/a | # redundancies like this should probably be the province of |
|---|
| 207 | n/a | # CCompiler, since the data structures used are inherited from it |
|---|
| 208 | n/a | # and therefore common to all CCompiler classes. |
|---|
| 209 | n/a | |
|---|
| 210 | n/a | pp_opts = [] |
|---|
| 211 | n/a | for macro in macros: |
|---|
| 212 | n/a | |
|---|
| 213 | n/a | if not isinstance(macro, tuple) and 1 <= len(macro) <= 2: |
|---|
| 214 | n/a | raise TypeError( |
|---|
| 215 | n/a | "bad macro definition '%s': each element of 'macros'" |
|---|
| 216 | n/a | "list must be a 1- or 2-tuple" % macro) |
|---|
| 217 | n/a | |
|---|
| 218 | n/a | if len(macro) == 1: # undefine this macro |
|---|
| 219 | n/a | pp_opts.append("-U%s" % macro[0]) |
|---|
| 220 | n/a | elif len(macro) == 2: |
|---|
| 221 | n/a | if macro[1] is None: # define with no explicit value |
|---|
| 222 | n/a | pp_opts.append("-D%s" % macro[0]) |
|---|
| 223 | n/a | else: |
|---|
| 224 | n/a | # XXX *don't* need to be clever about quoting the |
|---|
| 225 | n/a | # macro value here, because we're going to avoid the |
|---|
| 226 | n/a | # shell at all costs when we spawn the command! |
|---|
| 227 | n/a | pp_opts.append("-D%s=%s" % macro) |
|---|
| 228 | n/a | |
|---|
| 229 | n/a | for dir in include_dirs: |
|---|
| 230 | n/a | pp_opts.append("-I%s" % dir) |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | return pp_opts |
|---|
| 233 | n/a | |
|---|
| 234 | n/a | |
|---|
| 235 | n/a | def gen_lib_options(compiler, library_dirs, runtime_library_dirs, libraries): |
|---|
| 236 | n/a | """Generate linker options for searching library directories and |
|---|
| 237 | n/a | linking with specific libraries. |
|---|
| 238 | n/a | |
|---|
| 239 | n/a | 'libraries' and 'library_dirs' are, respectively, lists of library names |
|---|
| 240 | n/a | (not filenames!) and search directories. Returns a list of command-line |
|---|
| 241 | n/a | options suitable for use with some compiler (depending on the two format |
|---|
| 242 | n/a | strings passed in). |
|---|
| 243 | n/a | """ |
|---|
| 244 | n/a | lib_opts = [] |
|---|
| 245 | n/a | |
|---|
| 246 | n/a | for dir in library_dirs: |
|---|
| 247 | n/a | lib_opts.append(compiler.library_dir_option(dir)) |
|---|
| 248 | n/a | |
|---|
| 249 | n/a | for dir in runtime_library_dirs: |
|---|
| 250 | n/a | opt = compiler.runtime_library_dir_option(dir) |
|---|
| 251 | n/a | if isinstance(opt, list): |
|---|
| 252 | n/a | lib_opts.extend(opt) |
|---|
| 253 | n/a | else: |
|---|
| 254 | n/a | lib_opts.append(opt) |
|---|
| 255 | n/a | |
|---|
| 256 | n/a | # XXX it's important that we *not* remove redundant library mentions! |
|---|
| 257 | n/a | # sometimes you really do have to say "-lfoo -lbar -lfoo" in order to |
|---|
| 258 | n/a | # resolve all symbols. I just hope we never have to say "-lfoo obj.o |
|---|
| 259 | n/a | # -lbar" to get things to work -- that's certainly a possibility, but a |
|---|
| 260 | n/a | # pretty nasty way to arrange your C code. |
|---|
| 261 | n/a | |
|---|
| 262 | n/a | for lib in libraries: |
|---|
| 263 | n/a | lib_dir, lib_name = os.path.split(lib) |
|---|
| 264 | n/a | if lib_dir != '': |
|---|
| 265 | n/a | lib_file = compiler.find_library_file([lib_dir], lib_name) |
|---|
| 266 | n/a | if lib_file is not None: |
|---|
| 267 | n/a | lib_opts.append(lib_file) |
|---|
| 268 | n/a | else: |
|---|
| 269 | n/a | logger.warning("no library file corresponding to " |
|---|
| 270 | n/a | "'%s' found (skipping)" % lib) |
|---|
| 271 | n/a | else: |
|---|
| 272 | n/a | lib_opts.append(compiler.library_option(lib)) |
|---|
| 273 | n/a | |
|---|
| 274 | n/a | return lib_opts |
|---|