[llvm] [bazel] Add script for finding files not in the graph (PR #220343)

Jordan Rupprecht via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 14 19:04:04 PDT 2026


================
@@ -0,0 +1,113 @@
+#!/usr/bin/env python3
+
+# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+"""Checks that files in selected source subtrees are Bazel target inputs."""
+
+import subprocess
+import sys
+from fnmatch import fnmatch
+from pathlib import Path, PurePosixPath
+from typing import Optional, Set
+
+BAZEL_REPOSITORY = "@llvm-project"
+SUBTREES = [
+    # TODO: Add more directories as we support most of their files in bazel.
+    # "libc",
+    # "lldb",
+    "lld",
+]
+IGNORED_GLOBS = [
+    "**/*.md",
+    "**/*.txt",
+    "**/*.TXT",  # Both cases are used
+    "**/.clang-format",
+    "**/.clang-tidy",
+    "**/.gitignore",
+    "**/cmake/**",
+    "**/CMakeLists.txt",
+    "**/docs/**",
+    "**/utils/**",
+    "clang/www/**",
+    "lld/test/Unit/lit.cfg.py",  # cmake shims
+    "lld/test/Unit/lit.site.cfg.py.in",  # cmake shims
+]
+
+
+def source_files(project_root: Path) -> Set[str]:
+    files = set()
+    for subtree in SUBTREES:
+        subtree_root = project_root / subtree
+        if not subtree_root.is_dir():
+            raise RuntimeError(f"source subtree does not exist: {subtree_root}")
+        for path in subtree_root.rglob("*"):
+            relative_path = path.relative_to(project_root)
+            source_path = relative_path.as_posix()
+            if path.is_file() and not any(
+                fnmatch(source_path, glob) for glob in IGNORED_GLOBS
+            ):
+                files.add(source_path)
+    return files
+
+
+def label_to_source_path(label: str) -> Optional[str]:
+    repository, separator, remainder = label.partition("//")
+    if not separator or repository != BAZEL_REPOSITORY:
+        return None
+
+    package, separator, target = remainder.partition(":")
+    if not separator:
+        return None
+    return str(PurePosixPath(package, target))
+
+
+def bazel_input_files(bazel_workspace: Path) -> Set[str]:
+    target_patterns = " union ".join(
+        f"{BAZEL_REPOSITORY}//{subtree}/..." for subtree in SUBTREES
+    )
+    query = f'kind("source file", deps({target_patterns}))'
----------------
rupprecht wrote:

We can use `intersect` (or `^`) to restrict bazel query output to only include llvm-project files.

`bazel query "@llvm-project//... ^ deps(@llvm-project//a/... + @llvm-project//b/... + ...)"`

https://github.com/llvm/llvm-project/pull/220343


More information about the llvm-commits mailing list