blob: d73bb08e00b82f179a96676c40cc33c517ccc137 [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
Greg Ward3ca54bc2000-05-31 01:05:35 +000078 """
79
80 def __init__ (self, name, sources,
81 include_dirs=None,
82 define_macros=None,
83 undef_macros=None,
84 library_dirs=None,
85 libraries=None,
86 runtime_library_dirs=None,
87 extra_objects=None,
88 extra_compile_args=None,
89 extra_link_args=None,
90 export_symbols=None,
Jeremy Hylton09e532b2002-06-12 20:08:56 +000091 depends=None,
Greg Ward3ca54bc2000-05-31 01:05:35 +000092 ):
93
94 assert type(name) is StringType, "'name' must be a string"
95 assert (type(sources) is ListType and
Greg Ward3ca54bc2000-05-31 01:05:35 +000096 map(type, sources) == [StringType]*len(sources)), \
Greg Ward41ed12f2000-09-17 00:45:18 +000097 "'sources' must be a list of strings"
Greg Ward3ca54bc2000-05-31 01:05:35 +000098
99 self.name = name
100 self.sources = sources
101 self.include_dirs = include_dirs or []
102 self.define_macros = define_macros or []
103 self.undef_macros = undef_macros or []
104 self.library_dirs = library_dirs or []
105 self.libraries = libraries or []
106 self.runtime_library_dirs = runtime_library_dirs or []
107 self.extra_objects = extra_objects or []
108 self.extra_compile_args = extra_compile_args or []
109 self.extra_link_args = extra_link_args or []
Greg Ward1f6a0d42000-08-13 00:41:40 +0000110 self.export_symbols = export_symbols or []
Jeremy Hylton09e532b2002-06-12 20:08:56 +0000111 self.depends = depends or []
Greg Ward3ca54bc2000-05-31 01:05:35 +0000112
113# class Extension
Greg Ward41ed12f2000-09-17 00:45:18 +0000114
115
116def read_setup_file (filename):
117 from distutils.sysconfig import \
118 parse_makefile, expand_makefile_vars, _variable_rx
119 from distutils.text_file import TextFile
120 from distutils.util import split_quoted
121
122 # First pass over the file to gather "VAR = VALUE" assignments.
123 vars = parse_makefile(filename)
124
125 # Second pass to gobble up the real content: lines of the form
126 # <module> ... [<sourcefile> ...] [<cpparg> ...] [<library> ...]
127 file = TextFile(filename,
128 strip_comments=1, skip_blanks=1, join_lines=1,
129 lstrip_ws=1, rstrip_ws=1)
130 extensions = []
131
132 while 1:
133 line = file.readline()
134 if line is None: # eof
135 break
136 if _variable_rx.match(line): # VAR=VALUE, handled in first pass
137 continue
138
139 if line[0] == line[-1] == "*":
140 file.warn("'%s' lines not handled yet" % line)
141 continue
142
143 #print "original line: " + line
144 line = expand_makefile_vars(line, vars)
145 words = split_quoted(line)
146 #print "expanded line: " + line
147
148 # NB. this parses a slightly different syntax than the old
149 # makesetup script: here, there must be exactly one extension per
150 # line, and it must be the first word of the line. I have no idea
151 # why the old syntax supported multiple extensions per line, as
152 # they all wind up being the same.
153
154 module = words[0]
155 ext = Extension(module, [])
156 append_next_word = None
157
158 for word in words[1:]:
159 if append_next_word is not None:
160 append_next_word.append(word)
161 append_next_word = None
162 continue
163
164 suffix = os.path.splitext(word)[1]
165 switch = word[0:2] ; value = word[2:]
166
Andrew M. Kuchling3d2d9802001-12-21 15:34:17 +0000167 if suffix in (".c", ".cc", ".cpp", ".cxx", ".c++", ".m", ".mm"):
Greg Ward41ed12f2000-09-17 00:45:18 +0000168 # hmm, should we do something about C vs. C++ sources?
169 # or leave it up to the CCompiler implementation to
170 # worry about?
171 ext.sources.append(word)
172 elif switch == "-I":
173 ext.include_dirs.append(value)
174 elif switch == "-D":
Andrew M. Kuchlingac20f772001-03-22 03:48:31 +0000175 equals = string.find(value, "=")
Greg Ward41ed12f2000-09-17 00:45:18 +0000176 if equals == -1: # bare "-DFOO" -- no value
177 ext.define_macros.append((value, None))
178 else: # "-DFOO=blah"
179 ext.define_macros.append((value[0:equals],
180 value[equals+2:]))
181 elif switch == "-U":
182 ext.undef_macros.append(value)
183 elif switch == "-C": # only here 'cause makesetup has it!
184 ext.extra_compile_args.append(word)
185 elif switch == "-l":
186 ext.libraries.append(value)
187 elif switch == "-L":
188 ext.library_dirs.append(value)
189 elif switch == "-R":
190 ext.runtime_library_dirs.append(value)
191 elif word == "-rpath":
192 append_next_word = ext.runtime_library_dirs
193 elif word == "-Xlinker":
194 append_next_word = ext.extra_link_args
Andrew M. Kuchlingf4a4fb92002-03-29 18:00:19 +0000195 elif word == "-Xcompiler":
196 append_next_word = ext.extra_compile_args
Greg Ward41ed12f2000-09-17 00:45:18 +0000197 elif switch == "-u":
198 ext.extra_link_args.append(word)
199 if not value:
200 append_next_word = ext.extra_link_args
201 elif suffix in (".a", ".so", ".sl", ".o"):
202 # NB. a really faithful emulation of makesetup would
203 # append a .o file to extra_objects only if it
204 # had a slash in it; otherwise, it would s/.o/.c/
205 # and append it to sources. Hmmmm.
206 ext.extra_objects.append(word)
207 else:
208 file.warn("unrecognized argument '%s'" % word)
209
210 extensions.append(ext)
211
212 #print "module:", module
213 #print "source files:", source_files
214 #print "cpp args:", cpp_args
215 #print "lib args:", library_args
216
217 #extensions[module] = { 'sources': source_files,
218 # 'cpp_args': cpp_args,
219 # 'lib_args': library_args }
Fred Drakeb94b8492001-12-06 20:51:35 +0000220
Greg Ward41ed12f2000-09-17 00:45:18 +0000221 return extensions
222
223# read_setup_file ()