omdev 0.0.0.dev7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. omdev/__about__.py +35 -0
  2. omdev/__init__.py +0 -0
  3. omdev/amalg/__init__.py +0 -0
  4. omdev/amalg/__main__.py +4 -0
  5. omdev/amalg/amalg.py +513 -0
  6. omdev/classdot.py +61 -0
  7. omdev/cmake.py +164 -0
  8. omdev/exts/__init__.py +0 -0
  9. omdev/exts/_distutils/__init__.py +10 -0
  10. omdev/exts/_distutils/build_ext.py +367 -0
  11. omdev/exts/_distutils/compilers/__init__.py +3 -0
  12. omdev/exts/_distutils/compilers/ccompiler.py +1032 -0
  13. omdev/exts/_distutils/compilers/options.py +80 -0
  14. omdev/exts/_distutils/compilers/unixccompiler.py +385 -0
  15. omdev/exts/_distutils/dir_util.py +76 -0
  16. omdev/exts/_distutils/errors.py +62 -0
  17. omdev/exts/_distutils/extension.py +107 -0
  18. omdev/exts/_distutils/file_util.py +216 -0
  19. omdev/exts/_distutils/modified.py +47 -0
  20. omdev/exts/_distutils/spawn.py +103 -0
  21. omdev/exts/_distutils/sysconfig.py +349 -0
  22. omdev/exts/_distutils/util.py +201 -0
  23. omdev/exts/_distutils/version.py +308 -0
  24. omdev/exts/build.py +43 -0
  25. omdev/exts/cmake.py +195 -0
  26. omdev/exts/importhook.py +88 -0
  27. omdev/exts/scan.py +74 -0
  28. omdev/interp/__init__.py +1 -0
  29. omdev/interp/__main__.py +4 -0
  30. omdev/interp/cli.py +63 -0
  31. omdev/interp/inspect.py +105 -0
  32. omdev/interp/providers.py +67 -0
  33. omdev/interp/pyenv.py +353 -0
  34. omdev/interp/resolvers.py +76 -0
  35. omdev/interp/standalone.py +187 -0
  36. omdev/interp/system.py +125 -0
  37. omdev/interp/types.py +92 -0
  38. omdev/mypy/__init__.py +0 -0
  39. omdev/mypy/debug.py +86 -0
  40. omdev/pyproject/__init__.py +1 -0
  41. omdev/pyproject/__main__.py +4 -0
  42. omdev/pyproject/cli.py +319 -0
  43. omdev/pyproject/configs.py +97 -0
  44. omdev/pyproject/ext.py +107 -0
  45. omdev/pyproject/pkg.py +196 -0
  46. omdev/scripts/__init__.py +0 -0
  47. omdev/scripts/execrss.py +19 -0
  48. omdev/scripts/findimports.py +62 -0
  49. omdev/scripts/findmagic.py +70 -0
  50. omdev/scripts/interp.py +2118 -0
  51. omdev/scripts/pyproject.py +3584 -0
  52. omdev/scripts/traceimport.py +502 -0
  53. omdev/tokens.py +42 -0
  54. omdev/toml/__init__.py +1 -0
  55. omdev/toml/parser.py +823 -0
  56. omdev/toml/writer.py +104 -0
  57. omdev/tools/__init__.py +0 -0
  58. omdev/tools/dockertools.py +81 -0
  59. omdev/tools/sqlrepl.py +193 -0
  60. omdev/versioning/__init__.py +1 -0
  61. omdev/versioning/specifiers.py +531 -0
  62. omdev/versioning/versions.py +416 -0
  63. omdev-0.0.0.dev7.dist-info/LICENSE +21 -0
  64. omdev-0.0.0.dev7.dist-info/METADATA +24 -0
  65. omdev-0.0.0.dev7.dist-info/RECORD +67 -0
  66. omdev-0.0.0.dev7.dist-info/WHEEL +5 -0
  67. omdev-0.0.0.dev7.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1032 @@
1
+ """
2
+ Contains CCompiler, an abstract base class that defines the interface for the Distutils compiler abstraction model.
3
+ """
4
+ import logging
5
+ import os
6
+ import re
7
+ import sys
8
+ import typing as ta
9
+ import warnings
10
+
11
+ from omlish import check
12
+
13
+ from ..dir_util import mkpath
14
+ from ..errors import CompileError
15
+ from ..errors import DistutilsModuleError
16
+ from ..errors import DistutilsPlatformError
17
+ from ..errors import LinkError
18
+ from ..errors import UnknownFileError
19
+ from ..file_util import move_file
20
+ from ..modified import newer_group
21
+ from ..spawn import spawn
22
+ from ..util import execute
23
+ from ..util import split_quoted
24
+ from .options import Macro
25
+ from .options import gen_preprocess_options
26
+
27
+
28
+ log = logging.getLogger(__name__)
29
+
30
+
31
+ class CCompiler:
32
+ """
33
+ Abstract base class to define the interface that must be implemented by real compiler classes. Also has some
34
+ utility methods used by several compiler classes.
35
+
36
+ The basic idea behind a compiler abstraction class is that each instance can be used for all the compile/link steps
37
+ in building a single project. Thus, attributes common to all of those compile and link steps -- include
38
+ directories, macros to define, libraries to link against, etc. -- are attributes of the compiler instance. To allow
39
+ for variability in how individual files are treated, most of those attributes may be varied on a per-compilation or
40
+ per-link basis.
41
+ """
42
+
43
+ # 'compiler_type' is a class attribute that identifies this class. It keeps code that wants to know what kind of
44
+ # compiler it's dealing with from having to import all possible compiler classes just to do an 'isinstance'. In
45
+ # concrete CCompiler subclasses, 'compiler_type' should really, really be one of the keys of the 'compiler_class'
46
+ # dictionary (see below -- used by the 'new_compiler()' factory function) -- authors of new compiler interface
47
+ # classes are responsible for updating 'compiler_class'!
48
+ compiler_type: str
49
+
50
+ # things not handled by this compiler abstraction model:
51
+ # * client can't provide additional options for a compiler, e.g. warning, optimization, debugging flags. Perhaps
52
+ # this should be the domain of concrete compiler abstraction classes (UnixCCompiler, MSVCCompiler, etc.) -- or
53
+ # perhaps the base class should have methods for the common ones.
54
+ # * can't completely override the include or library searchg path, ie. no "cc -I -Idir1 -Idir2" or "cc -L -Ldir1
55
+ # -Ldir2". I'm not sure how widely supported this is even by Unix compilers, much less on other platforms. And
56
+ # I'm even less sure how useful it is; maybe for cross-compiling, but support for that is a ways off. (And
57
+ # anyways, cross compilers probably have a dedicated binary with the right paths compiled in. I hope.)
58
+ # * can't do really freaky things with the library list/library dirs, e.g. "-Ldir1 -lfoo -Ldir2 -lfoo" to link
59
+ # against different versions of libfoo.a in different locations. I think this is useless without the ability to
60
+ # null out the library search path anyways.
61
+
62
+ # Subclasses that rely on the standard filename generation methods implemented below should override these; see the
63
+ # comment near those methods ('object_filenames()' et. al.) for details:
64
+ src_extensions: ta.ClassVar[list[str]]
65
+ obj_extension: str
66
+ static_lib_extension: str
67
+ shared_lib_extension: str
68
+ static_lib_format: str
69
+ shared_lib_format: str # prob. same as static_lib_format
70
+ exe_extension: str
71
+
72
+ # Default language settings. language_map is used to detect a source file or Extension target language, checking
73
+ # source filenames. language_order is used to detect the language precedence, when deciding what language to use
74
+ # when mixing source types. For example, if some extension has two files with ".c" extension, and one with ".cpp",
75
+ # it is still linked as c++.
76
+ language_map: ta.ClassVar[dict[str, str]] = {
77
+ '.c': 'c',
78
+ '.cc': 'c++',
79
+ '.cpp': 'c++',
80
+ '.cxx': 'c++',
81
+ '.m': 'objc',
82
+ }
83
+ language_order: ta.ClassVar[list[str]] = ['c++', 'objc', 'c']
84
+
85
+ default_include_dirs: ta.ClassVar[list[str]] = []
86
+ default_library_dirs: ta.ClassVar[list[str]] = []
87
+
88
+ executables: ta.ClassVar[dict[str, list[str] | None]]
89
+
90
+ def __init__(
91
+ self,
92
+ verbose: int = 0,
93
+ dry_run: bool = False,
94
+ force: bool = False,
95
+ ) -> None:
96
+ super().__init__()
97
+
98
+ self.dry_run = dry_run
99
+ self.force = force
100
+ self.verbose = verbose
101
+
102
+ # 'output_dir': a common output directory for object, library, shared object, and shared library files
103
+ self.output_dir: str | None = None
104
+
105
+ # 'macros': a list of macro definitions (or undefinitions). A macro definition is a 2-tuple (name, value),
106
+ # where the value is either a string or None (no explicit value). A macro undefinition is a 1-tuple (name,).
107
+ self.macros: list[Macro] = []
108
+
109
+ # 'include_dirs': a list of directories to search for include files
110
+ self.include_dirs: list[str] = []
111
+
112
+ # 'libraries': a list of libraries to include in any link (library names, not filenames: eg. "foo" not
113
+ # "libfoo.a")
114
+ self.libraries: list[str] = []
115
+
116
+ # 'library_dirs': a list of directories to search for libraries
117
+ self.library_dirs: list[str] = []
118
+
119
+ # 'runtime_library_dirs': a list of directories to search for shared libraries/objects at runtime
120
+ self.runtime_library_dirs: list[str] = []
121
+
122
+ # 'objects': a list of object files (or similar, such as explicitly named library files) to include on any link
123
+ self.objects: list[str] = []
124
+
125
+ for key in self.executables:
126
+ self.set_executable(key, self.executables[key])
127
+
128
+ def set_executables(self, **kwargs):
129
+ """
130
+ Define the executables (and options for them) that will be run to perform the various stages of compilation.
131
+ The exact set of executables that may be specified here depends on the compiler class (via the 'executables'
132
+ class attribute), but most will have:
133
+ compiler the C/C++ compiler
134
+ linker_so linker used to create shared objects and libraries
135
+ linker_exe linker used to create binary executables
136
+ archiver static library creator
137
+
138
+ On platforms with a command-line (Unix, DOS/Windows), each of these is a string that will be split into
139
+ executable name and (optional) list of arguments. (Splitting the string is done similarly to how Unix shells
140
+ operate: words are delimited by spaces, but quotes and backslashes can override this. See
141
+ 'distutils.util.split_quoted()'.)
142
+ """
143
+
144
+ # Note that some CCompiler implementation classes will define class attributes 'cpp', 'cc', etc. with hard-coded
145
+ # executable names; this is appropriate when a compiler class is for exactly one compiler/OS combination (eg.
146
+ # MSVCCompiler). Other compiler classes (UnixCCompiler, in particular) are driven by information discovered at
147
+ # run-time, since there are many different ways to do basically the same things with Unix C compilers.
148
+
149
+ for key in kwargs:
150
+ if key not in self.executables:
151
+ raise ValueError(f"unknown executable '{key}' for class {self.__class__.__name__}")
152
+ self.set_executable(key, kwargs[key])
153
+
154
+ preprocessor: list[str]
155
+ compiler: list[str]
156
+ compiler_so: list[str]
157
+ compiler_cxx: list[str]
158
+ linker_so: list[str]
159
+ linker_exe: list[str]
160
+ archiver: list[str]
161
+ ranlib: list[str]
162
+
163
+ def set_executable(self, key, value):
164
+ if isinstance(value, str):
165
+ setattr(self, key, split_quoted(value))
166
+ else:
167
+ setattr(self, key, value)
168
+
169
+ def _find_macro(self, name):
170
+ for i, defn in enumerate(self.macros):
171
+ if defn[0] == name:
172
+ return i
173
+ return None
174
+
175
+ def _check_macro_definitions(self, definitions: list[Macro]) -> None:
176
+ """
177
+ Ensures that every element of 'definitions' is a valid macro definition, ie. either (name,value) 2-tuple or a
178
+ (name,) tuple. Do nothing if all definitions are OK, raise TypeError otherwise.
179
+ """
180
+ for defn in definitions:
181
+ if not (
182
+ isinstance(defn, tuple)
183
+ and (len(defn) == 1 or (len(defn) == 2 and (isinstance(defn[1], str) or defn[1] is None)))
184
+ and isinstance(defn[0], str)
185
+ ):
186
+ raise TypeError(
187
+ f"invalid macro definition '{defn}': "
188
+ 'must be tuple (string,), (string, string), or '
189
+ '(string, None)',
190
+ )
191
+
192
+ # -- Bookkeeping methods -------------------------------------------
193
+
194
+ def define_macro(self, name, value=None):
195
+ """
196
+ Define a preprocessor macro for all compilations driven by this compiler object. The optional parameter 'value'
197
+ should be a string; if it is not supplied, then the macro will be defined without an explicit value and the
198
+ exact outcome depends on the compiler used (XXX true? does ANSI say anything about this?)
199
+ """
200
+ # Delete from the list of macro definitions/undefinitions if already there (so that this one will take
201
+ # precedence).
202
+ i = self._find_macro(name)
203
+ if i is not None:
204
+ del self.macros[i]
205
+
206
+ self.macros.append((name, value))
207
+
208
+ def undefine_macro(self, name):
209
+ """
210
+ Undefine a preprocessor macro for all compilations driven by this compiler object. If the same macro is defined
211
+ by 'define_macro()' and undefined by 'undefine_macro()' the last call takes precedence (including multiple
212
+ redefinitions or undefinitions). If the macro is redefined/undefined on a per-compilation basis (ie. in the
213
+ call to 'compile()'), then that takes precedence.
214
+ """
215
+ # Delete from the list of macro definitions/undefinitions if already there (so that this one will take
216
+ # precedence).
217
+ i = self._find_macro(name)
218
+ if i is not None:
219
+ del self.macros[i]
220
+
221
+ undefn = (name,)
222
+ self.macros.append(undefn)
223
+
224
+ def add_include_dir(self, dir): # noqa
225
+ """
226
+ Add 'dir' to the list of directories that will be searched for header files. The compiler is instructed to
227
+ search directories in the order in which they are supplied by successive calls to 'add_include_dir()'.
228
+ """
229
+ self.include_dirs.append(dir)
230
+
231
+ def set_include_dirs(self, dirs):
232
+ """
233
+ Set the list of directories that will be searched to 'dirs' (a list of strings). Overrides any preceding calls
234
+ to 'add_include_dir()'; subsequence calls to 'add_include_dir()' add to the list passed to 'set_include_dirs()'.
235
+ This does not affect any list of standard include directories that the compiler may search by default.
236
+ """
237
+ self.include_dirs = dirs[:]
238
+
239
+ def add_library(self, libname):
240
+ """
241
+ Add 'libname' to the list of libraries that will be included in all links driven by this compiler object. Note
242
+ that 'libname' should *not* be the name of a file containing a library, but the name of the library itself: the
243
+ actual filename will be inferred by the linker, the compiler, or the compiler class (depending on the platform).
244
+
245
+ The linker will be instructed to link against libraries in the order they were supplied to 'add_library()'
246
+ and/or 'set_libraries()'. It is perfectly valid to duplicate library names; the linker will be instructed to
247
+ link against libraries as many times as they are mentioned.
248
+ """
249
+ self.libraries.append(libname)
250
+
251
+ def set_libraries(self, libnames):
252
+ """
253
+ Set the list of libraries to be included in all links driven by this compiler object to 'libnames' (a list of
254
+ strings). This does not affect any standard system libraries that the linker may include by default.
255
+ """
256
+ self.libraries = libnames[:]
257
+
258
+ def add_library_dir(self, dir): # noqa
259
+ """
260
+ Add 'dir' to the list of directories that will be searched for libraries specified to 'add_library()' and
261
+ 'set_libraries()'. The linker will be instructed to search for libraries in the order they are supplied to
262
+ 'add_library_dir()' and/or 'set_library_dirs()'.
263
+ """
264
+ self.library_dirs.append(dir)
265
+
266
+ def set_library_dirs(self, dirs):
267
+ """
268
+ Set the list of library search directories to 'dirs' (a list of strings). This does not affect any standard
269
+ library search path that the linker may search by default.
270
+ """
271
+ self.library_dirs = dirs[:]
272
+
273
+ def add_runtime_library_dir(self, dir): # noqa
274
+ """
275
+ Add 'dir' to the list of directories that will be searched for shared libraries at runtime.
276
+ """
277
+ self.runtime_library_dirs.append(dir)
278
+
279
+ def set_runtime_library_dirs(self, dirs):
280
+ """
281
+ Set the list of directories to search for shared libraries at runtime to 'dirs' (a list of strings). This does
282
+ not affect any standard search path that the runtime linker may search by default.
283
+ """
284
+ self.runtime_library_dirs = dirs[:]
285
+
286
+ def add_link_object(self, object): # noqa
287
+ """
288
+ Add 'object' to the list of object files (or analogues, such as explicitly named library files or the output of
289
+ "resource compilers") to be included in every link driven by this compiler object.
290
+ """
291
+ self.objects.append(object)
292
+
293
+ def set_link_objects(self, objects):
294
+ """
295
+ Set the list of object files (or analogues) to be included in every link to 'objects'. This does not affect any
296
+ standard object files that the linker may include by default (such as system libraries).
297
+ """
298
+ self.objects = objects[:]
299
+
300
+ # -- Private utility methods --------------------------------------
301
+ # (here for the convenience of subclasses)
302
+
303
+ # Helper method to prep compiler in subclass compile() methods
304
+
305
+ def _setup_compile(self, outdir, macros, incdirs, sources, depends, extra):
306
+ """Process arguments and decide which source files to compile."""
307
+ outdir, macros, incdirs = self._fix_compile_args(outdir, macros, incdirs)
308
+
309
+ if extra is None:
310
+ extra = []
311
+
312
+ # Get the list of expected output (object) files
313
+ objects = self.object_filenames(sources, strip_dir=0, output_dir=outdir)
314
+ check.equal(len(objects), len(sources))
315
+
316
+ pp_opts = gen_preprocess_options(macros, incdirs)
317
+
318
+ build = {}
319
+ for i in range(len(sources)):
320
+ src = sources[i]
321
+ obj = objects[i]
322
+ ext = os.path.splitext(src)[1]
323
+ self.mkpath(os.path.dirname(obj))
324
+ build[obj] = (src, ext)
325
+
326
+ return macros, objects, extra, pp_opts, build
327
+
328
+ def _get_cc_args(self, pp_opts, debug, before):
329
+ # works for unixccompiler, cygwinccompiler
330
+ cc_args = [*pp_opts, '-c']
331
+ if debug:
332
+ cc_args[:0] = ['-g']
333
+ if before:
334
+ cc_args[:0] = before
335
+ return cc_args
336
+
337
+ def _fix_compile_args(self, output_dir, macros, include_dirs):
338
+ """
339
+ Typecheck and fix-up some of the arguments to the 'compile()' method, and return fixed-up values. Specifically:
340
+ if 'output_dir' is None, replaces it with 'self.output_dir'; ensures that 'macros' is a list, and augments it
341
+ with 'self.macros'; ensures that 'include_dirs' is a list, and augments it with 'self.include_dirs'. Guarantees
342
+ that the returned values are of the correct type, i.e. for 'output_dir' either string or None, and for 'macros'
343
+ and 'include_dirs' either list or None.
344
+ """
345
+ if output_dir is None:
346
+ output_dir = self.output_dir
347
+ elif not isinstance(output_dir, str):
348
+ raise TypeError("'output_dir' must be a string or None")
349
+
350
+ if macros is None:
351
+ macros = list(self.macros)
352
+ elif isinstance(macros, list):
353
+ macros = macros + (self.macros or [])
354
+ else:
355
+ raise TypeError("'macros' (if supplied) must be a list of tuples")
356
+
357
+ if include_dirs is None:
358
+ include_dirs = list(self.include_dirs)
359
+ elif isinstance(include_dirs, (list, tuple)):
360
+ include_dirs = list(include_dirs) + (self.include_dirs or [])
361
+ else:
362
+ raise TypeError("'include_dirs' (if supplied) must be a list of strings")
363
+
364
+ # add include dirs for class
365
+ include_dirs += self.__class__.default_include_dirs
366
+
367
+ return output_dir, macros, include_dirs
368
+
369
+ def _prep_compile(self, sources, output_dir, depends=None):
370
+ """
371
+ Decide which source files must be recompiled.
372
+
373
+ Determine the list of object files corresponding to 'sources', and figure out which ones really need to be
374
+ recompiled. Return a list of all object files and a dictionary telling which source files can be skipped.
375
+ """
376
+ # Get the list of expected output (object) files
377
+ objects = self.object_filenames(sources, output_dir=output_dir)
378
+ check.equal(len(objects), len(sources))
379
+
380
+ # Return an empty dict for the "which source files can be skipped"
381
+ # return value to preserve API compatibility.
382
+ return objects, {}
383
+
384
+ def _fix_object_args(self, objects, output_dir):
385
+ """
386
+ Typecheck and fix up some arguments supplied to various methods. Specifically: ensure that 'objects' is a list;
387
+ if output_dir is None, replace with self.output_dir. Return fixed versions of 'objects' and 'output_dir'.
388
+ """
389
+ if not isinstance(objects, (list, tuple)):
390
+ raise TypeError("'objects' must be a list or tuple of strings")
391
+ objects = list(objects)
392
+
393
+ if output_dir is None:
394
+ output_dir = self.output_dir
395
+ elif not isinstance(output_dir, str):
396
+ raise TypeError("'output_dir' must be a string or None")
397
+
398
+ return (objects, output_dir)
399
+
400
+ def _fix_lib_args(self, libraries, library_dirs, runtime_library_dirs):
401
+ """
402
+ Typecheck and fix up some of the arguments supplied to the 'link_*' methods. Specifically: ensure that all
403
+ arguments are lists, and augment them with their permanent versions (eg. 'self.libraries' augments 'libraries').
404
+ Return a tuple with fixed versions of all arguments.
405
+ """
406
+ if libraries is None:
407
+ libraries = list(self.libraries)
408
+ elif isinstance(libraries, (list, tuple)):
409
+ libraries = list(libraries) + (self.libraries or [])
410
+ else:
411
+ raise TypeError("'libraries' (if supplied) must be a list of strings")
412
+
413
+ if library_dirs is None:
414
+ library_dirs = list(self.library_dirs)
415
+ elif isinstance(library_dirs, (list, tuple)):
416
+ library_dirs = list(library_dirs) + (self.library_dirs or [])
417
+ else:
418
+ raise TypeError("'library_dirs' (if supplied) must be a list of strings")
419
+
420
+ # add library dirs for class
421
+ library_dirs += self.__class__.default_library_dirs
422
+
423
+ if runtime_library_dirs is None:
424
+ runtime_library_dirs = list(self.runtime_library_dirs)
425
+ elif isinstance(runtime_library_dirs, (list, tuple)):
426
+ runtime_library_dirs = list(runtime_library_dirs) + self.runtime_library_dirs or []
427
+ else:
428
+ raise TypeError("'runtime_library_dirs' (if supplied) must be a list of strings")
429
+
430
+ return (libraries, library_dirs, runtime_library_dirs)
431
+
432
+ def _need_link(self, objects, output_file):
433
+ """Return true if we need to relink the files listed in 'objects' to recreate 'output_file'."""
434
+ if self.force:
435
+ return True
436
+ else:
437
+ if self.dry_run:
438
+ newer = newer_group(objects, output_file, missing='newer')
439
+ else:
440
+ newer = newer_group(objects, output_file)
441
+ return newer
442
+
443
+ def detect_language(self, sources):
444
+ """
445
+ Detect the language of a given file, or list of files. Uses language_map, and language_order to do the job.
446
+ """
447
+ if not isinstance(sources, list):
448
+ sources = [sources]
449
+ lang = None
450
+ index = len(self.language_order)
451
+ for source in sources:
452
+ base, ext = os.path.splitext(source)
453
+ extlang = self.language_map.get(ext)
454
+ try:
455
+ extindex = self.language_order.index(extlang) # type: ignore
456
+ if extindex < index:
457
+ lang = extlang
458
+ index = extindex
459
+ except ValueError:
460
+ pass
461
+ return lang
462
+
463
+ # -- Worker methods ------------------------------------------------
464
+ # (must be implemented by subclasses)
465
+
466
+ def preprocess(
467
+ self,
468
+ source,
469
+ output_file=None,
470
+ macros=None,
471
+ include_dirs=None,
472
+ extra_preargs=None,
473
+ extra_postargs=None,
474
+ ):
475
+ """
476
+ Preprocess a single C/C++ source file, named in 'source'. Output will be written to file named 'output_file', or
477
+ stdout if 'output_file' not supplied. 'macros' is a list of macro definitions as for 'compile()', which will
478
+ augment the macros set with 'define_macro()' and 'undefine_macro()'. 'include_dirs' is a list of directory
479
+ names that will be added to the default list.
480
+
481
+ Raises PreprocessError on failure.
482
+ """
483
+
484
+ def compile(
485
+ self,
486
+ sources,
487
+ output_dir=None,
488
+ macros=None,
489
+ include_dirs=None,
490
+ debug=0,
491
+ extra_preargs=None,
492
+ extra_postargs=None,
493
+ depends=None,
494
+ ):
495
+ """
496
+ Compile one or more source files.
497
+
498
+ 'sources' must be a list of filenames, most likely C/C++ files, but in reality anything that can be handled by a
499
+ particular compiler and compiler class (eg. MSVCCompiler can handle resource files in 'sources'). Return a list
500
+ of object filenames, one per source filename in 'sources'. Depending on the implementation, not all source
501
+ files will necessarily be compiled, but all corresponding object filenames will be returned.
502
+
503
+ If 'output_dir' is given, object files will be put under it, while retaining their original path component. That
504
+ is, "foo/bar.c" normally compiles to "foo/bar.o" (for a Unix implementation); if 'output_dir' is "build", then
505
+ it would compile to "build/foo/bar.o".
506
+
507
+ 'macros', if given, must be a list of macro definitions. A macro definition is either a (name, value) 2-tuple
508
+ or a (name,) 1-tuple. The former defines a macro; if the value is None, the macro is defined without an explicit
509
+ value. The 1-tuple case undefines a macro. Later definitions/redefinitions/ undefinitions take precedence.
510
+
511
+ 'include_dirs', if given, must be a list of strings, the directories to add to the default include file search
512
+ path for this compilation only.
513
+
514
+ 'debug' is a boolean; if true, the compiler will be instructed to output debug symbols in (or alongside) the
515
+ object file(s).
516
+
517
+ 'extra_preargs' and 'extra_postargs' are implementation- dependent. On platforms that have the notion of a
518
+ command-line (e.g. Unix, DOS/Windows), they are most likely lists of strings: extra command-line arguments to
519
+ prepend/append to the compiler command line. On other platforms, consult the implementation class
520
+ documentation. In any event, they are intended as an escape hatch for those occasions when the abstract compiler
521
+ framework doesn't cut the mustard.
522
+
523
+ 'depends', if given, is a list of filenames that all targets depend on. If a source file is older than any file
524
+ in depends, then the source file will be recompiled. This supports dependency tracking, but only at a coarse
525
+ granularity.
526
+
527
+ Raises CompileError on failure.
528
+ """
529
+ # A concrete compiler class can either override this method entirely or implement _compile().
530
+ macros, objects, extra_postargs, pp_opts, build = self._setup_compile(
531
+ output_dir, macros, include_dirs, sources, depends, extra_postargs,
532
+ )
533
+ cc_args = self._get_cc_args(pp_opts, debug, extra_preargs)
534
+
535
+ for obj in objects:
536
+ try:
537
+ src, ext = build[obj]
538
+ except KeyError:
539
+ continue
540
+ self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
541
+
542
+ # Return *all* object filenames, not just the ones we just built.
543
+ return objects
544
+
545
+ def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
546
+ """Compile 'src' to product 'obj'."""
547
+ # A concrete compiler class that does not override compile() should implement _compile().
548
+
549
+ def create_static_lib(
550
+ self,
551
+ objects,
552
+ output_libname,
553
+ output_dir=None,
554
+ debug=0,
555
+ target_lang=None,
556
+ ):
557
+ """
558
+ Link a bunch of stuff together to create a static library file. The "bunch of stuff" consists of the list of
559
+ object files supplied as 'objects', the extra object files supplied to 'add_link_object()' and/or
560
+ 'set_link_objects()', the libraries supplied to 'add_library()' and/or 'set_libraries()', and the libraries
561
+ supplied as 'libraries' (if any).
562
+
563
+ 'output_libname' should be a library name, not a filename; the filename will be inferred from the library name.
564
+ 'output_dir' is the directory where the library file will be put.
565
+
566
+ 'debug' is a boolean; if true, debugging information will be included in the library (note that on most
567
+ platforms, it is the compile step where this matters: the 'debug' flag is included here just for consistency).
568
+
569
+ 'target_lang' is the target language for which the given objects are being compiled. This allows specific
570
+ linkage time treatment of certain languages.
571
+
572
+ Raises LibError on failure.
573
+ """
574
+
575
+ # values for target_desc parameter in link()
576
+ SHARED_OBJECT = 'shared_object'
577
+ SHARED_LIBRARY = 'shared_library'
578
+ EXECUTABLE = 'executable'
579
+
580
+ def link(
581
+ self,
582
+ target_desc,
583
+ objects,
584
+ output_filename,
585
+ output_dir=None,
586
+ libraries=None,
587
+ library_dirs=None,
588
+ runtime_library_dirs=None,
589
+ export_symbols=None,
590
+ debug=0,
591
+ extra_preargs=None,
592
+ extra_postargs=None,
593
+ build_temp=None,
594
+ target_lang=None,
595
+ ):
596
+ """
597
+ Link a bunch of stuff together to create an executable or shared library file.
598
+
599
+ The "bunch of stuff" consists of the list of object files supplied as 'objects'. 'output_filename' should be a
600
+ filename. If 'output_dir' is supplied, 'output_filename' is relative to it (i.e. 'output_filename' can provide
601
+ directory components if needed).
602
+
603
+ 'libraries' is a list of libraries to link against. These are library names, not filenames, since they're
604
+ translated into filenames in a platform-specific way (eg. "foo" becomes "libfoo.a" on Unix and "foo.lib" on
605
+ DOS/Windows). However, they can include a directory component, which means the linker will look in that
606
+ specific directory rather than searching all the normal locations.
607
+
608
+ 'library_dirs', if supplied, should be a list of directories to search for libraries that were specified as bare
609
+ library names (ie. no directory component). These are on top of the system default and those supplied to
610
+ 'add_library_dir()' and/or 'set_library_dirs()'. 'runtime_library_dirs' is a list of directories that will be
611
+ embedded into the shared library and used to search for other shared libraries that *it* depends on at run-time.
612
+ (This may only be relevant on Unix.)
613
+
614
+ 'export_symbols' is a list of symbols that the shared library will export. (This appears to be relevant only on
615
+ Windows.)
616
+
617
+ 'debug' is as for 'compile()' and 'create_static_lib()', with the slight distinction that it actually matters
618
+ on most platforms (as opposed to 'create_static_lib()', which includes a 'debug' flag mostly for form's sake).
619
+
620
+ 'extra_preargs' and 'extra_postargs' are as for 'compile()' (except of course that they supply command-line
621
+ arguments for the particular linker being used).
622
+
623
+ 'target_lang' is the target language for which the given objects are being compiled. This allows specific
624
+ linkage time treatment of certain languages.
625
+
626
+ Raises LinkError on failure.
627
+ """
628
+ raise NotImplementedError
629
+
630
+ # Old 'link_*()' methods, rewritten to use the new 'link()' method.
631
+
632
+ def link_shared_lib(
633
+ self,
634
+ objects,
635
+ output_libname,
636
+ output_dir=None,
637
+ libraries=None,
638
+ library_dirs=None,
639
+ runtime_library_dirs=None,
640
+ export_symbols=None,
641
+ debug=0,
642
+ extra_preargs=None,
643
+ extra_postargs=None,
644
+ build_temp=None,
645
+ target_lang=None,
646
+ ):
647
+ self.link(
648
+ CCompiler.SHARED_LIBRARY,
649
+ objects,
650
+ self.library_filename(output_libname, lib_type='shared'),
651
+ output_dir,
652
+ libraries,
653
+ library_dirs,
654
+ runtime_library_dirs,
655
+ export_symbols,
656
+ debug,
657
+ extra_preargs,
658
+ extra_postargs,
659
+ build_temp,
660
+ target_lang,
661
+ )
662
+
663
+ def link_shared_object(
664
+ self,
665
+ objects,
666
+ output_filename,
667
+ output_dir=None,
668
+ libraries=None,
669
+ library_dirs=None,
670
+ runtime_library_dirs=None,
671
+ export_symbols=None,
672
+ debug=0,
673
+ extra_preargs=None,
674
+ extra_postargs=None,
675
+ build_temp=None,
676
+ target_lang=None,
677
+ ):
678
+ self.link(
679
+ CCompiler.SHARED_OBJECT,
680
+ objects,
681
+ output_filename,
682
+ output_dir,
683
+ libraries,
684
+ library_dirs,
685
+ runtime_library_dirs,
686
+ export_symbols,
687
+ debug,
688
+ extra_preargs,
689
+ extra_postargs,
690
+ build_temp,
691
+ target_lang,
692
+ )
693
+
694
+ def link_executable(
695
+ self,
696
+ objects,
697
+ output_progname,
698
+ output_dir=None,
699
+ libraries=None,
700
+ library_dirs=None,
701
+ runtime_library_dirs=None,
702
+ debug=0,
703
+ extra_preargs=None,
704
+ extra_postargs=None,
705
+ target_lang=None,
706
+ ):
707
+ self.link(
708
+ CCompiler.EXECUTABLE,
709
+ objects,
710
+ self.executable_filename(output_progname),
711
+ output_dir,
712
+ libraries,
713
+ library_dirs,
714
+ runtime_library_dirs,
715
+ None,
716
+ debug,
717
+ extra_preargs,
718
+ extra_postargs,
719
+ None,
720
+ target_lang,
721
+ )
722
+
723
+ # -- Miscellaneous methods -----------------------------------------
724
+ # These are all used by the 'gen_lib_options() function; there is no appropriate default implementation so
725
+ # subclasses should implement all of these.
726
+
727
+ def library_dir_option(self, dir): # noqa
728
+ """Return the compiler option to add 'dir' to the list of directories searched for libraries."""
729
+ raise NotImplementedError
730
+
731
+ def runtime_library_dir_option(self, dir): # noqa
732
+ """Return the compiler option to add 'dir' to the list of directories searched for runtime libraries."""
733
+ raise NotImplementedError
734
+
735
+ def library_option(self, lib):
736
+ """
737
+ Return the compiler option to add 'lib' to the list of libraries linked into the shared library or executable.
738
+ """
739
+ raise NotImplementedError
740
+
741
+ def has_function( # noqa: C901
742
+ self,
743
+ funcname,
744
+ includes=None,
745
+ include_dirs=None,
746
+ libraries=None,
747
+ library_dirs=None,
748
+ ):
749
+ """
750
+ Return a boolean indicating whether funcname is provided as a symbol on the current platform. The optional
751
+ arguments can be used to augment the compilation environment.
752
+
753
+ The libraries argument is a list of flags to be passed to the linker to make additional symbol definitions
754
+ available for linking.
755
+
756
+ The includes and include_dirs arguments are deprecated. Usually, supplying include files with function
757
+ declarations will cause function detection to fail even in cases where the symbol is available for linking.
758
+ """
759
+ # this can't be included at module scope because it tries to import math which might not be available at that
760
+ # point - maybe the necessary logic should just be inlined?
761
+ import tempfile
762
+
763
+ if includes is None:
764
+ includes = []
765
+ else:
766
+ warnings.warn('includes is deprecated', DeprecationWarning)
767
+ if include_dirs is None:
768
+ include_dirs = []
769
+ else:
770
+ warnings.warn('include_dirs is deprecated', DeprecationWarning)
771
+ if libraries is None:
772
+ libraries = []
773
+ if library_dirs is None:
774
+ library_dirs = []
775
+ fd, fname = tempfile.mkstemp('.c', funcname, text=True)
776
+ with os.fdopen(fd, 'w', encoding='utf-8') as f:
777
+ for incl in includes:
778
+ f.write(f"""#include "{incl}"\n""")
779
+ if not includes:
780
+ # Use "char func(void);" as the prototype to follow what autoconf does. This prototype does not match
781
+ # any well-known function the compiler might recognize as a builtin, so this ends up as a true link
782
+ # test. Without a fake prototype, the test would need to know the exact argument types, and the
783
+ # has_function interface does not provide that level of information.
784
+ f.write(
785
+ f"""\
786
+ #ifdef __cplusplus
787
+ extern "C"
788
+ #endif
789
+ char {funcname}(void);
790
+ """,
791
+ )
792
+ f.write(
793
+ f"""\
794
+ int main (int argc, char **argv) {{
795
+ {funcname}();
796
+ return 0;
797
+ }}
798
+ """,
799
+ )
800
+
801
+ try:
802
+ objects = self.compile([fname], include_dirs=include_dirs)
803
+ except CompileError:
804
+ return False
805
+ finally:
806
+ os.remove(fname)
807
+
808
+ try:
809
+ self.link_executable(
810
+ objects, 'a.out', libraries=libraries, library_dirs=library_dirs,
811
+ )
812
+ except (LinkError, TypeError):
813
+ return False
814
+ else:
815
+ os.remove(
816
+ self.executable_filename('a.out', output_dir=self.output_dir or ''),
817
+ )
818
+ finally:
819
+ for fn in objects:
820
+ os.remove(fn)
821
+ return True
822
+
823
+ def find_library_file(self, dirs, lib, debug=0):
824
+ """
825
+ Search the specified list of directories for a static or shared library file 'lib' and return the full path to
826
+ that file. If 'debug' true, look for a debugging version (if that makes sense on the current platform). Return
827
+ None if 'lib' wasn't found in any of the specified directories.
828
+ """
829
+ raise NotImplementedError
830
+
831
+ # -- Filename generation methods -----------------------------------
832
+
833
+ # The default implementation of the filename generating methods are prejudiced towards the Unix/DOS/Windows view of
834
+ # the world:
835
+ # * object files are named by replacing the source file extension (eg. .c/.cpp -> .o/.obj)
836
+ # * library files (shared or static) are named by plugging the
837
+ # library name and extension into a format string, eg.
838
+ # "lib%s.%s" % (lib_name, ".a") for Unix static libraries
839
+ # * executables are named by appending an extension (possibly empty) to the program name: eg. progname + ".exe"
840
+ # for Windows
841
+ #
842
+ # To reduce redundant code, these methods expect to find several attributes in the current object (presumably
843
+ # defined as class attributes):
844
+ # * src_extensions -
845
+ # list of C/C++ source file extensions, eg. ['.c', '.cpp']
846
+ # * obj_extension -
847
+ # object file extension, eg. '.o' or '.obj'
848
+ # * static_lib_extension -
849
+ # extension for static library files, eg. '.a' or '.lib'
850
+ # * shared_lib_extension -
851
+ # extension for shared library/object files, eg. '.so', '.dll'
852
+ # * static_lib_format -
853
+ # format string for generating static library filenames,
854
+ # eg. 'lib%s.%s' or '%s.%s'
855
+ # * shared_lib_format
856
+ # format string for generating shared library filenames (probably same as static_lib_format, since the extension
857
+ # is one of the intended parameters to the format string)
858
+ # * exe_extension -
859
+ # extension for executable files, eg. '' or '.exe'
860
+
861
+ def object_filenames(self, source_filenames, strip_dir=0, output_dir=''):
862
+ if output_dir is None:
863
+ output_dir = ''
864
+ return [
865
+ self._make_out_path(output_dir, strip_dir, src_name)
866
+ for src_name in source_filenames
867
+ ]
868
+
869
+ @property
870
+ def out_extensions(self):
871
+ return dict.fromkeys(self.src_extensions, self.obj_extension)
872
+
873
+ def _make_out_path(self, output_dir, strip_dir, src_name):
874
+ base, ext = os.path.splitext(src_name)
875
+ base = self._make_relative(base)
876
+ try:
877
+ new_ext = self.out_extensions[ext]
878
+ except LookupError:
879
+ raise UnknownFileError(f"unknown file type '{ext}' (from '{src_name}')") from None
880
+ if strip_dir:
881
+ base = os.path.basename(base)
882
+ return os.path.join(output_dir, base + new_ext)
883
+
884
+ @staticmethod
885
+ def _make_relative(base: str) -> str:
886
+ """
887
+ In order to ensure that a filename always honors the indicated output_dir, make sure it's relative. Ref
888
+ python/cpython#37775.
889
+ """
890
+ # Chop off the drive
891
+ no_drive = os.path.splitdrive(base)[1]
892
+ # If abs, chop off leading /
893
+ return no_drive[os.path.isabs(no_drive):]
894
+
895
+ def shared_object_filename(self, basename, strip_dir=0, output_dir=''):
896
+ check.not_none(output_dir)
897
+ if strip_dir:
898
+ basename = os.path.basename(basename)
899
+ return os.path.join(output_dir, basename + self.shared_lib_extension)
900
+
901
+ def executable_filename(self, basename, strip_dir=0, output_dir=''):
902
+ check.not_none(output_dir)
903
+ if strip_dir:
904
+ basename = os.path.basename(basename)
905
+ return os.path.join(output_dir, basename + (self.exe_extension or ''))
906
+
907
+ def library_filename(
908
+ self,
909
+ libname,
910
+ lib_type='static',
911
+ strip_dir=0,
912
+ output_dir='', # or 'shared'
913
+ ):
914
+ check.not_none(output_dir)
915
+ expected = ('static', 'shared', 'dylib', 'xcode_stub')
916
+ if lib_type not in expected:
917
+ raise ValueError(f"'lib_type' must be {expected!r}")
918
+ fmt = getattr(self, lib_type + '_lib_format')
919
+ ext = getattr(self, lib_type + '_lib_extension')
920
+
921
+ dir, base = os.path.split(libname) # noqa
922
+ filename = fmt % (base, ext)
923
+ if strip_dir:
924
+ dir = '' # noqa
925
+
926
+ return os.path.join(output_dir, dir, filename)
927
+
928
+ # -- Utility methods -----------------------------------------------
929
+
930
+ def announce(self, msg, level=1):
931
+ log.debug(msg)
932
+
933
+ def debug_print(self, msg):
934
+ print(msg)
935
+
936
+ def warn(self, msg):
937
+ sys.stderr.write(f'warning: {msg}\n')
938
+
939
+ def execute(self, func, args, msg=None, level=1):
940
+ execute(func, args, msg, self.dry_run)
941
+
942
+ def spawn(self, cmd, **kwargs):
943
+ spawn(cmd, dry_run=self.dry_run, **kwargs)
944
+
945
+ def move_file(self, src, dst):
946
+ return move_file(src, dst, dry_run=self.dry_run)
947
+
948
+ def mkpath(self, name, mode=0o777):
949
+ mkpath(name, mode, dry_run=self.dry_run)
950
+
951
+
952
+ # Map a sys.platform/os.name ('posix', 'nt') to the default compiler type for that platform. Keys are interpreted as re
953
+ # match patterns. Order is important; platform mappings are preferred over OS names.
954
+ _default_compilers = (
955
+ # Platform string mappings
956
+ ('posix', 'unix'),
957
+ )
958
+
959
+
960
+ def get_default_compiler(osname=None, platform=None):
961
+ """
962
+ Determine the default compiler to use for the given platform.
963
+
964
+ osname should be one of the standard Python OS names (i.e. the ones returned by os.name) and platform the common
965
+ value returned by sys.platform for the platform in question.
966
+
967
+ The default values are os.name and sys.platform in case the parameters are not given.
968
+ """
969
+ if osname is None:
970
+ osname = os.name
971
+ if platform is None:
972
+ platform = sys.platform
973
+ for pattern, compiler in _default_compilers:
974
+ if (
975
+ re.match(pattern, platform) is not None
976
+ or re.match(pattern, osname) is not None
977
+ ):
978
+ return compiler
979
+ # Default to Unix compiler
980
+ return 'unix'
981
+
982
+
983
+ # Map compiler types to (module_name, class_name) pairs -- ie. where to find the code that implements an interface to
984
+ # this compiler. (The module is assumed to be in the 'distutils' package.)
985
+ compiler_class = {
986
+ 'unix': ('unixccompiler', 'UnixCCompiler', 'standard UNIX-style compiler'),
987
+ }
988
+
989
+
990
+ def new_compiler(
991
+ plat=None,
992
+ compiler=None,
993
+ verbose=0,
994
+ dry_run=False,
995
+ force=False,
996
+ ):
997
+ """
998
+ Generate an instance of some CCompiler subclass for the supplied platform/compiler combination. 'plat' defaults to
999
+ 'os.name' (eg. 'posix', 'nt'), and 'compiler' defaults to the default compiler for that platform. Currently only
1000
+ 'posix' and 'nt' are supported, and the default compilers are "traditional Unix interface" (UnixCCompiler class) and
1001
+ Visual C++ (MSVCCompiler class). Note that it's perfectly possible to ask for a Unix compiler object under Windows,
1002
+ and a Microsoft compiler object under Unix -- if you supply a value for 'compiler', 'plat' is ignored.
1003
+ """
1004
+ if plat is None:
1005
+ plat = os.name
1006
+
1007
+ try:
1008
+ if compiler is None:
1009
+ compiler = get_default_compiler(plat)
1010
+
1011
+ (module_name, class_name, long_description) = compiler_class[compiler]
1012
+ except KeyError:
1013
+ msg = f"don't know how to compile C/C++ code on platform '{plat}'"
1014
+ if compiler is not None:
1015
+ msg = msg + f" with '{compiler}' compiler"
1016
+ raise DistutilsPlatformError(msg) from None
1017
+
1018
+ try:
1019
+ module_name = __package__ + '.' + module_name
1020
+ __import__(module_name)
1021
+ module = sys.modules[module_name]
1022
+ klass = vars(module)[class_name]
1023
+ except ImportError:
1024
+ raise DistutilsModuleError(f"can't compile C/C++ code: unable to load module '{module_name}'") from None
1025
+ except KeyError:
1026
+ raise DistutilsModuleError(
1027
+ f"can't compile C/C++ code: unable to find class '{class_name}' in module '{module_name}'",
1028
+ ) from None
1029
+
1030
+ # The None is necessary to preserve backwards compatibility with classes that expect verbose to be the first
1031
+ # positional argument.
1032
+ return klass(None, dry_run, force)