|
14 | 14 | import re |
15 | 15 | import subprocess |
16 | 16 | import sys |
| 17 | +import sysconfig |
17 | 18 | import tempfile |
18 | 19 | import zipfile |
| 20 | +from collections.abc import Sequence |
19 | 21 | from pathlib import Path |
20 | 22 |
|
21 | 23 | import Cython as _Cython |
@@ -705,52 +707,124 @@ def build_wheel(wheel_directory, config_settings=None, metadata_directory=None): |
705 | 707 | return wheel_name |
706 | 708 |
|
707 | 709 |
|
708 | | -def _find_clang_tidy() -> str: |
709 | | - """The clang-tidy executable of the clang-tidy wheel in the build environment. |
| 710 | +def _find_clang_tidy() -> str | None: |
| 711 | + """The clang-tidy executable of the build environment. |
710 | 712 |
|
711 | | - The wheel is a build-system requirement in pyproject.toml. |
| 713 | + Isolated builds install the clang-tidy wheel that pyproject.toml requires. |
| 714 | + It is located through the wheel's ``clang_tidy`` package. The pixi build |
| 715 | + uses the ``clang-tools`` conda package, which puts the executable in a |
| 716 | + different location. |
712 | 717 |
|
713 | | - It is located through the wheel's ``clang_tidy`` package, never PATH, so a |
714 | | - system clang-tidy cannot be picked up. Raises RuntimeError when the wheel is |
715 | | - not installed. |
| 718 | + PATH is never searched, so a system clang-tidy cannot be picked up. |
| 719 | + Returns None when neither is installed. |
716 | 720 | """ |
| 721 | + exe = f"clang-tidy{sysconfig.get_config_var('EXE')}" |
| 722 | + if sys.platform == "win32": |
| 723 | + conda_dir = Path("Library") / "bin" |
| 724 | + else: |
| 725 | + conda_dir = "bin" |
| 726 | + candidates = [Path(sys.prefix) / conda_dir / exe] |
717 | 727 | spec = importlib.util.find_spec("clang_tidy") |
718 | 728 | if spec is not None and spec.submodule_search_locations is not None: |
719 | | - exe = "clang-tidy" |
720 | | - if sys.platform == "win32": |
721 | | - exe = "clang-tidy.exe" |
722 | | - for base in spec.submodule_search_locations: |
723 | | - candidate = Path(base) / "data" / "bin" / exe |
724 | | - if candidate.is_file(): |
725 | | - return str(candidate) |
726 | | - raise RuntimeError( |
727 | | - "The clang-tidy build requirement of pyproject.toml is not installed. Isolated builds install it " |
728 | | - "automatically. For other builds, install the pinned version of it." |
| 729 | + wheel_candidates = [Path(base) / "data" / "bin" / exe for base in spec.submodule_search_locations] |
| 730 | + candidates = wheel_candidates + candidates |
| 731 | + for candidate in candidates: |
| 732 | + if candidate.is_file(): |
| 733 | + return str(candidate) |
| 734 | + return None |
| 735 | + |
| 736 | + |
| 737 | +@functools.cache |
| 738 | +def _warn_clang_tidy_missing() -> None: |
| 739 | + """Warn, once per build, that the clang-tidy checks do not run.""" |
| 740 | + banner = "!" * 78 |
| 741 | + print( |
| 742 | + f"\n{banner}\n" |
| 743 | + "WARNING: clang-tidy is not installed in the build environment, so the clang-tidy\n" |
| 744 | + "checks of the handwritten C++ sources will NOT run.\n" |
| 745 | + "Isolated builds install the clang-tidy wheel automatically. For other builds,\n" |
| 746 | + "install the version that pyproject.toml pins, as the wheel or as the conda-forge\n" |
| 747 | + "clang-tools package. The wheel does not exist on Windows ARM64.\n" |
| 748 | + f"{banner}\n", |
| 749 | + file=sys.stderr, |
| 750 | + flush=True, |
729 | 751 | ) |
730 | 752 |
|
731 | 753 |
|
732 | | -def _is_handwritten_cpp(path) -> bool: |
733 | | - """Whether path is a handwritten C++ source, not a Cython-generated one.""" |
734 | | - path = Path(path) |
735 | | - return path.suffix == ".cpp" and path.resolve().is_relative_to(_PACKAGE_DIR / "_cpp") |
| 754 | +def _get_handwritten_cpp_files(compile_command: Sequence[str]) -> list[Path]: |
| 755 | + """The handwritten C++ sources, not Cython-generated ones, that a compiler command line compiles.""" |
| 756 | + handwritten_dir = (_PACKAGE_DIR / "_cpp").resolve() |
| 757 | + files: list[Path] = [] |
| 758 | + for arg in compile_command: |
| 759 | + # MSVC passes each C++ source as /Tp<path>, which is removed from the result. |
| 760 | + path = Path(arg.removeprefix("/Tp")) |
| 761 | + if path.suffix == ".cpp" and path.resolve().is_relative_to(handwritten_dir): |
| 762 | + files.append(path) |
| 763 | + return files |
| 764 | + |
736 | 765 |
|
| 766 | +@functools.cache |
| 767 | +def _get_implicit_include_dirs(compiler: str) -> list[str]: |
| 768 | + """The include directories that a GCC-style compiler searches without being told. |
| 769 | +
|
| 770 | + They are built into the compiler, so they are not on the command line that |
| 771 | + setuptools runs, and clang-tidy cannot find them for a toolchain such as the |
| 772 | + conda cross-compiler, whose layout it does not know. Examples are the C++ |
| 773 | + standard library and the compiler's own limits.h. They are returned in |
| 774 | + search order. Raises RuntimeError when the compiler does not list them. |
| 775 | + """ |
| 776 | + # MSVC's include directories come from the INCLUDE environment variable, |
| 777 | + # which clang-tidy reads in its MSVC mode. Elsewhere clang-tidy needs the |
| 778 | + # include directories that the compiler has built in. |
| 779 | + if sys.platform == "win32": |
| 780 | + return [] |
737 | 781 |
|
738 | | -def check_with_clang_tidy(compile_command) -> None: |
| 782 | + result = subprocess.run( # noqa: S603 |
| 783 | + [compiler, "-E", "-x", "c++", "-", "-v"], |
| 784 | + input="", |
| 785 | + capture_output=True, |
| 786 | + text=True, |
| 787 | + ) |
| 788 | + dirs: list[str] = [] |
| 789 | + in_list = False |
| 790 | + for line in result.stderr.splitlines(): |
| 791 | + if line.startswith("#include <...> search starts here:"): |
| 792 | + in_list = True |
| 793 | + elif line.startswith("End of search list."): |
| 794 | + break |
| 795 | + elif in_list and not line.endswith("(framework directory)"): |
| 796 | + dirs.append(line.strip()) |
| 797 | + if result.returncode != 0 or not dirs: |
| 798 | + raise RuntimeError(f"Failed to query the implicit include directories of {compiler}:\n{result.stderr}") |
| 799 | + return dirs |
| 800 | + |
| 801 | + |
| 802 | +def check_with_clang_tidy(compile_command: Sequence[str]) -> None: |
739 | 803 | """Run clang-tidy on the handwritten .cpp that a compiler command line compiles. |
740 | 804 |
|
741 | 805 | ``compile_command`` is the exact command that setuptools is about to spawn, |
742 | 806 | so clang-tidy sees the same compiler, include directories, macros and |
743 | 807 | flags as the build. Commands that do not compile a handwritten source are |
744 | 808 | ignored. The configuration is cuda_core/.clang-tidy, which turns every |
745 | 809 | finding into an error. Raises RuntimeError with the findings. |
| 810 | +
|
| 811 | + When clang-tidy is not installed, the check is skipped with a prominent |
| 812 | + warning. |
746 | 813 | """ |
747 | | - sources = [arg for arg in compile_command if _is_handwritten_cpp(arg)] |
| 814 | + sources = _get_handwritten_cpp_files(compile_command) |
748 | 815 | if not sources: |
749 | 816 | return |
750 | 817 | (source,) = sources |
| 818 | + clang_tidy = _find_clang_tidy() |
| 819 | + if clang_tidy is None: |
| 820 | + _warn_clang_tidy_missing() |
| 821 | + return |
751 | 822 | config = Path(__file__).resolve().parent / ".clang-tidy" |
| 823 | + implicit_includes: list[str] = [] |
| 824 | + for include_dir in _get_implicit_include_dirs(compile_command[0]): |
| 825 | + implicit_includes.extend(["-isystem", include_dir]) |
752 | 826 | result = subprocess.run( # noqa: S603 |
753 | | - [_find_clang_tidy(), f"--config-file={config}", "--quiet", source, "--", *compile_command], |
| 827 | + [clang_tidy, f"--config-file={config}", "--quiet", source, "--", *compile_command, *implicit_includes], |
754 | 828 | capture_output=True, |
755 | 829 | text=True, |
756 | 830 | ) |
|
0 commit comments