Skip to content

Commit 8ad4030

Browse files
committed
Address comments in the review, and make clang-tidy work in the pixi build
1 parent 30f1ad1 commit 8ad4030

5 files changed

Lines changed: 102 additions & 27 deletions

File tree

‎cuda_core/.clang-tidy‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,5 @@ CheckOptions:
6666
misc-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic: true
6767
WarningsAsErrors: '*'
6868
# Only report diagnostics for our own headers, not CUDA/CPython/libstdc++.
69-
HeaderFilterRegex: 'cuda/core/_(cpp|include)/'
70-
ExcludeHeaderFilterRegex: 'cuda/core/_include/(dlpack|aoti_shim)\.h'
69+
HeaderFilterRegex: 'cuda[/\\]core[/\\]_(cpp|include)[/\\]'
70+
ExcludeHeaderFilterRegex: 'cuda[/\\]core[/\\]_include[/\\](dlpack|aoti_shim)\.h'

‎cuda_core/build_hooks.py‎

Lines changed: 97 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
import re
1515
import subprocess
1616
import sys
17+
import sysconfig
1718
import tempfile
1819
import zipfile
20+
from collections.abc import Sequence
1921
from pathlib import Path
2022

2123
import Cython as _Cython
@@ -705,52 +707,124 @@ def build_wheel(wheel_directory, config_settings=None, metadata_directory=None):
705707
return wheel_name
706708

707709

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.
710712
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.
712717
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.
716720
"""
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]
717727
spec = importlib.util.find_spec("clang_tidy")
718728
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,
729751
)
730752

731753

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+
736765

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 []
737781

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:
739803
"""Run clang-tidy on the handwritten .cpp that a compiler command line compiles.
740804
741805
``compile_command`` is the exact command that setuptools is about to spawn,
742806
so clang-tidy sees the same compiler, include directories, macros and
743807
flags as the build. Commands that do not compile a handwritten source are
744808
ignored. The configuration is cuda_core/.clang-tidy, which turns every
745809
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.
746813
"""
747-
sources = [arg for arg in compile_command if _is_handwritten_cpp(arg)]
814+
sources = _get_handwritten_cpp_files(compile_command)
748815
if not sources:
749816
return
750817
(source,) = sources
818+
clang_tidy = _find_clang_tidy()
819+
if clang_tidy is None:
820+
_warn_clang_tidy_missing()
821+
return
751822
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])
752826
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],
754828
capture_output=True,
755829
text=True,
756830
)

‎cuda_core/cuda/core/_cpp/rt/py.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace cuda_core::rt {
1313

14-
#if PY_VERSION_HEX < 0x030D0000
14+
#if PY_VERSION_HEX < 0x030D0000 && defined(Py_LIMITED_API)
1515
extern "C" int _Py_IsFinalizing(void);
1616
#endif
1717

‎cuda_core/pixi.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ cython = ">=3.2.5,<3.3"
270270
cuda-nvrtc-dev = "*"
271271
cuda-bindings = "*"
272272
dlpack = "*"
273+
clang-tools = "==22.1.8"
273274

274275
[package.target.linux-64.host-dependencies]
275276
cuda-cudart-dev_linux-64 = "*"

‎cuda_core/pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ requires = [
88
"setuptools-scm[simple]>=8,!=10.1",
99
"Cython>=3.2.5,<3.3",
1010
"cuda-pathfinder>=1.5",
11-
"clang-tidy==22.1.8",
11+
"clang-tidy==22.1.8; sys_platform != 'win32' or platform_machine != 'ARM64'",
1212
]
1313
build-backend = "build_hooks"
1414
backend-path = ["."]

0 commit comments

Comments
 (0)