blob: 7fbeb4e1f14e72fb151f02aa4217099020ac38a8 [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
6# created 2000/05/30, Greg Ward
7
8__revision__ = "$Id$"
9
Andrew M. Kuchlingac20f772001-03-22 03:48:31 +000010import os, string
Greg Ward3ca54bc2000-05-31 01:05:35 +000011from types import *
12
13
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).
Fred Drakeb94b8492001-12-06 20:51:35 +000019#
Greg Ward3ca54bc2000-05-31 01:05:35 +000020# 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.
23
24class Extension:
25 """Just a collection of attributes that describes an extension
26 module and everything needed to build it (hopefully in a portable
Greg Ward45b87bc2000-08-13 00:38:58 +000027 way, but there are hooks that let you be as unportable as you need).
Greg Ward3ca54bc2000-05-31 01:05:35 +000028
29 Instance attributes:
30 name : string
31 the full name of the extension, including any packages -- ie.
32 *not* a filename or pathname, but Python dotted name
33 sources : [string]
Greg Wardcb185572000-06-24 00:18:24 +000034 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.
Greg Ward3ca54bc2000-05-31 01:05:35 +000039 include_dirs : [string]
40 list of directories to search for C/C++ header files (in Unix
41 form for portability)
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
51 libraries : [string]
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
65 be anything.
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" +
75 extension_name.
Jeremy Hylton09e532b2002-06-12 20:08:56 +000076 depends : [string]
77 list of files that the extension depends on
Gustavo Niemeyer6b016852002-11-05 16:12:02 +000078 language : string
79 extension language (i.e. "c", "c++", "objc"). Will be detected
80 from the source extensions if not provided.
Greg Ward3ca54bc2000-05-31 01:05:35 +000081 """
82
83 def __init__ (self, name, sources,
84 include_dirs=None,
85 define_macros=None,
86 undef_macros=None,
87 library_dirs=None,
88 libraries=None,
89 runtime_library_dirs=None,
90 extra_objects=None,
91 extra_compile_args=None,
92 extra_link_args=None,
93 export_symbols=None,
Jeremy Hylton09e532b2002-06-12 20:08:56 +000094 depends=None,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +000095 language=None,
Greg Ward3ca54bc2000-05-31 01:05:35 +000096 ):
97
98 assert type(name) is StringType, "'name' must be a string"
99 assert (type(sources) is ListType and
Greg Ward3ca54bc2000-05-31 01:05:35 +0000100 map(type, sources) == [StringType]*len(sources)), \
Greg Ward41ed12f2000-09-17 00:45:18 +0000101 "'sources' must be a list of strings"
Greg Ward3ca54bc2000-05-31 01:05:35 +0000102
103 self.name = name
104 self.sources = sources
105 self.include_dirs = include_dirs or []
106 self.define_macros = define_macros or []
107 self.undef_macros = undef_macros or []
108 self.library_dirs = library_dirs or []
109 self.libraries = libraries or []
110 self.runtime_library_dirs = runtime_library_dirs or []
111 self.extra_objects = extra_objects or []
112 self.extra_compile_args = extra_compile_args or []
113 self.extra_link_args = extra_link_args or []
Greg Ward1f6a0d42000-08-13 00:41:40 +0000114 self.export_symbols = export_symbols or []
Jeremy Hylton09e532b2002-06-12 20:08:56 +0000115 self.depends = depends or []
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000116 self.language = language
Greg Ward3ca54bc2000-05-31 01:05:35 +0000117
118# class Extension
Greg Ward41ed12f2000-09-17 00:45:18 +0000119
120
121def read_setup_file (filename):
122 from distutils.sysconfig import \
123 parse_makefile, expand_makefile_vars, _variable_rx
124 from distutils.text_file import TextFile
125 from distutils.util import split_quoted
126
127 # First pass over the file to gather "VAR = VALUE" assignments.
128 vars = parse_makefile(filename)
129
130 # Second pass to gobble up the real content: lines of the form
131 # <module> ... [<sourcefile> ...] [<cpparg> ...] [<library> ...]
132 file = TextFile(filename,
133 strip_comments=1, skip_blanks=1, join_lines=1,
134 lstrip_ws=1, rstrip_ws=1)
135 extensions = []
136
137 while 1:
138 line = file.readline()
139 if line is None: # eof
140 break
141 if _variable_rx.match(line): # VAR=VALUE, handled in first pass
142 continue
143
144 if line[0] == line[-1] == "*":
145 file.warn("'%s' lines not handled yet" % line)
146 continue
147
148 #print "original line: " + line
149 line = expand_makefile_vars(line, vars)
150 words = split_quoted(line)
151 #print "expanded line: " + line
152
153 # NB. this parses a slightly different syntax than the old
154 # makesetup script: here, there must be exactly one extension per
155 # line, and it must be the first word of the line. I have no idea
156 # why the old syntax supported multiple extensions per line, as
157 # they all wind up being the same.
158
159 module = words[0]
160 ext = Extension(module, [])
161 append_next_word = None
162
163 for word in words[1:]:
164 if append_next_word is not None:
165 append_next_word.append(word)
166 append_next_word = None
167 continue
168
169 suffix = os.path.splitext(word)[1]
170 switch = word[0:2] ; value = word[2:]
171
Andrew M. Kuchling3d2d9802001-12-21 15:34:17 +0000172 if suffix in (".c", ".cc", ".cpp", ".cxx", ".c++", ".m", ".mm"):
Greg Ward41ed12f2000-09-17 00:45:18 +0000173 # hmm, should we do something about C vs. C++ sources?
174 # or leave it up to the CCompiler implementation to
175 # worry about?
176 ext.sources.append(word)
177 elif switch == "-I":
178 ext.include_dirs.append(value)
179 elif switch == "-D":
Andrew M. Kuchlingac20f772001-03-22 03:48:31 +0000180 equals = string.find(value, "=")
Greg Ward41ed12f2000-09-17 00:45:18 +0000181 if equals == -1: # bare "-DFOO" -- no value
182 ext.define_macros.append((value, None))
183 else: # "-DFOO=blah"
184 ext.define_macros.append((value[0:equals],
185 value[equals+2:]))
186 elif switch == "-U":
187 ext.undef_macros.append(value)
188 elif switch == "-C": # only here 'cause makesetup has it!
189 ext.extra_compile_args.append(word)
190 elif switch == "-l":
191 ext.libraries.append(value)
192 elif switch == "-L":
193 ext.library_dirs.append(value)
194 elif switch == "-R":
195 ext.runtime_library_dirs.append(value)
196 elif word == "-rpath":
197 append_next_word = ext.runtime_library_dirs
198 elif word == "-Xlinker":
199 append_next_word = ext.extra_link_args
Andrew M. Kuchlingf4a4fb92002-03-29 18:00:19 +0000200 elif word == "-Xcompiler":
201 append_next_word = ext.extra_compile_args
Greg Ward41ed12f2000-09-17 00:45:18 +0000202 elif switch == "-u":
203 ext.extra_link_args.append(word)
204 if not value:
205 append_next_word = ext.extra_link_args
206 elif suffix in (".a", ".so", ".sl", ".o"):
207 # NB. a really faithful emulation of makesetup would
208 # append a .o file to extra_objects only if it
209 # had a slash in it; otherwise, it would s/.o/.c/
210 # and append it to sources. Hmmmm.
211 ext.extra_objects.append(word)
212 else:
213 file.warn("unrecognized argument '%s'" % word)
214
215 extensions.append(ext)
216
217 #print "module:", module
218 #print "source files:", source_files
219 #print "cpp args:", cpp_args
220 #print "lib args:", library_args
221
222 #extensions[module] = { 'sources': source_files,
223 # 'cpp_args': cpp_args,
224 # 'lib_args': library_args }
Fred Drakeb94b8492001-12-06 20:51:35 +0000225
Greg Ward41ed12f2000-09-17 00:45:18 +0000226 return extensions
227
228# read_setup_file ()