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