Migrate target generators to use the new target overload

Change-Id: If728c58750192605d535f0b8e71610fc6a6a6964
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/29621
Reviewed-by: Takuto Ikuta <tikuta@google.com>
Commit-Queue: Matt Stark <msta@google.com>
diff --git a/src/gn/action_target_generator.cc b/src/gn/action_target_generator.cc
index 443d18c..3bdedf9 100644
--- a/src/gn/action_target_generator.cc
+++ b/src/gn/action_target_generator.cc
@@ -22,18 +22,16 @@
     Target* target,
     Scope* scope,
     const FunctionCallNode* function_call,
-    Target::OutputType type,
     Err* err)
-    : TargetGenerator(target, scope, function_call, err), output_type_(type) {}
+    : TargetGenerator(target, scope, function_call, err) {}
 
 ActionTargetGenerator::~ActionTargetGenerator() = default;
 
 void ActionTargetGenerator::DoRun() {
-  target_->set_output_type(output_type_);
-
   if (!FillSources())
     return;
-  if (output_type_ == Target::ACTION_FOREACH && target_->sources().empty()) {
+  if (target_->output_type() == Target::ACTION_FOREACH &&
+      target_->sources().empty()) {
     // Foreach rules must always have some sources to have an effect.
     *err_ =
         Err(function_call_, "action_foreach target has no sources.",
@@ -57,7 +55,7 @@
   if (!FillResponseFileContents())
     return;
 
-  if (!FillOutputs(output_type_ == Target::ACTION_FOREACH))
+  if (!FillOutputs(target_->output_type() == Target::ACTION_FOREACH))
     return;
 
   if (!FillDepfile())
@@ -228,7 +226,7 @@
     return false;
   }
 
-  if (output_type_ == Target::ACTION) {
+  if (target_->output_type() == Target::ACTION) {
     if (!outputs.required_types().empty()) {
       *err_ = Err(
           function_call_, "Action has patterns in the output.",
@@ -237,7 +235,7 @@
           "\"action_foreach\" target.");
       return false;
     }
-  } else if (output_type_ == Target::ACTION_FOREACH) {
+  } else if (target_->output_type() == Target::ACTION_FOREACH) {
     // A foreach target should always have a pattern in the outputs.
     if (outputs.required_types().empty()) {
       *err_ = Err(
diff --git a/src/gn/action_target_generator.h b/src/gn/action_target_generator.h
index 6cbe8ae..d6f6d31 100644
--- a/src/gn/action_target_generator.h
+++ b/src/gn/action_target_generator.h
@@ -14,7 +14,6 @@
   ActionTargetGenerator(Target* target,
                         Scope* scope,
                         const FunctionCallNode* function_call,
-                        Target::OutputType type,
                         Err* err);
   ~ActionTargetGenerator() override;
 
@@ -33,8 +32,6 @@
   // Checks for errors in the outputs variable.
   bool CheckOutputs();
 
-  Target::OutputType output_type_;
-
   ActionTargetGenerator(const ActionTargetGenerator&) = delete;
   ActionTargetGenerator& operator=(const ActionTargetGenerator&) = delete;
 };
diff --git a/src/gn/binary_target_generator.cc b/src/gn/binary_target_generator.cc
index 7d6d416..379d954 100644
--- a/src/gn/binary_target_generator.cc
+++ b/src/gn/binary_target_generator.cc
@@ -25,15 +25,12 @@
     Target* target,
     Scope* scope,
     const FunctionCallNode* function_call,
-    Target::OutputType type,
     Err* err)
-    : TargetGenerator(target, scope, function_call, err), output_type_(type) {}
+    : TargetGenerator(target, scope, function_call, err) {}
 
 BinaryTargetGenerator::~BinaryTargetGenerator() = default;
 
 void BinaryTargetGenerator::DoRun() {
-  target_->set_output_type(output_type_);
-
   if (!FillOutputName())
     return;
 
diff --git a/src/gn/binary_target_generator.h b/src/gn/binary_target_generator.h
index 51530d0..bc706dc 100644
--- a/src/gn/binary_target_generator.h
+++ b/src/gn/binary_target_generator.h
@@ -15,7 +15,6 @@
   BinaryTargetGenerator(Target* target,
                         Scope* scope,
                         const FunctionCallNode* function_call,
-                        Target::OutputType type,
                         Err* err);
   ~BinaryTargetGenerator() override;
 
@@ -35,8 +34,6 @@
   bool FillModuleName();
   bool FillModuleType();
 
-  Target::OutputType output_type_;
-
   BinaryTargetGenerator(const BinaryTargetGenerator&) = delete;
   BinaryTargetGenerator& operator=(const BinaryTargetGenerator&) = delete;
 };
diff --git a/src/gn/bundle_data_target_generator.cc b/src/gn/bundle_data_target_generator.cc
index 18af1ab..d9c9fed 100644
--- a/src/gn/bundle_data_target_generator.cc
+++ b/src/gn/bundle_data_target_generator.cc
@@ -21,8 +21,6 @@
 BundleDataTargetGenerator::~BundleDataTargetGenerator() = default;
 
 void BundleDataTargetGenerator::DoRun() {
-  target_->set_output_type(Target::BUNDLE_DATA);
-
   if (!FillSources())
     return;
   if (!FillOutputs())
diff --git a/src/gn/copy_target_generator.cc b/src/gn/copy_target_generator.cc
index a0dc924..5c779cf 100644
--- a/src/gn/copy_target_generator.cc
+++ b/src/gn/copy_target_generator.cc
@@ -19,8 +19,6 @@
 CopyTargetGenerator::~CopyTargetGenerator() = default;
 
 void CopyTargetGenerator::DoRun() {
-  target_->set_output_type(Target::COPY_FILES);
-
   if (!FillSources())
     return;
   if (!FillOutputs(true))
diff --git a/src/gn/create_bundle_target_generator.cc b/src/gn/create_bundle_target_generator.cc
index c4c2ac4..6d48bb6 100644
--- a/src/gn/create_bundle_target_generator.cc
+++ b/src/gn/create_bundle_target_generator.cc
@@ -28,8 +28,6 @@
 CreateBundleTargetGenerator::~CreateBundleTargetGenerator() = default;
 
 void CreateBundleTargetGenerator::DoRun() {
-  target_->set_output_type(Target::CREATE_BUNDLE);
-
   BundleData& bundle_data = target_->bundle_data();
   if (!FillBundleDir(SourceDir(), variables::kBundleRootDir,
                      &bundle_data.root_dir()))
diff --git a/src/gn/generated_file_target_generator.cc b/src/gn/generated_file_target_generator.cc
index e227e4e..76fa765 100644
--- a/src/gn/generated_file_target_generator.cc
+++ b/src/gn/generated_file_target_generator.cc
@@ -15,15 +15,12 @@
     Target* target,
     Scope* scope,
     const FunctionCallNode* function_call,
-    Target::OutputType type,
     Err* err)
-    : TargetGenerator(target, scope, function_call, err), output_type_(type) {}
+    : TargetGenerator(target, scope, function_call, err) {}
 
 GeneratedFileTargetGenerator::~GeneratedFileTargetGenerator() = default;
 
 void GeneratedFileTargetGenerator::DoRun() {
-  target_->set_output_type(output_type_);
-
   if (!FillOutputs(false))
     return;
   if (target_->action_values().outputs().list().size() != 1) {
diff --git a/src/gn/generated_file_target_generator.h b/src/gn/generated_file_target_generator.h
index d2a7924..ec772c2 100644
--- a/src/gn/generated_file_target_generator.h
+++ b/src/gn/generated_file_target_generator.h
@@ -16,7 +16,6 @@
   GeneratedFileTargetGenerator(Target* target,
                                Scope* scope,
                                const FunctionCallNode* function_call,
-                               Target::OutputType type,
                                Err* err);
   ~GeneratedFileTargetGenerator() override;
 
@@ -46,8 +45,6 @@
   bool contents_defined_ = false;
   bool data_keys_defined_ = false;
 
-  Target::OutputType output_type_;
-
   GeneratedFileTargetGenerator(const GeneratedFileTargetGenerator&) = delete;
   GeneratedFileTargetGenerator& operator=(const GeneratedFileTargetGenerator&) =
       delete;
diff --git a/src/gn/group_target_generator.cc b/src/gn/group_target_generator.cc
index 0a4631f..f0e97e3 100644
--- a/src/gn/group_target_generator.cc
+++ b/src/gn/group_target_generator.cc
@@ -17,8 +17,6 @@
 GroupTargetGenerator::~GroupTargetGenerator() = default;
 
 void GroupTargetGenerator::DoRun() {
-  target_->set_output_type(Target::GROUP);
-
   if (!FillPublicInputs())
     return;
 }
diff --git a/src/gn/target_generator.cc b/src/gn/target_generator.cc
index c5b14d8..b910252 100644
--- a/src/gn/target_generator.cc
+++ b/src/gn/target_generator.cc
@@ -109,65 +109,70 @@
   if (g_scheduler->verbose_logging())
     g_scheduler->Log("Defining target", label.GetUserVisibleName(true));
 
-  std::unique_ptr<Target> target =
-      std::make_unique<Target>(scope->settings(), label, Target::UNKNOWN,
-                               scope->CollectBuildDependencyFiles());
-  target->set_defined_from(function_call);
+  std::unique_ptr<Target> target;
+  auto make_target = [&](Target::OutputType type) {
+    target = std::make_unique<Target>(scope->settings(), label, type,
+                                      scope->CollectBuildDependencyFiles());
+    target->set_defined_from(function_call);
+    return target.get();
+  };
 
   // Create and call out to the proper generator.
   if (output_type == functions::kBundleData) {
-    BundleDataTargetGenerator generator(target.get(), scope, function_call,
-                                        err);
+    BundleDataTargetGenerator generator(make_target(Target::BUNDLE_DATA), scope,
+                                        function_call, err);
     generator.Run();
   } else if (output_type == functions::kCreateBundle) {
-    CreateBundleTargetGenerator generator(target.get(), scope, function_call,
-                                          err);
+    CreateBundleTargetGenerator generator(make_target(Target::CREATE_BUNDLE),
+                                          scope, function_call, err);
     generator.Run();
   } else if (output_type == functions::kCopy) {
-    CopyTargetGenerator generator(target.get(), scope, function_call, err);
+    CopyTargetGenerator generator(make_target(Target::COPY_FILES), scope,
+                                  function_call, err);
     generator.Run();
   } else if (output_type == functions::kAction) {
-    ActionTargetGenerator generator(target.get(), scope, function_call,
-                                    Target::ACTION, err);
+    ActionTargetGenerator generator(make_target(Target::ACTION), scope,
+                                    function_call, err);
     generator.Run();
   } else if (output_type == functions::kActionForEach) {
-    ActionTargetGenerator generator(target.get(), scope, function_call,
-                                    Target::ACTION_FOREACH, err);
+    ActionTargetGenerator generator(make_target(Target::ACTION_FOREACH), scope,
+                                    function_call, err);
     generator.Run();
   } else if (output_type == functions::kExecutable) {
-    BinaryTargetGenerator generator(target.get(), scope, function_call,
-                                    Target::EXECUTABLE, err);
+    BinaryTargetGenerator generator(make_target(Target::EXECUTABLE), scope,
+                                    function_call, err);
     generator.Run();
   } else if (output_type == functions::kGroup) {
-    GroupTargetGenerator generator(target.get(), scope, function_call, err);
+    GroupTargetGenerator generator(make_target(Target::GROUP), scope,
+                                   function_call, err);
     generator.Run();
   } else if (output_type == functions::kLoadableModule) {
-    BinaryTargetGenerator generator(target.get(), scope, function_call,
-                                    Target::LOADABLE_MODULE, err);
+    BinaryTargetGenerator generator(make_target(Target::LOADABLE_MODULE), scope,
+                                    function_call, err);
     generator.Run();
   } else if (output_type == functions::kSharedLibrary) {
-    BinaryTargetGenerator generator(target.get(), scope, function_call,
-                                    Target::SHARED_LIBRARY, err);
+    BinaryTargetGenerator generator(make_target(Target::SHARED_LIBRARY), scope,
+                                    function_call, err);
     generator.Run();
   } else if (output_type == functions::kSourceSet) {
-    BinaryTargetGenerator generator(target.get(), scope, function_call,
-                                    Target::SOURCE_SET, err);
+    BinaryTargetGenerator generator(make_target(Target::SOURCE_SET), scope,
+                                    function_call, err);
     generator.Run();
   } else if (output_type == functions::kStaticLibrary) {
-    BinaryTargetGenerator generator(target.get(), scope, function_call,
-                                    Target::STATIC_LIBRARY, err);
+    BinaryTargetGenerator generator(make_target(Target::STATIC_LIBRARY), scope,
+                                    function_call, err);
     generator.Run();
   } else if (output_type == functions::kGeneratedFile) {
-    GeneratedFileTargetGenerator generator(target.get(), scope, function_call,
-                                           Target::GENERATED_FILE, err);
+    GeneratedFileTargetGenerator generator(make_target(Target::GENERATED_FILE),
+                                           scope, function_call, err);
     generator.Run();
   } else if (output_type == functions::kRustLibrary) {
-    BinaryTargetGenerator generator(target.get(), scope, function_call,
-                                    Target::RUST_LIBRARY, err);
+    BinaryTargetGenerator generator(make_target(Target::RUST_LIBRARY), scope,
+                                    function_call, err);
     generator.Run();
   } else if (output_type == functions::kRustProcMacro) {
-    BinaryTargetGenerator generator(target.get(), scope, function_call,
-                                    Target::RUST_PROC_MACRO, err);
+    BinaryTargetGenerator generator(make_target(Target::RUST_PROC_MACRO), scope,
+                                    function_call, err);
     generator.Run();
   } else {
     *err = Err(function_call, "Not a known target type",