[bazel_to_cmake] Check enforce_glob lists, including on added test files (#25362)

A test file missing from an `enforce_glob` list fails only the Bazel CI
job, with an error that does not name the file, as in #25122. The
generated CMake build leaves the test out without any error.

`bazel_to_cmake` already reads every `BUILD.bazel` without running
Bazel, so it can check the lists both before commit and in the lint job.
The existing hooks run it only when `BUILD.bazel` changes. Adding a test
file does not change `BUILD.bazel`, so the new hook also runs it on test
files.

Assisted-By: Claude

---------

Signed-off-by: Jelle Schühmacher <schuehmacher@roofline.ai>
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index ec3c99e..4108ada 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -116,6 +116,12 @@
         language: python
         entry: ./build_tools/bazel_to_cmake/bazel_to_cmake.py
         files: "^(compiler|core|runtime|samples|tests|tools)/(.*/)?(CMakeLists.txt)$"
+      # Adding a test file leaves BUILD.bazel unchanged, so the hooks above miss it.
+      - id: check_enforce_glob
+        name: Check enforce_glob lists against added test files
+        language: python
+        entry: ./build_tools/bazel_to_cmake/bazel_to_cmake.py
+        files: '^(compiler|core|runtime|samples|tests|tools)/(.*/)?[^/]+\.(mlir|td)$'
 
       - id: amdgpu_target_map
         name: Check AMDGPU target map generated files
diff --git a/build_tools/bazel_to_cmake/bazel_to_cmake.py b/build_tools/bazel_to_cmake/bazel_to_cmake.py
index 4964a6a..34355c8 100755
--- a/build_tools/bazel_to_cmake/bazel_to_cmake.py
+++ b/build_tools/bazel_to_cmake/bazel_to_cmake.py
@@ -327,6 +327,9 @@
             indent=2,
         )
         return Status.FAILED
+    except ValueError as e:
+        log(f"ERROR generating {rel_dir_path}.\nReason: {e}", indent=2)
+        return Status.FAILED
     converted_content = (
         preserved_header + header + converted_build_file + preserved_footer
     )
@@ -367,7 +370,7 @@
 
     if args.paths:
         try:
-            directories = [path_to_directory(path) for path in args.paths]
+            directories = list({path_to_directory(path) for path in args.paths})
         except FileNotFoundError as e:
             log(f"ERROR: {e}")
             sys.exit(1)
diff --git a/build_tools/bazel_to_cmake/bazel_to_cmake_converter.py b/build_tools/bazel_to_cmake/bazel_to_cmake_converter.py
index 31e0e1d..17685e5 100644
--- a/build_tools/bazel_to_cmake/bazel_to_cmake_converter.py
+++ b/build_tools/bazel_to_cmake/bazel_to_cmake_converter.py
@@ -16,6 +16,7 @@
 import itertools
 import re
 import os
+import pathlib
 
 import bazel_to_cmake_targets
 
@@ -817,7 +818,28 @@
         """Direct handler for iree_wasm_cc_test (loaded from .bzl)."""
         self.wasm_cc_test(**kwargs)
 
-    def enforce_glob(self, files, **kwargs):
+    def enforce_glob(self, files, include, exclude=None, **kwargs):
+        # Mirrors build_tools/bazel/enforce_glob.bzl so a stale list fails here.
+        build_dir = pathlib.Path(self._build_dir)
+        matched = set()
+        for pattern in include:
+            matched.update(
+                p.relative_to(build_dir).as_posix()
+                for p in build_dir.glob(pattern)
+                if p.is_file()
+            )
+        for pattern in exclude or []:
+            matched.difference_update(
+                p.relative_to(build_dir).as_posix() for p in build_dir.glob(pattern)
+            )
+        matched.discard("CMakeLists.txt")
+        missing = sorted(matched - set(files))
+        extra = sorted(set(files) - matched)
+        if missing or extra:
+            raise ValueError(
+                f"enforce_glob list does not match {include}."
+                f" Missing {missing}. Extra {extra}."
+            )
         return files
 
     def glob(self, include, exclude=None, exclude_directories=1):