blob: abfa13853093d1b197a23bfbd4a787151a000019 [file] [log] [blame]
Greg Ward28a5f442000-06-06 02:57:07 +00001"""distutils.command.config
2
3Implements the Distutils 'config' command, a (mostly) empty command class
4that exists mainly to be sub-classed by specific module distributions and
5applications. The idea is that while every "config" command is different,
6at least they're all named the same, and users always see "config" in the
7list of standard commands. Also, this is a good place to put common
8configure-like tasks: "try to compile this C code", or "figure out where
9this header file lives".
10"""
11
Greg Ward28a5f442000-06-06 02:57:07 +000012__revision__ = "$Id$"
13
Greg Ward59ac7092000-06-21 03:00:50 +000014import sys, os, string, re
Greg Ward2e38a502000-10-14 03:40:20 +000015from types import *
Greg Ward28a5f442000-06-06 02:57:07 +000016from distutils.core import Command
17from distutils.errors import DistutilsExecError
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000018from distutils import log
Greg Ward28a5f442000-06-06 02:57:07 +000019
20LANG_EXT = {'c': '.c',
21 'c++': '.cxx'}
22
23class config (Command):
24
25 description = "prepare to build"
26
27 user_options = [
28 ('compiler=', None,
29 "specify the compiler type"),
30 ('cc=', None,
31 "specify the compiler executable"),
32 ('include-dirs=', 'I',
33 "list of directories to search for header files"),
34 ('define=', 'D',
35 "C preprocessor macros to define"),
36 ('undef=', 'U',
37 "C preprocessor macros to undefine"),
38 ('libraries=', 'l',
39 "external C libraries to link with"),
40 ('library-dirs=', 'L',
41 "directories to search for external C libraries"),
Greg Ward59ac7092000-06-21 03:00:50 +000042
43 ('noisy', None,
44 "show every action (compile, link, run, ...) taken"),
45 ('dump-source', None,
46 "dump generated source files before attempting to compile them"),
Greg Ward28a5f442000-06-06 02:57:07 +000047 ]
48
49
50 # The three standard command methods: since the "config" command
51 # does nothing by default, these are empty.
52
53 def initialize_options (self):
54 self.compiler = None
55 self.cc = None
56 self.include_dirs = None
57 #self.define = None
58 #self.undef = None
59 self.libraries = None
60 self.library_dirs = None
61
Greg Ward59ac7092000-06-21 03:00:50 +000062 # maximal output for now
63 self.noisy = 1
64 self.dump_source = 1
65
66 # list of temporary files generated along-the-way that we have
67 # to clean at some point
68 self.temp_files = []
69
Greg Ward28a5f442000-06-06 02:57:07 +000070 def finalize_options (self):
Greg Ward2e38a502000-10-14 03:40:20 +000071 if self.include_dirs is None:
72 self.include_dirs = self.distribution.include_dirs or []
73 elif type(self.include_dirs) is StringType:
74 self.include_dirs = string.split(self.include_dirs, os.pathsep)
75
76 if self.libraries is None:
77 self.libraries = []
78 elif type(self.libraries) is StringType:
79 self.libraries = [self.libraries]
80
81 if self.library_dirs is None:
82 self.library_dirs = []
83 elif type(self.library_dirs) is StringType:
84 self.library_dirs = string.split(self.library_dirs, os.pathsep)
85
Greg Ward28a5f442000-06-06 02:57:07 +000086
87 def run (self):
88 pass
89
90
91 # Utility methods for actual "config" commands. The interfaces are
92 # loosely based on Autoconf macros of similar names. Sub-classes
93 # may use these freely.
94
95 def _check_compiler (self):
96 """Check that 'self.compiler' really is a CCompiler object;
97 if not, make it one.
98 """
99 # We do this late, and only on-demand, because this is an expensive
100 # import.
101 from distutils.ccompiler import CCompiler, new_compiler
102 if not isinstance(self.compiler, CCompiler):
Greg Wardcb1f4c42000-09-30 18:27:54 +0000103 self.compiler = new_compiler(compiler=self.compiler,
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000104 dry_run=self.dry_run, force=1)
Greg Ward28a5f442000-06-06 02:57:07 +0000105 if self.include_dirs:
106 self.compiler.set_include_dirs(self.include_dirs)
107 if self.libraries:
108 self.compiler.set_libraries(self.libraries)
109 if self.library_dirs:
110 self.compiler.set_library_dirs(self.library_dirs)
111
112
Greg Ward59ac7092000-06-21 03:00:50 +0000113 def _gen_temp_sourcefile (self, body, headers, lang):
Greg Ward28a5f442000-06-06 02:57:07 +0000114 filename = "_configtest" + LANG_EXT[lang]
115 file = open(filename, "w")
Greg Ward59ac7092000-06-21 03:00:50 +0000116 if headers:
117 for header in headers:
118 file.write("#include <%s>\n" % header)
119 file.write("\n")
Greg Ward28a5f442000-06-06 02:57:07 +0000120 file.write(body)
Greg Ward59ac7092000-06-21 03:00:50 +0000121 if body[-1] != "\n":
122 file.write("\n")
Greg Ward28a5f442000-06-06 02:57:07 +0000123 file.close()
124 return filename
125
Greg Ward855dab92000-06-27 01:21:22 +0000126 def _preprocess (self, body, headers, include_dirs, lang):
Greg Ward59ac7092000-06-21 03:00:50 +0000127 src = self._gen_temp_sourcefile(body, headers, lang)
128 out = "_configtest.i"
129 self.temp_files.extend([src, out])
Greg Ward855dab92000-06-27 01:21:22 +0000130 self.compiler.preprocess(src, out, include_dirs=include_dirs)
Greg Ward59ac7092000-06-21 03:00:50 +0000131 return (src, out)
132
Greg Ward855dab92000-06-27 01:21:22 +0000133 def _compile (self, body, headers, include_dirs, lang):
Greg Ward59ac7092000-06-21 03:00:50 +0000134 src = self._gen_temp_sourcefile(body, headers, lang)
135 if self.dump_source:
136 dump_file(src, "compiling '%s':" % src)
137 (obj,) = self.compiler.object_filenames([src])
138 self.temp_files.extend([src, obj])
Greg Ward855dab92000-06-27 01:21:22 +0000139 self.compiler.compile([src], include_dirs=include_dirs)
Greg Ward28a5f442000-06-06 02:57:07 +0000140 return (src, obj)
141
Greg Ward855dab92000-06-27 01:21:22 +0000142 def _link (self, body,
143 headers, include_dirs,
144 libraries, library_dirs, lang):
145 (src, obj) = self._compile(body, headers, include_dirs, lang)
Fred Drake21d45352001-12-06 21:01:19 +0000146 prog = os.path.splitext(os.path.basename(src))[0]
Greg Ward59ac7092000-06-21 03:00:50 +0000147 self.compiler.link_executable([obj], prog,
148 libraries=libraries,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000149 library_dirs=library_dirs,
150 target_lang=lang)
Andrew M. Kuchlingb1d60292001-08-16 14:08:02 +0000151
152 prog = prog + self.compiler.exe_extension
153 self.temp_files.append(prog)
154
Greg Ward59ac7092000-06-21 03:00:50 +0000155 return (src, obj, prog)
Greg Ward28a5f442000-06-06 02:57:07 +0000156
157 def _clean (self, *filenames):
Greg Ward59ac7092000-06-21 03:00:50 +0000158 if not filenames:
159 filenames = self.temp_files
160 self.temp_files = []
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000161 log.info("removing: %s", string.join(filenames))
Greg Ward28a5f442000-06-06 02:57:07 +0000162 for filename in filenames:
163 try:
164 os.remove(filename)
165 except OSError:
166 pass
167
168
Greg Ward28a5f442000-06-06 02:57:07 +0000169 # 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.
176
Greg Ward59ac7092000-06-21 03:00:50 +0000177 # XXX need access to the header search path and maybe default macros.
178
Greg Ward855dab92000-06-27 01:21:22 +0000179 def try_cpp (self, body=None, headers=None, include_dirs=None, lang="c"):
Greg Ward59ac7092000-06-21 03:00:50 +0000180 """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.)
185 """
186 from distutils.ccompiler import CompileError
187 self._check_compiler()
188 ok = 1
189 try:
Andrew M. Kuchling6fb8d3a2001-08-16 13:56:40 +0000190 self._preprocess(body, headers, include_dirs, lang)
Greg Ward59ac7092000-06-21 03:00:50 +0000191 except CompileError:
192 ok = 0
193
194 self._clean()
195 return ok
196
Greg Ward855dab92000-06-27 01:21:22 +0000197 def search_cpp (self, pattern, body=None,
198 headers=None, include_dirs=None, lang="c"):
Greg Ward59ac7092000-06-21 03:00:50 +0000199 """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.
205 """
206
207 self._check_compiler()
Andrew M. Kuchling6fb8d3a2001-08-16 13:56:40 +0000208 (src, out) = self._preprocess(body, headers, include_dirs, lang)
Greg Ward59ac7092000-06-21 03:00:50 +0000209
210 if type(pattern) is StringType:
211 pattern = re.compile(pattern)
212
213 file = open(out)
214 match = 0
215 while 1:
216 line = file.readline()
217 if line == '':
218 break
Andrew M. Kuchling6fb8d3a2001-08-16 13:56:40 +0000219 if pattern.search(line):
Greg Ward59ac7092000-06-21 03:00:50 +0000220 match = 1
221 break
222
223 file.close()
224 self._clean()
225 return match
226
Greg Ward855dab92000-06-27 01:21:22 +0000227 def try_compile (self, body, headers=None, include_dirs=None, lang="c"):
Greg Ward59ac7092000-06-21 03:00:50 +0000228 """Try to compile a source file built from 'body' and 'headers'.
229 Return true on success, false otherwise.
Greg Ward28a5f442000-06-06 02:57:07 +0000230 """
231 from distutils.ccompiler import CompileError
232 self._check_compiler()
233 try:
Andrew M. Kuchling6fb8d3a2001-08-16 13:56:40 +0000234 self._compile(body, headers, include_dirs, lang)
Greg Ward28a5f442000-06-06 02:57:07 +0000235 ok = 1
236 except CompileError:
237 ok = 0
238
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000239 log.info(ok and "success!" or "failure.")
Greg Ward59ac7092000-06-21 03:00:50 +0000240 self._clean()
Greg Ward28a5f442000-06-06 02:57:07 +0000241 return ok
242
Greg Ward855dab92000-06-27 01:21:22 +0000243 def try_link (self, body,
244 headers=None, include_dirs=None,
Greg Ward59ac7092000-06-21 03:00:50 +0000245 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
Greg Ward28a5f442000-06-06 02:57:07 +0000249 otherwise.
250 """
251 from distutils.ccompiler import CompileError, LinkError
252 self._check_compiler()
253 try:
Greg Ward855dab92000-06-27 01:21:22 +0000254 self._link(body, headers, include_dirs,
255 libraries, library_dirs, lang)
Greg Ward59ac7092000-06-21 03:00:50 +0000256 ok = 1
257 except (CompileError, LinkError):
258 ok = 0
259
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000260 log.info(ok and "success!" or "failure.")
Greg Ward59ac7092000-06-21 03:00:50 +0000261 self._clean()
262 return ok
Fred Drake21d45352001-12-06 21:01:19 +0000263
Greg Ward855dab92000-06-27 01:21:22 +0000264 def try_run (self, body,
265 headers=None, include_dirs=None,
Greg Ward59ac7092000-06-21 03:00:50 +0000266 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.
271 """
272 from distutils.ccompiler import CompileError, LinkError
273 self._check_compiler()
274 try:
Andrew M. Kuchling246c4252001-08-13 13:56:24 +0000275 src, obj, exe = self._link(body, headers, include_dirs,
276 libraries, library_dirs, lang)
Greg Ward28a5f442000-06-06 02:57:07 +0000277 self.spawn([exe])
278 ok = 1
279 except (CompileError, LinkError, DistutilsExecError):
280 ok = 0
281
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000282 log.info(ok and "success!" or "failure.")
Greg Ward59ac7092000-06-21 03:00:50 +0000283 self._clean()
Greg Ward28a5f442000-06-06 02:57:07 +0000284 return ok
285
Greg Ward59ac7092000-06-21 03:00:50 +0000286
287 # -- High-level methods --------------------------------------------
288 # (these are the ones that are actually likely to be useful
289 # when implementing a real-world config command!)
290
Greg Ward855dab92000-06-27 01:21:22 +0000291 def check_func (self, func,
292 headers=None, include_dirs=None,
Greg Ward59ac7092000-06-21 03:00:50 +0000293 libraries=None, library_dirs=None,
294 decl=0, call=0):
295
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.
299
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.
308 """
309
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"
321
Greg Ward855dab92000-06-27 01:21:22 +0000322 return self.try_link(body, headers, include_dirs,
323 libraries, library_dirs)
Greg Ward59ac7092000-06-21 03:00:50 +0000324
325 # check_func ()
326
Greg Ward855dab92000-06-27 01:21:22 +0000327 def check_lib (self, library, library_dirs=None,
Greg Ward4cd6f2a2000-10-14 03:56:42 +0000328 headers=None, include_dirs=None, other_libraries=[]):
Greg Ward855dab92000-06-27 01:21:22 +0000329 """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
Greg Ward4cd6f2a2000-10-14 03:56:42 +0000333 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.
Greg Ward855dab92000-06-27 01:21:22 +0000336 """
337 self._check_compiler()
338 return self.try_link("int main (void) { }",
Greg Ward4cd6f2a2000-10-14 03:56:42 +0000339 headers, include_dirs,
340 [library]+other_libraries, library_dirs)
Greg Ward855dab92000-06-27 01:21:22 +0000341
342 def check_header (self, header, include_dirs=None,
343 library_dirs=None, lang="c"):
Greg Ward59ac7092000-06-21 03:00:50 +0000344 """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.
347 """
Andrew M. Kuchling4013cbd2002-09-09 12:10:00 +0000348 return self.try_cpp(body="/* No body */", headers=[header],
349 include_dirs=include_dirs)
Greg Ward59ac7092000-06-21 03:00:50 +0000350
351
Greg Ward28a5f442000-06-06 02:57:07 +0000352# class config
Greg Ward59ac7092000-06-21 03:00:50 +0000353
354
355def dump_file (filename, head=None):
356 if head is None:
357 print filename + ":"
358 else:
359 print head
360
361 file = open(filename)
362 sys.stdout.write(file.read())
363 file.close()