build_compilers.py 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. """Point native source builds under PM's pinned interpreter at tools this host has.
  2. python-build-standalone records the toolchain it was built with in sysconfig:
  3. on Linux ``CC = 'clang -pthread'``, ``CXX = 'clang++ -pthread'``, linkers that
  4. start with the same commands, and an ``AR`` under its deleted ``/tools/llvm``
  5. build directory; on macOS only that archiver is foreign. setuptools compiles
  6. every extension with those values unless the environment overrides them, so a
  7. Debian/Ubuntu/WSL host with only gcc/g++ failed any sdist build with
  8. ``No such file or directory: 'clang++'`` (python-olm, via the matrix extra).
  9. Stdlib only: ``pm.environment.classify_uv_failure`` imports this on the
  10. bootstrap runner's pre-3.11 system python.
  11. """
  12. from __future__ import annotations
  13. from collections.abc import Mapping
  14. import functools
  15. import json
  16. from pathlib import Path
  17. import re
  18. import shutil
  19. import subprocess
  20. import sys
  21. # setuptools derives LDSHARED from an overridden CC but never LDCXXSHARED from
  22. # CXX, so each compiler's own linker is rewritten with it.
  23. _COMPILERS = (("CC", "LDSHARED"), ("CXX", "LDCXXSHARED"))
  24. _GCC_FAMILY = {"clang": "gcc", "clang++": "g++"}
  25. _RECORDED = ("CC", "CXX", "LDSHARED", "LDCXXSHARED", "AR")
  26. _MISSING_COMPILER = (
  27. re.compile(r"No such file or directory: '([^']+)'"),
  28. # distutils' spawn when the executable is absent; a compiler that ran and failed
  29. # ("command 'gcc' failed with exit code 1") is a source error, not a missing tool.
  30. re.compile(r"command '([^']+)' failed: No such file or directory"),
  31. re.compile(r"No CMAKE_(?:C|CXX)_COMPILER could be found"),
  32. )
  33. _COMPILER_NAME = re.compile(r"(?:clang(?:\+\+)?|g?cc|g\+\+|c\+\+)(?:-[\d.]+)?")
  34. _COMPILER_REMEDY = ("{what}: install a C/C++ compiler (Debian/Ubuntu/WSL: `sudo apt install build-essential`; "
  35. "Fedora: `sudo dnf install gcc gcc-c++`; macOS: `xcode-select --install`) "
  36. "or point CC/CXX at one, then retry")
  37. @functools.lru_cache(maxsize=None) # noqa: UP033 -- functools.cache is 3.9+; the bootstrap runner can be older
  38. def _recorded_toolchain(python: str) -> dict[str, str]:
  39. code = ("import json, sysconfig; "
  40. f"print(json.dumps({{k: sysconfig.get_config_var(k) or '' for k in {_RECORDED!r}}}))")
  41. try:
  42. result = subprocess.run([python, "-I", "-c", code], capture_output=True, text=True,
  43. timeout=30, check=False)
  44. return json.loads(result.stdout) if result.returncode == 0 else {}
  45. except (OSError, subprocess.TimeoutExpired, ValueError):
  46. return {}
  47. def _linux_defaults(recorded: Mapping[str, str], env: Mapping[str, str]) -> dict[str, str]:
  48. search = env.get("PATH")
  49. defaults: dict[str, str] = {}
  50. for compiler_var, linker_var in _COMPILERS:
  51. command = recorded.get(compiler_var, "")
  52. program = command.split(" ", 1)[0]
  53. substitute = _GCC_FAMILY.get(program)
  54. if (compiler_var in env or substitute is None or shutil.which(program, path=search)
  55. or not shutil.which(substitute, path=search)):
  56. continue
  57. defaults[compiler_var] = substitute + command[len(program):]
  58. linker = recorded.get(linker_var, "")
  59. if linker_var not in env and linker.split(" ", 1)[0] == program:
  60. defaults[linker_var] = substitute + linker[len(program):]
  61. archiver = recorded.get("AR", "")
  62. if archiver and not shutil.which(archiver, path=search) and shutil.which("ar", path=search):
  63. defaults["AR"] = "ar"
  64. return defaults
  65. def pinned_build_defaults(python: Path, env: Mapping[str, str]) -> dict[str, str]:
  66. """Defaults for build variables the pinned interpreter records but this host lacks.
  67. Callers ``setdefault`` them, so a CC/CXX/AR the user set always wins. A recorded
  68. compiler with no gcc-family equivalent on PATH is left alone; the build then
  69. fails and ``missing_compiler_remedy`` names the fix.
  70. """
  71. if sys.platform == "darwin":
  72. return {"AR": "/usr/bin/ar"}
  73. if sys.platform != "linux":
  74. return {}
  75. return _linux_defaults(_recorded_toolchain(str(python)), env)
  76. def missing_compiler_remedy(output: str) -> str:
  77. """The install hint for a build that failed because no compiler exists, else ''."""
  78. for pattern in _MISSING_COMPILER:
  79. for match in pattern.finditer(output):
  80. if not match.groups():
  81. return _COMPILER_REMEDY.format(what="this package compiles native code and no compiler was found")
  82. if _COMPILER_NAME.fullmatch(Path(match.group(1)).name):
  83. return _COMPILER_REMEDY.format(what=f"the build needs `{match.group(1)}`, which is not on PATH")
  84. return ""