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" +
78 def __init__ (self
, name
, sources
,
84 runtime_library_dirs
=None,
86 extra_compile_args
=None,
91 assert type(name
) is StringType
, "'name' must be a string"
92 assert (type(sources
) is ListType
and
93 map(type, sources
) == [StringType
]*len(sources
)), \
94 "'sources' must be a list of strings"
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 []
107 self
.export_symbols
= export_symbols
or []
112 def read_setup_file (filename
):
113 from distutils
.sysconfig
import \
114 parse_makefile
, expand_makefile_vars
, _variable_rx
115 from distutils
.text_file
import TextFile
116 from distutils
.util
import split_quoted
118 # First pass over the file to gather "VAR = VALUE" assignments.
119 vars = parse_makefile(filename
)
121 # Second pass to gobble up the real content: lines of the form
122 # <module> ... [<sourcefile> ...] [<cpparg> ...] [<library> ...]
123 file = TextFile(filename
,
124 strip_comments
=1, skip_blanks
=1, join_lines
=1,
125 lstrip_ws
=1, rstrip_ws
=1)
129 line
= file.readline()
130 if line
is None: # eof
132 if _variable_rx
.match(line
): # VAR=VALUE, handled in first pass
135 if line
[0] == line
[-1] == "*":
136 file.warn("'%s' lines not handled yet" % line
)
139 #print "original line: " + line
140 line
= expand_makefile_vars(line
, vars)
141 words
= split_quoted(line
)
142 #print "expanded line: " + line
144 # NB. this parses a slightly different syntax than the old
145 # makesetup script: here, there must be exactly one extension per
146 # line, and it must be the first word of the line. I have no idea
147 # why the old syntax supported multiple extensions per line, as
148 # they all wind up being the same.
151 ext
= Extension(module
, [])
152 append_next_word
= None
154 for word
in words
[1:]:
155 if append_next_word
is not None:
156 append_next_word
.append(word
)
157 append_next_word
= None
160 suffix
= os
.path
.splitext(word
)[1]
161 switch
= word
[0:2] ; value
= word
[2:]
163 if suffix
in (".c", ".cc", ".cpp", ".cxx", ".c++"):
164 # hmm, should we do something about C vs. C++ sources?
165 # or leave it up to the CCompiler implementation to
167 ext
.sources
.append(word
)
169 ext
.include_dirs
.append(value
)
171 equals
= string
.find(value
, "=")
172 if equals
== -1: # bare "-DFOO" -- no value
173 ext
.define_macros
.append((value
, None))
175 ext
.define_macros
.append((value
[0:equals
],
178 ext
.undef_macros
.append(value
)
179 elif switch
== "-C": # only here 'cause makesetup has it!
180 ext
.extra_compile_args
.append(word
)
182 ext
.libraries
.append(value
)
184 ext
.library_dirs
.append(value
)
186 ext
.runtime_library_dirs
.append(value
)
187 elif word
== "-rpath":
188 append_next_word
= ext
.runtime_library_dirs
189 elif word
== "-Xlinker":
190 append_next_word
= ext
.extra_link_args
192 ext
.extra_link_args
.append(word
)
194 append_next_word
= ext
.extra_link_args
195 elif suffix
in (".a", ".so", ".sl", ".o"):
196 # NB. a really faithful emulation of makesetup would
197 # append a .o file to extra_objects only if it
198 # had a slash in it; otherwise, it would s/.o/.c/
199 # and append it to sources. Hmmmm.
200 ext
.extra_objects
.append(word
)
202 file.warn("unrecognized argument '%s'" % word
)
204 extensions
.append(ext
)
206 #print "module:", module
207 #print "source files:", source_files
208 #print "cpp args:", cpp_args
209 #print "lib args:", library_args
211 #extensions[module] = { 'sources': source_files,
212 # 'cpp_args': cpp_args,
213 # 'lib_args': library_args }