+++ /dev/null
-"""distutils.command.config\r
-\r
-Implements the Distutils 'config' command, a (mostly) empty command class\r
-that exists mainly to be sub-classed by specific module distributions and\r
-applications. The idea is that while every "config" command is different,\r
-at least they're all named the same, and users always see "config" in the\r
-list of standard commands. Also, this is a good place to put common\r
-configure-like tasks: "try to compile this C code", or "figure out where\r
-this header file lives".\r
-"""\r
-\r
-__revision__ = "$Id$"\r
-\r
-import os\r
-import re\r
-\r
-from distutils.core import Command\r
-from distutils.errors import DistutilsExecError\r
-from distutils.ccompiler import customize_compiler\r
-from distutils import log\r
-\r
-LANG_EXT = {'c': '.c', 'c++': '.cxx'}\r
-\r
-class config(Command):\r
-\r
- description = "prepare to build"\r
-\r
- user_options = [\r
- ('compiler=', None,\r
- "specify the compiler type"),\r
- ('cc=', None,\r
- "specify the compiler executable"),\r
- ('include-dirs=', 'I',\r
- "list of directories to search for header files"),\r
- ('define=', 'D',\r
- "C preprocessor macros to define"),\r
- ('undef=', 'U',\r
- "C preprocessor macros to undefine"),\r
- ('libraries=', 'l',\r
- "external C libraries to link with"),\r
- ('library-dirs=', 'L',\r
- "directories to search for external C libraries"),\r
-\r
- ('noisy', None,\r
- "show every action (compile, link, run, ...) taken"),\r
- ('dump-source', None,\r
- "dump generated source files before attempting to compile them"),\r
- ]\r
-\r
-\r
- # The three standard command methods: since the "config" command\r
- # does nothing by default, these are empty.\r
-\r
- def initialize_options(self):\r
- self.compiler = None\r
- self.cc = None\r
- self.include_dirs = None\r
- self.libraries = None\r
- self.library_dirs = None\r
-\r
- # maximal output for now\r
- self.noisy = 1\r
- self.dump_source = 1\r
-\r
- # list of temporary files generated along-the-way that we have\r
- # to clean at some point\r
- self.temp_files = []\r
-\r
- def finalize_options(self):\r
- if self.include_dirs is None:\r
- self.include_dirs = self.distribution.include_dirs or []\r
- elif isinstance(self.include_dirs, str):\r
- self.include_dirs = self.include_dirs.split(os.pathsep)\r
-\r
- if self.libraries is None:\r
- self.libraries = []\r
- elif isinstance(self.libraries, str):\r
- self.libraries = [self.libraries]\r
-\r
- if self.library_dirs is None:\r
- self.library_dirs = []\r
- elif isinstance(self.library_dirs, str):\r
- self.library_dirs = self.library_dirs.split(os.pathsep)\r
-\r
- def run(self):\r
- pass\r
-\r
-\r
- # Utility methods for actual "config" commands. The interfaces are\r
- # loosely based on Autoconf macros of similar names. Sub-classes\r
- # may use these freely.\r
-\r
- def _check_compiler(self):\r
- """Check that 'self.compiler' really is a CCompiler object;\r
- if not, make it one.\r
- """\r
- # We do this late, and only on-demand, because this is an expensive\r
- # import.\r
- from distutils.ccompiler import CCompiler, new_compiler\r
- if not isinstance(self.compiler, CCompiler):\r
- self.compiler = new_compiler(compiler=self.compiler,\r
- dry_run=self.dry_run, force=1)\r
- customize_compiler(self.compiler)\r
- if self.include_dirs:\r
- self.compiler.set_include_dirs(self.include_dirs)\r
- if self.libraries:\r
- self.compiler.set_libraries(self.libraries)\r
- if self.library_dirs:\r
- self.compiler.set_library_dirs(self.library_dirs)\r
-\r
-\r
- def _gen_temp_sourcefile(self, body, headers, lang):\r
- filename = "_configtest" + LANG_EXT[lang]\r
- file = open(filename, "w")\r
- if headers:\r
- for header in headers:\r
- file.write("#include <%s>\n" % header)\r
- file.write("\n")\r
- file.write(body)\r
- if body[-1] != "\n":\r
- file.write("\n")\r
- file.close()\r
- return filename\r
-\r
- def _preprocess(self, body, headers, include_dirs, lang):\r
- src = self._gen_temp_sourcefile(body, headers, lang)\r
- out = "_configtest.i"\r
- self.temp_files.extend([src, out])\r
- self.compiler.preprocess(src, out, include_dirs=include_dirs)\r
- return (src, out)\r
-\r
- def _compile(self, body, headers, include_dirs, lang):\r
- src = self._gen_temp_sourcefile(body, headers, lang)\r
- if self.dump_source:\r
- dump_file(src, "compiling '%s':" % src)\r
- (obj,) = self.compiler.object_filenames([src])\r
- self.temp_files.extend([src, obj])\r
- self.compiler.compile([src], include_dirs=include_dirs)\r
- return (src, obj)\r
-\r
- def _link(self, body, headers, include_dirs, libraries, library_dirs,\r
- lang):\r
- (src, obj) = self._compile(body, headers, include_dirs, lang)\r
- prog = os.path.splitext(os.path.basename(src))[0]\r
- self.compiler.link_executable([obj], prog,\r
- libraries=libraries,\r
- library_dirs=library_dirs,\r
- target_lang=lang)\r
-\r
- if self.compiler.exe_extension is not None:\r
- prog = prog + self.compiler.exe_extension\r
- self.temp_files.append(prog)\r
-\r
- return (src, obj, prog)\r
-\r
- def _clean(self, *filenames):\r
- if not filenames:\r
- filenames = self.temp_files\r
- self.temp_files = []\r
- log.info("removing: %s", ' '.join(filenames))\r
- for filename in filenames:\r
- try:\r
- os.remove(filename)\r
- except OSError:\r
- pass\r
-\r
-\r
- # XXX these ignore the dry-run flag: what to do, what to do? even if\r
- # you want a dry-run build, you still need some sort of configuration\r
- # info. My inclination is to make it up to the real config command to\r
- # consult 'dry_run', and assume a default (minimal) configuration if\r
- # true. The problem with trying to do it here is that you'd have to\r
- # return either true or false from all the 'try' methods, neither of\r
- # which is correct.\r
-\r
- # XXX need access to the header search path and maybe default macros.\r
-\r
- def try_cpp(self, body=None, headers=None, include_dirs=None, lang="c"):\r
- """Construct a source file from 'body' (a string containing lines\r
- of C/C++ code) and 'headers' (a list of header files to include)\r
- and run it through the preprocessor. Return true if the\r
- preprocessor succeeded, false if there were any errors.\r
- ('body' probably isn't of much use, but what the heck.)\r
- """\r
- from distutils.ccompiler import CompileError\r
- self._check_compiler()\r
- ok = 1\r
- try:\r
- self._preprocess(body, headers, include_dirs, lang)\r
- except CompileError:\r
- ok = 0\r
-\r
- self._clean()\r
- return ok\r
-\r
- def search_cpp(self, pattern, body=None, headers=None, include_dirs=None,\r
- lang="c"):\r
- """Construct a source file (just like 'try_cpp()'), run it through\r
- the preprocessor, and return true if any line of the output matches\r
- 'pattern'. 'pattern' should either be a compiled regex object or a\r
- string containing a regex. If both 'body' and 'headers' are None,\r
- preprocesses an empty file -- which can be useful to determine the\r
- symbols the preprocessor and compiler set by default.\r
- """\r
- self._check_compiler()\r
- src, out = self._preprocess(body, headers, include_dirs, lang)\r
-\r
- if isinstance(pattern, str):\r
- pattern = re.compile(pattern)\r
-\r
- file = open(out)\r
- match = 0\r
- while 1:\r
- line = file.readline()\r
- if line == '':\r
- break\r
- if pattern.search(line):\r
- match = 1\r
- break\r
-\r
- file.close()\r
- self._clean()\r
- return match\r
-\r
- def try_compile(self, body, headers=None, include_dirs=None, lang="c"):\r
- """Try to compile a source file built from 'body' and 'headers'.\r
- Return true on success, false otherwise.\r
- """\r
- from distutils.ccompiler import CompileError\r
- self._check_compiler()\r
- try:\r
- self._compile(body, headers, include_dirs, lang)\r
- ok = 1\r
- except CompileError:\r
- ok = 0\r
-\r
- log.info(ok and "success!" or "failure.")\r
- self._clean()\r
- return ok\r
-\r
- def try_link(self, body, headers=None, include_dirs=None, libraries=None,\r
- library_dirs=None, lang="c"):\r
- """Try to compile and link a source file, built from 'body' and\r
- 'headers', to executable form. Return true on success, false\r
- otherwise.\r
- """\r
- from distutils.ccompiler import CompileError, LinkError\r
- self._check_compiler()\r
- try:\r
- self._link(body, headers, include_dirs,\r
- libraries, library_dirs, lang)\r
- ok = 1\r
- except (CompileError, LinkError):\r
- ok = 0\r
-\r
- log.info(ok and "success!" or "failure.")\r
- self._clean()\r
- return ok\r
-\r
- def try_run(self, body, headers=None, include_dirs=None, libraries=None,\r
- library_dirs=None, lang="c"):\r
- """Try to compile, link to an executable, and run a program\r
- built from 'body' and 'headers'. Return true on success, false\r
- otherwise.\r
- """\r
- from distutils.ccompiler import CompileError, LinkError\r
- self._check_compiler()\r
- try:\r
- src, obj, exe = self._link(body, headers, include_dirs,\r
- libraries, library_dirs, lang)\r
- self.spawn([exe])\r
- ok = 1\r
- except (CompileError, LinkError, DistutilsExecError):\r
- ok = 0\r
-\r
- log.info(ok and "success!" or "failure.")\r
- self._clean()\r
- return ok\r
-\r
-\r
- # -- High-level methods --------------------------------------------\r
- # (these are the ones that are actually likely to be useful\r
- # when implementing a real-world config command!)\r
-\r
- def check_func(self, func, headers=None, include_dirs=None,\r
- libraries=None, library_dirs=None, decl=0, call=0):\r
-\r
- """Determine if function 'func' is available by constructing a\r
- source file that refers to 'func', and compiles and links it.\r
- If everything succeeds, returns true; otherwise returns false.\r
-\r
- The constructed source file starts out by including the header\r
- files listed in 'headers'. If 'decl' is true, it then declares\r
- 'func' (as "int func()"); you probably shouldn't supply 'headers'\r
- and set 'decl' true in the same call, or you might get errors about\r
- a conflicting declarations for 'func'. Finally, the constructed\r
- 'main()' function either references 'func' or (if 'call' is true)\r
- calls it. 'libraries' and 'library_dirs' are used when\r
- linking.\r
- """\r
-\r
- self._check_compiler()\r
- body = []\r
- if decl:\r
- body.append("int %s ();" % func)\r
- body.append("int main () {")\r
- if call:\r
- body.append(" %s();" % func)\r
- else:\r
- body.append(" %s;" % func)\r
- body.append("}")\r
- body = "\n".join(body) + "\n"\r
-\r
- return self.try_link(body, headers, include_dirs,\r
- libraries, library_dirs)\r
-\r
- # check_func ()\r
-\r
- def check_lib(self, library, library_dirs=None, headers=None,\r
- include_dirs=None, other_libraries=[]):\r
- """Determine if 'library' is available to be linked against,\r
- without actually checking that any particular symbols are provided\r
- by it. 'headers' will be used in constructing the source file to\r
- be compiled, but the only effect of this is to check if all the\r
- header files listed are available. Any libraries listed in\r
- 'other_libraries' will be included in the link, in case 'library'\r
- has symbols that depend on other libraries.\r
- """\r
- self._check_compiler()\r
- return self.try_link("int main (void) { }",\r
- headers, include_dirs,\r
- [library]+other_libraries, library_dirs)\r
-\r
- def check_header(self, header, include_dirs=None, library_dirs=None,\r
- lang="c"):\r
- """Determine if the system header file named by 'header_file'\r
- exists and can be found by the preprocessor; return true if so,\r
- false otherwise.\r
- """\r
- return self.try_cpp(body="/* No body */", headers=[header],\r
- include_dirs=include_dirs)\r
-\r
-\r
-def dump_file(filename, head=None):\r
- """Dumps a file content into log.info.\r
-\r
- If head is not None, will be dumped before the file content.\r
- """\r
- if head is None:\r
- log.info('%s' % filename)\r
- else:\r
- log.info(head)\r
- file = open(filename)\r
- try:\r
- log.info(file.read())\r
- finally:\r
- file.close()\r