Greg Ward | 3ca54bc | 2000-05-31 01:05:35 +0000 | [diff] [blame] | 1 | """distutils.extension |
| 2 | |
| 3 | Provides the Extension class, used to describe C/C++ extension |
| 4 | modules in setup scripts.""" |
| 5 | |
| 6 | # created 2000/05/30, Greg Ward |
| 7 | |
| 8 | __revision__ = "$Id$" |
| 9 | |
| 10 | from types import * |
| 11 | |
| 12 | |
| 13 | # This class is really only used by the "build_ext" command, so it might |
| 14 | # make sense to put it in distutils.command.build_ext. However, that |
| 15 | # module is already big enough, and I want to make this class a bit more |
| 16 | # complex to simplify some common cases ("foo" module in "foo.c") and do |
| 17 | # better error-checking ("foo.c" actually exists). |
| 18 | # |
| 19 | # Also, putting this in build_ext.py means every setup script would have to |
| 20 | # import that large-ish module (indirectly, through distutils.core) in |
| 21 | # order to do anything. |
| 22 | |
| 23 | class Extension: |
| 24 | """Just a collection of attributes that describes an extension |
| 25 | module and everything needed to build it (hopefully in a portable |
Greg Ward | 45b87bc | 2000-08-13 00:38:58 +0000 | [diff] [blame] | 26 | way, but there are hooks that let you be as unportable as you need). |
Greg Ward | 3ca54bc | 2000-05-31 01:05:35 +0000 | [diff] [blame] | 27 | |
| 28 | Instance attributes: |
| 29 | name : string |
| 30 | the full name of the extension, including any packages -- ie. |
| 31 | *not* a filename or pathname, but Python dotted name |
| 32 | sources : [string] |
Greg Ward | cb18557 | 2000-06-24 00:18:24 +0000 | [diff] [blame] | 33 | list of source filenames, relative to the distribution root |
| 34 | (where the setup script lives), in Unix form (slash-separated) |
| 35 | for portability. Source files may be C, C++, SWIG (.i), |
| 36 | platform-specific resource files, or whatever else is recognized |
| 37 | by the "build_ext" command as source for a Python extension. |
Greg Ward | 3ca54bc | 2000-05-31 01:05:35 +0000 | [diff] [blame] | 38 | include_dirs : [string] |
| 39 | list of directories to search for C/C++ header files (in Unix |
| 40 | form for portability) |
| 41 | define_macros : [(name : string, value : string|None)] |
| 42 | list of macros to define; each macro is defined using a 2-tuple, |
| 43 | where 'value' is either the string to define it to or None to |
| 44 | define it without a particular value (equivalent of "#define |
| 45 | FOO" in source or -DFOO on Unix C compiler command line) |
| 46 | undef_macros : [string] |
| 47 | list of macros to undefine explicitly |
| 48 | library_dirs : [string] |
| 49 | list of directories to search for C/C++ libraries at link time |
| 50 | libraries : [string] |
| 51 | list of library names (not filenames or paths) to link against |
| 52 | runtime_library_dirs : [string] |
| 53 | list of directories to search for C/C++ libraries at run time |
| 54 | (for shared extensions, this is when the extension is loaded) |
| 55 | extra_objects : [string] |
| 56 | list of extra files to link with (eg. object files not implied |
| 57 | by 'sources', static library that must be explicitly specified, |
| 58 | binary resource files, etc.) |
| 59 | extra_compile_args : [string] |
| 60 | any extra platform- and compiler-specific information to use |
| 61 | when compiling the source files in 'sources'. For platforms and |
| 62 | compilers where "command line" makes sense, this is typically a |
| 63 | list of command-line arguments, but for other platforms it could |
| 64 | be anything. |
| 65 | extra_link_args : [string] |
| 66 | any extra platform- and compiler-specific information to use |
| 67 | when linking object files together to create the extension (or |
| 68 | to create a new static Python interpreter). Similar |
| 69 | interpretation as for 'extra_compile_args'. |
| 70 | export_symbols : [string] |
| 71 | list of symbols to be exported from a shared extension. Not |
| 72 | used on all platforms, and not generally necessary for Python |
| 73 | extensions, which typically export exactly one symbol: "init" + |
| 74 | extension_name. |
Greg Ward | 3ca54bc | 2000-05-31 01:05:35 +0000 | [diff] [blame] | 75 | """ |
| 76 | |
| 77 | def __init__ (self, name, sources, |
| 78 | include_dirs=None, |
| 79 | define_macros=None, |
| 80 | undef_macros=None, |
| 81 | library_dirs=None, |
| 82 | libraries=None, |
| 83 | runtime_library_dirs=None, |
| 84 | extra_objects=None, |
| 85 | extra_compile_args=None, |
| 86 | extra_link_args=None, |
| 87 | export_symbols=None, |
Greg Ward | 3ca54bc | 2000-05-31 01:05:35 +0000 | [diff] [blame] | 88 | ): |
| 89 | |
| 90 | assert type(name) is StringType, "'name' must be a string" |
| 91 | assert (type(sources) is ListType and |
| 92 | len(sources) >= 1 and |
| 93 | map(type, sources) == [StringType]*len(sources)), \ |
| 94 | "'sources' must be a non-empty list of strings" |
| 95 | |
| 96 | self.name = name |
| 97 | self.sources = sources |
| 98 | self.include_dirs = include_dirs or [] |
| 99 | self.define_macros = define_macros or [] |
| 100 | self.undef_macros = undef_macros or [] |
| 101 | self.library_dirs = library_dirs or [] |
| 102 | self.libraries = libraries or [] |
| 103 | self.runtime_library_dirs = runtime_library_dirs or [] |
| 104 | self.extra_objects = extra_objects or [] |
| 105 | self.extra_compile_args = extra_compile_args or [] |
| 106 | self.extra_link_args = extra_link_args or [] |
Greg Ward | 1f6a0d4 | 2000-08-13 00:41:40 +0000 | [diff] [blame] | 107 | self.export_symbols = export_symbols or [] |
Greg Ward | 3ca54bc | 2000-05-31 01:05:35 +0000 | [diff] [blame] | 108 | |
| 109 | # class Extension |