| 1 | n/a | """Abstract base class for compilers. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | This modules contains CCompiler, an abstract base class that defines the |
|---|
| 4 | n/a | interface for the compiler abstraction model used by packaging. |
|---|
| 5 | n/a | """ |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | import os |
|---|
| 8 | n/a | from shutil import move |
|---|
| 9 | n/a | from packaging import logger |
|---|
| 10 | n/a | from packaging.util import split_quoted, execute, newer_group, spawn |
|---|
| 11 | n/a | from packaging.errors import (CompileError, LinkError, UnknownFileError) |
|---|
| 12 | n/a | from packaging.compiler import gen_preprocess_options |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | class CCompiler: |
|---|
| 16 | n/a | """Abstract base class to define the interface that must be implemented |
|---|
| 17 | n/a | by real compiler classes. Also has some utility methods used by |
|---|
| 18 | n/a | several compiler classes. |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | The basic idea behind a compiler abstraction class is that each |
|---|
| 21 | n/a | instance can be used for all the compile/link steps in building a |
|---|
| 22 | n/a | single project. Thus, attributes common to all of those compile and |
|---|
| 23 | n/a | link steps -- include directories, macros to define, libraries to link |
|---|
| 24 | n/a | against, etc. -- are attributes of the compiler instance. To allow for |
|---|
| 25 | n/a | variability in how individual files are treated, most of those |
|---|
| 26 | n/a | attributes may be varied on a per-compilation or per-link basis. |
|---|
| 27 | n/a | """ |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | # 'name' is a class attribute that identifies this class. It |
|---|
| 30 | n/a | # keeps code that wants to know what kind of compiler it's dealing with |
|---|
| 31 | n/a | # from having to import all possible compiler classes just to do an |
|---|
| 32 | n/a | # 'isinstance'. |
|---|
| 33 | n/a | name = None |
|---|
| 34 | n/a | description = None |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | # XXX things not handled by this compiler abstraction model: |
|---|
| 37 | n/a | # * client can't provide additional options for a compiler, |
|---|
| 38 | n/a | # e.g. warning, optimization, debugging flags. Perhaps this |
|---|
| 39 | n/a | # should be the domain of concrete compiler abstraction classes |
|---|
| 40 | n/a | # (UnixCCompiler, MSVCCompiler, etc.) -- or perhaps the base |
|---|
| 41 | n/a | # class should have methods for the common ones. |
|---|
| 42 | n/a | # * can't completely override the include or library searchg |
|---|
| 43 | n/a | # path, ie. no "cc -I -Idir1 -Idir2" or "cc -L -Ldir1 -Ldir2". |
|---|
| 44 | n/a | # I'm not sure how widely supported this is even by Unix |
|---|
| 45 | n/a | # compilers, much less on other platforms. And I'm even less |
|---|
| 46 | n/a | # sure how useful it is; maybe for cross-compiling, but |
|---|
| 47 | n/a | # support for that is a ways off. (And anyways, cross |
|---|
| 48 | n/a | # compilers probably have a dedicated binary with the |
|---|
| 49 | n/a | # right paths compiled in. I hope.) |
|---|
| 50 | n/a | # * can't do really freaky things with the library list/library |
|---|
| 51 | n/a | # dirs, e.g. "-Ldir1 -lfoo -Ldir2 -lfoo" to link against |
|---|
| 52 | n/a | # different versions of libfoo.a in different locations. I |
|---|
| 53 | n/a | # think this is useless without the ability to null out the |
|---|
| 54 | n/a | # library search path anyways. |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | # Subclasses that rely on the standard filename generation methods |
|---|
| 58 | n/a | # implemented below should override these; see the comment near |
|---|
| 59 | n/a | # those methods ('object_filenames()' et. al.) for details: |
|---|
| 60 | n/a | src_extensions = None # list of strings |
|---|
| 61 | n/a | obj_extension = None # string |
|---|
| 62 | n/a | static_lib_extension = None |
|---|
| 63 | n/a | shared_lib_extension = None # string |
|---|
| 64 | n/a | static_lib_format = None # format string |
|---|
| 65 | n/a | shared_lib_format = None # prob. same as static_lib_format |
|---|
| 66 | n/a | exe_extension = None # string |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | # Default language settings. language_map is used to detect a source |
|---|
| 69 | n/a | # file or Extension target language, checking source filenames. |
|---|
| 70 | n/a | # language_order is used to detect the language precedence, when deciding |
|---|
| 71 | n/a | # what language to use when mixing source types. For example, if some |
|---|
| 72 | n/a | # extension has two files with ".c" extension, and one with ".cpp", it |
|---|
| 73 | n/a | # is still linked as c++. |
|---|
| 74 | n/a | language_map = {".c": "c", |
|---|
| 75 | n/a | ".cc": "c++", |
|---|
| 76 | n/a | ".cpp": "c++", |
|---|
| 77 | n/a | ".cxx": "c++", |
|---|
| 78 | n/a | ".m": "objc", |
|---|
| 79 | n/a | } |
|---|
| 80 | n/a | language_order = ["c++", "objc", "c"] |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | def __init__(self, dry_run=False, force=False): |
|---|
| 83 | n/a | self.dry_run = dry_run |
|---|
| 84 | n/a | self.force = force |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | # 'output_dir': a common output directory for object, library, |
|---|
| 87 | n/a | # shared object, and shared library files |
|---|
| 88 | n/a | self.output_dir = None |
|---|
| 89 | n/a | |
|---|
| 90 | n/a | # 'macros': a list of macro definitions (or undefinitions). A |
|---|
| 91 | n/a | # macro definition is a 2-tuple (name, value), where the value is |
|---|
| 92 | n/a | # either a string or None (no explicit value). A macro |
|---|
| 93 | n/a | # undefinition is a 1-tuple (name,). |
|---|
| 94 | n/a | self.macros = [] |
|---|
| 95 | n/a | |
|---|
| 96 | n/a | # 'include_dirs': a list of directories to search for include files |
|---|
| 97 | n/a | self.include_dirs = [] |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | # 'libraries': a list of libraries to include in any link |
|---|
| 100 | n/a | # (library names, not filenames: eg. "foo" not "libfoo.a") |
|---|
| 101 | n/a | self.libraries = [] |
|---|
| 102 | n/a | |
|---|
| 103 | n/a | # 'library_dirs': a list of directories to search for libraries |
|---|
| 104 | n/a | self.library_dirs = [] |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | # 'runtime_library_dirs': a list of directories to search for |
|---|
| 107 | n/a | # shared libraries/objects at runtime |
|---|
| 108 | n/a | self.runtime_library_dirs = [] |
|---|
| 109 | n/a | |
|---|
| 110 | n/a | # 'objects': a list of object files (or similar, such as explicitly |
|---|
| 111 | n/a | # named library files) to include on any link |
|---|
| 112 | n/a | self.objects = [] |
|---|
| 113 | n/a | |
|---|
| 114 | n/a | for key, value in self.executables.items(): |
|---|
| 115 | n/a | self.set_executable(key, value) |
|---|
| 116 | n/a | |
|---|
| 117 | n/a | def set_executables(self, **args): |
|---|
| 118 | n/a | """Define the executables (and options for them) that will be run |
|---|
| 119 | n/a | to perform the various stages of compilation. The exact set of |
|---|
| 120 | n/a | executables that may be specified here depends on the compiler |
|---|
| 121 | n/a | class (via the 'executables' class attribute), but most will have: |
|---|
| 122 | n/a | compiler the C/C++ compiler |
|---|
| 123 | n/a | linker_so linker used to create shared objects and libraries |
|---|
| 124 | n/a | linker_exe linker used to create binary executables |
|---|
| 125 | n/a | archiver static library creator |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | On platforms with a command line (Unix, DOS/Windows), each of these |
|---|
| 128 | n/a | is a string that will be split into executable name and (optional) |
|---|
| 129 | n/a | list of arguments. (Splitting the string is done similarly to how |
|---|
| 130 | n/a | Unix shells operate: words are delimited by spaces, but quotes and |
|---|
| 131 | n/a | backslashes can override this. See |
|---|
| 132 | n/a | 'distutils.util.split_quoted()'.) |
|---|
| 133 | n/a | """ |
|---|
| 134 | n/a | |
|---|
| 135 | n/a | # Note that some CCompiler implementation classes will define class |
|---|
| 136 | n/a | # attributes 'cpp', 'cc', etc. with hard-coded executable names; |
|---|
| 137 | n/a | # this is appropriate when a compiler class is for exactly one |
|---|
| 138 | n/a | # compiler/OS combination (eg. MSVCCompiler). Other compiler |
|---|
| 139 | n/a | # classes (UnixCCompiler, in particular) are driven by information |
|---|
| 140 | n/a | # discovered at run-time, since there are many different ways to do |
|---|
| 141 | n/a | # basically the same things with Unix C compilers. |
|---|
| 142 | n/a | |
|---|
| 143 | n/a | for key, value in args.items(): |
|---|
| 144 | n/a | if key not in self.executables: |
|---|
| 145 | n/a | raise ValueError("unknown executable '%s' for class %s" % \ |
|---|
| 146 | n/a | (key, self.__class__.__name__)) |
|---|
| 147 | n/a | self.set_executable(key, value) |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | def set_executable(self, key, value): |
|---|
| 150 | n/a | if isinstance(value, str): |
|---|
| 151 | n/a | setattr(self, key, split_quoted(value)) |
|---|
| 152 | n/a | else: |
|---|
| 153 | n/a | setattr(self, key, value) |
|---|
| 154 | n/a | |
|---|
| 155 | n/a | def _find_macro(self, name): |
|---|
| 156 | n/a | i = 0 |
|---|
| 157 | n/a | for defn in self.macros: |
|---|
| 158 | n/a | if defn[0] == name: |
|---|
| 159 | n/a | return i |
|---|
| 160 | n/a | i = i + 1 |
|---|
| 161 | n/a | return None |
|---|
| 162 | n/a | |
|---|
| 163 | n/a | def _check_macro_definitions(self, definitions): |
|---|
| 164 | n/a | """Ensures that every element of 'definitions' is a valid macro |
|---|
| 165 | n/a | definition, ie. either (name,value) 2-tuple or a (name,) tuple. Do |
|---|
| 166 | n/a | nothing if all definitions are OK, raise TypeError otherwise. |
|---|
| 167 | n/a | """ |
|---|
| 168 | n/a | for defn in definitions: |
|---|
| 169 | n/a | if not (isinstance(defn, tuple) and |
|---|
| 170 | n/a | (len(defn) == 1 or |
|---|
| 171 | n/a | (len(defn) == 2 and |
|---|
| 172 | n/a | (isinstance(defn[1], str) or defn[1] is None))) and |
|---|
| 173 | n/a | isinstance(defn[0], str)): |
|---|
| 174 | n/a | raise TypeError(("invalid macro definition '%s': " % defn) + \ |
|---|
| 175 | n/a | "must be tuple (string,), (string, string), or " + \ |
|---|
| 176 | n/a | "(string, None)") |
|---|
| 177 | n/a | |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | # -- Bookkeeping methods ------------------------------------------- |
|---|
| 180 | n/a | |
|---|
| 181 | n/a | def define_macro(self, name, value=None): |
|---|
| 182 | n/a | """Define a preprocessor macro for all compilations driven by this |
|---|
| 183 | n/a | compiler object. The optional parameter 'value' should be a |
|---|
| 184 | n/a | string; if it is not supplied, then the macro will be defined |
|---|
| 185 | n/a | without an explicit value and the exact outcome depends on the |
|---|
| 186 | n/a | compiler used (XXX true? does ANSI say anything about this?) |
|---|
| 187 | n/a | """ |
|---|
| 188 | n/a | # Delete from the list of macro definitions/undefinitions if |
|---|
| 189 | n/a | # already there (so that this one will take precedence). |
|---|
| 190 | n/a | i = self._find_macro(name) |
|---|
| 191 | n/a | if i is not None: |
|---|
| 192 | n/a | del self.macros[i] |
|---|
| 193 | n/a | |
|---|
| 194 | n/a | defn = (name, value) |
|---|
| 195 | n/a | self.macros.append(defn) |
|---|
| 196 | n/a | |
|---|
| 197 | n/a | def undefine_macro(self, name): |
|---|
| 198 | n/a | """Undefine a preprocessor macro for all compilations driven by |
|---|
| 199 | n/a | this compiler object. If the same macro is defined by |
|---|
| 200 | n/a | 'define_macro()' and undefined by 'undefine_macro()' the last call |
|---|
| 201 | n/a | takes precedence (including multiple redefinitions or |
|---|
| 202 | n/a | undefinitions). If the macro is redefined/undefined on a |
|---|
| 203 | n/a | per-compilation basis (ie. in the call to 'compile()'), then that |
|---|
| 204 | n/a | takes precedence. |
|---|
| 205 | n/a | """ |
|---|
| 206 | n/a | # Delete from the list of macro definitions/undefinitions if |
|---|
| 207 | n/a | # already there (so that this one will take precedence). |
|---|
| 208 | n/a | i = self._find_macro(name) |
|---|
| 209 | n/a | if i is not None: |
|---|
| 210 | n/a | del self.macros[i] |
|---|
| 211 | n/a | |
|---|
| 212 | n/a | undefn = (name,) |
|---|
| 213 | n/a | self.macros.append(undefn) |
|---|
| 214 | n/a | |
|---|
| 215 | n/a | def add_include_dir(self, dir): |
|---|
| 216 | n/a | """Add 'dir' to the list of directories that will be searched for |
|---|
| 217 | n/a | header files. The compiler is instructed to search directories in |
|---|
| 218 | n/a | the order in which they are supplied by successive calls to |
|---|
| 219 | n/a | 'add_include_dir()'. |
|---|
| 220 | n/a | """ |
|---|
| 221 | n/a | self.include_dirs.append(dir) |
|---|
| 222 | n/a | |
|---|
| 223 | n/a | def set_include_dirs(self, dirs): |
|---|
| 224 | n/a | """Set the list of directories that will be searched to 'dirs' (a |
|---|
| 225 | n/a | list of strings). Overrides any preceding calls to |
|---|
| 226 | n/a | 'add_include_dir()'; subsequence calls to 'add_include_dir()' add |
|---|
| 227 | n/a | to the list passed to 'set_include_dirs()'. This does not affect |
|---|
| 228 | n/a | any list of standard include directories that the compiler may |
|---|
| 229 | n/a | search by default. |
|---|
| 230 | n/a | """ |
|---|
| 231 | n/a | self.include_dirs = dirs[:] |
|---|
| 232 | n/a | |
|---|
| 233 | n/a | def add_library(self, libname): |
|---|
| 234 | n/a | """Add 'libname' to the list of libraries that will be included in |
|---|
| 235 | n/a | all links driven by this compiler object. Note that 'libname' |
|---|
| 236 | n/a | should *not* be the name of a file containing a library, but the |
|---|
| 237 | n/a | name of the library itself: the actual filename will be inferred by |
|---|
| 238 | n/a | the linker, the compiler, or the compiler class (depending on the |
|---|
| 239 | n/a | platform). |
|---|
| 240 | n/a | |
|---|
| 241 | n/a | The linker will be instructed to link against libraries in the |
|---|
| 242 | n/a | order they were supplied to 'add_library()' and/or |
|---|
| 243 | n/a | 'set_libraries()'. It is perfectly valid to duplicate library |
|---|
| 244 | n/a | names; the linker will be instructed to link against libraries as |
|---|
| 245 | n/a | many times as they are mentioned. |
|---|
| 246 | n/a | """ |
|---|
| 247 | n/a | self.libraries.append(libname) |
|---|
| 248 | n/a | |
|---|
| 249 | n/a | def set_libraries(self, libnames): |
|---|
| 250 | n/a | """Set the list of libraries to be included in all links driven by |
|---|
| 251 | n/a | this compiler object to 'libnames' (a list of strings). This does |
|---|
| 252 | n/a | not affect any standard system libraries that the linker may |
|---|
| 253 | n/a | include by default. |
|---|
| 254 | n/a | """ |
|---|
| 255 | n/a | self.libraries = libnames[:] |
|---|
| 256 | n/a | |
|---|
| 257 | n/a | |
|---|
| 258 | n/a | def add_library_dir(self, dir): |
|---|
| 259 | n/a | """Add 'dir' to the list of directories that will be searched for |
|---|
| 260 | n/a | libraries specified to 'add_library()' and 'set_libraries()'. The |
|---|
| 261 | n/a | linker will be instructed to search for libraries in the order they |
|---|
| 262 | n/a | are supplied to 'add_library_dir()' and/or 'set_library_dirs()'. |
|---|
| 263 | n/a | """ |
|---|
| 264 | n/a | self.library_dirs.append(dir) |
|---|
| 265 | n/a | |
|---|
| 266 | n/a | def set_library_dirs(self, dirs): |
|---|
| 267 | n/a | """Set the list of library search directories to 'dirs' (a list of |
|---|
| 268 | n/a | strings). This does not affect any standard library search path |
|---|
| 269 | n/a | that the linker may search by default. |
|---|
| 270 | n/a | """ |
|---|
| 271 | n/a | self.library_dirs = dirs[:] |
|---|
| 272 | n/a | |
|---|
| 273 | n/a | def add_runtime_library_dir(self, dir): |
|---|
| 274 | n/a | """Add 'dir' to the list of directories that will be searched for |
|---|
| 275 | n/a | shared libraries at runtime. |
|---|
| 276 | n/a | """ |
|---|
| 277 | n/a | self.runtime_library_dirs.append(dir) |
|---|
| 278 | n/a | |
|---|
| 279 | n/a | def set_runtime_library_dirs(self, dirs): |
|---|
| 280 | n/a | """Set the list of directories to search for shared libraries at |
|---|
| 281 | n/a | runtime to 'dirs' (a list of strings). This does not affect any |
|---|
| 282 | n/a | standard search path that the runtime linker may search by |
|---|
| 283 | n/a | default. |
|---|
| 284 | n/a | """ |
|---|
| 285 | n/a | self.runtime_library_dirs = dirs[:] |
|---|
| 286 | n/a | |
|---|
| 287 | n/a | def add_link_object(self, object): |
|---|
| 288 | n/a | """Add 'object' to the list of object files (or analogues, such as |
|---|
| 289 | n/a | explicitly named library files or the output of "resource |
|---|
| 290 | n/a | compilers") to be included in every link driven by this compiler |
|---|
| 291 | n/a | object. |
|---|
| 292 | n/a | """ |
|---|
| 293 | n/a | self.objects.append(object) |
|---|
| 294 | n/a | |
|---|
| 295 | n/a | def set_link_objects(self, objects): |
|---|
| 296 | n/a | """Set the list of object files (or analogues) to be included in |
|---|
| 297 | n/a | every link to 'objects'. This does not affect any standard object |
|---|
| 298 | n/a | files that the linker may include by default (such as system |
|---|
| 299 | n/a | libraries). |
|---|
| 300 | n/a | """ |
|---|
| 301 | n/a | self.objects = objects[:] |
|---|
| 302 | n/a | |
|---|
| 303 | n/a | |
|---|
| 304 | n/a | # -- Private utility methods -------------------------------------- |
|---|
| 305 | n/a | # (here for the convenience of subclasses) |
|---|
| 306 | n/a | |
|---|
| 307 | n/a | # Helper method to prep compiler in subclass compile() methods |
|---|
| 308 | n/a | def _setup_compile(self, outdir, macros, incdirs, sources, depends, |
|---|
| 309 | n/a | extra): |
|---|
| 310 | n/a | """Process arguments and decide which source files to compile.""" |
|---|
| 311 | n/a | if outdir is None: |
|---|
| 312 | n/a | outdir = self.output_dir |
|---|
| 313 | n/a | elif not isinstance(outdir, str): |
|---|
| 314 | n/a | raise TypeError("'output_dir' must be a string or None") |
|---|
| 315 | n/a | |
|---|
| 316 | n/a | if macros is None: |
|---|
| 317 | n/a | macros = self.macros |
|---|
| 318 | n/a | elif isinstance(macros, list): |
|---|
| 319 | n/a | macros = macros + (self.macros or []) |
|---|
| 320 | n/a | else: |
|---|
| 321 | n/a | raise TypeError("'macros' (if supplied) must be a list of tuples") |
|---|
| 322 | n/a | |
|---|
| 323 | n/a | if incdirs is None: |
|---|
| 324 | n/a | incdirs = self.include_dirs |
|---|
| 325 | n/a | elif isinstance(incdirs, (list, tuple)): |
|---|
| 326 | n/a | incdirs = list(incdirs) + (self.include_dirs or []) |
|---|
| 327 | n/a | else: |
|---|
| 328 | n/a | raise TypeError( |
|---|
| 329 | n/a | "'include_dirs' (if supplied) must be a list of strings") |
|---|
| 330 | n/a | |
|---|
| 331 | n/a | if extra is None: |
|---|
| 332 | n/a | extra = [] |
|---|
| 333 | n/a | |
|---|
| 334 | n/a | # Get the list of expected output (object) files |
|---|
| 335 | n/a | objects = self.object_filenames(sources, |
|---|
| 336 | n/a | strip_dir=False, |
|---|
| 337 | n/a | output_dir=outdir) |
|---|
| 338 | n/a | assert len(objects) == len(sources) |
|---|
| 339 | n/a | |
|---|
| 340 | n/a | pp_opts = gen_preprocess_options(macros, incdirs) |
|---|
| 341 | n/a | |
|---|
| 342 | n/a | build = {} |
|---|
| 343 | n/a | for i in range(len(sources)): |
|---|
| 344 | n/a | src = sources[i] |
|---|
| 345 | n/a | obj = objects[i] |
|---|
| 346 | n/a | ext = os.path.splitext(src)[1] |
|---|
| 347 | n/a | self.mkpath(os.path.dirname(obj)) |
|---|
| 348 | n/a | build[obj] = (src, ext) |
|---|
| 349 | n/a | |
|---|
| 350 | n/a | return macros, objects, extra, pp_opts, build |
|---|
| 351 | n/a | |
|---|
| 352 | n/a | def _get_cc_args(self, pp_opts, debug, before): |
|---|
| 353 | n/a | # works for unixccompiler and cygwinccompiler |
|---|
| 354 | n/a | cc_args = pp_opts + ['-c'] |
|---|
| 355 | n/a | if debug: |
|---|
| 356 | n/a | cc_args[:0] = ['-g'] |
|---|
| 357 | n/a | if before: |
|---|
| 358 | n/a | cc_args[:0] = before |
|---|
| 359 | n/a | return cc_args |
|---|
| 360 | n/a | |
|---|
| 361 | n/a | def _fix_compile_args(self, output_dir, macros, include_dirs): |
|---|
| 362 | n/a | """Typecheck and fix-up some of the arguments to the 'compile()' |
|---|
| 363 | n/a | method, and return fixed-up values. Specifically: if 'output_dir' |
|---|
| 364 | n/a | is None, replaces it with 'self.output_dir'; ensures that 'macros' |
|---|
| 365 | n/a | is a list, and augments it with 'self.macros'; ensures that |
|---|
| 366 | n/a | 'include_dirs' is a list, and augments it with 'self.include_dirs'. |
|---|
| 367 | n/a | Guarantees that the returned values are of the correct type, |
|---|
| 368 | n/a | i.e. for 'output_dir' either string or None, and for 'macros' and |
|---|
| 369 | n/a | 'include_dirs' either list or None. |
|---|
| 370 | n/a | """ |
|---|
| 371 | n/a | if output_dir is None: |
|---|
| 372 | n/a | output_dir = self.output_dir |
|---|
| 373 | n/a | elif not isinstance(output_dir, str): |
|---|
| 374 | n/a | raise TypeError("'output_dir' must be a string or None") |
|---|
| 375 | n/a | |
|---|
| 376 | n/a | if macros is None: |
|---|
| 377 | n/a | macros = self.macros |
|---|
| 378 | n/a | elif isinstance(macros, list): |
|---|
| 379 | n/a | macros = macros + (self.macros or []) |
|---|
| 380 | n/a | else: |
|---|
| 381 | n/a | raise TypeError("'macros' (if supplied) must be a list of tuples") |
|---|
| 382 | n/a | |
|---|
| 383 | n/a | if include_dirs is None: |
|---|
| 384 | n/a | include_dirs = self.include_dirs |
|---|
| 385 | n/a | elif isinstance(include_dirs, (list, tuple)): |
|---|
| 386 | n/a | include_dirs = list(include_dirs) + (self.include_dirs or []) |
|---|
| 387 | n/a | else: |
|---|
| 388 | n/a | raise TypeError( |
|---|
| 389 | n/a | "'include_dirs' (if supplied) must be a list of strings") |
|---|
| 390 | n/a | |
|---|
| 391 | n/a | return output_dir, macros, include_dirs |
|---|
| 392 | n/a | |
|---|
| 393 | n/a | def _fix_object_args(self, objects, output_dir): |
|---|
| 394 | n/a | """Typecheck and fix up some arguments supplied to various methods. |
|---|
| 395 | n/a | Specifically: ensure that 'objects' is a list; if output_dir is |
|---|
| 396 | n/a | None, replace with self.output_dir. Return fixed versions of |
|---|
| 397 | n/a | 'objects' and 'output_dir'. |
|---|
| 398 | n/a | """ |
|---|
| 399 | n/a | if not isinstance(objects, (list, tuple)): |
|---|
| 400 | n/a | raise TypeError("'objects' must be a list or tuple of strings") |
|---|
| 401 | n/a | objects = list(objects) |
|---|
| 402 | n/a | |
|---|
| 403 | n/a | if output_dir is None: |
|---|
| 404 | n/a | output_dir = self.output_dir |
|---|
| 405 | n/a | elif not isinstance(output_dir, str): |
|---|
| 406 | n/a | raise TypeError("'output_dir' must be a string or None") |
|---|
| 407 | n/a | |
|---|
| 408 | n/a | return objects, output_dir |
|---|
| 409 | n/a | |
|---|
| 410 | n/a | def _fix_lib_args(self, libraries, library_dirs, runtime_library_dirs): |
|---|
| 411 | n/a | """Typecheck and fix up some of the arguments supplied to the |
|---|
| 412 | n/a | 'link_*' methods. Specifically: ensure that all arguments are |
|---|
| 413 | n/a | lists, and augment them with their permanent versions |
|---|
| 414 | n/a | (eg. 'self.libraries' augments 'libraries'). Return a tuple with |
|---|
| 415 | n/a | fixed versions of all arguments. |
|---|
| 416 | n/a | """ |
|---|
| 417 | n/a | if libraries is None: |
|---|
| 418 | n/a | libraries = self.libraries |
|---|
| 419 | n/a | elif isinstance(libraries, (list, tuple)): |
|---|
| 420 | n/a | libraries = list(libraries) + (self.libraries or []) |
|---|
| 421 | n/a | else: |
|---|
| 422 | n/a | raise TypeError( |
|---|
| 423 | n/a | "'libraries' (if supplied) must be a list of strings") |
|---|
| 424 | n/a | |
|---|
| 425 | n/a | if library_dirs is None: |
|---|
| 426 | n/a | library_dirs = self.library_dirs |
|---|
| 427 | n/a | elif isinstance(library_dirs, (list, tuple)): |
|---|
| 428 | n/a | library_dirs = list(library_dirs) + (self.library_dirs or []) |
|---|
| 429 | n/a | else: |
|---|
| 430 | n/a | raise TypeError( |
|---|
| 431 | n/a | "'library_dirs' (if supplied) must be a list of strings") |
|---|
| 432 | n/a | |
|---|
| 433 | n/a | if runtime_library_dirs is None: |
|---|
| 434 | n/a | runtime_library_dirs = self.runtime_library_dirs |
|---|
| 435 | n/a | elif isinstance(runtime_library_dirs, (list, tuple)): |
|---|
| 436 | n/a | runtime_library_dirs = (list(runtime_library_dirs) + |
|---|
| 437 | n/a | (self.runtime_library_dirs or [])) |
|---|
| 438 | n/a | else: |
|---|
| 439 | n/a | raise TypeError("'runtime_library_dirs' (if supplied) " |
|---|
| 440 | n/a | "must be a list of strings") |
|---|
| 441 | n/a | |
|---|
| 442 | n/a | return libraries, library_dirs, runtime_library_dirs |
|---|
| 443 | n/a | |
|---|
| 444 | n/a | def _need_link(self, objects, output_file): |
|---|
| 445 | n/a | """Return true if we need to relink the files listed in 'objects' |
|---|
| 446 | n/a | to recreate 'output_file'. |
|---|
| 447 | n/a | """ |
|---|
| 448 | n/a | if self.force: |
|---|
| 449 | n/a | return True |
|---|
| 450 | n/a | else: |
|---|
| 451 | n/a | if self.dry_run: |
|---|
| 452 | n/a | newer = newer_group(objects, output_file, missing='newer') |
|---|
| 453 | n/a | else: |
|---|
| 454 | n/a | newer = newer_group(objects, output_file) |
|---|
| 455 | n/a | return newer |
|---|
| 456 | n/a | |
|---|
| 457 | n/a | def detect_language(self, sources): |
|---|
| 458 | n/a | """Detect the language of a given file, or list of files. Uses |
|---|
| 459 | n/a | language_map, and language_order to do the job. |
|---|
| 460 | n/a | """ |
|---|
| 461 | n/a | if not isinstance(sources, list): |
|---|
| 462 | n/a | sources = [sources] |
|---|
| 463 | n/a | lang = None |
|---|
| 464 | n/a | index = len(self.language_order) |
|---|
| 465 | n/a | for source in sources: |
|---|
| 466 | n/a | base, ext = os.path.splitext(source) |
|---|
| 467 | n/a | extlang = self.language_map.get(ext) |
|---|
| 468 | n/a | try: |
|---|
| 469 | n/a | extindex = self.language_order.index(extlang) |
|---|
| 470 | n/a | if extindex < index: |
|---|
| 471 | n/a | lang = extlang |
|---|
| 472 | n/a | index = extindex |
|---|
| 473 | n/a | except ValueError: |
|---|
| 474 | n/a | pass |
|---|
| 475 | n/a | return lang |
|---|
| 476 | n/a | |
|---|
| 477 | n/a | # -- Worker methods ------------------------------------------------ |
|---|
| 478 | n/a | # (must be implemented by subclasses) |
|---|
| 479 | n/a | |
|---|
| 480 | n/a | def preprocess(self, source, output_file=None, macros=None, |
|---|
| 481 | n/a | include_dirs=None, extra_preargs=None, extra_postargs=None): |
|---|
| 482 | n/a | """Preprocess a single C/C++ source file, named in 'source'. |
|---|
| 483 | n/a | Output will be written to file named 'output_file', or stdout if |
|---|
| 484 | n/a | 'output_file' not supplied. 'macros' is a list of macro |
|---|
| 485 | n/a | definitions as for 'compile()', which will augment the macros set |
|---|
| 486 | n/a | with 'define_macro()' and 'undefine_macro()'. 'include_dirs' is a |
|---|
| 487 | n/a | list of directory names that will be added to the default list. |
|---|
| 488 | n/a | |
|---|
| 489 | n/a | Raises PreprocessError on failure. |
|---|
| 490 | n/a | """ |
|---|
| 491 | n/a | pass |
|---|
| 492 | n/a | |
|---|
| 493 | n/a | def compile(self, sources, output_dir=None, macros=None, |
|---|
| 494 | n/a | include_dirs=None, debug=False, extra_preargs=None, |
|---|
| 495 | n/a | extra_postargs=None, depends=None): |
|---|
| 496 | n/a | """Compile one or more source files. |
|---|
| 497 | n/a | |
|---|
| 498 | n/a | 'sources' must be a list of filenames, most likely C/C++ |
|---|
| 499 | n/a | files, but in reality anything that can be handled by a |
|---|
| 500 | n/a | particular compiler and compiler class (eg. MSVCCompiler can |
|---|
| 501 | n/a | handle resource files in 'sources'). Return a list of object |
|---|
| 502 | n/a | filenames, one per source filename in 'sources'. Depending on |
|---|
| 503 | n/a | the implementation, not all source files will necessarily be |
|---|
| 504 | n/a | compiled, but all corresponding object filenames will be |
|---|
| 505 | n/a | returned. |
|---|
| 506 | n/a | |
|---|
| 507 | n/a | If 'output_dir' is given, object files will be put under it, while |
|---|
| 508 | n/a | retaining their original path component. That is, "foo/bar.c" |
|---|
| 509 | n/a | normally compiles to "foo/bar.o" (for a Unix implementation); if |
|---|
| 510 | n/a | 'output_dir' is "build", then it would compile to |
|---|
| 511 | n/a | "build/foo/bar.o". |
|---|
| 512 | n/a | |
|---|
| 513 | n/a | 'macros', if given, must be a list of macro definitions. A macro |
|---|
| 514 | n/a | definition is either a (name, value) 2-tuple or a (name,) 1-tuple. |
|---|
| 515 | n/a | The former defines a macro; if the value is None, the macro is |
|---|
| 516 | n/a | defined without an explicit value. The 1-tuple case undefines a |
|---|
| 517 | n/a | macro. Later definitions/redefinitions/ undefinitions take |
|---|
| 518 | n/a | precedence. |
|---|
| 519 | n/a | |
|---|
| 520 | n/a | 'include_dirs', if given, must be a list of strings, the |
|---|
| 521 | n/a | directories to add to the default include file search path for this |
|---|
| 522 | n/a | compilation only. |
|---|
| 523 | n/a | |
|---|
| 524 | n/a | 'debug' is a boolean; if true, the compiler will be instructed to |
|---|
| 525 | n/a | output debug symbols in (or alongside) the object file(s). |
|---|
| 526 | n/a | |
|---|
| 527 | n/a | 'extra_preargs' and 'extra_postargs' are implementation- dependent. |
|---|
| 528 | n/a | On platforms that have the notion of a command line (e.g. Unix, |
|---|
| 529 | n/a | DOS/Windows), they are most likely lists of strings: extra |
|---|
| 530 | n/a | command-line arguments to prepand/append to the compiler command |
|---|
| 531 | n/a | line. On other platforms, consult the implementation class |
|---|
| 532 | n/a | documentation. In any event, they are intended as an escape hatch |
|---|
| 533 | n/a | for those occasions when the abstract compiler framework doesn't |
|---|
| 534 | n/a | cut the mustard. |
|---|
| 535 | n/a | |
|---|
| 536 | n/a | 'depends', if given, is a list of filenames that all targets |
|---|
| 537 | n/a | depend on. If a source file is older than any file in |
|---|
| 538 | n/a | depends, then the source file will be recompiled. This |
|---|
| 539 | n/a | supports dependency tracking, but only at a coarse |
|---|
| 540 | n/a | granularity. |
|---|
| 541 | n/a | |
|---|
| 542 | n/a | Raises CompileError on failure. |
|---|
| 543 | n/a | """ |
|---|
| 544 | n/a | # A concrete compiler class can either override this method |
|---|
| 545 | n/a | # entirely or implement _compile(). |
|---|
| 546 | n/a | |
|---|
| 547 | n/a | macros, objects, extra_postargs, pp_opts, build = \ |
|---|
| 548 | n/a | self._setup_compile(output_dir, macros, include_dirs, sources, |
|---|
| 549 | n/a | depends, extra_postargs) |
|---|
| 550 | n/a | cc_args = self._get_cc_args(pp_opts, debug, extra_preargs) |
|---|
| 551 | n/a | |
|---|
| 552 | n/a | for obj in objects: |
|---|
| 553 | n/a | try: |
|---|
| 554 | n/a | src, ext = build[obj] |
|---|
| 555 | n/a | except KeyError: |
|---|
| 556 | n/a | continue |
|---|
| 557 | n/a | self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts) |
|---|
| 558 | n/a | |
|---|
| 559 | n/a | # Return *all* object filenames, not just the ones we just built. |
|---|
| 560 | n/a | return objects |
|---|
| 561 | n/a | |
|---|
| 562 | n/a | def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts): |
|---|
| 563 | n/a | """Compile 'src' to product 'obj'.""" |
|---|
| 564 | n/a | |
|---|
| 565 | n/a | # A concrete compiler class that does not override compile() |
|---|
| 566 | n/a | # should implement _compile(). |
|---|
| 567 | n/a | pass |
|---|
| 568 | n/a | |
|---|
| 569 | n/a | def create_static_lib(self, objects, output_libname, output_dir=None, |
|---|
| 570 | n/a | debug=False, target_lang=None): |
|---|
| 571 | n/a | """Link a bunch of stuff together to create a static library file. |
|---|
| 572 | n/a | The "bunch of stuff" consists of the list of object files supplied |
|---|
| 573 | n/a | as 'objects', the extra object files supplied to |
|---|
| 574 | n/a | 'add_link_object()' and/or 'set_link_objects()', the libraries |
|---|
| 575 | n/a | supplied to 'add_library()' and/or 'set_libraries()', and the |
|---|
| 576 | n/a | libraries supplied as 'libraries' (if any). |
|---|
| 577 | n/a | |
|---|
| 578 | n/a | 'output_libname' should be a library name, not a filename; the |
|---|
| 579 | n/a | filename will be inferred from the library name. 'output_dir' is |
|---|
| 580 | n/a | the directory where the library file will be put. |
|---|
| 581 | n/a | |
|---|
| 582 | n/a | 'debug' is a boolean; if true, debugging information will be |
|---|
| 583 | n/a | included in the library (note that on most platforms, it is the |
|---|
| 584 | n/a | compile step where this matters: the 'debug' flag is included here |
|---|
| 585 | n/a | just for consistency). |
|---|
| 586 | n/a | |
|---|
| 587 | n/a | 'target_lang' is the target language for which the given objects |
|---|
| 588 | n/a | are being compiled. This allows specific linkage time treatment of |
|---|
| 589 | n/a | certain languages. |
|---|
| 590 | n/a | |
|---|
| 591 | n/a | Raises LibError on failure. |
|---|
| 592 | n/a | """ |
|---|
| 593 | n/a | pass |
|---|
| 594 | n/a | |
|---|
| 595 | n/a | # values for target_desc parameter in link() |
|---|
| 596 | n/a | SHARED_OBJECT = "shared_object" |
|---|
| 597 | n/a | SHARED_LIBRARY = "shared_library" |
|---|
| 598 | n/a | EXECUTABLE = "executable" |
|---|
| 599 | n/a | |
|---|
| 600 | n/a | def link(self, target_desc, objects, output_filename, output_dir=None, |
|---|
| 601 | n/a | libraries=None, library_dirs=None, runtime_library_dirs=None, |
|---|
| 602 | n/a | export_symbols=None, debug=False, extra_preargs=None, |
|---|
| 603 | n/a | extra_postargs=None, build_temp=None, target_lang=None): |
|---|
| 604 | n/a | """Link a bunch of stuff together to create an executable or |
|---|
| 605 | n/a | shared library file. |
|---|
| 606 | n/a | |
|---|
| 607 | n/a | The "bunch of stuff" consists of the list of object files supplied |
|---|
| 608 | n/a | as 'objects'. 'output_filename' should be a filename. If |
|---|
| 609 | n/a | 'output_dir' is supplied, 'output_filename' is relative to it |
|---|
| 610 | n/a | (i.e. 'output_filename' can provide directory components if |
|---|
| 611 | n/a | needed). |
|---|
| 612 | n/a | |
|---|
| 613 | n/a | 'libraries' is a list of libraries to link against. These are |
|---|
| 614 | n/a | library names, not filenames, since they're translated into |
|---|
| 615 | n/a | filenames in a platform-specific way (eg. "foo" becomes "libfoo.a" |
|---|
| 616 | n/a | on Unix and "foo.lib" on DOS/Windows). However, they can include a |
|---|
| 617 | n/a | directory component, which means the linker will look in that |
|---|
| 618 | n/a | specific directory rather than searching all the normal locations. |
|---|
| 619 | n/a | |
|---|
| 620 | n/a | 'library_dirs', if supplied, should be a list of directories to |
|---|
| 621 | n/a | search for libraries that were specified as bare library names |
|---|
| 622 | n/a | (ie. no directory component). These are on top of the system |
|---|
| 623 | n/a | default and those supplied to 'add_library_dir()' and/or |
|---|
| 624 | n/a | 'set_library_dirs()'. 'runtime_library_dirs' is a list of |
|---|
| 625 | n/a | directories that will be embedded into the shared library and used |
|---|
| 626 | n/a | to search for other shared libraries that *it* depends on at |
|---|
| 627 | n/a | run-time. (This may only be relevant on Unix.) |
|---|
| 628 | n/a | |
|---|
| 629 | n/a | 'export_symbols' is a list of symbols that the shared library will |
|---|
| 630 | n/a | export. (This appears to be relevant only on Windows.) |
|---|
| 631 | n/a | |
|---|
| 632 | n/a | 'debug' is as for 'compile()' and 'create_static_lib()', with the |
|---|
| 633 | n/a | slight distinction that it actually matters on most platforms (as |
|---|
| 634 | n/a | opposed to 'create_static_lib()', which includes a 'debug' flag |
|---|
| 635 | n/a | mostly for form's sake). |
|---|
| 636 | n/a | |
|---|
| 637 | n/a | 'extra_preargs' and 'extra_postargs' are as for 'compile()' (except |
|---|
| 638 | n/a | of course that they supply command-line arguments for the |
|---|
| 639 | n/a | particular linker being used). |
|---|
| 640 | n/a | |
|---|
| 641 | n/a | 'target_lang' is the target language for which the given objects |
|---|
| 642 | n/a | are being compiled. This allows specific linkage time treatment of |
|---|
| 643 | n/a | certain languages. |
|---|
| 644 | n/a | |
|---|
| 645 | n/a | Raises LinkError on failure. |
|---|
| 646 | n/a | """ |
|---|
| 647 | n/a | raise NotImplementedError |
|---|
| 648 | n/a | |
|---|
| 649 | n/a | |
|---|
| 650 | n/a | # Old 'link_*()' methods, rewritten to use the new 'link()' method. |
|---|
| 651 | n/a | |
|---|
| 652 | n/a | def link_shared_lib(self, objects, output_libname, output_dir=None, |
|---|
| 653 | n/a | libraries=None, library_dirs=None, |
|---|
| 654 | n/a | runtime_library_dirs=None, export_symbols=None, |
|---|
| 655 | n/a | debug=False, extra_preargs=None, extra_postargs=None, |
|---|
| 656 | n/a | build_temp=None, target_lang=None): |
|---|
| 657 | n/a | self.link(CCompiler.SHARED_LIBRARY, objects, |
|---|
| 658 | n/a | self.library_filename(output_libname, lib_type='shared'), |
|---|
| 659 | n/a | output_dir, |
|---|
| 660 | n/a | libraries, library_dirs, runtime_library_dirs, |
|---|
| 661 | n/a | export_symbols, debug, |
|---|
| 662 | n/a | extra_preargs, extra_postargs, build_temp, target_lang) |
|---|
| 663 | n/a | |
|---|
| 664 | n/a | def link_shared_object(self, objects, output_filename, output_dir=None, |
|---|
| 665 | n/a | libraries=None, library_dirs=None, |
|---|
| 666 | n/a | runtime_library_dirs=None, export_symbols=None, |
|---|
| 667 | n/a | debug=False, extra_preargs=None, extra_postargs=None, |
|---|
| 668 | n/a | build_temp=None, target_lang=None): |
|---|
| 669 | n/a | self.link(CCompiler.SHARED_OBJECT, objects, |
|---|
| 670 | n/a | output_filename, output_dir, |
|---|
| 671 | n/a | libraries, library_dirs, runtime_library_dirs, |
|---|
| 672 | n/a | export_symbols, debug, |
|---|
| 673 | n/a | extra_preargs, extra_postargs, build_temp, target_lang) |
|---|
| 674 | n/a | |
|---|
| 675 | n/a | def link_executable(self, objects, output_progname, output_dir=None, |
|---|
| 676 | n/a | libraries=None, library_dirs=None, |
|---|
| 677 | n/a | runtime_library_dirs=None, debug=False, |
|---|
| 678 | n/a | extra_preargs=None, extra_postargs=None, |
|---|
| 679 | n/a | target_lang=None): |
|---|
| 680 | n/a | self.link(CCompiler.EXECUTABLE, objects, |
|---|
| 681 | n/a | self.executable_filename(output_progname), output_dir, |
|---|
| 682 | n/a | libraries, library_dirs, runtime_library_dirs, None, |
|---|
| 683 | n/a | debug, extra_preargs, extra_postargs, None, target_lang) |
|---|
| 684 | n/a | |
|---|
| 685 | n/a | |
|---|
| 686 | n/a | # -- Miscellaneous methods ----------------------------------------- |
|---|
| 687 | n/a | # These are all used by the 'gen_lib_options() function; there is |
|---|
| 688 | n/a | # no appropriate default implementation so subclasses should |
|---|
| 689 | n/a | # implement all of these. |
|---|
| 690 | n/a | |
|---|
| 691 | n/a | def library_dir_option(self, dir): |
|---|
| 692 | n/a | """Return the compiler option to add 'dir' to the list of |
|---|
| 693 | n/a | directories searched for libraries. |
|---|
| 694 | n/a | """ |
|---|
| 695 | n/a | raise NotImplementedError |
|---|
| 696 | n/a | |
|---|
| 697 | n/a | def runtime_library_dir_option(self, dir): |
|---|
| 698 | n/a | """Return the compiler option to add 'dir' to the list of |
|---|
| 699 | n/a | directories searched for runtime libraries. |
|---|
| 700 | n/a | """ |
|---|
| 701 | n/a | raise NotImplementedError |
|---|
| 702 | n/a | |
|---|
| 703 | n/a | def library_option(self, lib): |
|---|
| 704 | n/a | """Return the compiler option to add 'dir' to the list of libraries |
|---|
| 705 | n/a | linked into the shared library or executable. |
|---|
| 706 | n/a | """ |
|---|
| 707 | n/a | raise NotImplementedError |
|---|
| 708 | n/a | |
|---|
| 709 | n/a | def has_function(self, funcname, includes=None, include_dirs=None, |
|---|
| 710 | n/a | libraries=None, library_dirs=None): |
|---|
| 711 | n/a | """Return a boolean indicating whether funcname is supported on |
|---|
| 712 | n/a | the current platform. The optional arguments can be used to |
|---|
| 713 | n/a | augment the compilation environment. |
|---|
| 714 | n/a | """ |
|---|
| 715 | n/a | |
|---|
| 716 | n/a | # this can't be included at module scope because it tries to |
|---|
| 717 | n/a | # import math which might not be available at that point - maybe |
|---|
| 718 | n/a | # the necessary logic should just be inlined? |
|---|
| 719 | n/a | import tempfile |
|---|
| 720 | n/a | if includes is None: |
|---|
| 721 | n/a | includes = [] |
|---|
| 722 | n/a | if include_dirs is None: |
|---|
| 723 | n/a | include_dirs = [] |
|---|
| 724 | n/a | if libraries is None: |
|---|
| 725 | n/a | libraries = [] |
|---|
| 726 | n/a | if library_dirs is None: |
|---|
| 727 | n/a | library_dirs = [] |
|---|
| 728 | n/a | fd, fname = tempfile.mkstemp(".c", funcname, text=True) |
|---|
| 729 | n/a | with os.fdopen(fd, "w") as f: |
|---|
| 730 | n/a | for incl in includes: |
|---|
| 731 | n/a | f.write("""#include "%s"\n""" % incl) |
|---|
| 732 | n/a | f.write("""\ |
|---|
| 733 | n/a | main (int argc, char **argv) { |
|---|
| 734 | n/a | %s(); |
|---|
| 735 | n/a | } |
|---|
| 736 | n/a | """ % funcname) |
|---|
| 737 | n/a | try: |
|---|
| 738 | n/a | objects = self.compile([fname], include_dirs=include_dirs) |
|---|
| 739 | n/a | except CompileError: |
|---|
| 740 | n/a | return False |
|---|
| 741 | n/a | |
|---|
| 742 | n/a | try: |
|---|
| 743 | n/a | self.link_executable(objects, "a.out", |
|---|
| 744 | n/a | libraries=libraries, |
|---|
| 745 | n/a | library_dirs=library_dirs) |
|---|
| 746 | n/a | except (LinkError, TypeError): |
|---|
| 747 | n/a | return False |
|---|
| 748 | n/a | return True |
|---|
| 749 | n/a | |
|---|
| 750 | n/a | def find_library_file(self, dirs, lib, debug=False): |
|---|
| 751 | n/a | """Search the specified list of directories for a static or shared |
|---|
| 752 | n/a | library file 'lib' and return the full path to that file. If |
|---|
| 753 | n/a | 'debug' is true, look for a debugging version (if that makes sense on |
|---|
| 754 | n/a | the current platform). Return None if 'lib' wasn't found in any of |
|---|
| 755 | n/a | the specified directories. |
|---|
| 756 | n/a | """ |
|---|
| 757 | n/a | raise NotImplementedError |
|---|
| 758 | n/a | |
|---|
| 759 | n/a | # -- Filename generation methods ----------------------------------- |
|---|
| 760 | n/a | |
|---|
| 761 | n/a | # The default implementation of the filename generating methods are |
|---|
| 762 | n/a | # prejudiced towards the Unix/DOS/Windows view of the world: |
|---|
| 763 | n/a | # * object files are named by replacing the source file extension |
|---|
| 764 | n/a | # (eg. .c/.cpp -> .o/.obj) |
|---|
| 765 | n/a | # * library files (shared or static) are named by plugging the |
|---|
| 766 | n/a | # library name and extension into a format string, eg. |
|---|
| 767 | n/a | # "lib%s.%s" % (lib_name, ".a") for Unix static libraries |
|---|
| 768 | n/a | # * executables are named by appending an extension (possibly |
|---|
| 769 | n/a | # empty) to the program name: eg. progname + ".exe" for |
|---|
| 770 | n/a | # Windows |
|---|
| 771 | n/a | # |
|---|
| 772 | n/a | # To reduce redundant code, these methods expect to find |
|---|
| 773 | n/a | # several attributes in the current object (presumably defined |
|---|
| 774 | n/a | # as class attributes): |
|---|
| 775 | n/a | # * src_extensions - |
|---|
| 776 | n/a | # list of C/C++ source file extensions, eg. ['.c', '.cpp'] |
|---|
| 777 | n/a | # * obj_extension - |
|---|
| 778 | n/a | # object file extension, eg. '.o' or '.obj' |
|---|
| 779 | n/a | # * static_lib_extension - |
|---|
| 780 | n/a | # extension for static library files, eg. '.a' or '.lib' |
|---|
| 781 | n/a | # * shared_lib_extension - |
|---|
| 782 | n/a | # extension for shared library/object files, eg. '.so', '.dll' |
|---|
| 783 | n/a | # * static_lib_format - |
|---|
| 784 | n/a | # format string for generating static library filenames, |
|---|
| 785 | n/a | # eg. 'lib%s.%s' or '%s.%s' |
|---|
| 786 | n/a | # * shared_lib_format |
|---|
| 787 | n/a | # format string for generating shared library filenames |
|---|
| 788 | n/a | # (probably same as static_lib_format, since the extension |
|---|
| 789 | n/a | # is one of the intended parameters to the format string) |
|---|
| 790 | n/a | # * exe_extension - |
|---|
| 791 | n/a | # extension for executable files, eg. '' or '.exe' |
|---|
| 792 | n/a | |
|---|
| 793 | n/a | def object_filenames(self, source_filenames, strip_dir=False, output_dir=''): |
|---|
| 794 | n/a | if output_dir is None: |
|---|
| 795 | n/a | output_dir = '' |
|---|
| 796 | n/a | obj_names = [] |
|---|
| 797 | n/a | for src_name in source_filenames: |
|---|
| 798 | n/a | base, ext = os.path.splitext(src_name) |
|---|
| 799 | n/a | base = os.path.splitdrive(base)[1] # Chop off the drive |
|---|
| 800 | n/a | base = base[os.path.isabs(base):] # If abs, chop off leading / |
|---|
| 801 | n/a | if ext not in self.src_extensions: |
|---|
| 802 | n/a | raise UnknownFileError("unknown file type '%s' (from '%s')" % |
|---|
| 803 | n/a | (ext, src_name)) |
|---|
| 804 | n/a | if strip_dir: |
|---|
| 805 | n/a | base = os.path.basename(base) |
|---|
| 806 | n/a | obj_names.append(os.path.join(output_dir, |
|---|
| 807 | n/a | base + self.obj_extension)) |
|---|
| 808 | n/a | return obj_names |
|---|
| 809 | n/a | |
|---|
| 810 | n/a | def shared_object_filename(self, basename, strip_dir=False, output_dir=''): |
|---|
| 811 | n/a | assert output_dir is not None |
|---|
| 812 | n/a | if strip_dir: |
|---|
| 813 | n/a | basename = os.path.basename(basename) |
|---|
| 814 | n/a | return os.path.join(output_dir, basename + self.shared_lib_extension) |
|---|
| 815 | n/a | |
|---|
| 816 | n/a | def executable_filename(self, basename, strip_dir=False, output_dir=''): |
|---|
| 817 | n/a | assert output_dir is not None |
|---|
| 818 | n/a | if strip_dir: |
|---|
| 819 | n/a | basename = os.path.basename(basename) |
|---|
| 820 | n/a | return os.path.join(output_dir, basename + (self.exe_extension or '')) |
|---|
| 821 | n/a | |
|---|
| 822 | n/a | def library_filename(self, libname, lib_type='static', # or 'shared' |
|---|
| 823 | n/a | strip_dir=False, output_dir=''): |
|---|
| 824 | n/a | assert output_dir is not None |
|---|
| 825 | n/a | if lib_type not in ("static", "shared", "dylib"): |
|---|
| 826 | n/a | raise ValueError( |
|---|
| 827 | n/a | "'lib_type' must be 'static', 'shared' or 'dylib'") |
|---|
| 828 | n/a | fmt = getattr(self, lib_type + "_lib_format") |
|---|
| 829 | n/a | ext = getattr(self, lib_type + "_lib_extension") |
|---|
| 830 | n/a | |
|---|
| 831 | n/a | dir, base = os.path.split(libname) |
|---|
| 832 | n/a | filename = fmt % (base, ext) |
|---|
| 833 | n/a | if strip_dir: |
|---|
| 834 | n/a | dir = '' |
|---|
| 835 | n/a | |
|---|
| 836 | n/a | return os.path.join(output_dir, dir, filename) |
|---|
| 837 | n/a | |
|---|
| 838 | n/a | |
|---|
| 839 | n/a | # -- Utility methods ----------------------------------------------- |
|---|
| 840 | n/a | |
|---|
| 841 | n/a | def execute(self, func, args, msg=None, level=1): |
|---|
| 842 | n/a | execute(func, args, msg, self.dry_run) |
|---|
| 843 | n/a | |
|---|
| 844 | n/a | def spawn(self, cmd): |
|---|
| 845 | n/a | spawn(cmd, dry_run=self.dry_run) |
|---|
| 846 | n/a | |
|---|
| 847 | n/a | def move_file(self, src, dst): |
|---|
| 848 | n/a | logger.info("moving %r to %r", src, dst) |
|---|
| 849 | n/a | if self.dry_run: |
|---|
| 850 | n/a | return |
|---|
| 851 | n/a | return move(src, dst) |
|---|
| 852 | n/a | |
|---|
| 853 | n/a | def mkpath(self, name, mode=0o777): |
|---|
| 854 | n/a | name = os.path.normpath(name) |
|---|
| 855 | n/a | if os.path.isdir(name) or name == '': |
|---|
| 856 | n/a | return |
|---|
| 857 | n/a | if self.dry_run: |
|---|
| 858 | n/a | head = '' |
|---|
| 859 | n/a | for part in name.split(os.sep): |
|---|
| 860 | n/a | logger.info("created directory %s%s", head, part) |
|---|
| 861 | n/a | head += part + os.sep |
|---|
| 862 | n/a | return |
|---|
| 863 | n/a | os.makedirs(name, mode) |
|---|