blob: 7efbb74f8953b564ffafcee37d0ff3897e0833e1 [file] [log] [blame]
Greg Ward3ca54bc2000-05-31 01:05:35 +00001"""distutils.extension
2
3Provides the Extension class, used to describe C/C++ extension
4modules in setup scripts."""
5
Tarek Ziadé68407212009-06-03 11:17:15 +00006import os
Tarek Ziadé36797272010-07-22 12:50:05 +00007import sys
Tarek Ziadé68407212009-06-03 11:17:15 +00008import warnings
Greg Ward3ca54bc2000-05-31 01:05:35 +00009
10# This class is really only used by the "build_ext" command, so it might
11# make sense to put it in distutils.command.build_ext. However, that
12# module is already big enough, and I want to make this class a bit more
13# complex to simplify some common cases ("foo" module in "foo.c") and do
14# better error-checking ("foo.c" actually exists).
Fred Drakeb94b8492001-12-06 20:51:35 +000015#
Greg Ward3ca54bc2000-05-31 01:05:35 +000016# Also, putting this in build_ext.py means every setup script would have to
17# import that large-ish module (indirectly, through distutils.core) in
18# order to do anything.
19
20class Extension:
21 """Just a collection of attributes that describes an extension
22 module and everything needed to build it (hopefully in a portable
Greg Ward45b87bc2000-08-13 00:38:58 +000023 way, but there are hooks that let you be as unportable as you need).
Greg Ward3ca54bc2000-05-31 01:05:35 +000024
25 Instance attributes:
26 name : string
27 the full name of the extension, including any packages -- ie.
28 *not* a filename or pathname, but Python dotted name
29 sources : [string]
Greg Wardcb185572000-06-24 00:18:24 +000030 list of source filenames, relative to the distribution root
31 (where the setup script lives), in Unix form (slash-separated)
32 for portability. Source files may be C, C++, SWIG (.i),
33 platform-specific resource files, or whatever else is recognized
34 by the "build_ext" command as source for a Python extension.
Greg Ward3ca54bc2000-05-31 01:05:35 +000035 include_dirs : [string]
36 list of directories to search for C/C++ header files (in Unix
37 form for portability)
38 define_macros : [(name : string, value : string|None)]
39 list of macros to define; each macro is defined using a 2-tuple,
40 where 'value' is either the string to define it to or None to
41 define it without a particular value (equivalent of "#define
42 FOO" in source or -DFOO on Unix C compiler command line)
43 undef_macros : [string]
44 list of macros to undefine explicitly
45 library_dirs : [string]
46 list of directories to search for C/C++ libraries at link time
47 libraries : [string]
48 list of library names (not filenames or paths) to link against
49 runtime_library_dirs : [string]
50 list of directories to search for C/C++ libraries at run time
51 (for shared extensions, this is when the extension is loaded)
52 extra_objects : [string]
53 list of extra files to link with (eg. object files not implied
54 by 'sources', static library that must be explicitly specified,
55 binary resource files, etc.)
56 extra_compile_args : [string]
57 any extra platform- and compiler-specific information to use
58 when compiling the source files in 'sources'. For platforms and
59 compilers where "command line" makes sense, this is typically a
60 list of command-line arguments, but for other platforms it could
61 be anything.
62 extra_link_args : [string]
63 any extra platform- and compiler-specific information to use
64 when linking object files together to create the extension (or
65 to create a new static Python interpreter). Similar
66 interpretation as for 'extra_compile_args'.
67 export_symbols : [string]
68 list of symbols to be exported from a shared extension. Not
69 used on all platforms, and not generally necessary for Python
70 extensions, which typically export exactly one symbol: "init" +
71 extension_name.
Anthony Baxtera0240342004-10-14 10:02:08 +000072 swig_opts : [string]
73 any extra options to pass to SWIG if a source file has the .i
74 extension.
Jeremy Hylton09e532b2002-06-12 20:08:56 +000075 depends : [string]
76 list of files that the extension depends on
Gustavo Niemeyer6b016852002-11-05 16:12:02 +000077 language : string
78 extension language (i.e. "c", "c++", "objc"). Will be detected
79 from the source extensions if not provided.
Tarek Ziadéb2e36f12009-03-31 22:37:55 +000080 optional : boolean
81 specifies that a build failure in the extension should not abort the
82 build process, but simply not install the failing extension.
Greg Ward3ca54bc2000-05-31 01:05:35 +000083 """
84
Andrew M. Kuchling6ffdaab2003-01-27 16:30:36 +000085 # When adding arguments to this constructor, be sure to update
86 # setup_keywords in core.py.
Collin Winter5b7e9d72007-08-30 03:52:21 +000087 def __init__(self, name, sources,
Greg Ward3ca54bc2000-05-31 01:05:35 +000088 include_dirs=None,
89 define_macros=None,
90 undef_macros=None,
91 library_dirs=None,
92 libraries=None,
93 runtime_library_dirs=None,
94 extra_objects=None,
95 extra_compile_args=None,
96 extra_link_args=None,
97 export_symbols=None,
Anthony Baxtera0240342004-10-14 10:02:08 +000098 swig_opts = None,
Jeremy Hylton09e532b2002-06-12 20:08:56 +000099 depends=None,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000100 language=None,
Tarek Ziadéb2e36f12009-03-31 22:37:55 +0000101 optional=None,
Andrew M. Kuchling3f1c9a92002-11-13 20:54:21 +0000102 **kw # To catch unknown keywords
Greg Ward3ca54bc2000-05-31 01:05:35 +0000103 ):
Tarek Ziadé2f19bb12009-07-22 08:57:28 +0000104 if not isinstance(name, str):
105 raise AssertionError("'name' must be a string")
106 if not (isinstance(sources, list) and
107 all(isinstance(v, str) for v in sources)):
108 raise AssertionError("'sources' must be a list of strings")
Greg Ward3ca54bc2000-05-31 01:05:35 +0000109
110 self.name = name
111 self.sources = sources
112 self.include_dirs = include_dirs or []
113 self.define_macros = define_macros or []
114 self.undef_macros = undef_macros or []
115 self.library_dirs = library_dirs or []
116 self.libraries = libraries or []
117 self.runtime_library_dirs = runtime_library_dirs or []
118 self.extra_objects = extra_objects or []
119 self.extra_compile_args = extra_compile_args or []
120 self.extra_link_args = extra_link_args or []
Greg Ward1f6a0d42000-08-13 00:41:40 +0000121 self.export_symbols = export_symbols or []
Anthony Baxtera0240342004-10-14 10:02:08 +0000122 self.swig_opts = swig_opts or []
Jeremy Hylton09e532b2002-06-12 20:08:56 +0000123 self.depends = depends or []
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000124 self.language = language
Tarek Ziadéb2e36f12009-03-31 22:37:55 +0000125 self.optional = optional
Greg Ward3ca54bc2000-05-31 01:05:35 +0000126
Andrew M. Kuchling3f1c9a92002-11-13 20:54:21 +0000127 # If there are unknown keyword options, warn about them
Tarek Ziadé68407212009-06-03 11:17:15 +0000128 if len(kw) > 0:
129 options = [repr(option) for option in kw]
130 options = ', '.join(sorted(options))
131 msg = "Unknown Extension options: %s" % options
132 warnings.warn(msg)
Greg Ward41ed12f2000-09-17 00:45:18 +0000133
Giampaolo Rodola'4a692ce2014-03-27 14:14:16 +0100134 def __repr__(self):
135 return '<%s.%s(%r) at %#x>' % (
136 self.__class__.__module__,
Serhiy Storchaka521e5862014-07-22 15:00:37 +0300137 self.__class__.__qualname__,
Giampaolo Rodola'4a692ce2014-03-27 14:14:16 +0100138 self.name,
139 id(self))
140
141
Collin Winter5b7e9d72007-08-30 03:52:21 +0000142def read_setup_file(filename):
Tarek Ziadée6ed2f92009-06-03 10:31:15 +0000143 """Reads a Setup file and returns Extension instances."""
Tarek Ziadé36797272010-07-22 12:50:05 +0000144 from distutils.sysconfig import (parse_makefile, expand_makefile_vars,
Tarek Ziadée6ed2f92009-06-03 10:31:15 +0000145 _variable_rx)
146
Greg Ward41ed12f2000-09-17 00:45:18 +0000147 from distutils.text_file import TextFile
148 from distutils.util import split_quoted
149
150 # First pass over the file to gather "VAR = VALUE" assignments.
Tarek Ziadé36797272010-07-22 12:50:05 +0000151 vars = parse_makefile(filename)
Greg Ward41ed12f2000-09-17 00:45:18 +0000152
153 # Second pass to gobble up the real content: lines of the form
154 # <module> ... [<sourcefile> ...] [<cpparg> ...] [<library> ...]
155 file = TextFile(filename,
156 strip_comments=1, skip_blanks=1, join_lines=1,
157 lstrip_ws=1, rstrip_ws=1)
Éric Araujobee5cef2010-11-05 23:51:56 +0000158 try:
159 extensions = []
Greg Ward41ed12f2000-09-17 00:45:18 +0000160
Éric Araujobee5cef2010-11-05 23:51:56 +0000161 while True:
162 line = file.readline()
163 if line is None: # eof
164 break
165 if _variable_rx.match(line): # VAR=VALUE, handled in first pass
Greg Ward41ed12f2000-09-17 00:45:18 +0000166 continue
167
Éric Araujobee5cef2010-11-05 23:51:56 +0000168 if line[0] == line[-1] == "*":
169 file.warn("'%s' lines not handled yet" % line)
170 continue
Greg Ward41ed12f2000-09-17 00:45:18 +0000171
Éric Araujobee5cef2010-11-05 23:51:56 +0000172 line = expand_makefile_vars(line, vars)
173 words = split_quoted(line)
174
175 # NB. this parses a slightly different syntax than the old
176 # makesetup script: here, there must be exactly one extension per
177 # line, and it must be the first word of the line. I have no idea
178 # why the old syntax supported multiple extensions per line, as
179 # they all wind up being the same.
180
181 module = words[0]
182 ext = Extension(module, [])
183 append_next_word = None
184
185 for word in words[1:]:
186 if append_next_word is not None:
187 append_next_word.append(word)
188 append_next_word = None
189 continue
190
191 suffix = os.path.splitext(word)[1]
192 switch = word[0:2] ; value = word[2:]
193
194 if suffix in (".c", ".cc", ".cpp", ".cxx", ".c++", ".m", ".mm"):
195 # hmm, should we do something about C vs. C++ sources?
196 # or leave it up to the CCompiler implementation to
197 # worry about?
198 ext.sources.append(word)
199 elif switch == "-I":
200 ext.include_dirs.append(value)
201 elif switch == "-D":
202 equals = value.find("=")
203 if equals == -1: # bare "-DFOO" -- no value
204 ext.define_macros.append((value, None))
205 else: # "-DFOO=blah"
206 ext.define_macros.append((value[0:equals],
207 value[equals+2:]))
208 elif switch == "-U":
209 ext.undef_macros.append(value)
210 elif switch == "-C": # only here 'cause makesetup has it!
211 ext.extra_compile_args.append(word)
212 elif switch == "-l":
213 ext.libraries.append(value)
214 elif switch == "-L":
215 ext.library_dirs.append(value)
216 elif switch == "-R":
217 ext.runtime_library_dirs.append(value)
218 elif word == "-rpath":
219 append_next_word = ext.runtime_library_dirs
220 elif word == "-Xlinker":
Greg Ward41ed12f2000-09-17 00:45:18 +0000221 append_next_word = ext.extra_link_args
Éric Araujobee5cef2010-11-05 23:51:56 +0000222 elif word == "-Xcompiler":
223 append_next_word = ext.extra_compile_args
224 elif switch == "-u":
225 ext.extra_link_args.append(word)
226 if not value:
227 append_next_word = ext.extra_link_args
228 elif suffix in (".a", ".so", ".sl", ".o", ".dylib"):
229 # NB. a really faithful emulation of makesetup would
230 # append a .o file to extra_objects only if it
231 # had a slash in it; otherwise, it would s/.o/.c/
232 # and append it to sources. Hmmmm.
233 ext.extra_objects.append(word)
234 else:
235 file.warn("unrecognized argument '%s'" % word)
Greg Ward41ed12f2000-09-17 00:45:18 +0000236
Éric Araujobee5cef2010-11-05 23:51:56 +0000237 extensions.append(ext)
238 finally:
239 file.close()
Greg Ward41ed12f2000-09-17 00:45:18 +0000240
Greg Ward41ed12f2000-09-17 00:45:18 +0000241 return extensions