Add CMake guards to allow building just IREE's runtime (#4279)

The runtime can be built by setting IREE_BUILD_COMPILER=OFF and IREE_ENABLE_MLIR=OFF. Fixes #4155

* Add CMake guards to allow building just IREE's runtime

* Clean up iree_lit_tests and iree_check_tests by adding guards against building them directly, rather than in the files that use them

* Load iree_cmake_extra_content in BUILD files

* Update CMake guards to return from the file to avoid weird indents
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b9f3888..4915a0f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -501,11 +501,7 @@
 add_subdirectory(iree/schemas)
 add_subdirectory(iree/testing)
 add_subdirectory(iree/test)
-
-if(${IREE_ENABLE_MLIR})
-  # The VM requires LLVM to build its op definitions.
-  add_subdirectory(iree/vm)
-endif()
+add_subdirectory(iree/vm)
 
 if(${IREE_BUILD_COMPILER})
   add_subdirectory(iree/compiler)
diff --git a/bindings/javatests/CMakeLists.txt b/bindings/javatests/CMakeLists.txt
index 268f43b..cec74ee 100644
--- a/bindings/javatests/CMakeLists.txt
+++ b/bindings/javatests/CMakeLists.txt
@@ -12,4 +12,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+if (NOT ${IREE_BUILD_TESTS})
+  return()
+endif()
+
 add_subdirectory(com/google/iree)
diff --git a/bindings/javatests/com/google/iree/CMakeLists.txt b/bindings/javatests/com/google/iree/CMakeLists.txt
index 15f9896..1d983f2 100644
--- a/bindings/javatests/com/google/iree/CMakeLists.txt
+++ b/bindings/javatests/com/google/iree/CMakeLists.txt
@@ -12,25 +12,27 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-iree_bytecode_module(
-  NAME
-    simple_mul_bytecode_module
-  SRC
-    "simple_mul.mlir"
-  CC_NAMESPACE
-    "iree::java"
-  FLAGS
-    "-iree-mlir-to-vm-bytecode-module"
-    "-iree-hal-target-backends=vmla"
-)
+if(${IREE_BUILD_COMPILER})
+  iree_bytecode_module(
+    NAME
+      simple_mul_bytecode_module
+    SRC
+      "simple_mul.mlir"
+    CC_NAMESPACE
+      "iree::java"
+    FLAGS
+      "-iree-mlir-to-vm-bytecode-module"
+      "-iree-hal-target-backends=vmla"
+  )
 
-iree_cc_binary(
-  NAME
-    integration_test
-   SRCS
-    "integration_test.cc"
-  DEPS
-     bindings::java::com::google::iree::native::cc_wrappers
-     bindings::javatests::com::google::iree::simple_mul_bytecode_module_cc
-     iree::base::status
-)
+  iree_cc_binary(
+    NAME
+      integration_test
+     SRCS
+      "integration_test.cc"
+    DEPS
+       bindings::java::com::google::iree::native::cc_wrappers
+       bindings::javatests::com::google::iree::simple_mul_bytecode_module_cc
+       iree::base::status
+  )
+endif()
diff --git a/build_tools/cmake/iree_check_test.cmake b/build_tools/cmake/iree_check_test.cmake
index 2974a93..e2a69ed 100644
--- a/build_tools/cmake/iree_check_test.cmake
+++ b/build_tools/cmake/iree_check_test.cmake
@@ -32,7 +32,7 @@
 #   LABELS: Additional labels to apply to the test. The package path and
 #       "driver=${DRIVER}" are added automatically.
 function(iree_check_test)
-  if(NOT IREE_BUILD_TESTS)
+  if(NOT IREE_BUILD_TESTS OR NOT IREE_BUILD_COMPILER)
     return()
   endif()
 
@@ -180,7 +180,7 @@
 #   LABELS: Additional labels to apply to the generated tests. The package path is
 #       added automatically.
 function(iree_check_single_backend_test_suite)
-  if(NOT IREE_BUILD_TESTS)
+  if(NOT IREE_BUILD_TESTS OR NOT IREE_BUILD_COMPILER)
     return()
   endif()
 
@@ -249,7 +249,7 @@
 #   LABELS: Additional labels to apply to the generated tests. The package path is
 #       added automatically.
 function(iree_check_test_suite)
-  if(NOT IREE_BUILD_TESTS)
+  if(NOT IREE_BUILD_TESTS OR NOT IREE_BUILD_COMPILER)
     return()
   endif()
 
diff --git a/build_tools/cmake/iree_lit_test.cmake b/build_tools/cmake/iree_lit_test.cmake
index 1a80cb4..0a36455 100644
--- a/build_tools/cmake/iree_lit_test.cmake
+++ b/build_tools/cmake/iree_lit_test.cmake
@@ -31,7 +31,7 @@
 # TODO(gcmn): allow using alternative driver
 # A driver other than the default iree/tools/run_lit.sh is not currently supported.
 function(iree_lit_test)
-  if(NOT IREE_BUILD_TESTS)
+  if(NOT IREE_BUILD_TESTS OR NOT IREE_BUILD_COMPILER)
     return()
   endif()
 
@@ -108,7 +108,7 @@
 # TODO(gcmn): allow using alternative driver
 # A driver other than the default iree/tools/run_lit.sh is not currently supported.
 function(iree_lit_test_suite)
-  if(NOT IREE_BUILD_TESTS)
+  if(NOT IREE_BUILD_TESTS OR NOT IREE_BUILD_COMPILER)
     return()
   endif()
 
diff --git a/iree/modules/strings/BUILD b/iree/modules/strings/BUILD
index fcb1a40..da6012d 100644
--- a/iree/modules/strings/BUILD
+++ b/iree/modules/strings/BUILD
@@ -1,3 +1,4 @@
+load("//iree:build_defs.oss.bzl", "iree_cmake_extra_content")
 load("//iree/tools:compilation.bzl", "iree_bytecode_module")
 
 package(
@@ -31,6 +32,15 @@
     ],
 )
 
+iree_cmake_extra_content(
+    content = """
+if (NOT ${IREE_BUILD_COMPILER} OR NOT ${IREE_BUILD_TESTS})
+  return()
+endif()
+""",
+    inline = True,
+)
+
 cc_test(
     name = "strings_module_test",
     srcs = ["strings_module_test.cc"],
diff --git a/iree/modules/strings/CMakeLists.txt b/iree/modules/strings/CMakeLists.txt
index 5aac9ba..4dcbcf0 100644
--- a/iree/modules/strings/CMakeLists.txt
+++ b/iree/modules/strings/CMakeLists.txt
@@ -38,6 +38,10 @@
   PUBLIC
 )
 
+if (NOT ${IREE_BUILD_COMPILER} OR NOT ${IREE_BUILD_TESTS})
+  return()
+endif()
+
 iree_cc_test(
   NAME
     strings_module_test
diff --git a/iree/modules/tensorlist/BUILD b/iree/modules/tensorlist/BUILD
index 74deecd..86f1c2a 100644
--- a/iree/modules/tensorlist/BUILD
+++ b/iree/modules/tensorlist/BUILD
@@ -12,6 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+load("//iree:build_defs.oss.bzl", "iree_cmake_extra_content")
 load("//iree/tools:compilation.bzl", "iree_bytecode_module")
 
 package(
@@ -20,6 +21,32 @@
     licenses = ["notice"],  # Apache 2.0
 )
 
+cc_library(
+    name = "native_module",
+    srcs = ["native_module.cc"],
+    hdrs = ["native_module.h"],
+    deps = [
+        "//iree/base:api",
+        "//iree/base:ref_ptr",
+        "//iree/base:status",
+        "//iree/hal:api",
+        "//iree/modules/hal",
+        "//iree/vm",
+        "//iree/vm:cc",
+        "@com_google_absl//absl/container:inlined_vector",
+        "@com_google_absl//absl/types:span",
+    ],
+)
+
+iree_cmake_extra_content(
+    content = """
+if (NOT ${IREE_BUILD_COMPILER} OR NOT ${IREE_BUILD_TESTS})
+  return()
+endif()
+""",
+    inline = True,
+)
+
 iree_bytecode_module(
     name = "tensorlist_test_module",
     src = "tensorlist_test.mlir",
@@ -49,20 +76,3 @@
         "@com_google_absl//absl/types:span",
     ],
 )
-
-cc_library(
-    name = "native_module",
-    srcs = ["native_module.cc"],
-    hdrs = ["native_module.h"],
-    deps = [
-        "//iree/base:api",
-        "//iree/base:ref_ptr",
-        "//iree/base:status",
-        "//iree/hal:api",
-        "//iree/modules/hal",
-        "//iree/vm",
-        "//iree/vm:cc",
-        "@com_google_absl//absl/container:inlined_vector",
-        "@com_google_absl//absl/types:span",
-    ],
-)
diff --git a/iree/modules/tensorlist/CMakeLists.txt b/iree/modules/tensorlist/CMakeLists.txt
index 1092601..a82e5f5 100644
--- a/iree/modules/tensorlist/CMakeLists.txt
+++ b/iree/modules/tensorlist/CMakeLists.txt
@@ -14,6 +14,30 @@
 
 iree_add_all_subdirs()
 
+iree_cc_library(
+  NAME
+    native_module
+  HDRS
+    "native_module.h"
+  SRCS
+    "native_module.cc"
+  DEPS
+    absl::inlined_vector
+    absl::span
+    iree::base::api
+    iree::base::ref_ptr
+    iree::base::status
+    iree::hal::api
+    iree::modules::hal
+    iree::vm
+    iree::vm::cc
+  PUBLIC
+)
+
+if (NOT ${IREE_BUILD_COMPILER} OR NOT ${IREE_BUILD_TESTS})
+  return()
+endif()
+
 iree_bytecode_module(
   NAME
     tensorlist_test_module
@@ -49,23 +73,3 @@
     iree::vm::bytecode_module
     iree::vm::cc
 )
-
-iree_cc_library(
-  NAME
-    native_module
-  HDRS
-    "native_module.h"
-  SRCS
-    "native_module.cc"
-  DEPS
-    absl::inlined_vector
-    absl::span
-    iree::base::api
-    iree::base::ref_ptr
-    iree::base::status
-    iree::hal::api
-    iree::modules::hal
-    iree::vm
-    iree::vm::cc
-  PUBLIC
-)
diff --git a/iree/vm/BUILD b/iree/vm/BUILD
index e613f40..17d82ab 100644
--- a/iree/vm/BUILD
+++ b/iree/vm/BUILD
@@ -12,6 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+load("//iree:build_defs.oss.bzl", "iree_cmake_extra_content")
 load("//iree/tools:compilation.bzl", "iree_bytecode_module")
 
 package(
@@ -212,6 +213,13 @@
 #     ],
 # )
 
+iree_cmake_extra_content(
+    content = """
+if(${IREE_BUILD_COMPILER} AND ${IREE_BUILD_TESTS})
+""",
+    inline = True,
+)
+
 cc_test(
     name = "bytecode_module_test",
     srcs = [
@@ -271,6 +279,13 @@
     flags = ["-iree-vm-ir-to-bytecode-module"],
 )
 
+iree_cmake_extra_content(
+    content = """
+endif()
+""",
+    inline = True,
+)
+
 #===------------------------------------------------------------------------===#
 # Emit-C modules
 #===------------------------------------------------------------------------===#
diff --git a/iree/vm/CMakeLists.txt b/iree/vm/CMakeLists.txt
index dd6d63e..88c3fb7 100644
--- a/iree/vm/CMakeLists.txt
+++ b/iree/vm/CMakeLists.txt
@@ -177,6 +177,8 @@
   PUBLIC
 )
 
+if(${IREE_BUILD_COMPILER} AND ${IREE_BUILD_TESTS})
+
 iree_cc_test(
   NAME
     bytecode_module_test
@@ -247,6 +249,8 @@
   PUBLIC
 )
 
+endif()
+
 iree_cc_library(
   NAME
     c_funcs
diff --git a/iree/vm/test/BUILD b/iree/vm/test/BUILD
index 616efd0..b2ed44d 100644
--- a/iree/vm/test/BUILD
+++ b/iree/vm/test/BUILD
@@ -12,6 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+load("//iree:build_defs.oss.bzl", "iree_cmake_extra_content")
 load("//iree/tools:compilation.bzl", "iree_bytecode_module")
 load("//build_tools/embed_data:build_defs.bzl", "cc_embed_data")
 
@@ -21,6 +22,15 @@
     licenses = ["notice"],  # Apache 2.0
 )
 
+iree_cmake_extra_content(
+    content = """
+if (NOT ${IREE_BUILD_COMPILER} OR NOT ${IREE_BUILD_TESTS})
+  return()
+endif()
+""",
+    inline = True,
+)
+
 cc_embed_data(
     name = "all_bytecode_modules_cc",
     srcs = [
diff --git a/iree/vm/test/CMakeLists.txt b/iree/vm/test/CMakeLists.txt
index 282b4cf..3f86f3f 100644
--- a/iree/vm/test/CMakeLists.txt
+++ b/iree/vm/test/CMakeLists.txt
@@ -14,6 +14,10 @@
 
 iree_add_all_subdirs()
 
+if (NOT ${IREE_BUILD_COMPILER} OR NOT ${IREE_BUILD_TESTS})
+  return()
+endif()
+
 iree_cc_embed_data(
   NAME
     all_bytecode_modules_cc