blob: f7e7b4edc9195814e69b2186be56fd6fe96f4010 [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
Greg Ward3ca54bc2000-05-31 01:05:35 +00006__revision__ = "$Id$"
7
Neal Norwitz9d72bb42007-04-17 08:48:32 +00008import os, sys
Greg Ward3ca54bc2000-05-31 01:05:35 +00009
Andrew M. Kuchling3f1c9a92002-11-13 20:54:21 +000010try:
11 import warnings
12except ImportError:
13 warnings = None
Greg Ward3ca54bc2000-05-31 01:05:35 +000014
15# This class is really only used by the "build_ext" command, so it might
16# make sense to put it in distutils.command.build_ext. However, that
17# module is already big enough, and I want to make this class a bit more
18# complex to simplify some common cases ("foo" module in "foo.c") and do
19# better error-checking ("foo.c" actually exists).
Fred Drakeb94b8492001-12-06 20:51:35 +000020#
Greg Ward3ca54bc2000-05-31 01:05:35 +000021# Also, putting this in build_ext.py means every setup script would have to
22# import that large-ish module (indirectly, through distutils.core) in
23# order to do anything.
24
25class Extension:
26 """Just a collection of attributes that describes an extension
27 module and everything needed to build it (hopefully in a portable
Greg Ward45b87bc2000-08-13 00:38:58 +000028 way, but there are hooks that let you be as unportable as you need).
Greg Ward3ca54bc2000-05-31 01:05:35 +000029
30 Instance attributes:
31 name : string
32 the full name of the extension, including any packages -- ie.
33 *not* a filename or pathname, but Python dotted name
34 sources : [string]
Greg Wardcb185572000-06-24 00:18:24 +000035 list of source filenames, relative to the distribution root
36 (where the setup script lives), in Unix form (slash-separated)
37 for portability. Source files may be C, C++, SWIG (.i),
38 platform-specific resource files, or whatever else is recognized
39 by the "build_ext" command as source for a Python extension.
Greg Ward3ca54bc2000-05-31 01:05:35 +000040 include_dirs : [string]
41 list of directories to search for C/C++ header files (in Unix
42 form for portability)
43 define_macros : [(name : string, value : string|None)]
44 list of macros to define; each macro is defined using a 2-tuple,
45 where 'value' is either the string to define it to or None to
46 define it without a particular value (equivalent of "#define
47 FOO" in source or -DFOO on Unix C compiler command line)
48 undef_macros : [string]
49 list of macros to undefine explicitly
50 library_dirs : [string]
51 list of directories to search for C/C++ libraries at link time
52 libraries : [string]
53 list of library names (not filenames or paths) to link against
54 runtime_library_dirs : [string]
55 list of directories to search for C/C++ libraries at run time
56 (for shared extensions, this is when the extension is loaded)
57 extra_objects : [string]
58 list of extra files to link with (eg. object files not implied
59 by 'sources', static library that must be explicitly specified,
60 binary resource files, etc.)
61 extra_compile_args : [string]
62 any extra platform- and compiler-specific information to use
63 when compiling the source files in 'sources'. For platforms and
64 compilers where "command line" makes sense, this is typically a
65 list of command-line arguments, but for other platforms it could
66 be anything.
67 extra_link_args : [string]
68 any extra platform- and compiler-specific information to use
69 when linking object files together to create the extension (or
70 to create a new static Python interpreter). Similar
71 interpretation as for 'extra_compile_args'.
72 export_symbols : [string]
73 list of symbols to be exported from a shared extension. Not
74 used on all platforms, and not generally necessary for Python
75 extensions, which typically export exactly one symbol: "init" +
76 extension_name.
Anthony Baxtera0240342004-10-14 10:02:08 +000077 swig_opts : [string]
78 any extra options to pass to SWIG if a source file has the .i
79 extension.
Jeremy Hylton09e532b2002-06-12 20:08:56 +000080 depends : [string]
81 list of files that the extension depends on
Gustavo Niemeyer6b016852002-11-05 16:12:02 +000082 language : string
83 extension language (i.e. "c", "c++", "objc"). Will be detected
84 from the source extensions if not provided.
Tarek Ziadéb2e36f12009-03-31 22:37:55 +000085 optional : boolean
86 specifies that a build failure in the extension should not abort the
87 build process, but simply not install the failing extension.
Greg Ward3ca54bc2000-05-31 01:05:35 +000088 """
89
Andrew M. Kuchling6ffdaab2003-01-27 16:30:36 +000090 # When adding arguments to this constructor, be sure to update
91 # setup_keywords in core.py.
Collin Winter5b7e9d72007-08-30 03:52:21 +000092 def __init__(self, name, sources,
Greg Ward3ca54bc2000-05-31 01:05:35 +000093 include_dirs=None,
94 define_macros=None,
95 undef_macros=None,
96 library_dirs=None,
97 libraries=None,
98 runtime_library_dirs=None,
99 extra_objects=None,
100 extra_compile_args=None,
101 extra_link_args=None,
102 export_symbols=None,
Anthony Baxtera0240342004-10-14 10:02:08 +0000103 swig_opts = None,
Jeremy Hylton09e532b2002-06-12 20:08:56 +0000104 depends=None,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000105 language=None,
Tarek Ziadéb2e36f12009-03-31 22:37:55 +0000106 optional=None,
Andrew M. Kuchling3f1c9a92002-11-13 20:54:21 +0000107 **kw # To catch unknown keywords
Greg Ward3ca54bc2000-05-31 01:05:35 +0000108 ):
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000109 assert isinstance(name, str), "'name' must be a string"
Guido van Rossum13257902007-06-07 23:15:56 +0000110 assert (isinstance(sources, list) and
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000111 all(isinstance(v, str) for v in sources)), \
Greg Ward41ed12f2000-09-17 00:45:18 +0000112 "'sources' must be a list of strings"
Greg Ward3ca54bc2000-05-31 01:05:35 +0000113
114 self.name = name
115 self.sources = sources
116 self.include_dirs = include_dirs or []
117 self.define_macros = define_macros or []
118 self.undef_macros = undef_macros or []
119 self.library_dirs = library_dirs or []
120 self.libraries = libraries or []
121 self.runtime_library_dirs = runtime_library_dirs or []
122 self.extra_objects = extra_objects or []
123 self.extra_compile_args = extra_compile_args or []
124 self.extra_link_args = extra_link_args or []
Greg Ward1f6a0d42000-08-13 00:41:40 +0000125 self.export_symbols = export_symbols or []
Anthony Baxtera0240342004-10-14 10:02:08 +0000126 self.swig_opts = swig_opts or []
Jeremy Hylton09e532b2002-06-12 20:08:56 +0000127 self.depends = depends or []
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000128 self.language = language
Tarek Ziadéb2e36f12009-03-31 22:37:55 +0000129 self.optional = optional
Greg Ward3ca54bc2000-05-31 01:05:35 +0000130
Andrew M. Kuchling3f1c9a92002-11-13 20:54:21 +0000131 # If there are unknown keyword options, warn about them
132 if len(kw):
Collin Winter5b7e9d72007-08-30 03:52:21 +0000133 L = map(repr, sorted(kw))
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000134 msg = "Unknown Extension options: " + ', '.join(L)
Andrew M. Kuchling3f1c9a92002-11-13 20:54:21 +0000135 if warnings is not None:
136 warnings.warn(msg)
137 else:
138 sys.stderr.write(msg + '\n')
Greg Ward41ed12f2000-09-17 00:45:18 +0000139
140
Collin Winter5b7e9d72007-08-30 03:52:21 +0000141def read_setup_file(filename):
Greg Ward41ed12f2000-09-17 00:45:18 +0000142 from distutils.sysconfig import \
143 parse_makefile, expand_makefile_vars, _variable_rx
144 from distutils.text_file import TextFile
145 from distutils.util import split_quoted
146
147 # First pass over the file to gather "VAR = VALUE" assignments.
148 vars = parse_makefile(filename)
149
150 # Second pass to gobble up the real content: lines of the form
151 # <module> ... [<sourcefile> ...] [<cpparg> ...] [<library> ...]
152 file = TextFile(filename,
153 strip_comments=1, skip_blanks=1, join_lines=1,
154 lstrip_ws=1, rstrip_ws=1)
155 extensions = []
156
Collin Winter5b7e9d72007-08-30 03:52:21 +0000157 while True:
Greg Ward41ed12f2000-09-17 00:45:18 +0000158 line = file.readline()
159 if line is None: # eof
160 break
161 if _variable_rx.match(line): # VAR=VALUE, handled in first pass
162 continue
163
164 if line[0] == line[-1] == "*":
165 file.warn("'%s' lines not handled yet" % line)
166 continue
167
168 #print "original line: " + line
169 line = expand_makefile_vars(line, vars)
170 words = split_quoted(line)
171 #print "expanded line: " + line
172
173 # NB. this parses a slightly different syntax than the old
174 # makesetup script: here, there must be exactly one extension per
175 # line, and it must be the first word of the line. I have no idea
176 # why the old syntax supported multiple extensions per line, as
177 # they all wind up being the same.
178
179 module = words[0]
180 ext = Extension(module, [])
181 append_next_word = None
182
183 for word in words[1:]:
184 if append_next_word is not None:
185 append_next_word.append(word)
186 append_next_word = None
187 continue
188
189 suffix = os.path.splitext(word)[1]
190 switch = word[0:2] ; value = word[2:]
191
Andrew M. Kuchling3d2d9802001-12-21 15:34:17 +0000192 if suffix in (".c", ".cc", ".cpp", ".cxx", ".c++", ".m", ".mm"):
Greg Ward41ed12f2000-09-17 00:45:18 +0000193 # hmm, should we do something about C vs. C++ sources?
194 # or leave it up to the CCompiler implementation to
195 # worry about?
196 ext.sources.append(word)
197 elif switch == "-I":
198 ext.include_dirs.append(value)
199 elif switch == "-D":
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000200 equals = value.find("=")
Greg Ward41ed12f2000-09-17 00:45:18 +0000201 if equals == -1: # bare "-DFOO" -- no value
202 ext.define_macros.append((value, None))
203 else: # "-DFOO=blah"
204 ext.define_macros.append((value[0:equals],
205 value[equals+2:]))
206 elif switch == "-U":
207 ext.undef_macros.append(value)
208 elif switch == "-C": # only here 'cause makesetup has it!
209 ext.extra_compile_args.append(word)
210 elif switch == "-l":
211 ext.libraries.append(value)
212 elif switch == "-L":
213 ext.library_dirs.append(value)
214 elif switch == "-R":
215 ext.runtime_library_dirs.append(value)
216 elif word == "-rpath":
217 append_next_word = ext.runtime_library_dirs
218 elif word == "-Xlinker":
219 append_next_word = ext.extra_link_args
Andrew M. Kuchlingf4a4fb92002-03-29 18:00:19 +0000220 elif word == "-Xcompiler":
221 append_next_word = ext.extra_compile_args
Greg Ward41ed12f2000-09-17 00:45:18 +0000222 elif switch == "-u":
223 ext.extra_link_args.append(word)
224 if not value:
225 append_next_word = ext.extra_link_args
Andrew M. Kuchling31ddfb62002-11-27 13:45:26 +0000226 elif suffix in (".a", ".so", ".sl", ".o", ".dylib"):
Greg Ward41ed12f2000-09-17 00:45:18 +0000227 # NB. a really faithful emulation of makesetup would
228 # append a .o file to extra_objects only if it
229 # had a slash in it; otherwise, it would s/.o/.c/
230 # and append it to sources. Hmmmm.
231 ext.extra_objects.append(word)
232 else:
233 file.warn("unrecognized argument '%s'" % word)
234
235 extensions.append(ext)
236
237 #print "module:", module
238 #print "source files:", source_files
239 #print "cpp args:", cpp_args
240 #print "lib args:", library_args
241
242 #extensions[module] = { 'sources': source_files,
243 # 'cpp_args': cpp_args,
244 # 'lib_args': library_args }
Fred Drakeb94b8492001-12-06 20:51:35 +0000245
Greg Ward41ed12f2000-09-17 00:45:18 +0000246 return extensions