config.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. """distutils.command.config
  2. Implements the Distutils 'config' command, a (mostly) empty command class
  3. that exists mainly to be sub-classed by specific module distributions and
  4. applications. The idea is that while every "config" command is different,
  5. at least they're all named the same, and users always see "config" in the
  6. list of standard commands. Also, this is a good place to put common
  7. configure-like tasks: "try to compile this C code", or "figure out where
  8. this header file lives".
  9. """
  10. from __future__ import annotations
  11. import os
  12. import pathlib
  13. import re
  14. from collections.abc import Sequence
  15. from distutils._log import log
  16. from ..ccompiler import CCompiler, CompileError, LinkError, new_compiler
  17. from ..core import Command
  18. from ..errors import DistutilsExecError
  19. from ..sysconfig import customize_compiler
  20. LANG_EXT = {"c": ".c", "c++": ".cxx"}
  21. class config(Command):
  22. description = "prepare to build"
  23. user_options = [
  24. ('compiler=', None, "specify the compiler type"),
  25. ('cc=', None, "specify the compiler executable"),
  26. ('include-dirs=', 'I', "list of directories to search for header files"),
  27. ('define=', 'D', "C preprocessor macros to define"),
  28. ('undef=', 'U', "C preprocessor macros to undefine"),
  29. ('libraries=', 'l', "external C libraries to link with"),
  30. ('library-dirs=', 'L', "directories to search for external C libraries"),
  31. ('noisy', None, "show every action (compile, link, run, ...) taken"),
  32. (
  33. 'dump-source',
  34. None,
  35. "dump generated source files before attempting to compile them",
  36. ),
  37. ]
  38. # The three standard command methods: since the "config" command
  39. # does nothing by default, these are empty.
  40. def initialize_options(self):
  41. self.compiler = None
  42. self.cc = None
  43. self.include_dirs = None
  44. self.libraries = None
  45. self.library_dirs = None
  46. # maximal output for now
  47. self.noisy = 1
  48. self.dump_source = 1
  49. # list of temporary files generated along-the-way that we have
  50. # to clean at some point
  51. self.temp_files = []
  52. def finalize_options(self):
  53. if self.include_dirs is None:
  54. self.include_dirs = self.distribution.include_dirs or []
  55. elif isinstance(self.include_dirs, str):
  56. self.include_dirs = self.include_dirs.split(os.pathsep)
  57. if self.libraries is None:
  58. self.libraries = []
  59. elif isinstance(self.libraries, str):
  60. self.libraries = [self.libraries]
  61. if self.library_dirs is None:
  62. self.library_dirs = []
  63. elif isinstance(self.library_dirs, str):
  64. self.library_dirs = self.library_dirs.split(os.pathsep)
  65. def run(self):
  66. pass
  67. # Utility methods for actual "config" commands. The interfaces are
  68. # loosely based on Autoconf macros of similar names. Sub-classes
  69. # may use these freely.
  70. def _check_compiler(self):
  71. """Check that 'self.compiler' really is a CCompiler object;
  72. if not, make it one.
  73. """
  74. if not isinstance(self.compiler, CCompiler):
  75. self.compiler = new_compiler(compiler=self.compiler, force=True)
  76. customize_compiler(self.compiler)
  77. if self.include_dirs:
  78. self.compiler.set_include_dirs(self.include_dirs)
  79. if self.libraries:
  80. self.compiler.set_libraries(self.libraries)
  81. if self.library_dirs:
  82. self.compiler.set_library_dirs(self.library_dirs)
  83. def _gen_temp_sourcefile(self, body, headers, lang):
  84. filename = "_configtest" + LANG_EXT[lang]
  85. with open(filename, "w", encoding='utf-8') as file:
  86. if headers:
  87. for header in headers:
  88. file.write(f"#include <{header}>\n")
  89. file.write("\n")
  90. file.write(body)
  91. if body[-1] != "\n":
  92. file.write("\n")
  93. return filename
  94. def _preprocess(self, body, headers, include_dirs, lang):
  95. src = self._gen_temp_sourcefile(body, headers, lang)
  96. out = "_configtest.i"
  97. self.temp_files.extend([src, out])
  98. self.compiler.preprocess(src, out, include_dirs=include_dirs)
  99. return (src, out)
  100. def _compile(self, body, headers, include_dirs, lang):
  101. src = self._gen_temp_sourcefile(body, headers, lang)
  102. if self.dump_source:
  103. dump_file(src, f"compiling '{src}':")
  104. (obj,) = self.compiler.object_filenames([src])
  105. self.temp_files.extend([src, obj])
  106. self.compiler.compile([src], include_dirs=include_dirs)
  107. return (src, obj)
  108. def _link(self, body, headers, include_dirs, libraries, library_dirs, lang):
  109. (src, obj) = self._compile(body, headers, include_dirs, lang)
  110. prog = os.path.splitext(os.path.basename(src))[0]
  111. self.compiler.link_executable(
  112. [obj],
  113. prog,
  114. libraries=libraries,
  115. library_dirs=library_dirs,
  116. target_lang=lang,
  117. )
  118. if self.compiler.exe_extension is not None:
  119. prog = prog + self.compiler.exe_extension
  120. self.temp_files.append(prog)
  121. return (src, obj, prog)
  122. def _clean(self, *filenames):
  123. if not filenames:
  124. filenames = self.temp_files
  125. self.temp_files = []
  126. log.info("removing: %s", ' '.join(filenames))
  127. for filename in filenames:
  128. try:
  129. os.remove(filename)
  130. except OSError:
  131. pass
  132. # XXX need access to the header search path and maybe default macros.
  133. def try_cpp(self, body=None, headers=None, include_dirs=None, lang="c"):
  134. """Construct a source file from 'body' (a string containing lines
  135. of C/C++ code) and 'headers' (a list of header files to include)
  136. and run it through the preprocessor. Return true if the
  137. preprocessor succeeded, false if there were any errors.
  138. ('body' probably isn't of much use, but what the heck.)
  139. """
  140. self._check_compiler()
  141. ok = True
  142. try:
  143. self._preprocess(body, headers, include_dirs, lang)
  144. except CompileError:
  145. ok = False
  146. self._clean()
  147. return ok
  148. def search_cpp(self, pattern, body=None, headers=None, include_dirs=None, lang="c"):
  149. """Construct a source file (just like 'try_cpp()'), run it through
  150. the preprocessor, and return true if any line of the output matches
  151. 'pattern'. 'pattern' should either be a compiled regex object or a
  152. string containing a regex. If both 'body' and 'headers' are None,
  153. preprocesses an empty file -- which can be useful to determine the
  154. symbols the preprocessor and compiler set by default.
  155. """
  156. self._check_compiler()
  157. src, out = self._preprocess(body, headers, include_dirs, lang)
  158. if isinstance(pattern, str):
  159. pattern = re.compile(pattern)
  160. with open(out, encoding='utf-8') as file:
  161. match = any(pattern.search(line) for line in file)
  162. self._clean()
  163. return match
  164. def try_compile(self, body, headers=None, include_dirs=None, lang="c"):
  165. """Try to compile a source file built from 'body' and 'headers'.
  166. Return true on success, false otherwise.
  167. """
  168. self._check_compiler()
  169. try:
  170. self._compile(body, headers, include_dirs, lang)
  171. ok = True
  172. except CompileError:
  173. ok = False
  174. log.info(ok and "success!" or "failure.")
  175. self._clean()
  176. return ok
  177. def try_link(
  178. self,
  179. body,
  180. headers=None,
  181. include_dirs=None,
  182. libraries=None,
  183. library_dirs=None,
  184. lang="c",
  185. ):
  186. """Try to compile and link a source file, built from 'body' and
  187. 'headers', to executable form. Return true on success, false
  188. otherwise.
  189. """
  190. self._check_compiler()
  191. try:
  192. self._link(body, headers, include_dirs, libraries, library_dirs, lang)
  193. ok = True
  194. except (CompileError, LinkError):
  195. ok = False
  196. log.info(ok and "success!" or "failure.")
  197. self._clean()
  198. return ok
  199. def try_run(
  200. self,
  201. body,
  202. headers=None,
  203. include_dirs=None,
  204. libraries=None,
  205. library_dirs=None,
  206. lang="c",
  207. ):
  208. """Try to compile, link to an executable, and run a program
  209. built from 'body' and 'headers'. Return true on success, false
  210. otherwise.
  211. """
  212. self._check_compiler()
  213. try:
  214. src, obj, exe = self._link(
  215. body, headers, include_dirs, libraries, library_dirs, lang
  216. )
  217. self.spawn([exe])
  218. ok = True
  219. except (CompileError, LinkError, DistutilsExecError):
  220. ok = False
  221. log.info(ok and "success!" or "failure.")
  222. self._clean()
  223. return ok
  224. # -- High-level methods --------------------------------------------
  225. # (these are the ones that are actually likely to be useful
  226. # when implementing a real-world config command!)
  227. def check_func(
  228. self,
  229. func,
  230. headers=None,
  231. include_dirs=None,
  232. libraries=None,
  233. library_dirs=None,
  234. decl=False,
  235. call=False,
  236. ):
  237. """Determine if function 'func' is available by constructing a
  238. source file that refers to 'func', and compiles and links it.
  239. If everything succeeds, returns true; otherwise returns false.
  240. The constructed source file starts out by including the header
  241. files listed in 'headers'. If 'decl' is true, it then declares
  242. 'func' (as "int func()"); you probably shouldn't supply 'headers'
  243. and set 'decl' true in the same call, or you might get errors about
  244. a conflicting declarations for 'func'. Finally, the constructed
  245. 'main()' function either references 'func' or (if 'call' is true)
  246. calls it. 'libraries' and 'library_dirs' are used when
  247. linking.
  248. """
  249. self._check_compiler()
  250. body = []
  251. if decl:
  252. body.append(f"int {func} ();")
  253. body.append("int main () {")
  254. if call:
  255. body.append(f" {func}();")
  256. else:
  257. body.append(f" {func};")
  258. body.append("}")
  259. body = "\n".join(body) + "\n"
  260. return self.try_link(body, headers, include_dirs, libraries, library_dirs)
  261. def check_lib(
  262. self,
  263. library,
  264. library_dirs=None,
  265. headers=None,
  266. include_dirs=None,
  267. other_libraries: Sequence[str] = [],
  268. ):
  269. """Determine if 'library' is available to be linked against,
  270. without actually checking that any particular symbols are provided
  271. by it. 'headers' will be used in constructing the source file to
  272. be compiled, but the only effect of this is to check if all the
  273. header files listed are available. Any libraries listed in
  274. 'other_libraries' will be included in the link, in case 'library'
  275. has symbols that depend on other libraries.
  276. """
  277. self._check_compiler()
  278. return self.try_link(
  279. "int main (void) { }",
  280. headers,
  281. include_dirs,
  282. [library] + list(other_libraries),
  283. library_dirs,
  284. )
  285. def check_header(self, header, include_dirs=None, library_dirs=None, lang="c"):
  286. """Determine if the system header file named by 'header_file'
  287. exists and can be found by the preprocessor; return true if so,
  288. false otherwise.
  289. """
  290. return self.try_cpp(
  291. body="/* No body */", headers=[header], include_dirs=include_dirs
  292. )
  293. def dump_file(filename, head=None):
  294. """Dumps a file content into log.info.
  295. If head is not None, will be dumped before the file content.
  296. """
  297. if head is None:
  298. log.info('%s', filename)
  299. else:
  300. log.info(head)
  301. log.info(pathlib.Path(filename).read_text(encoding='utf-8'))