Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 1 | """distutils.command.config |
| 2 | |
| 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 | """ |
| 11 | |
| 12 | # created 2000/05/29, Greg Ward |
| 13 | |
| 14 | __revision__ = "$Id$" |
| 15 | |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 16 | import sys, os, string, re |
Greg Ward | 2e38a50 | 2000-10-14 03:40:20 +0000 | [diff] [blame] | 17 | from types import * |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 18 | from distutils.core import Command |
| 19 | from distutils.errors import DistutilsExecError |
| 20 | |
| 21 | |
| 22 | LANG_EXT = {'c': '.c', |
| 23 | 'c++': '.cxx'} |
| 24 | |
| 25 | class config (Command): |
| 26 | |
| 27 | description = "prepare to build" |
| 28 | |
| 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"), |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 44 | |
| 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"), |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 49 | ] |
| 50 | |
| 51 | |
| 52 | # The three standard command methods: since the "config" command |
| 53 | # does nothing by default, these are empty. |
| 54 | |
| 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 |
| 63 | |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 64 | # maximal output for now |
| 65 | self.noisy = 1 |
| 66 | self.dump_source = 1 |
| 67 | |
| 68 | # list of temporary files generated along-the-way that we have |
| 69 | # to clean at some point |
| 70 | self.temp_files = [] |
| 71 | |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 72 | def finalize_options (self): |
Greg Ward | 2e38a50 | 2000-10-14 03:40:20 +0000 | [diff] [blame] | 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) |
| 77 | |
| 78 | if self.libraries is None: |
| 79 | self.libraries = [] |
| 80 | elif type(self.libraries) is StringType: |
| 81 | self.libraries = [self.libraries] |
| 82 | |
| 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) |
| 87 | |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 88 | |
| 89 | def run (self): |
| 90 | pass |
| 91 | |
| 92 | |
| 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. |
| 96 | |
| 97 | def _check_compiler (self): |
| 98 | """Check that 'self.compiler' really is a CCompiler object; |
| 99 | if not, make it one. |
| 100 | """ |
| 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): |
Greg Ward | cb1f4c4 | 2000-09-30 18:27:54 +0000 | [diff] [blame] | 105 | self.compiler = new_compiler(compiler=self.compiler, |
| 106 | verbose=self.noisy, |
| 107 | dry_run=self.dry_run, |
| 108 | force=1) |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 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) |
| 115 | |
| 116 | |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 117 | def _gen_temp_sourcefile (self, body, headers, lang): |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 118 | filename = "_configtest" + LANG_EXT[lang] |
| 119 | file = open(filename, "w") |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 120 | if headers: |
| 121 | for header in headers: |
| 122 | file.write("#include <%s>\n" % header) |
| 123 | file.write("\n") |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 124 | file.write(body) |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 125 | if body[-1] != "\n": |
| 126 | file.write("\n") |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 127 | file.close() |
| 128 | return filename |
| 129 | |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 130 | def _preprocess (self, body, headers, include_dirs, lang): |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 131 | src = self._gen_temp_sourcefile(body, headers, lang) |
| 132 | out = "_configtest.i" |
| 133 | self.temp_files.extend([src, out]) |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 134 | self.compiler.preprocess(src, out, include_dirs=include_dirs) |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 135 | return (src, out) |
| 136 | |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 137 | def _compile (self, body, headers, include_dirs, lang): |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 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]) |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 143 | self.compiler.compile([src], include_dirs=include_dirs) |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 144 | return (src, obj) |
| 145 | |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 146 | def _link (self, body, |
| 147 | headers, include_dirs, |
| 148 | libraries, library_dirs, lang): |
| 149 | (src, obj) = self._compile(body, headers, include_dirs, lang) |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 150 | prog = os.path.splitext(os.path.basename(src))[0] |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 151 | self.compiler.link_executable([obj], prog, |
| 152 | libraries=libraries, |
| 153 | library_dirs=library_dirs) |
Andrew M. Kuchling | b1d6029 | 2001-08-16 14:08:02 +0000 | [diff] [blame] | 154 | |
| 155 | prog = prog + self.compiler.exe_extension |
| 156 | self.temp_files.append(prog) |
| 157 | |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 158 | return (src, obj, prog) |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 159 | |
| 160 | def _clean (self, *filenames): |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 161 | if not filenames: |
| 162 | filenames = self.temp_files |
| 163 | self.temp_files = [] |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 164 | self.announce("removing: " + string.join(filenames)) |
| 165 | for filename in filenames: |
| 166 | try: |
| 167 | os.remove(filename) |
| 168 | except OSError: |
| 169 | pass |
| 170 | |
| 171 | |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 172 | # XXX these ignore the dry-run flag: what to do, what to do? even if |
| 173 | # you want a dry-run build, you still need some sort of configuration |
| 174 | # info. My inclination is to make it up to the real config command to |
| 175 | # consult 'dry_run', and assume a default (minimal) configuration if |
| 176 | # true. The problem with trying to do it here is that you'd have to |
| 177 | # return either true or false from all the 'try' methods, neither of |
| 178 | # which is correct. |
| 179 | |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 180 | # XXX need access to the header search path and maybe default macros. |
| 181 | |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 182 | def try_cpp (self, body=None, headers=None, include_dirs=None, lang="c"): |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 183 | """Construct a source file from 'body' (a string containing lines |
| 184 | of C/C++ code) and 'headers' (a list of header files to include) |
| 185 | and run it through the preprocessor. Return true if the |
| 186 | preprocessor succeeded, false if there were any errors. |
| 187 | ('body' probably isn't of much use, but what the heck.) |
| 188 | """ |
| 189 | from distutils.ccompiler import CompileError |
| 190 | self._check_compiler() |
| 191 | ok = 1 |
| 192 | try: |
Andrew M. Kuchling | 6fb8d3a | 2001-08-16 13:56:40 +0000 | [diff] [blame] | 193 | self._preprocess(body, headers, include_dirs, lang) |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 194 | except CompileError: |
| 195 | ok = 0 |
| 196 | |
| 197 | self._clean() |
| 198 | return ok |
| 199 | |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 200 | def search_cpp (self, pattern, body=None, |
| 201 | headers=None, include_dirs=None, lang="c"): |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 202 | """Construct a source file (just like 'try_cpp()'), run it through |
| 203 | the preprocessor, and return true if any line of the output matches |
| 204 | 'pattern'. 'pattern' should either be a compiled regex object or a |
| 205 | string containing a regex. If both 'body' and 'headers' are None, |
| 206 | preprocesses an empty file -- which can be useful to determine the |
| 207 | symbols the preprocessor and compiler set by default. |
| 208 | """ |
| 209 | |
| 210 | self._check_compiler() |
Andrew M. Kuchling | 6fb8d3a | 2001-08-16 13:56:40 +0000 | [diff] [blame] | 211 | (src, out) = self._preprocess(body, headers, include_dirs, lang) |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 212 | |
| 213 | if type(pattern) is StringType: |
| 214 | pattern = re.compile(pattern) |
| 215 | |
| 216 | file = open(out) |
| 217 | match = 0 |
| 218 | while 1: |
| 219 | line = file.readline() |
| 220 | if line == '': |
| 221 | break |
Andrew M. Kuchling | 6fb8d3a | 2001-08-16 13:56:40 +0000 | [diff] [blame] | 222 | if pattern.search(line): |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 223 | match = 1 |
| 224 | break |
| 225 | |
| 226 | file.close() |
| 227 | self._clean() |
| 228 | return match |
| 229 | |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 230 | def try_compile (self, body, headers=None, include_dirs=None, lang="c"): |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 231 | """Try to compile a source file built from 'body' and 'headers'. |
| 232 | Return true on success, false otherwise. |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 233 | """ |
| 234 | from distutils.ccompiler import CompileError |
| 235 | self._check_compiler() |
| 236 | try: |
Andrew M. Kuchling | 6fb8d3a | 2001-08-16 13:56:40 +0000 | [diff] [blame] | 237 | self._compile(body, headers, include_dirs, lang) |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 238 | ok = 1 |
| 239 | except CompileError: |
| 240 | ok = 0 |
| 241 | |
| 242 | self.announce(ok and "success!" or "failure.") |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 243 | self._clean() |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 244 | return ok |
| 245 | |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 246 | def try_link (self, body, |
| 247 | headers=None, include_dirs=None, |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 248 | libraries=None, library_dirs=None, |
| 249 | lang="c"): |
| 250 | """Try to compile and link a source file, built from 'body' and |
| 251 | 'headers', to executable form. Return true on success, false |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 252 | otherwise. |
| 253 | """ |
| 254 | from distutils.ccompiler import CompileError, LinkError |
| 255 | self._check_compiler() |
| 256 | try: |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 257 | self._link(body, headers, include_dirs, |
| 258 | libraries, library_dirs, lang) |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 259 | ok = 1 |
| 260 | except (CompileError, LinkError): |
| 261 | ok = 0 |
| 262 | |
| 263 | self.announce(ok and "success!" or "failure.") |
| 264 | self._clean() |
| 265 | return ok |
| 266 | |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 267 | def try_run (self, body, |
| 268 | headers=None, include_dirs=None, |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 269 | libraries=None, library_dirs=None, |
| 270 | lang="c"): |
| 271 | """Try to compile, link to an executable, and run a program |
| 272 | built from 'body' and 'headers'. Return true on success, false |
| 273 | otherwise. |
| 274 | """ |
| 275 | from distutils.ccompiler import CompileError, LinkError |
| 276 | self._check_compiler() |
| 277 | try: |
Andrew M. Kuchling | 246c425 | 2001-08-13 13:56:24 +0000 | [diff] [blame] | 278 | src, obj, exe = self._link(body, headers, include_dirs, |
| 279 | libraries, library_dirs, lang) |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 280 | self.spawn([exe]) |
| 281 | ok = 1 |
| 282 | except (CompileError, LinkError, DistutilsExecError): |
| 283 | ok = 0 |
| 284 | |
| 285 | self.announce(ok and "success!" or "failure.") |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 286 | self._clean() |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 287 | return ok |
| 288 | |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 289 | |
| 290 | # -- High-level methods -------------------------------------------- |
| 291 | # (these are the ones that are actually likely to be useful |
| 292 | # when implementing a real-world config command!) |
| 293 | |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 294 | def check_func (self, func, |
| 295 | headers=None, include_dirs=None, |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 296 | libraries=None, library_dirs=None, |
| 297 | decl=0, call=0): |
| 298 | |
| 299 | """Determine if function 'func' is available by constructing a |
| 300 | source file that refers to 'func', and compiles and links it. |
| 301 | If everything succeeds, returns true; otherwise returns false. |
| 302 | |
| 303 | The constructed source file starts out by including the header |
| 304 | files listed in 'headers'. If 'decl' is true, it then declares |
| 305 | 'func' (as "int func()"); you probably shouldn't supply 'headers' |
| 306 | and set 'decl' true in the same call, or you might get errors about |
| 307 | a conflicting declarations for 'func'. Finally, the constructed |
| 308 | 'main()' function either references 'func' or (if 'call' is true) |
| 309 | calls it. 'libraries' and 'library_dirs' are used when |
| 310 | linking. |
| 311 | """ |
| 312 | |
| 313 | self._check_compiler() |
| 314 | body = [] |
| 315 | if decl: |
| 316 | body.append("int %s ();" % func) |
| 317 | body.append("int main () {") |
| 318 | if call: |
| 319 | body.append(" %s();" % func) |
| 320 | else: |
| 321 | body.append(" %s;" % func) |
| 322 | body.append("}") |
| 323 | body = string.join(body, "\n") + "\n" |
| 324 | |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 325 | return self.try_link(body, headers, include_dirs, |
| 326 | libraries, library_dirs) |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 327 | |
| 328 | # check_func () |
| 329 | |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 330 | def check_lib (self, library, library_dirs=None, |
Greg Ward | 4cd6f2a | 2000-10-14 03:56:42 +0000 | [diff] [blame] | 331 | headers=None, include_dirs=None, other_libraries=[]): |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 332 | """Determine if 'library' is available to be linked against, |
| 333 | without actually checking that any particular symbols are provided |
| 334 | by it. 'headers' will be used in constructing the source file to |
| 335 | be compiled, but the only effect of this is to check if all the |
Greg Ward | 4cd6f2a | 2000-10-14 03:56:42 +0000 | [diff] [blame] | 336 | header files listed are available. Any libraries listed in |
| 337 | 'other_libraries' will be included in the link, in case 'library' |
| 338 | has symbols that depend on other libraries. |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 339 | """ |
| 340 | self._check_compiler() |
| 341 | return self.try_link("int main (void) { }", |
Greg Ward | 4cd6f2a | 2000-10-14 03:56:42 +0000 | [diff] [blame] | 342 | headers, include_dirs, |
| 343 | [library]+other_libraries, library_dirs) |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 344 | |
| 345 | def check_header (self, header, include_dirs=None, |
| 346 | library_dirs=None, lang="c"): |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 347 | """Determine if the system header file named by 'header_file' |
| 348 | exists and can be found by the preprocessor; return true if so, |
| 349 | false otherwise. |
| 350 | """ |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 351 | return self.try_cpp(headers=[header], include_dirs=include_dirs) |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 352 | |
| 353 | |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 354 | # class config |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 355 | |
| 356 | |
| 357 | def dump_file (filename, head=None): |
| 358 | if head is None: |
| 359 | print filename + ":" |
| 360 | else: |
| 361 | print head |
| 362 | |
| 363 | file = open(filename) |
| 364 | sys.stdout.write(file.read()) |
| 365 | file.close() |