Bazel to CMake: Don't evaluate glob during conversion

Evaluating during conversion seemed like a good idea but at least right now it's causing issues because people don't think to update a CMakeLists file when they haven't changed a build file. This does limit our globs more (no excludes), but I think for the purpose we actually use globs shouldn't be too much of a problem. We can revert if we've got some better automation to run bazel to cmake at some point.

Includes some cleanup of convert_unimplemented_function to make it more applicable to things that aren't rules. I tried out having it print out the entire set of args and kwargs the function was called with but actually getting this right would mean passing all these to the function and then to do that properly you have to use inspect on the functions and it ended up seeming a bit gross. Instead just allowed a free-form string message explaining the error.

Tested:
The same 8 tests fail before and after
95% tests passed, 8 tests failed out of 168

Total Test time (real) =  38.29 sec

The following tests FAILED:
	 74 - iree_compiler_Dialect_HAL_Target_test_lit_smoketest.mlir_test (Failed)
	 75 - iree_compiler_Dialect_HAL_Transforms_test_lit_transformation.mlir_test (Failed)
	158 - iree_compiler_Translation_SPIRV_LinalgToSPIRV_test_lit_single_pw_op.mlir_test (Failed)
	163 - iree_compiler_Translation_test_lit_do_not_optimize.mlir_test (Failed)
	165 - iree_tools_vm_util_test (Failed)
	166 - iree_tools_test_lit_multiple_args.mlir_test (Failed)
	167 - iree_tools_test_lit_scalars.mlir_test (Failed)
	168 - iree_tools_test_lit_simple.mlir_test (Failed)

Closes https://github.com/google/iree/pull/674

COPYBARA_INTEGRATE_REVIEW=https://github.com/google/iree/pull/674 from GMNGeoffrey:cmake-glob 2c683c522b4c4d31a0bc441455eea3f66886bed8
PiperOrigin-RevId: 293264667
diff --git a/build_tools/bazel_to_cmake/bazel_to_cmake.py b/build_tools/bazel_to_cmake/bazel_to_cmake.py
index 48dc512..c15b9b8 100755
--- a/build_tools/bazel_to_cmake/bazel_to_cmake.py
+++ b/build_tools/bazel_to_cmake/bazel_to_cmake.py
@@ -255,12 +255,14 @@
     deps_list = "\n".join(["    %s" % (dep,) for dep in deps_list])
     return "  DEPS\n%s\n" % (deps_list,)
 
-  def _convert_unimplemented_function(self, rule, *args, **kwargs):
-    name = kwargs.get("name", "unnamed")
-    message = "Unimplemented %(rule)s %(name)s\n" % {"rule": rule, "name": name}
+  def _convert_unimplemented_function(self, function, details=""):
+    message = "Unimplemented %(function)s: %(details)s" % {
+        "function": function,
+        "details": details,
+    }
     if not self.converter.first_error:
       self.converter.first_error = NotImplementedError(message)
-    self.converter.body += "# %s" % (message,)
+    self.converter.body += "# %s\n" % (message,)
 
   # ------------------------------------------------------------------------- #
   # Function handlers that convert BUILD definitions to CMake definitions.    #
@@ -280,11 +282,11 @@
   def iree_build_test(self, **kwargs):
     pass
 
-  def filegroup(self, **kwargs):
+  def filegroup(self, name, **kwargs):
     # Not implemented yet. Might be a no-op, or may want to evaluate the srcs
     # attribute and pass them along to any targets that depend on the filegroup.
     # Cross-package dependencies and complicated globs could be hard to handle.
-    self._convert_unimplemented_function("filegroup", **kwargs)
+    self._convert_unimplemented_function("filegroup", name)
 
   def exports_files(self, *args, **kwargs):
     # No mapping to CMake, ignore.
@@ -297,9 +299,11 @@
     # See https://cmake.org/cmake/help/v3.12/command/file.html#filesystem
 
     if exclude_directories != 1:
-      raise NotImplementedError("Non-default exclude_directories not supported")
+      self._convert_unimplemented_function("glob", "with exclude")
+    if exclude:
+      self._convert_unimplemented_function("glob", "with exclude_directories")
 
-    filepaths = []
+    glob_vars = []
     for pattern in include:
       if "**" in pattern:
         # bazel's glob has some specific restrictions about crossing package boundaries.
@@ -307,23 +311,15 @@
         # silently give different behavior, just error out.
         # See https://docs.bazel.build/versions/master/be/functions.html#glob
         raise NotImplementedError("Recursive globs not supported")
-
-      filepaths += glob.glob(self.converter.directory_path + "/" + pattern)
-
-    exclude_filepaths = set([])
-    for pattern in exclude:
-      if "**" in pattern:
-        # See comment above
-        raise NotImplementedError("Recursive globs not supported")
-      exclude_filepaths.update(
-          glob.glob(self.converter.directory_path + "/" + pattern))
-
-    basenames = sorted([
-        os.path.basename(path)
-        for path in filepaths
-        if path not in exclude_filepaths
-    ])
-    return basenames
+      # Bazel `*.mlir` glob -> CMake Variable `_GLOB_X_MLIR`
+      glob_var = "_GLOB_" + pattern.replace("*", "X").replace(".", "_").upper()
+      glob_vars.append("${%s}" % (glob_var,))
+      self.converter.body += ("file(GLOB %(var)s CONFIGURE_DEPENDS "
+                              "%(pattern)s)\n") % {
+                                  "var": glob_var,
+                                  "pattern": pattern
+                              }
+    return glob_vars
 
   def config_setting(self, **kwargs):
     # No mapping to CMake, ignore.
@@ -395,8 +391,8 @@
                     identifier=None,
                     **kwargs):
     if identifier:
-      self._convert_unimplemented_function(
-          "cc_embed_data (identifier)", name=name)
+      self._convert_unimplemented_function("cc_embed_data",
+                                           name + " has identifier")
     name_block = self._convert_name_block(name)
     srcs_block = self._convert_srcs_block(srcs)
     cc_file_output_block = self._convert_cc_file_output_block(cc_file_output)