Merged release21-maint changes.
[python/dscho.git] / Lib / distutils / command / config.py
blobce6cff8cef054ab4a7577fce93b4bcc07f0c83e2
1 """distutils.command.config
3 Implements the Distutils 'config' command, a (mostly) empty command class
4 that exists mainly to be sub-classed by specific module distributions and
5 applications. The idea is that while every "config" command is different,
6 at least they're all named the same, and users always see "config" in the
7 list of standard commands. Also, this is a good place to put common
8 configure-like tasks: "try to compile this C code", or "figure out where
9 this header file lives".
10 """
12 # created 2000/05/29, Greg Ward
14 __revision__ = "$Id$"
16 import sys, os, string, re
17 from types import *
18 from distutils.core import Command
19 from distutils.errors import DistutilsExecError
22 LANG_EXT = {'c': '.c',
23 'c++': '.cxx'}
25 class config (Command):
27 description = "prepare to build"
29 user_options = [
30 ('compiler=', None,
31 "specify the compiler type"),
32 ('cc=', None,
33 "specify the compiler executable"),
34 ('include-dirs=', 'I',
35 "list of directories to search for header files"),
36 ('define=', 'D',
37 "C preprocessor macros to define"),
38 ('undef=', 'U',
39 "C preprocessor macros to undefine"),
40 ('libraries=', 'l',
41 "external C libraries to link with"),
42 ('library-dirs=', 'L',
43 "directories to search for external C libraries"),
45 ('noisy', None,
46 "show every action (compile, link, run, ...) taken"),
47 ('dump-source', None,
48 "dump generated source files before attempting to compile them"),
52 # The three standard command methods: since the "config" command
53 # does nothing by default, these are empty.
55 def initialize_options (self):
56 self.compiler = None
57 self.cc = None
58 self.include_dirs = None
59 #self.define = None
60 #self.undef = None
61 self.libraries = None
62 self.library_dirs = None
64 # maximal output for now
65 self.noisy = 1
66 self.dump_source = 1
68 # list of temporary files generated along-the-way that we have
69 # to clean at some point
70 self.temp_files = []
72 def finalize_options (self):
73 if self.include_dirs is None:
74 self.include_dirs = self.distribution.include_dirs or []
75 elif type(self.include_dirs) is StringType:
76 self.include_dirs = string.split(self.include_dirs, os.pathsep)
78 if self.libraries is None:
79 self.libraries = []
80 elif type(self.libraries) is StringType:
81 self.libraries = [self.libraries]
83 if self.library_dirs is None:
84 self.library_dirs = []
85 elif type(self.library_dirs) is StringType:
86 self.library_dirs = string.split(self.library_dirs, os.pathsep)
89 def run (self):
90 pass
93 # Utility methods for actual "config" commands. The interfaces are
94 # loosely based on Autoconf macros of similar names. Sub-classes
95 # may use these freely.
97 def _check_compiler (self):
98 """Check that 'self.compiler' really is a CCompiler object;
99 if not, make it one.
101 # We do this late, and only on-demand, because this is an expensive
102 # import.
103 from distutils.ccompiler import CCompiler, new_compiler
104 if not isinstance(self.compiler, CCompiler):
105 self.compiler = new_compiler(compiler=self.compiler,
106 verbose=self.noisy,
107 dry_run=self.dry_run,
108 force=1)
109 if self.include_dirs:
110 self.compiler.set_include_dirs(self.include_dirs)
111 if self.libraries:
112 self.compiler.set_libraries(self.libraries)
113 if self.library_dirs:
114 self.compiler.set_library_dirs(self.library_dirs)
117 def _gen_temp_sourcefile (self, body, headers, lang):
118 filename = "_configtest" + LANG_EXT[lang]
119 file = open(filename, "w")
120 if headers:
121 for header in headers:
122 file.write("#include <%s>\n" % header)
123 file.write("\n")
124 file.write(body)
125 if body[-1] != "\n":
126 file.write("\n")
127 file.close()
128 return filename
130 def _preprocess (self, body, headers, include_dirs, lang):
131 src = self._gen_temp_sourcefile(body, headers, lang)
132 out = "_configtest.i"
133 self.temp_files.extend([src, out])
134 self.compiler.preprocess(src, out, include_dirs=include_dirs)
135 return (src, out)
137 def _compile (self, body, headers, include_dirs, lang):
138 src = self._gen_temp_sourcefile(body, headers, lang)
139 if self.dump_source:
140 dump_file(src, "compiling '%s':" % src)
141 (obj,) = self.compiler.object_filenames([src])
142 self.temp_files.extend([src, obj])
143 self.compiler.compile([src], include_dirs=include_dirs)
144 return (src, obj)
146 def _link (self, body,
147 headers, include_dirs,
148 libraries, library_dirs, lang):
149 (src, obj) = self._compile(body, headers, include_dirs, lang)
150 prog = os.path.splitext(os.path.basename(src))[0]
151 self.temp_files.append(prog) # XXX should be prog + exe_ext
152 self.compiler.link_executable([obj], prog,
153 libraries=libraries,
154 library_dirs=library_dirs)
155 return (src, obj, prog)
157 def _clean (self, *filenames):
158 if not filenames:
159 filenames = self.temp_files
160 self.temp_files = []
161 self.announce("removing: " + string.join(filenames))
162 for filename in filenames:
163 try:
164 os.remove(filename)
165 except OSError:
166 pass
169 # XXX these ignore the dry-run flag: what to do, what to do? even if
170 # you want a dry-run build, you still need some sort of configuration
171 # info. My inclination is to make it up to the real config command to
172 # consult 'dry_run', and assume a default (minimal) configuration if
173 # true. The problem with trying to do it here is that you'd have to
174 # return either true or false from all the 'try' methods, neither of
175 # which is correct.
177 # XXX need access to the header search path and maybe default macros.
179 def try_cpp (self, body=None, headers=None, include_dirs=None, lang="c"):
180 """Construct a source file from 'body' (a string containing lines
181 of C/C++ code) and 'headers' (a list of header files to include)
182 and run it through the preprocessor. Return true if the
183 preprocessor succeeded, false if there were any errors.
184 ('body' probably isn't of much use, but what the heck.)
186 from distutils.ccompiler import CompileError
187 self._check_compiler()
188 ok = 1
189 try:
190 self._preprocess(body, headers, lang)
191 except CompileError:
192 ok = 0
194 self._clean()
195 return ok
197 def search_cpp (self, pattern, body=None,
198 headers=None, include_dirs=None, lang="c"):
199 """Construct a source file (just like 'try_cpp()'), run it through
200 the preprocessor, and return true if any line of the output matches
201 'pattern'. 'pattern' should either be a compiled regex object or a
202 string containing a regex. If both 'body' and 'headers' are None,
203 preprocesses an empty file -- which can be useful to determine the
204 symbols the preprocessor and compiler set by default.
207 self._check_compiler()
208 (src, out) = self._preprocess(body, headers, lang)
210 if type(pattern) is StringType:
211 pattern = re.compile(pattern)
213 file = open(out)
214 match = 0
215 while 1:
216 line = file.readline()
217 if line == '':
218 break
219 if pattern.search(pattern):
220 match = 1
221 break
223 file.close()
224 self._clean()
225 return match
227 def try_compile (self, body, headers=None, include_dirs=None, lang="c"):
228 """Try to compile a source file built from 'body' and 'headers'.
229 Return true on success, false otherwise.
231 from distutils.ccompiler import CompileError
232 self._check_compiler()
233 try:
234 self._compile(body, headers, lang)
235 ok = 1
236 except CompileError:
237 ok = 0
239 self.announce(ok and "success!" or "failure.")
240 self._clean()
241 return ok
243 def try_link (self, body,
244 headers=None, include_dirs=None,
245 libraries=None, library_dirs=None,
246 lang="c"):
247 """Try to compile and link a source file, built from 'body' and
248 'headers', to executable form. Return true on success, false
249 otherwise.
251 from distutils.ccompiler import CompileError, LinkError
252 self._check_compiler()
253 try:
254 self._link(body, headers, include_dirs,
255 libraries, library_dirs, lang)
256 ok = 1
257 except (CompileError, LinkError):
258 ok = 0
260 self.announce(ok and "success!" or "failure.")
261 self._clean()
262 return ok
264 def try_run (self, body,
265 headers=None, include_dirs=None,
266 libraries=None, library_dirs=None,
267 lang="c"):
268 """Try to compile, link to an executable, and run a program
269 built from 'body' and 'headers'. Return true on success, false
270 otherwise.
272 from distutils.ccompiler import CompileError, LinkError
273 self._check_compiler()
274 try:
275 self._link(body, headers, include_dirs,
276 libraries, library_dirs, lang)
277 self.spawn([exe])
278 ok = 1
279 except (CompileError, LinkError, DistutilsExecError):
280 ok = 0
282 self.announce(ok and "success!" or "failure.")
283 self._clean()
284 return ok
287 # -- High-level methods --------------------------------------------
288 # (these are the ones that are actually likely to be useful
289 # when implementing a real-world config command!)
291 def check_func (self, func,
292 headers=None, include_dirs=None,
293 libraries=None, library_dirs=None,
294 decl=0, call=0):
296 """Determine if function 'func' is available by constructing a
297 source file that refers to 'func', and compiles and links it.
298 If everything succeeds, returns true; otherwise returns false.
300 The constructed source file starts out by including the header
301 files listed in 'headers'. If 'decl' is true, it then declares
302 'func' (as "int func()"); you probably shouldn't supply 'headers'
303 and set 'decl' true in the same call, or you might get errors about
304 a conflicting declarations for 'func'. Finally, the constructed
305 'main()' function either references 'func' or (if 'call' is true)
306 calls it. 'libraries' and 'library_dirs' are used when
307 linking.
310 self._check_compiler()
311 body = []
312 if decl:
313 body.append("int %s ();" % func)
314 body.append("int main () {")
315 if call:
316 body.append(" %s();" % func)
317 else:
318 body.append(" %s;" % func)
319 body.append("}")
320 body = string.join(body, "\n") + "\n"
322 return self.try_link(body, headers, include_dirs,
323 libraries, library_dirs)
325 # check_func ()
327 def check_lib (self, library, library_dirs=None,
328 headers=None, include_dirs=None, other_libraries=[]):
329 """Determine if 'library' is available to be linked against,
330 without actually checking that any particular symbols are provided
331 by it. 'headers' will be used in constructing the source file to
332 be compiled, but the only effect of this is to check if all the
333 header files listed are available. Any libraries listed in
334 'other_libraries' will be included in the link, in case 'library'
335 has symbols that depend on other libraries.
337 self._check_compiler()
338 return self.try_link("int main (void) { }",
339 headers, include_dirs,
340 [library]+other_libraries, library_dirs)
342 def check_header (self, header, include_dirs=None,
343 library_dirs=None, lang="c"):
344 """Determine if the system header file named by 'header_file'
345 exists and can be found by the preprocessor; return true if so,
346 false otherwise.
348 return self.try_cpp(headers=[header], include_dirs=include_dirs)
351 # class config
354 def dump_file (filename, head=None):
355 if head is None:
356 print filename + ":"
357 else:
358 print head
360 file = open(filename)
361 sys.stdout.write(file.read())
362 file.close()