Manual py3k backport: [svn r74155] Issue #6242: Fix deallocator of io.StringIO and...
[python.git] / Lib / distutils / extension.py
blob6af18108012df751f8914f6d5f2b00adfb80b992
1 """distutils.extension
3 Provides the Extension class, used to describe C/C++ extension
4 modules in setup scripts."""
6 __revision__ = "$Id$"
8 import os
9 import sys
10 import warnings
12 # This class is really only used by the "build_ext" command, so it might
13 # make sense to put it in distutils.command.build_ext. However, that
14 # module is already big enough, and I want to make this class a bit more
15 # complex to simplify some common cases ("foo" module in "foo.c") and do
16 # better error-checking ("foo.c" actually exists).
18 # Also, putting this in build_ext.py means every setup script would have to
19 # import that large-ish module (indirectly, through distutils.core) in
20 # order to do anything.
22 class Extension:
23 """Just a collection of attributes that describes an extension
24 module and everything needed to build it (hopefully in a portable
25 way, but there are hooks that let you be as unportable as you need).
27 Instance attributes:
28 name : string
29 the full name of the extension, including any packages -- ie.
30 *not* a filename or pathname, but Python dotted name
31 sources : [string]
32 list of source filenames, relative to the distribution root
33 (where the setup script lives), in Unix form (slash-separated)
34 for portability. Source files may be C, C++, SWIG (.i),
35 platform-specific resource files, or whatever else is recognized
36 by the "build_ext" command as source for a Python extension.
37 include_dirs : [string]
38 list of directories to search for C/C++ header files (in Unix
39 form for portability)
40 define_macros : [(name : string, value : string|None)]
41 list of macros to define; each macro is defined using a 2-tuple,
42 where 'value' is either the string to define it to or None to
43 define it without a particular value (equivalent of "#define
44 FOO" in source or -DFOO on Unix C compiler command line)
45 undef_macros : [string]
46 list of macros to undefine explicitly
47 library_dirs : [string]
48 list of directories to search for C/C++ libraries at link time
49 libraries : [string]
50 list of library names (not filenames or paths) to link against
51 runtime_library_dirs : [string]
52 list of directories to search for C/C++ libraries at run time
53 (for shared extensions, this is when the extension is loaded)
54 extra_objects : [string]
55 list of extra files to link with (eg. object files not implied
56 by 'sources', static library that must be explicitly specified,
57 binary resource files, etc.)
58 extra_compile_args : [string]
59 any extra platform- and compiler-specific information to use
60 when compiling the source files in 'sources'. For platforms and
61 compilers where "command line" makes sense, this is typically a
62 list of command-line arguments, but for other platforms it could
63 be anything.
64 extra_link_args : [string]
65 any extra platform- and compiler-specific information to use
66 when linking object files together to create the extension (or
67 to create a new static Python interpreter). Similar
68 interpretation as for 'extra_compile_args'.
69 export_symbols : [string]
70 list of symbols to be exported from a shared extension. Not
71 used on all platforms, and not generally necessary for Python
72 extensions, which typically export exactly one symbol: "init" +
73 extension_name.
74 swig_opts : [string]
75 any extra options to pass to SWIG if a source file has the .i
76 extension.
77 depends : [string]
78 list of files that the extension depends on
79 language : string
80 extension language (i.e. "c", "c++", "objc"). Will be detected
81 from the source extensions if not provided.
82 optional : boolean
83 specifies that a build failure in the extension should not abort the
84 build process, but simply not install the failing extension.
85 """
87 # When adding arguments to this constructor, be sure to update
88 # setup_keywords in core.py.
89 def __init__ (self, name, sources,
90 include_dirs=None,
91 define_macros=None,
92 undef_macros=None,
93 library_dirs=None,
94 libraries=None,
95 runtime_library_dirs=None,
96 extra_objects=None,
97 extra_compile_args=None,
98 extra_link_args=None,
99 export_symbols=None,
100 swig_opts = None,
101 depends=None,
102 language=None,
103 optional=None,
104 **kw # To catch unknown keywords
106 if not isinstance(name, str):
107 raise AssertionError("'name' must be a string")
108 if not (isinstance(sources, list) and
109 all(isinstance(v, str) for v in sources)):
110 raise AssertionError("'sources' must be a list of strings")
112 self.name = name
113 self.sources = sources
114 self.include_dirs = include_dirs or []
115 self.define_macros = define_macros or []
116 self.undef_macros = undef_macros or []
117 self.library_dirs = library_dirs or []
118 self.libraries = libraries or []
119 self.runtime_library_dirs = runtime_library_dirs or []
120 self.extra_objects = extra_objects or []
121 self.extra_compile_args = extra_compile_args or []
122 self.extra_link_args = extra_link_args or []
123 self.export_symbols = export_symbols or []
124 self.swig_opts = swig_opts or []
125 self.depends = depends or []
126 self.language = language
127 self.optional = optional
129 # If there are unknown keyword options, warn about them
130 if len(kw) > 0:
131 options = [repr(option) for option in kw]
132 options = ', '.join(sorted(options))
133 msg = "Unknown Extension options: %s" % options
134 warnings.warn(msg)
136 def read_setup_file(filename):
137 """Reads a Setup file and returns Extension instances."""
138 from distutils.sysconfig import (parse_makefile, expand_makefile_vars,
139 _variable_rx)
141 from distutils.text_file import TextFile
142 from distutils.util import split_quoted
144 # First pass over the file to gather "VAR = VALUE" assignments.
145 vars = parse_makefile(filename)
147 # Second pass to gobble up the real content: lines of the form
148 # <module> ... [<sourcefile> ...] [<cpparg> ...] [<library> ...]
149 file = TextFile(filename,
150 strip_comments=1, skip_blanks=1, join_lines=1,
151 lstrip_ws=1, rstrip_ws=1)
152 extensions = []
154 while 1:
155 line = file.readline()
156 if line is None: # eof
157 break
158 if _variable_rx.match(line): # VAR=VALUE, handled in first pass
159 continue
161 if line[0] == line[-1] == "*":
162 file.warn("'%s' lines not handled yet" % line)
163 continue
165 line = expand_makefile_vars(line, vars)
166 words = split_quoted(line)
168 # NB. this parses a slightly different syntax than the old
169 # makesetup script: here, there must be exactly one extension per
170 # line, and it must be the first word of the line. I have no idea
171 # why the old syntax supported multiple extensions per line, as
172 # they all wind up being the same.
174 module = words[0]
175 ext = Extension(module, [])
176 append_next_word = None
178 for word in words[1:]:
179 if append_next_word is not None:
180 append_next_word.append(word)
181 append_next_word = None
182 continue
184 suffix = os.path.splitext(word)[1]
185 switch = word[0:2] ; value = word[2:]
187 if suffix in (".c", ".cc", ".cpp", ".cxx", ".c++", ".m", ".mm"):
188 # hmm, should we do something about C vs. C++ sources?
189 # or leave it up to the CCompiler implementation to
190 # worry about?
191 ext.sources.append(word)
192 elif switch == "-I":
193 ext.include_dirs.append(value)
194 elif switch == "-D":
195 equals = value.find("=")
196 if equals == -1: # bare "-DFOO" -- no value
197 ext.define_macros.append((value, None))
198 else: # "-DFOO=blah"
199 ext.define_macros.append((value[0:equals],
200 value[equals+2:]))
201 elif switch == "-U":
202 ext.undef_macros.append(value)
203 elif switch == "-C": # only here 'cause makesetup has it!
204 ext.extra_compile_args.append(word)
205 elif switch == "-l":
206 ext.libraries.append(value)
207 elif switch == "-L":
208 ext.library_dirs.append(value)
209 elif switch == "-R":
210 ext.runtime_library_dirs.append(value)
211 elif word == "-rpath":
212 append_next_word = ext.runtime_library_dirs
213 elif word == "-Xlinker":
214 append_next_word = ext.extra_link_args
215 elif word == "-Xcompiler":
216 append_next_word = ext.extra_compile_args
217 elif switch == "-u":
218 ext.extra_link_args.append(word)
219 if not value:
220 append_next_word = ext.extra_link_args
221 elif suffix in (".a", ".so", ".sl", ".o", ".dylib"):
222 # NB. a really faithful emulation of makesetup would
223 # append a .o file to extra_objects only if it
224 # had a slash in it; otherwise, it would s/.o/.c/
225 # and append it to sources. Hmmmm.
226 ext.extra_objects.append(word)
227 else:
228 file.warn("unrecognized argument '%s'" % word)
230 extensions.append(ext)
232 return extensions