Wrap all uses of SPIRV-Cross in #define.

Put all cross-platform code references to Spirv-Cross behind
ifdef DAWN_USE_SPIRV_CROSS. Hide build dependencies behind
dawn_use_spirv_cross, which itself is set from dawn_enable_opengl,
since it is the only backend which is still using SPIRV-Cross.

Bug: dawn:1036

Change-Id: Id61512230b50a32c63827a16fece40d7899968d8
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/61400
Commit-Queue: Kai Ninomiya <kainino@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
diff --git a/scripts/dawn_features.gni b/scripts/dawn_features.gni
index ac9531c..f3be9a9 100644
--- a/scripts/dawn_features.gni
+++ b/scripts/dawn_features.gni
@@ -104,5 +104,9 @@
 dawn_supports_glfw_for_windowing =
     (is_win && !dawn_is_winuwp) || (is_linux && !is_chromeos) || is_mac
 
-# Much of the backend code is shared, so define a convenience var.
+# Much of the GL backend code is shared, so define a convenience var.
 dawn_enable_opengl = dawn_enable_opengles || dawn_enable_desktop_gl
+
+# The GL backends are the last to use SPIRV-Cross, so only compile it in
+# if they are enabled.
+dawn_use_spirv_cross = dawn_enable_opengl
diff --git a/src/common/BUILD.gn b/src/common/BUILD.gn
index afaa18f..ebb7018 100644
--- a/src/common/BUILD.gn
+++ b/src/common/BUILD.gn
@@ -85,6 +85,10 @@
     defines += [ "DAWN_USE_X11" ]
   }
 
+  if (dawn_use_spirv_cross) {
+    defines += [ "DAWN_USE_SPIRV_CROSS" ]
+  }
+
   if (dawn_enable_error_injection) {
     defines += [ "DAWN_ENABLE_ERROR_INJECTION" ]
   }
diff --git a/src/dawn_native/BUILD.gn b/src/dawn_native/BUILD.gn
index f73bfe2..27731a5 100644
--- a/src/dawn_native/BUILD.gn
+++ b/src/dawn_native/BUILD.gn
@@ -145,11 +145,13 @@
     ":dawn_native_headers",
     ":dawn_native_utils_gen",
     "${dawn_root}/src/common",
-    "${dawn_root}/third_party/gn/spirv_cross:spirv_cross",
     "${dawn_spirv_tools_dir}:spvtools_opt",
     "${dawn_spirv_tools_dir}:spvtools_val",
     "${dawn_tint_dir}/src:libtint",
   ]
+  if (dawn_use_spirv_cross) {
+    deps += [ "${dawn_root}/third_party/gn/spirv_cross:spirv_cross" ]
+  }
   defines = []
   libs = []
   data_deps = []
@@ -282,8 +284,6 @@
     "Sampler.h",
     "ShaderModule.cpp",
     "ShaderModule.h",
-    "SpirvUtils.cpp",
-    "SpirvUtils.h",
     "StagingBuffer.cpp",
     "StagingBuffer.h",
     "Subresource.cpp",
@@ -313,6 +313,13 @@
     ]
   }
 
+  if (dawn_use_spirv_cross) {
+    sources += [
+      "SpirvUtils.cpp",
+      "SpirvUtils.h",
+    ]
+  }
+
   # Only win32 app needs to link with user32.lib
   # In UWP, all availiable APIs are defined in WindowsApp.lib
   if (is_win && !dawn_is_winuwp) {
diff --git a/src/dawn_native/ShaderModule.cpp b/src/dawn_native/ShaderModule.cpp
index 9db81ed..87af445 100644
--- a/src/dawn_native/ShaderModule.cpp
+++ b/src/dawn_native/ShaderModule.cpp
@@ -24,12 +24,16 @@
 #include "dawn_native/Pipeline.h"
 #include "dawn_native/PipelineLayout.h"
 #include "dawn_native/RenderPipeline.h"
-#include "dawn_native/SpirvUtils.h"
+#if defined(DAWN_USE_SPIRV_CROSS)
+#    include "dawn_native/SpirvUtils.h"
+#endif
 #include "dawn_native/TintUtils.h"
 
 #include <spirv-tools/libspirv.hpp>
 #include <spirv-tools/optimizer.hpp>
-#include <spirv_cross.hpp>
+#if defined(DAWN_USE_SPIRV_CROSS)
+#    include <spirv_cross.hpp>
+#endif
 
 // Tint include must be after spirv_cross.hpp, because spirv-cross has its own
 // version of spirv_headers. We also need to undef SPV_REVISION because SPIRV-Cross
@@ -529,44 +533,6 @@
             return requiredBufferSizes;
         }
 
-        ResultOrError<std::vector<uint32_t>> RunRobustBufferAccessPass(
-            const std::vector<uint32_t>& spirv) {
-            spvtools::Optimizer opt(SPV_ENV_VULKAN_1_1);
-
-            std::ostringstream errorStream;
-            errorStream << "SPIRV Optimizer failure:" << std::endl;
-            opt.SetMessageConsumer([&errorStream](spv_message_level_t level, const char*,
-                                                  const spv_position_t& position,
-                                                  const char* message) {
-                switch (level) {
-                    case SPV_MSG_FATAL:
-                    case SPV_MSG_INTERNAL_ERROR:
-                    case SPV_MSG_ERROR:
-                        errorStream << "error: line " << position.index << ": " << message
-                                    << std::endl;
-                        break;
-                    case SPV_MSG_WARNING:
-                        errorStream << "warning: line " << position.index << ": " << message
-                                    << std::endl;
-                        break;
-                    case SPV_MSG_INFO:
-                        errorStream << "info: line " << position.index << ": " << message
-                                    << std::endl;
-                        break;
-                    default:
-                        break;
-                }
-            });
-            opt.RegisterPass(spvtools::CreateGraphicsRobustAccessPass());
-
-            std::vector<uint32_t> result;
-            if (!opt.Run(spirv.data(), spirv.size(), &result, spvtools::ValidatorOptions(),
-                         false)) {
-                return DAWN_VALIDATION_ERROR(errorStream.str().c_str());
-            }
-            return std::move(result);
-        }
-
         MaybeError ValidateCompatibilityWithBindGroupLayout(DeviceBase* device,
                                                             BindGroupIndex group,
                                                             const EntryPointMetadata& entryPoint,
@@ -713,6 +679,7 @@
             return {};
         }
 
+#if defined(DAWN_USE_SPIRV_CROSS)
         ResultOrError<std::unique_ptr<EntryPointMetadata>> ExtractSpirvInfo(
             const DeviceBase* device,
             const spirv_cross::Compiler& compiler,
@@ -956,6 +923,7 @@
 
             return {std::move(metadata)};
         }
+#endif
 
         ResultOrError<EntryPointMetadataTable> ReflectShaderUsingTint(
             DeviceBase*,
@@ -1656,21 +1624,11 @@
         mTintSource = std::move(parseResult->tintSource);
         mSpirv = std::move(parseResult->spirv);
 
-        if (GetDevice()->IsToggleEnabled(Toggle::UseTintGenerator)) {
-            DAWN_TRY_ASSIGN(mEntryPoints, ReflectShaderUsingTint(GetDevice(), mTintProgram.get()));
-        } else {
-            // If not using Tint to generate backend code, run the robust buffer access pass now
-            // since all backends will use this SPIR-V. If Tint is used, the robustness pass should
-            // be run per-backend.
-            if (GetDevice()->IsRobustnessEnabled()) {
-                DAWN_TRY_ASSIGN(mSpirv, RunRobustBufferAccessPass(mSpirv));
-            }
-            DAWN_TRY_ASSIGN(mEntryPoints, ReflectShaderUsingSPIRVCross(GetDevice(), mSpirv));
-        }
-
+        DAWN_TRY_ASSIGN(mEntryPoints, ReflectShaderUsingTint(GetDevice(), mTintProgram.get()));
         return {};
     }
 
+#if defined(DAWN_USE_SPIRV_CROSS)
     ResultOrError<EntryPointMetadataTable> ShaderModuleBase::ReflectShaderUsingSPIRVCross(
         DeviceBase* device,
         const std::vector<uint32_t>& spirv) {
@@ -1688,6 +1646,7 @@
         }
         return std::move(result);
     }
+#endif
 
     size_t PipelineLayoutEntryPointPairHashFunc::operator()(
         const PipelineLayoutEntryPointPair& pair) const {
diff --git a/src/dawn_native/ShaderModule.h b/src/dawn_native/ShaderModule.h
index c8ad650..7d92c42 100644
--- a/src/dawn_native/ShaderModule.h
+++ b/src/dawn_native/ShaderModule.h
@@ -240,9 +240,11 @@
 
       protected:
         MaybeError InitializeBase(ShaderModuleParseResult* parseResult);
+#if defined(DAWN_USE_SPIRV_CROSS)
         static ResultOrError<EntryPointMetadataTable> ReflectShaderUsingSPIRVCross(
             DeviceBase* device,
             const std::vector<uint32_t>& spirv);
+#endif
 
       private:
         ShaderModuleBase(DeviceBase* device, ObjectBase::ErrorTag tag);
diff --git a/src/dawn_native/SpirvUtils.h b/src/dawn_native/SpirvUtils.h
index ff356df..3719794 100644
--- a/src/dawn_native/SpirvUtils.h
+++ b/src/dawn_native/SpirvUtils.h
@@ -18,6 +18,10 @@
 #ifndef DAWNNATIVE_SPIRV_UTILS_H_
 #define DAWNNATIVE_SPIRV_UTILS_H_
 
+#if !defined(DAWN_USE_SPIRV_CROSS)
+#    error "SpirvCross.h should not be included if dawn_use_spirv_cross is false"
+#endif
+
 #include "dawn_native/Format.h"
 #include "dawn_native/PerStage.h"
 #include "dawn_native/VertexFormat.h"
diff --git a/src/dawn_native/null/DeviceNull.cpp b/src/dawn_native/null/DeviceNull.cpp
index 06945e4..6baad31 100644
--- a/src/dawn_native/null/DeviceNull.cpp
+++ b/src/dawn_native/null/DeviceNull.cpp
@@ -20,8 +20,6 @@
 #include "dawn_native/Instance.h"
 #include "dawn_native/Surface.h"
 
-#include <spirv_cross.hpp>
-
 namespace dawn_native { namespace null {
 
     // Implementation of pre-Device objects: the null adapter, null backend connection and Connect()