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