blob: 4d72c402ce33b0cc4cdb12b2136553d73d5af744 [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
Andrew M. Kuchling2db92a62002-11-20 16:10:29 +00008import os, string, sys
Greg Ward3ca54bc2000-05-31 01:05:35 +00009from types import *
10
Andrew M. Kuchling3f1c9a92002-11-13 20:54:21 +000011try:
12 import warnings
13except ImportError:
14 warnings = None
Greg Ward3ca54bc2000-05-31 01:05:35 +000015
16# This class is really only used by the "build_ext" command, so it might
17# make sense to put it in distutils.command.build_ext. However, that
18# module is already big enough, and I want to make this class a bit more
19# complex to simplify some common cases ("foo" module in "foo.c") and do
20# better error-checking ("foo.c" actually exists).
Fred Drakeb94b8492001-12-06 20:51:35 +000021#
Greg Ward3ca54bc2000-05-31 01:05:35 +000022# Also, putting this in build_ext.py means every setup script would have to
23# import that large-ish module (indirectly, through distutils.core) in
24# order to do anything.
25
26class Extension:
27 """Just a collection of attributes that describes an extension
28 module and everything needed to build it (hopefully in a portable
Greg Ward45b87bc2000-08-13 00:38:58 +000029 way, but there are hooks that let you be as unportable as you need).
Greg Ward3ca54bc2000-05-31 01:05:35 +000030
31 Instance attributes:
32 name : string
33 the full name of the extension, including any packages -- ie.
34 *not* a filename or pathname, but Python dotted name
35 sources : [string]
Greg Wardcb185572000-06-24 00:18:24 +000036 list of source filenames, relative to the distribution root
37 (where the setup script lives), in Unix form (slash-separated)
38 for portability. Source files may be C, C++, SWIG (.i),
39 platform-specific resource files, or whatever else is recognized
40 by the "build_ext" command as source for a Python extension.
Greg Ward3ca54bc2000-05-31 01:05:35 +000041 include_dirs : [string]
42 list of directories to search for C/C++ header files (in Unix
43 form for portability)
44 define_macros : [(name : string, value : string|None)]
45 list of macros to define; each macro is defined using a 2-tuple,
46 where 'value' is either the string to define it to or None to
47 define it without a particular value (equivalent of "#define
48 FOO" in source or -DFOO on Unix C compiler command line)
49 undef_macros : [string]
50 list of macros to undefine explicitly
51 library_dirs : [string]
52 list of directories to search for C/C++ libraries at link time
53 libraries : [string]
54 list of library names (not filenames or paths) to link against
55 runtime_library_dirs : [string]
56 list of directories to search for C/C++ libraries at run time
57 (for shared extensions, this is when the extension is loaded)
58 extra_objects : [string]
59 list of extra files to link with (eg. object files not implied
60 by 'sources', static library that must be explicitly specified,
61 binary resource files, etc.)
62 extra_compile_args : [string]
63 any extra platform- and compiler-specific information to use
64 when compiling the source files in 'sources'. For platforms and
65 compilers where "command line" makes sense, this is typically a
66 list of command-line arguments, but for other platforms it could
67 be anything.
68 extra_link_args : [string]
69 any extra platform- and compiler-specific information to use
70 when linking object files together to create the extension (or
71 to create a new static Python interpreter). Similar
72 interpretation as for 'extra_compile_args'.
73 export_symbols : [string]
74 list of symbols to be exported from a shared extension. Not
75 used on all platforms, and not generally necessary for Python
76 extensions, which typically export exactly one symbol: "init" +
77 extension_name.
Anthony Baxtera0240342004-10-14 10:02:08 +000078 swig_opts : [string]
79 any extra options to pass to SWIG if a source file has the .i
80 extension.
Jeremy Hylton09e532b2002-06-12 20:08:56 +000081 depends : [string]
82 list of files that the extension depends on
Gustavo Niemeyer6b016852002-11-05 16:12:02 +000083 language : string
84 extension language (i.e. "c", "c++", "objc"). Will be detected
85 from the source extensions if not provided.
Tarek Ziadé9e47ce42009-03-31 22:27:23 +000086 optional : boolean
87 specifies that a build failure in the extension should not abort the
88 build process, but simply not install the failing extension.
Greg Ward3ca54bc2000-05-31 01:05:35 +000089 """
90
Andrew M. Kuchling6ffdaab2003-01-27 16:30:36 +000091 # When adding arguments to this constructor, be sure to update
92 # setup_keywords in core.py.
Greg Ward3ca54bc2000-05-31 01:05:35 +000093 def __init__ (self, name, sources,
94 include_dirs=None,
95 define_macros=None,
96 undef_macros=None,
97 library_dirs=None,
98 libraries=None,
99 runtime_library_dirs=None,
100 extra_objects=None,
101 extra_compile_args=None,
102 extra_link_args=None,
103 export_symbols=None,
Anthony Baxtera0240342004-10-14 10:02:08 +0000104 swig_opts = None,
Jeremy Hylton09e532b2002-06-12 20:08:56 +0000105 depends=None,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000106 language=None,
Tarek Ziadé9e47ce42009-03-31 22:27:23 +0000107 optional=None,
Andrew M. Kuchling3f1c9a92002-11-13 20:54:21 +0000108 **kw # To catch unknown keywords
Greg Ward3ca54bc2000-05-31 01:05:35 +0000109 ):
Greg Ward3ca54bc2000-05-31 01:05:35 +0000110 assert type(name) is StringType, "'name' must be a string"
111 assert (type(sources) is ListType and
Greg Ward3ca54bc2000-05-31 01:05:35 +0000112 map(type, sources) == [StringType]*len(sources)), \
Greg Ward41ed12f2000-09-17 00:45:18 +0000113 "'sources' must be a list of strings"
Greg Ward3ca54bc2000-05-31 01:05:35 +0000114
115 self.name = name
116 self.sources = sources
117 self.include_dirs = include_dirs or []
118 self.define_macros = define_macros or []
119 self.undef_macros = undef_macros or []
120 self.library_dirs = library_dirs or []
121 self.libraries = libraries or []
122 self.runtime_library_dirs = runtime_library_dirs or []
123 self.extra_objects = extra_objects or []
124 self.extra_compile_args = extra_compile_args or []
125 self.extra_link_args = extra_link_args or []
Greg Ward1f6a0d42000-08-13 00:41:40 +0000126 self.export_symbols = export_symbols or []
Anthony Baxtera0240342004-10-14 10:02:08 +0000127 self.swig_opts = swig_opts or []
Jeremy Hylton09e532b2002-06-12 20:08:56 +0000128 self.depends = depends or []
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000129 self.language = language
Tarek Ziadé9e47ce42009-03-31 22:27:23 +0000130 self.optional = optional
Greg Ward3ca54bc2000-05-31 01:05:35 +0000131
Andrew M. Kuchling3f1c9a92002-11-13 20:54:21 +0000132 # If there are unknown keyword options, warn about them
133 if len(kw):
134 L = kw.keys() ; L.sort()
135 L = map(repr, L)
136 msg = "Unknown Extension options: " + string.join(L, ', ')
137 if warnings is not None:
138 warnings.warn(msg)
139 else:
140 sys.stderr.write(msg + '\n')
Greg Ward3ca54bc2000-05-31 01:05:35 +0000141# class Extension
Greg Ward41ed12f2000-09-17 00:45:18 +0000142
143
Tarek Ziadéc6709972009-06-03 10:26:26 +0000144def read_setup_file(filename):
145 """Reads a Setup file and returns Extension instances."""
146 from distutils.sysconfig import (parse_makefile, expand_makefile_vars,
147 _variable_rx)
148
Greg Ward41ed12f2000-09-17 00:45:18 +0000149 from distutils.text_file import TextFile
150 from distutils.util import split_quoted
151
152 # First pass over the file to gather "VAR = VALUE" assignments.
153 vars = parse_makefile(filename)
154
155 # Second pass to gobble up the real content: lines of the form
156 # <module> ... [<sourcefile> ...] [<cpparg> ...] [<library> ...]
157 file = TextFile(filename,
158 strip_comments=1, skip_blanks=1, join_lines=1,
159 lstrip_ws=1, rstrip_ws=1)
160 extensions = []
161
162 while 1:
163 line = file.readline()
164 if line is None: # eof
165 break
166 if _variable_rx.match(line): # VAR=VALUE, handled in first pass
167 continue
168
169 if line[0] == line[-1] == "*":
170 file.warn("'%s' lines not handled yet" % line)
171 continue
172
Greg Ward41ed12f2000-09-17 00:45:18 +0000173 line = expand_makefile_vars(line, vars)
174 words = split_quoted(line)
Greg Ward41ed12f2000-09-17 00:45:18 +0000175
176 # NB. this parses a slightly different syntax than the old
177 # makesetup script: here, there must be exactly one extension per
178 # line, and it must be the first word of the line. I have no idea
179 # why the old syntax supported multiple extensions per line, as
180 # they all wind up being the same.
181
182 module = words[0]
183 ext = Extension(module, [])
184 append_next_word = None
185
186 for word in words[1:]:
187 if append_next_word is not None:
188 append_next_word.append(word)
189 append_next_word = None
190 continue
191
192 suffix = os.path.splitext(word)[1]
193 switch = word[0:2] ; value = word[2:]
194
Andrew M. Kuchling3d2d9802001-12-21 15:34:17 +0000195 if suffix in (".c", ".cc", ".cpp", ".cxx", ".c++", ".m", ".mm"):
Greg Ward41ed12f2000-09-17 00:45:18 +0000196 # hmm, should we do something about C vs. C++ sources?
197 # or leave it up to the CCompiler implementation to
198 # worry about?
199 ext.sources.append(word)
200 elif switch == "-I":
201 ext.include_dirs.append(value)
202 elif switch == "-D":
Andrew M. Kuchlingac20f772001-03-22 03:48:31 +0000203 equals = string.find(value, "=")
Greg Ward41ed12f2000-09-17 00:45:18 +0000204 if equals == -1: # bare "-DFOO" -- no value
205 ext.define_macros.append((value, None))
206 else: # "-DFOO=blah"
207 ext.define_macros.append((value[0:equals],
208 value[equals+2:]))
209 elif switch == "-U":
210 ext.undef_macros.append(value)
211 elif switch == "-C": # only here 'cause makesetup has it!
212 ext.extra_compile_args.append(word)
213 elif switch == "-l":
214 ext.libraries.append(value)
215 elif switch == "-L":
216 ext.library_dirs.append(value)
217 elif switch == "-R":
218 ext.runtime_library_dirs.append(value)
219 elif word == "-rpath":
220 append_next_word = ext.runtime_library_dirs
221 elif word == "-Xlinker":
222 append_next_word = ext.extra_link_args
Andrew M. Kuchlingf4a4fb92002-03-29 18:00:19 +0000223 elif word == "-Xcompiler":
224 append_next_word = ext.extra_compile_args
Greg Ward41ed12f2000-09-17 00:45:18 +0000225 elif switch == "-u":
226 ext.extra_link_args.append(word)
227 if not value:
228 append_next_word = ext.extra_link_args
Andrew M. Kuchling31ddfb62002-11-27 13:45:26 +0000229 elif suffix in (".a", ".so", ".sl", ".o", ".dylib"):
Greg Ward41ed12f2000-09-17 00:45:18 +0000230 # NB. a really faithful emulation of makesetup would
231 # append a .o file to extra_objects only if it
232 # had a slash in it; otherwise, it would s/.o/.c/
233 # and append it to sources. Hmmmm.
234 ext.extra_objects.append(word)
235 else:
236 file.warn("unrecognized argument '%s'" % word)
237
238 extensions.append(ext)
239
Greg Ward41ed12f2000-09-17 00:45:18 +0000240 return extensions