3 Provides the Extension class, used to describe C/C++ extension
4 modules in setup scripts."""
6 # created 2000/05/30, Greg Ward
14 # This class is really only used by the "build_ext" command, so it might
15 # make sense to put it in distutils.command.build_ext. However, that
16 # module is already big enough, and I want to make this class a bit more
17 # complex to simplify some common cases ("foo" module in "foo.c") and do
18 # better error-checking ("foo.c" actually exists).
20 # Also, putting this in build_ext.py means every setup script would have to
21 # import that large-ish module (indirectly, through distutils.core) in
22 # order to do anything.
25 """Just a collection of attributes that describes an extension
26 module and everything needed to build it (hopefully in a portable
27 way, but there are hooks that let you be as unportable as you need).
31 the full name of the extension, including any packages -- ie.
32 *not* a filename or pathname, but Python dotted name
34 list of source filenames, relative to the distribution root
35 (where the setup script lives), in Unix form (slash-separated)
36 for portability. Source files may be C, C++, SWIG (.i),
37 platform-specific resource files, or whatever else is recognized
38 by the "build_ext" command as source for a Python extension.
39 include_dirs : [string]
40 list of directories to search for C/C++ header files (in Unix
42 define_macros : [(name : string, value : string|None)]
43 list of macros to define; each macro is defined using a 2-tuple,
44 where 'value' is either the string to define it to or None to
45 define it without a particular value (equivalent of "#define
46 FOO" in source or -DFOO on Unix C compiler command line)
47 undef_macros : [string]
48 list of macros to undefine explicitly
49 library_dirs : [string]
50 list of directories to search for C/C++ libraries at link time
52 list of library names (not filenames or paths) to link against
53 runtime_library_dirs : [string]
54 list of directories to search for C/C++ libraries at run time
55 (for shared extensions, this is when the extension is loaded)
56 extra_objects : [string]
57 list of extra files to link with (eg. object files not implied
58 by 'sources', static library that must be explicitly specified,
59 binary resource files, etc.)
60 extra_compile_args : [string]
61 any extra platform- and compiler-specific information to use
62 when compiling the source files in 'sources'. For platforms and
63 compilers where "command line" makes sense, this is typically a
64 list of command-line arguments, but for other platforms it could
66 extra_link_args : [string]
67 any extra platform- and compiler-specific information to use
68 when linking object files together to create the extension (or
69 to create a new static Python interpreter). Similar
70 interpretation as for 'extra_compile_args'.
71 export_symbols : [string]
72 list of symbols to be exported from a shared extension. Not
73 used on all platforms, and not generally necessary for Python
74 extensions, which typically export exactly one symbol: "init" +
77 list of files that the extension depends on
80 def __init__ (self
, name
, sources
,
86 runtime_library_dirs
=None,
88 extra_compile_args
=None,
94 assert type(name
) is StringType
, "'name' must be a string"
95 assert (type(sources
) is ListType
and
96 map(type, sources
) == [StringType
]*len(sources
)), \
97 "'sources' must be a list of strings"
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
.depends
= depends
or []
116 def read_setup_file (filename
):
117 from distutils
.sysconfig
import \
118 parse_makefile
, expand_makefile_vars
, _variable_rx
119 from distutils
.text_file
import TextFile
120 from distutils
.util
import split_quoted
122 # First pass over the file to gather "VAR = VALUE" assignments.
123 vars = parse_makefile(filename
)
125 # Second pass to gobble up the real content: lines of the form
126 # <module> ... [<sourcefile> ...] [<cpparg> ...] [<library> ...]
127 file = TextFile(filename
,
128 strip_comments
=1, skip_blanks
=1, join_lines
=1,
129 lstrip_ws
=1, rstrip_ws
=1)
133 line
= file.readline()
134 if line
is None: # eof
136 if _variable_rx
.match(line
): # VAR=VALUE, handled in first pass
139 if line
[0] == line
[-1] == "*":
140 file.warn("'%s' lines not handled yet" % line
)
143 #print "original line: " + line
144 line
= expand_makefile_vars(line
, vars)
145 words
= split_quoted(line
)
146 #print "expanded line: " + line
148 # NB. this parses a slightly different syntax than the old
149 # makesetup script: here, there must be exactly one extension per
150 # line, and it must be the first word of the line. I have no idea
151 # why the old syntax supported multiple extensions per line, as
152 # they all wind up being the same.
155 ext
= Extension(module
, [])
156 append_next_word
= None
158 for word
in words
[1:]:
159 if append_next_word
is not None:
160 append_next_word
.append(word
)
161 append_next_word
= None
164 suffix
= os
.path
.splitext(word
)[1]
165 switch
= word
[0:2] ; value
= word
[2:]
167 if suffix
in (".c", ".cc", ".cpp", ".cxx", ".c++", ".m", ".mm"):
168 # hmm, should we do something about C vs. C++ sources?
169 # or leave it up to the CCompiler implementation to
171 ext
.sources
.append(word
)
173 ext
.include_dirs
.append(value
)
175 equals
= string
.find(value
, "=")
176 if equals
== -1: # bare "-DFOO" -- no value
177 ext
.define_macros
.append((value
, None))
179 ext
.define_macros
.append((value
[0:equals
],
182 ext
.undef_macros
.append(value
)
183 elif switch
== "-C": # only here 'cause makesetup has it!
184 ext
.extra_compile_args
.append(word
)
186 ext
.libraries
.append(value
)
188 ext
.library_dirs
.append(value
)
190 ext
.runtime_library_dirs
.append(value
)
191 elif word
== "-rpath":
192 append_next_word
= ext
.runtime_library_dirs
193 elif word
== "-Xlinker":
194 append_next_word
= ext
.extra_link_args
195 elif word
== "-Xcompiler":
196 append_next_word
= ext
.extra_compile_args
198 ext
.extra_link_args
.append(word
)
200 append_next_word
= ext
.extra_link_args
201 elif suffix
in (".a", ".so", ".sl", ".o"):
202 # NB. a really faithful emulation of makesetup would
203 # append a .o file to extra_objects only if it
204 # had a slash in it; otherwise, it would s/.o/.c/
205 # and append it to sources. Hmmmm.
206 ext
.extra_objects
.append(word
)
208 file.warn("unrecognized argument '%s'" % word
)
210 extensions
.append(ext
)
212 #print "module:", module
213 #print "source files:", source_files
214 #print "cpp args:", cpp_args
215 #print "lib args:", library_args
217 #extensions[module] = { 'sources': source_files,
218 # 'cpp_args': cpp_args,
219 # 'lib_args': library_args }