Add support for SPIRV writing to BUILD.gn

BUG=tint:48

Change-Id: I6b92bb372b783ad4498fdc4817d0893851b0bb90
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/19680
Reviewed-by: dan sinclair <dsinclair@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index aebdacf..af7b8cb 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -26,6 +26,12 @@
   } else {
     defines += [ "TINT_BUILD_SPV_READER=0" ]
   }
+
+  if (tint_build_spv_writer) {
+    defines += [ "TINT_BUILD_SPV_WRITER=1" ]
+  } else {
+    defines += [ "TINT_BUILD_SPV_WRITER=0" ]
+  }
 }
 
 # libtint source sets are divided into a non-optional core in :libtint_core and
@@ -244,12 +250,42 @@
   }
 }
 
+source_set("libtint_spv_writer") {
+  sources = [
+    "src/writer/spirv/binary_writer.cc",
+    "src/writer/spirv/binary_writer.h",
+    "src/writer/spirv/builder.cc",
+    "src/writer/spirv/builder.h",
+    "src/writer/spirv/function.cc",
+    "src/writer/spirv/function.h",
+    "src/writer/spirv/generator.cc",
+    "src/writer/spirv/generator.h",
+    "src/writer/spirv/instruction.cc",
+    "src/writer/spirv/instruction.h",
+    "src/writer/spirv/operand.cc",
+    "src/writer/spirv/operand.h",
+  ]
+
+  deps = [ "${tint_spirv_headers_dir}/:spv_headers" ]
+
+  configs += [ ":tint_common_config" ]
+
+  if (build_with_chromium) {
+    configs -= [ "//build/config/compiler:chromium_code" ]
+    configs += [ "//build/config/compiler:no_chromium_code" ]
+  }
+}
+
 source_set("libtint") {
   deps = [ ":libtint_core" ]
   if (tint_build_spv_reader) {
     deps += [ ":libtint_spv_reader" ]
   }
 
+  if (tint_build_spv_writer) {
+    deps += [ ":libtint_spv_writer" ]
+  }
+
   configs += [ ":tint_common_config" ]
 
   if (build_with_chromium) {
diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc
index 540de5c..c388e70 100644
--- a/src/writer/spirv/builder.cc
+++ b/src/writer/spirv/builder.cc
@@ -223,7 +223,7 @@
     error_ = "missing generated ID for variable";
     return 0;
   }
-  auto var = spirv_id_to_variable_[id];
+  auto* var = spirv_id_to_variable_[id];
   if (var->is_const()) {
     return id;
   }
@@ -457,7 +457,7 @@
     return GenerateLiteralIfNeeded(expr->AsScalarConstructor()->literal());
   }
   if (expr->IsTypeConstructor()) {
-    auto init = expr->AsTypeConstructor();
+    auto* init = expr->AsTypeConstructor();
     auto type_id = GenerateTypeIfNeeded(init->type());
     if (type_id == 0) {
       return 0;
@@ -568,7 +568,7 @@
 
   // Handle int and float and the vectors of those types. Other types
   // should have been rejected by validation.
-  auto lhs_type = expr->lhs()->result_type();
+  auto* lhs_type = expr->lhs()->result_type();
   bool lhs_is_float_or_vec =
       lhs_type->IsF32() ||
       (lhs_type->IsVector() && lhs_type->AsVector()->type()->IsF32());
@@ -888,7 +888,7 @@
 bool Builder::GenerateStructType(ast::type::StructType* struct_type,
                                  const Operand& result) {
   auto struct_id = result.to_i();
-  auto impl = struct_type->impl();
+  auto* impl = struct_type->impl();
 
   if (!struct_type->name().empty()) {
     push_debug(spv::Op::OpName,
diff --git a/src/writer/spirv/function.cc b/src/writer/spirv/function.cc
index 2b4dae2..b040ff0 100644
--- a/src/writer/spirv/function.cc
+++ b/src/writer/spirv/function.cc
@@ -27,6 +27,8 @@
                    const std::vector<Instruction>& params)
     : declaration_(declaration), label_op_(label_op), params_(params) {}
 
+Function::Function(const Function& other) = default;
+
 Function::~Function() = default;
 
 void Function::iterate(std::function<void(const Instruction&)> cb) const {
diff --git a/src/writer/spirv/function.h b/src/writer/spirv/function.h
index 65eb394..26ddfa7 100644
--- a/src/writer/spirv/function.h
+++ b/src/writer/spirv/function.h
@@ -42,7 +42,7 @@
            const std::vector<Instruction>& params);
   /// Copy constructor
   /// @param other the function to copy
-  Function(const Function& other) = default;
+  Function(const Function& other);
   ~Function();
 
   /// Iterates over the function call the cb on each instruction
diff --git a/src/writer/spirv/instruction.cc b/src/writer/spirv/instruction.cc
index d4994d6..78f6afa 100644
--- a/src/writer/spirv/instruction.cc
+++ b/src/writer/spirv/instruction.cc
@@ -23,6 +23,8 @@
 Instruction::Instruction(spv::Op op, std::vector<Operand> operands)
     : op_(op), operands_(std::move(operands)) {}
 
+Instruction::Instruction(const Instruction&) = default;
+
 Instruction::~Instruction() = default;
 
 uint32_t Instruction::word_length() const {
diff --git a/src/writer/spirv/instruction.h b/src/writer/spirv/instruction.h
index 4632eeb..c321310 100644
--- a/src/writer/spirv/instruction.h
+++ b/src/writer/spirv/instruction.h
@@ -32,7 +32,7 @@
   /// @param operands the operand values for the instruction
   Instruction(spv::Op op, std::vector<Operand> operands);
   /// Copy Constructor
-  Instruction(const Instruction&) = default;
+  Instruction(const Instruction&);
   ~Instruction();
 
   /// @returns the instructions op
diff --git a/tint_overrides_with_defaults.gni b/tint_overrides_with_defaults.gni
index dc7d731..7fb33e5 100644
--- a/tint_overrides_with_defaults.gni
+++ b/tint_overrides_with_defaults.gni
@@ -41,6 +41,11 @@
     tint_build_spv_reader = false
   }
 
+  # Build the SPIR-V output writer
+  if (!defined(tint_build_spv_writer)) {
+    tint_build_spv_writer = false
+  }
+
   # TODO(rharrison): Implement support for the reset of the reader/writers
 
   # Generate fuzzers