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 | |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 12 | __revision__ = "$Id$" |
| 13 | |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 14 | import sys, os, re |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 15 | from distutils.core import Command |
| 16 | from distutils.errors import DistutilsExecError |
Andrew M. Kuchling | 679bc9f | 2003-02-18 01:28:51 +0000 | [diff] [blame] | 17 | from distutils.sysconfig import customize_compiler |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 18 | from distutils import log |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 19 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 20 | LANG_EXT = {"c": ".c", "c++": ".cxx"} |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 21 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 22 | class config(Command): |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 23 | |
| 24 | description = "prepare to build" |
| 25 | |
| 26 | user_options = [ |
| 27 | ('compiler=', None, |
| 28 | "specify the compiler type"), |
| 29 | ('cc=', None, |
| 30 | "specify the compiler executable"), |
| 31 | ('include-dirs=', 'I', |
| 32 | "list of directories to search for header files"), |
| 33 | ('define=', 'D', |
| 34 | "C preprocessor macros to define"), |
| 35 | ('undef=', 'U', |
| 36 | "C preprocessor macros to undefine"), |
| 37 | ('libraries=', 'l', |
| 38 | "external C libraries to link with"), |
| 39 | ('library-dirs=', 'L', |
| 40 | "directories to search for external C libraries"), |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 41 | |
| 42 | ('noisy', None, |
| 43 | "show every action (compile, link, run, ...) taken"), |
| 44 | ('dump-source', None, |
| 45 | "dump generated source files before attempting to compile them"), |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 46 | ] |
| 47 | |
| 48 | |
| 49 | # The three standard command methods: since the "config" command |
| 50 | # does nothing by default, these are empty. |
| 51 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 52 | def initialize_options(self): |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 53 | self.compiler = None |
| 54 | self.cc = None |
| 55 | self.include_dirs = None |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 56 | self.libraries = None |
| 57 | self.library_dirs = None |
| 58 | |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 59 | # maximal output for now |
| 60 | self.noisy = 1 |
| 61 | self.dump_source = 1 |
| 62 | |
| 63 | # list of temporary files generated along-the-way that we have |
| 64 | # to clean at some point |
| 65 | self.temp_files = [] |
| 66 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 67 | def finalize_options(self): |
Greg Ward | 2e38a50 | 2000-10-14 03:40:20 +0000 | [diff] [blame] | 68 | if self.include_dirs is None: |
| 69 | self.include_dirs = self.distribution.include_dirs or [] |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 70 | elif isinstance(self.include_dirs, str): |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 71 | self.include_dirs = self.include_dirs.split(os.pathsep) |
Greg Ward | 2e38a50 | 2000-10-14 03:40:20 +0000 | [diff] [blame] | 72 | |
| 73 | if self.libraries is None: |
| 74 | self.libraries = [] |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 75 | elif isinstance(self.libraries, str): |
Greg Ward | 2e38a50 | 2000-10-14 03:40:20 +0000 | [diff] [blame] | 76 | self.libraries = [self.libraries] |
| 77 | |
| 78 | if self.library_dirs is None: |
| 79 | self.library_dirs = [] |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 80 | elif isinstance(self.library_dirs, str): |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 81 | self.library_dirs = self.library_dirs.split(os.pathsep) |
Greg Ward | 2e38a50 | 2000-10-14 03:40:20 +0000 | [diff] [blame] | 82 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 83 | def run(self): |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 84 | pass |
| 85 | |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 86 | # Utility methods for actual "config" commands. The interfaces are |
| 87 | # loosely based on Autoconf macros of similar names. Sub-classes |
| 88 | # may use these freely. |
| 89 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 90 | def _check_compiler(self): |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 91 | """Check that 'self.compiler' really is a CCompiler object; |
| 92 | if not, make it one. |
| 93 | """ |
| 94 | # We do this late, and only on-demand, because this is an expensive |
| 95 | # import. |
| 96 | from distutils.ccompiler import CCompiler, new_compiler |
| 97 | if not isinstance(self.compiler, CCompiler): |
Greg Ward | cb1f4c4 | 2000-09-30 18:27:54 +0000 | [diff] [blame] | 98 | self.compiler = new_compiler(compiler=self.compiler, |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 99 | dry_run=self.dry_run, force=1) |
Andrew M. Kuchling | 679bc9f | 2003-02-18 01:28:51 +0000 | [diff] [blame] | 100 | customize_compiler(self.compiler) |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 101 | if self.include_dirs: |
| 102 | self.compiler.set_include_dirs(self.include_dirs) |
| 103 | if self.libraries: |
| 104 | self.compiler.set_libraries(self.libraries) |
| 105 | if self.library_dirs: |
| 106 | self.compiler.set_library_dirs(self.library_dirs) |
| 107 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 108 | def _gen_temp_sourcefile(self, body, headers, lang): |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 109 | filename = "_configtest" + LANG_EXT[lang] |
| 110 | file = open(filename, "w") |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 111 | if headers: |
| 112 | for header in headers: |
| 113 | file.write("#include <%s>\n" % header) |
| 114 | file.write("\n") |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 115 | file.write(body) |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 116 | if body[-1] != "\n": |
| 117 | file.write("\n") |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 118 | file.close() |
| 119 | return filename |
| 120 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 121 | def _preprocess(self, body, headers, include_dirs, lang): |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 122 | src = self._gen_temp_sourcefile(body, headers, lang) |
| 123 | out = "_configtest.i" |
| 124 | self.temp_files.extend([src, out]) |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 125 | self.compiler.preprocess(src, out, include_dirs=include_dirs) |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 126 | return (src, out) |
| 127 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 128 | def _compile(self, body, headers, include_dirs, lang): |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 129 | src = self._gen_temp_sourcefile(body, headers, lang) |
| 130 | if self.dump_source: |
| 131 | dump_file(src, "compiling '%s':" % src) |
| 132 | (obj,) = self.compiler.object_filenames([src]) |
| 133 | self.temp_files.extend([src, obj]) |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 134 | self.compiler.compile([src], include_dirs=include_dirs) |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 135 | return (src, obj) |
| 136 | |
Tarek Ziadé | 9d25489 | 2009-04-12 15:07:31 +0000 | [diff] [blame] | 137 | def _link(self, body, headers, include_dirs, libraries, library_dirs, |
| 138 | lang): |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 139 | (src, obj) = self._compile(body, headers, include_dirs, lang) |
Fred Drake | 21d4535 | 2001-12-06 21:01:19 +0000 | [diff] [blame] | 140 | prog = os.path.splitext(os.path.basename(src))[0] |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 141 | self.compiler.link_executable([obj], prog, |
| 142 | libraries=libraries, |
Gustavo Niemeyer | 6b01685 | 2002-11-05 16:12:02 +0000 | [diff] [blame] | 143 | library_dirs=library_dirs, |
| 144 | target_lang=lang) |
Andrew M. Kuchling | b1d6029 | 2001-08-16 14:08:02 +0000 | [diff] [blame] | 145 | |
Just van Rossum | ca3fec7 | 2003-02-03 11:43:54 +0000 | [diff] [blame] | 146 | if self.compiler.exe_extension is not None: |
| 147 | prog = prog + self.compiler.exe_extension |
Andrew M. Kuchling | b1d6029 | 2001-08-16 14:08:02 +0000 | [diff] [blame] | 148 | self.temp_files.append(prog) |
| 149 | |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 150 | return (src, obj, prog) |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 151 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 152 | def _clean(self, *filenames): |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 153 | if not filenames: |
| 154 | filenames = self.temp_files |
| 155 | self.temp_files = [] |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 156 | log.info("removing: %s", ' '.join(filenames)) |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 157 | for filename in filenames: |
| 158 | try: |
| 159 | os.remove(filename) |
| 160 | except OSError: |
| 161 | pass |
| 162 | |
| 163 | |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 164 | # XXX these ignore the dry-run flag: what to do, what to do? even if |
| 165 | # you want a dry-run build, you still need some sort of configuration |
| 166 | # info. My inclination is to make it up to the real config command to |
| 167 | # consult 'dry_run', and assume a default (minimal) configuration if |
| 168 | # true. The problem with trying to do it here is that you'd have to |
| 169 | # return either true or false from all the 'try' methods, neither of |
| 170 | # which is correct. |
| 171 | |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 172 | # XXX need access to the header search path and maybe default macros. |
| 173 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 174 | 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] | 175 | """Construct a source file from 'body' (a string containing lines |
| 176 | of C/C++ code) and 'headers' (a list of header files to include) |
| 177 | and run it through the preprocessor. Return true if the |
| 178 | preprocessor succeeded, false if there were any errors. |
| 179 | ('body' probably isn't of much use, but what the heck.) |
| 180 | """ |
| 181 | from distutils.ccompiler import CompileError |
| 182 | self._check_compiler() |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 183 | ok = True |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 184 | try: |
Andrew M. Kuchling | 6fb8d3a | 2001-08-16 13:56:40 +0000 | [diff] [blame] | 185 | self._preprocess(body, headers, include_dirs, lang) |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 186 | except CompileError: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 187 | ok = False |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 188 | |
| 189 | self._clean() |
| 190 | return ok |
| 191 | |
Tarek Ziadé | 9d25489 | 2009-04-12 15:07:31 +0000 | [diff] [blame] | 192 | def search_cpp(self, pattern, body=None, headers=None, include_dirs=None, |
| 193 | lang="c"): |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 194 | """Construct a source file (just like 'try_cpp()'), run it through |
| 195 | the preprocessor, and return true if any line of the output matches |
| 196 | 'pattern'. 'pattern' should either be a compiled regex object or a |
| 197 | string containing a regex. If both 'body' and 'headers' are None, |
| 198 | preprocesses an empty file -- which can be useful to determine the |
| 199 | symbols the preprocessor and compiler set by default. |
| 200 | """ |
| 201 | |
| 202 | self._check_compiler() |
Andrew M. Kuchling | 6fb8d3a | 2001-08-16 13:56:40 +0000 | [diff] [blame] | 203 | (src, out) = self._preprocess(body, headers, include_dirs, lang) |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 204 | |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 205 | if isinstance(pattern, str): |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 206 | pattern = re.compile(pattern) |
| 207 | |
| 208 | file = open(out) |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 209 | match = False |
| 210 | while True: |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 211 | line = file.readline() |
| 212 | if line == '': |
| 213 | break |
Andrew M. Kuchling | 6fb8d3a | 2001-08-16 13:56:40 +0000 | [diff] [blame] | 214 | if pattern.search(line): |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 215 | match = True |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 216 | break |
| 217 | |
| 218 | file.close() |
| 219 | self._clean() |
| 220 | return match |
| 221 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 222 | def try_compile(self, body, headers=None, include_dirs=None, lang="c"): |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 223 | """Try to compile a source file built from 'body' and 'headers'. |
| 224 | Return true on success, false otherwise. |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 225 | """ |
| 226 | from distutils.ccompiler import CompileError |
| 227 | self._check_compiler() |
| 228 | try: |
Andrew M. Kuchling | 6fb8d3a | 2001-08-16 13:56:40 +0000 | [diff] [blame] | 229 | self._compile(body, headers, include_dirs, lang) |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 230 | ok = True |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 231 | except CompileError: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 232 | ok = False |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 233 | |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 234 | log.info(ok and "success!" or "failure.") |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 235 | self._clean() |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 236 | return ok |
| 237 | |
Tarek Ziadé | 9d25489 | 2009-04-12 15:07:31 +0000 | [diff] [blame] | 238 | def try_link(self, body, headers=None, include_dirs=None, libraries=None, |
| 239 | library_dirs=None, lang="c"): |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 240 | """Try to compile and link a source file, built from 'body' and |
| 241 | 'headers', to executable form. Return true on success, false |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 242 | otherwise. |
| 243 | """ |
| 244 | from distutils.ccompiler import CompileError, LinkError |
| 245 | self._check_compiler() |
| 246 | try: |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 247 | self._link(body, headers, include_dirs, |
| 248 | libraries, library_dirs, lang) |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 249 | ok = True |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 250 | except (CompileError, LinkError): |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 251 | ok = False |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 252 | |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 253 | log.info(ok and "success!" or "failure.") |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 254 | self._clean() |
| 255 | return ok |
Fred Drake | 21d4535 | 2001-12-06 21:01:19 +0000 | [diff] [blame] | 256 | |
Tarek Ziadé | 9d25489 | 2009-04-12 15:07:31 +0000 | [diff] [blame] | 257 | def try_run(self, body, headers=None, include_dirs=None, libraries=None, |
| 258 | library_dirs=None, lang="c"): |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 259 | """Try to compile, link to an executable, and run a program |
| 260 | built from 'body' and 'headers'. Return true on success, false |
| 261 | otherwise. |
| 262 | """ |
| 263 | from distutils.ccompiler import CompileError, LinkError |
| 264 | self._check_compiler() |
| 265 | try: |
Andrew M. Kuchling | 246c425 | 2001-08-13 13:56:24 +0000 | [diff] [blame] | 266 | src, obj, exe = self._link(body, headers, include_dirs, |
| 267 | libraries, library_dirs, lang) |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 268 | self.spawn([exe]) |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 269 | ok = True |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 270 | except (CompileError, LinkError, DistutilsExecError): |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 271 | ok = False |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 272 | |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 273 | log.info(ok and "success!" or "failure.") |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 274 | self._clean() |
Greg Ward | 28a5f44 | 2000-06-06 02:57:07 +0000 | [diff] [blame] | 275 | return ok |
| 276 | |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 277 | |
| 278 | # -- High-level methods -------------------------------------------- |
| 279 | # (these are the ones that are actually likely to be useful |
| 280 | # when implementing a real-world config command!) |
| 281 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 282 | def check_func(self, func, headers=None, include_dirs=None, |
| 283 | libraries=None, library_dirs=None, decl=0, call=0): |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 284 | """Determine if function 'func' is available by constructing a |
| 285 | source file that refers to 'func', and compiles and links it. |
| 286 | If everything succeeds, returns true; otherwise returns false. |
| 287 | |
| 288 | The constructed source file starts out by including the header |
| 289 | files listed in 'headers'. If 'decl' is true, it then declares |
| 290 | 'func' (as "int func()"); you probably shouldn't supply 'headers' |
| 291 | and set 'decl' true in the same call, or you might get errors about |
| 292 | a conflicting declarations for 'func'. Finally, the constructed |
| 293 | 'main()' function either references 'func' or (if 'call' is true) |
| 294 | calls it. 'libraries' and 'library_dirs' are used when |
| 295 | linking. |
| 296 | """ |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 297 | self._check_compiler() |
| 298 | body = [] |
| 299 | if decl: |
| 300 | body.append("int %s ();" % func) |
| 301 | body.append("int main () {") |
| 302 | if call: |
| 303 | body.append(" %s();" % func) |
| 304 | else: |
| 305 | body.append(" %s;" % func) |
| 306 | body.append("}") |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 307 | body = "\n".join(body) + "\n" |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 308 | |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 309 | return self.try_link(body, headers, include_dirs, |
| 310 | libraries, library_dirs) |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 311 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 312 | def check_lib(self, library, library_dirs=None, headers=None, |
| 313 | include_dirs=None, other_libraries=[]): |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 314 | """Determine if 'library' is available to be linked against, |
| 315 | without actually checking that any particular symbols are provided |
| 316 | by it. 'headers' will be used in constructing the source file to |
| 317 | 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] | 318 | header files listed are available. Any libraries listed in |
| 319 | 'other_libraries' will be included in the link, in case 'library' |
| 320 | has symbols that depend on other libraries. |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 321 | """ |
| 322 | self._check_compiler() |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 323 | return self.try_link("int main (void) { }", headers, include_dirs, |
| 324 | [library] + other_libraries, library_dirs) |
Greg Ward | 855dab9 | 2000-06-27 01:21:22 +0000 | [diff] [blame] | 325 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 326 | def check_header(self, header, include_dirs=None, library_dirs=None, |
| 327 | lang="c"): |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 328 | """Determine if the system header file named by 'header_file' |
| 329 | exists and can be found by the preprocessor; return true if so, |
| 330 | false otherwise. |
| 331 | """ |
Andrew M. Kuchling | 4013cbd | 2002-09-09 12:10:00 +0000 | [diff] [blame] | 332 | return self.try_cpp(body="/* No body */", headers=[header], |
| 333 | include_dirs=include_dirs) |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 334 | |
| 335 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 336 | def dump_file(filename, head=None): |
Tarek Ziadé | 0eb1304 | 2009-04-12 14:57:46 +0000 | [diff] [blame] | 337 | """Dumps a file content into log.info. |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 338 | |
Tarek Ziadé | 0eb1304 | 2009-04-12 14:57:46 +0000 | [diff] [blame] | 339 | If head is not None, will be dumped before the file content. |
| 340 | """ |
| 341 | if head is None: |
| 342 | log.info('%s' % filename) |
| 343 | else: |
| 344 | log.info(head) |
Greg Ward | 59ac709 | 2000-06-21 03:00:50 +0000 | [diff] [blame] | 345 | file = open(filename) |
Tarek Ziadé | 0eb1304 | 2009-04-12 14:57:46 +0000 | [diff] [blame] | 346 | try: |
| 347 | log.info(file.read()) |
| 348 | finally: |
| 349 | file.close() |