Create Starlark rule for shared-binary tests.

This allows running tests using a prebuilt binary with different arguments and data dependencies. It avoids a pass-through shell script which causes issues on some platforms.

PiperOrigin-RevId: 302484282
diff --git a/build_tools/bazel/run_binary_test.bzl b/build_tools/bazel/run_binary_test.bzl
new file mode 100644
index 0000000..b1e31b7
--- /dev/null
+++ b/build_tools/bazel/run_binary_test.bzl
@@ -0,0 +1,62 @@
+# Copyright 2020 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Creates a test from the binary output of another rule.
+
+The rule instantiation can pass additional arguments to the binary and provide
+it with additional data files (as well as the standard bazel test classification
+attiributes). This allows compiling the binary once and not recompiling or
+relinking it for each test rule. It also avoids a wrapper shell script, which
+adds unnecessary shell dependencies and confuses some tooling about the type of
+the binary.
+
+Example usage:
+
+run_binary_test(
+    name = "my_test",
+    args = ["--input_file=$(location :data_file)"],
+    data = [":data_file"],
+    test_binary = ":some_cc_binary",
+)
+"""
+
+def _run_binary_test_impl(ctx):
+    ctx.actions.run_shell(
+        inputs = [ctx.file.test_binary],
+        outputs = [ctx.outputs.executable],
+        command = "cp $1 $2",
+        arguments = [ctx.file.test_binary.path, ctx.outputs.executable.path],
+        mnemonic = "CopyExecutable",
+    )
+
+    data_runfiles = ctx.runfiles(files = ctx.files.data)
+
+    binary_runfiles = ctx.attr.test_binary[DefaultInfo].default_runfiles
+
+    return [DefaultInfo(
+        executable = ctx.outputs.executable,
+        runfiles = data_runfiles.merge(binary_runfiles),
+    )]
+
+run_binary_test = rule(
+    _run_binary_test_impl,
+    attrs = {
+        "test_binary": attr.label(
+            mandatory = True,
+            allow_single_file = True,
+        ),
+        "data": attr.label_list(allow_empty = True, allow_files = True),
+    },
+    test = True,
+)
diff --git a/iree/modules/check/BUILD b/iree/modules/check/BUILD
index 1a9a4ca..4616e1d 100644
--- a/iree/modules/check/BUILD
+++ b/iree/modules/check/BUILD
@@ -64,8 +64,6 @@
     ] + PLATFORM_VULKAN_DEPS + IREE_DRIVER_MODULES,
 )
 
-exports_files(["check_module.sh"])
-
 cc_library(
     name = "native_module",
     testonly = True,
diff --git a/iree/modules/check/check_module.sh b/iree/modules/check/check_module.sh
deleted file mode 100755
index 0e8203b..0000000
--- a/iree/modules/check/check_module.sh
+++ /dev/null
@@ -1,18 +0,0 @@
-#!/bin/bash
-
-# Copyright 2020 Google LLC
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#      https://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-
-"$TEST_SRCDIR/iree_core/iree/modules/check/iree-check-module" "$@"
diff --git a/iree/modules/check/test/BUILD b/iree/modules/check/test/BUILD
index 0fa6fa5..bfe4411 100644
--- a/iree/modules/check/test/BUILD
+++ b/iree/modules/check/test/BUILD
@@ -14,6 +14,7 @@
 
 load("//iree/tools:compilation.bzl", "iree_bytecode_module")
 load("//iree:lit_test.bzl", "iree_lit_test_suite")
+load("//build_tools/bazel:run_binary_test.bzl", "run_binary_test")
 
 package(
     default_visibility = ["//visibility:public"],
@@ -37,17 +38,14 @@
     translate_tool = "//iree/compiler/Dialect/Modules/Check:check-translate",
 )
 
-sh_test(
+run_binary_test(
     name = "success_test",
-    srcs = ["//iree/modules/check:check_module.sh"],
     args = [
         "--driver=vmla",
         "--input_file=$(location :success_module)",
     ],
-    data = [
-        ":success_module",
-        "//iree/modules/check:iree-check-module",
-    ],
+    data = [":success_module"],
+    test_binary = "//iree/modules/check:iree-check-module",
 )
 
 iree_bytecode_module(
@@ -57,16 +55,13 @@
     translate_tool = "//iree/compiler/Dialect/Modules/Check:check-translate",
 )
 
-sh_test(
+run_binary_test(
     name = "failure_test",
-    srcs = ["//iree/modules/check:check_module.sh"],
     args = [
         "--driver=vmla",
         "--input_file=$(location :failure_module)",
         "--expect_failure",
     ],
-    data = [
-        ":failure_module",
-        "//iree/modules/check:iree-check-module",
-    ],
+    data = [":failure_module"],
+    test_binary = "//iree/modules/check:iree-check-module",
 )