blob: 53ca8fd0c579f2d8fd58f0bb79d3ad66c37a4ffb [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
Tarek Ziadéfe327b92009-06-03 11:12:08 +00008import os
9import sys
10import warnings
Greg Ward3ca54bc2000-05-31 01:05:35 +000011
12# This class is really only used by the "build_ext" command, so it might
13# make sense to put it in distutils.command.build_ext. However, that
14# module is already big enough, and I want to make this class a bit more
15# complex to simplify some common cases ("foo" module in "foo.c") and do
16# better error-checking ("foo.c" actually exists).
Fred Drakeb94b8492001-12-06 20:51:35 +000017#
Greg Ward3ca54bc2000-05-31 01:05:35 +000018# Also, putting this in build_ext.py means every setup script would have to
19# import that large-ish module (indirectly, through distutils.core) in
20# order to do anything.
21
22class Extension:
23 """Just a collection of attributes that describes an extension
24 module and everything needed to build it (hopefully in a portable
Greg Ward45b87bc2000-08-13 00:38:58 +000025 way, but there are hooks that let you be as unportable as you need).
Greg Ward3ca54bc2000-05-31 01:05:35 +000026
27 Instance attributes:
28 name : string
29 the full name of the extension, including any packages -- ie.
30 *not* a filename or pathname, but Python dotted name
31 sources : [string]
Greg Wardcb185572000-06-24 00:18:24 +000032 list of source filenames, relative to the distribution root
33 (where the setup script lives), in Unix form (slash-separated)
34 for portability. Source files may be C, C++, SWIG (.i),
35 platform-specific resource files, or whatever else is recognized
36 by the "build_ext" command as source for a Python extension.
Greg Ward3ca54bc2000-05-31 01:05:35 +000037 include_dirs : [string]
38 list of directories to search for C/C++ header files (in Unix
39 form for portability)
40 define_macros : [(name : string, value : string|None)]
41 list of macros to define; each macro is defined using a 2-tuple,
42 where 'value' is either the string to define it to or None to
43 define it without a particular value (equivalent of "#define
44 FOO" in source or -DFOO on Unix C compiler command line)
45 undef_macros : [string]
46 list of macros to undefine explicitly
47 library_dirs : [string]
48 list of directories to search for C/C++ libraries at link time
49 libraries : [string]
50 list of library names (not filenames or paths) to link against
51 runtime_library_dirs : [string]
52 list of directories to search for C/C++ libraries at run time
53 (for shared extensions, this is when the extension is loaded)
54 extra_objects : [string]
55 list of extra files to link with (eg. object files not implied
56 by 'sources', static library that must be explicitly specified,
57 binary resource files, etc.)
58 extra_compile_args : [string]
59 any extra platform- and compiler-specific information to use
60 when compiling the source files in 'sources'. For platforms and
61 compilers where "command line" makes sense, this is typically a
62 list of command-line arguments, but for other platforms it could
63 be anything.
64 extra_link_args : [string]
65 any extra platform- and compiler-specific information to use
66 when linking object files together to create the extension (or
67 to create a new static Python interpreter). Similar
68 interpretation as for 'extra_compile_args'.
69 export_symbols : [string]
70 list of symbols to be exported from a shared extension. Not
71 used on all platforms, and not generally necessary for Python
72 extensions, which typically export exactly one symbol: "init" +
73 extension_name.
Anthony Baxtera0240342004-10-14 10:02:08 +000074 swig_opts : [string]
75 any extra options to pass to SWIG if a source file has the .i
76 extension.
Jeremy Hylton09e532b2002-06-12 20:08:56 +000077 depends : [string]
78 list of files that the extension depends on
Gustavo Niemeyer6b016852002-11-05 16:12:02 +000079 language : string
80 extension language (i.e. "c", "c++", "objc"). Will be detected
81 from the source extensions if not provided.
Tarek Ziadé9e47ce42009-03-31 22:27:23 +000082 optional : boolean
83 specifies that a build failure in the extension should not abort the
84 build process, but simply not install the failing extension.
Greg Ward3ca54bc2000-05-31 01:05:35 +000085 """
86
Andrew M. Kuchling6ffdaab2003-01-27 16:30:36 +000087 # When adding arguments to this constructor, be sure to update
88 # setup_keywords in core.py.
Greg Ward3ca54bc2000-05-31 01:05:35 +000089 def __init__ (self, name, sources,
90 include_dirs=None,
91 define_macros=None,
92 undef_macros=None,
93 library_dirs=None,
94 libraries=None,
95 runtime_library_dirs=None,
96 extra_objects=None,
97 extra_compile_args=None,
98 extra_link_args=None,
99 export_symbols=None,
Anthony Baxtera0240342004-10-14 10:02:08 +0000100 swig_opts = None,
Jeremy Hylton09e532b2002-06-12 20:08:56 +0000101 depends=None,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000102 language=None,
Tarek Ziadé9e47ce42009-03-31 22:27:23 +0000103 optional=None,
Andrew M. Kuchling3f1c9a92002-11-13 20:54:21 +0000104 **kw # To catch unknown keywords
Greg Ward3ca54bc2000-05-31 01:05:35 +0000105 ):
Tarek Ziadé8df921b2009-06-03 11:20:44 +0000106 assert isinstance(name, str), "'name' must be a string"
Tarek Ziadéfe327b92009-06-03 11:12:08 +0000107 assert (isinstance(sources, list) and
108 all(isinstance(v, str) for v in sources)), \
Greg Ward41ed12f2000-09-17 00:45:18 +0000109 "'sources' must be a list of strings"
Greg Ward3ca54bc2000-05-31 01:05:35 +0000110
111 self.name = name
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 []
Greg Ward1f6a0d42000-08-13 00:41:40 +0000122 self.export_symbols = export_symbols or []
Anthony Baxtera0240342004-10-14 10:02:08 +0000123 self.swig_opts = swig_opts or []
Jeremy Hylton09e532b2002-06-12 20:08:56 +0000124 self.depends = depends or []
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000125 self.language = language
Tarek Ziadé9e47ce42009-03-31 22:27:23 +0000126 self.optional = optional
Greg Ward3ca54bc2000-05-31 01:05:35 +0000127
Andrew M. Kuchling3f1c9a92002-11-13 20:54:21 +0000128 # If there are unknown keyword options, warn about them
Tarek Ziadéfe327b92009-06-03 11:12:08 +0000129 if len(kw) > 0:
130 options = [repr(option) for option in kw]
131 options = ', '.join(sorted(options))
132 msg = "Unknown Extension options: %s" % options
133 warnings.warn(msg)
Greg Ward41ed12f2000-09-17 00:45:18 +0000134
Tarek Ziadéc6709972009-06-03 10:26:26 +0000135def read_setup_file(filename):
136 """Reads a Setup file and returns Extension instances."""
137 from distutils.sysconfig import (parse_makefile, expand_makefile_vars,
138 _variable_rx)
139
Greg Ward41ed12f2000-09-17 00:45:18 +0000140 from distutils.text_file import TextFile
141 from distutils.util import split_quoted
142
143 # First pass over the file to gather "VAR = VALUE" assignments.
144 vars = parse_makefile(filename)
145
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)
151 extensions = []
152
153 while 1:
154 line = file.readline()
155 if line is None: # eof
156 break
157 if _variable_rx.match(line): # VAR=VALUE, handled in first pass
158 continue
159
160 if line[0] == line[-1] == "*":
161 file.warn("'%s' lines not handled yet" % line)
162 continue
163
Greg Ward41ed12f2000-09-17 00:45:18 +0000164 line = expand_makefile_vars(line, vars)
165 words = split_quoted(line)
Greg Ward41ed12f2000-09-17 00:45:18 +0000166
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.
172
173 module = words[0]
174 ext = Extension(module, [])
175 append_next_word = None
176
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
181 continue
182
183 suffix = os.path.splitext(word)[1]
184 switch = word[0:2] ; value = word[2:]
185
Andrew M. Kuchling3d2d9802001-12-21 15:34:17 +0000186 if suffix in (".c", ".cc", ".cpp", ".cxx", ".c++", ".m", ".mm"):
Greg Ward41ed12f2000-09-17 00:45:18 +0000187 # hmm, should we do something about C vs. C++ sources?
188 # or leave it up to the CCompiler implementation to
189 # worry about?
190 ext.sources.append(word)
191 elif switch == "-I":
192 ext.include_dirs.append(value)
193 elif switch == "-D":
Tarek Ziadéfe327b92009-06-03 11:12:08 +0000194 equals = value.find("=")
Greg Ward41ed12f2000-09-17 00:45:18 +0000195 if equals == -1: # bare "-DFOO" -- no value
196 ext.define_macros.append((value, None))
197 else: # "-DFOO=blah"
198 ext.define_macros.append((value[0:equals],
199 value[equals+2:]))
200 elif switch == "-U":
201 ext.undef_macros.append(value)
202 elif switch == "-C": # only here 'cause makesetup has it!
203 ext.extra_compile_args.append(word)
204 elif switch == "-l":
205 ext.libraries.append(value)
206 elif switch == "-L":
207 ext.library_dirs.append(value)
208 elif switch == "-R":
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
Andrew M. Kuchlingf4a4fb92002-03-29 18:00:19 +0000214 elif word == "-Xcompiler":
215 append_next_word = ext.extra_compile_args
Greg Ward41ed12f2000-09-17 00:45:18 +0000216 elif switch == "-u":
217 ext.extra_link_args.append(word)
218 if not value:
219 append_next_word = ext.extra_link_args
Andrew M. Kuchling31ddfb62002-11-27 13:45:26 +0000220 elif suffix in (".a", ".so", ".sl", ".o", ".dylib"):
Greg Ward41ed12f2000-09-17 00:45:18 +0000221 # 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)
226 else:
227 file.warn("unrecognized argument '%s'" % word)
228
229 extensions.append(ext)
230
Greg Ward41ed12f2000-09-17 00:45:18 +0000231 return extensions