| 1 | n/a | """Class representing C/C++ extension modules.""" |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | from packaging import logger |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | # This class is really only used by the "build_ext" command, so it might |
|---|
| 6 | n/a | # make sense to put it in distutils.command.build_ext. However, that |
|---|
| 7 | n/a | # module is already big enough, and I want to make this class a bit more |
|---|
| 8 | n/a | # complex to simplify some common cases ("foo" module in "foo.c") and do |
|---|
| 9 | n/a | # better error-checking ("foo.c" actually exists). |
|---|
| 10 | n/a | # |
|---|
| 11 | n/a | # Also, putting this in build_ext.py means every setup script would have to |
|---|
| 12 | n/a | # import that large-ish module (indirectly, through distutils.core) in |
|---|
| 13 | n/a | # order to do anything. |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | class Extension: |
|---|
| 17 | n/a | """Just a collection of attributes that describes an extension |
|---|
| 18 | n/a | module and everything needed to build it (hopefully in a portable |
|---|
| 19 | n/a | way, but there are hooks that let you be as unportable as you need). |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | Instance attributes: |
|---|
| 22 | n/a | name : string |
|---|
| 23 | n/a | the full name of the extension, including any packages -- ie. |
|---|
| 24 | n/a | *not* a filename or pathname, but Python dotted name |
|---|
| 25 | n/a | sources : [string] |
|---|
| 26 | n/a | list of source filenames, relative to the distribution root |
|---|
| 27 | n/a | (where the setup script lives), in Unix form (slash-separated) |
|---|
| 28 | n/a | for portability. Source files may be C, C++, SWIG (.i), |
|---|
| 29 | n/a | platform-specific resource files, or whatever else is recognized |
|---|
| 30 | n/a | by the "build_ext" command as source for a Python extension. |
|---|
| 31 | n/a | include_dirs : [string] |
|---|
| 32 | n/a | list of directories to search for C/C++ header files (in Unix |
|---|
| 33 | n/a | form for portability) |
|---|
| 34 | n/a | define_macros : [(name : string, value : string|None)] |
|---|
| 35 | n/a | list of macros to define; each macro is defined using a 2-tuple, |
|---|
| 36 | n/a | where 'value' is either the string to define it to or None to |
|---|
| 37 | n/a | define it without a particular value (equivalent of "#define |
|---|
| 38 | n/a | FOO" in source or -DFOO on Unix C compiler command line) |
|---|
| 39 | n/a | undef_macros : [string] |
|---|
| 40 | n/a | list of macros to undefine explicitly |
|---|
| 41 | n/a | library_dirs : [string] |
|---|
| 42 | n/a | list of directories to search for C/C++ libraries at link time |
|---|
| 43 | n/a | libraries : [string] |
|---|
| 44 | n/a | list of library names (not filenames or paths) to link against |
|---|
| 45 | n/a | runtime_library_dirs : [string] |
|---|
| 46 | n/a | list of directories to search for C/C++ libraries at run time |
|---|
| 47 | n/a | (for shared extensions, this is when the extension is loaded) |
|---|
| 48 | n/a | extra_objects : [string] |
|---|
| 49 | n/a | list of extra files to link with (eg. object files not implied |
|---|
| 50 | n/a | by 'sources', static library that must be explicitly specified, |
|---|
| 51 | n/a | binary resource files, etc.) |
|---|
| 52 | n/a | extra_compile_args : [string] |
|---|
| 53 | n/a | any extra platform- and compiler-specific information to use |
|---|
| 54 | n/a | when compiling the source files in 'sources'. For platforms and |
|---|
| 55 | n/a | compilers where "command line" makes sense, this is typically a |
|---|
| 56 | n/a | list of command-line arguments, but for other platforms it could |
|---|
| 57 | n/a | be anything. |
|---|
| 58 | n/a | extra_link_args : [string] |
|---|
| 59 | n/a | any extra platform- and compiler-specific information to use |
|---|
| 60 | n/a | when linking object files together to create the extension (or |
|---|
| 61 | n/a | to create a new static Python interpreter). Similar |
|---|
| 62 | n/a | interpretation as for 'extra_compile_args'. |
|---|
| 63 | n/a | export_symbols : [string] |
|---|
| 64 | n/a | list of symbols to be exported from a shared extension. Not |
|---|
| 65 | n/a | used on all platforms, and not generally necessary for Python |
|---|
| 66 | n/a | extensions, which typically export exactly one symbol: "init" + |
|---|
| 67 | n/a | extension_name. |
|---|
| 68 | n/a | swig_opts : [string] |
|---|
| 69 | n/a | any extra options to pass to SWIG if a source file has the .i |
|---|
| 70 | n/a | extension. |
|---|
| 71 | n/a | depends : [string] |
|---|
| 72 | n/a | list of files that the extension depends on |
|---|
| 73 | n/a | language : string |
|---|
| 74 | n/a | extension language (i.e. "c", "c++", "objc"). Will be detected |
|---|
| 75 | n/a | from the source extensions if not provided. |
|---|
| 76 | n/a | optional : boolean |
|---|
| 77 | n/a | specifies that a build failure in the extension should not abort the |
|---|
| 78 | n/a | build process, but simply not install the failing extension. |
|---|
| 79 | n/a | """ |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | # **kwargs are allowed so that a warning is emitted instead of an |
|---|
| 82 | n/a | # exception |
|---|
| 83 | n/a | def __init__(self, name, sources, include_dirs=None, define_macros=None, |
|---|
| 84 | n/a | undef_macros=None, library_dirs=None, libraries=None, |
|---|
| 85 | n/a | runtime_library_dirs=None, extra_objects=None, |
|---|
| 86 | n/a | extra_compile_args=None, extra_link_args=None, |
|---|
| 87 | n/a | export_symbols=None, swig_opts=None, depends=None, |
|---|
| 88 | n/a | language=None, optional=None, **kw): |
|---|
| 89 | n/a | if not isinstance(name, str): |
|---|
| 90 | n/a | raise AssertionError("'name' must be a string") |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | if not isinstance(sources, list): |
|---|
| 93 | n/a | raise AssertionError("'sources' must be a list of strings") |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | for v in sources: |
|---|
| 96 | n/a | if not isinstance(v, str): |
|---|
| 97 | n/a | raise AssertionError("'sources' must be a list of strings") |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | self.name = name |
|---|
| 100 | n/a | self.sources = sources |
|---|
| 101 | n/a | self.include_dirs = include_dirs or [] |
|---|
| 102 | n/a | self.define_macros = define_macros or [] |
|---|
| 103 | n/a | self.undef_macros = undef_macros or [] |
|---|
| 104 | n/a | self.library_dirs = library_dirs or [] |
|---|
| 105 | n/a | self.libraries = libraries or [] |
|---|
| 106 | n/a | self.runtime_library_dirs = runtime_library_dirs or [] |
|---|
| 107 | n/a | self.extra_objects = extra_objects or [] |
|---|
| 108 | n/a | self.extra_compile_args = extra_compile_args or [] |
|---|
| 109 | n/a | self.extra_link_args = extra_link_args or [] |
|---|
| 110 | n/a | self.export_symbols = export_symbols or [] |
|---|
| 111 | n/a | self.swig_opts = swig_opts or [] |
|---|
| 112 | n/a | self.depends = depends or [] |
|---|
| 113 | n/a | self.language = language |
|---|
| 114 | n/a | self.optional = optional |
|---|
| 115 | n/a | |
|---|
| 116 | n/a | # If there are unknown keyword options, warn about them |
|---|
| 117 | n/a | if len(kw) > 0: |
|---|
| 118 | n/a | options = [repr(option) for option in kw] |
|---|
| 119 | n/a | options = ', '.join(sorted(options)) |
|---|
| 120 | n/a | logger.warning( |
|---|
| 121 | n/a | 'unknown arguments given to Extension: %s', options) |
|---|