Add pcm files to the deps of phony target

This allows to build pcm files when we specify phony target as build
targets.

Bug: 464099050
Change-Id: I670734af1461ded0b9a1698381b83fc9195a3ba0
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/20880
Commit-Queue: Takuto Ikuta <tikuta@google.com>
Reviewed-by: David Turner <digit@google.com>
diff --git a/src/gn/ninja_c_binary_target_writer.cc b/src/gn/ninja_c_binary_target_writer.cc
index 04ea485..862ac98 100644
--- a/src/gn/ninja_c_binary_target_writer.cc
+++ b/src/gn/ninja_c_binary_target_writer.cc
@@ -134,7 +134,7 @@
   std::vector<OutputFile>* stamp_files = &obj_files;  // default
   if (!target_->source_types_used().SwiftSourceUsed()) {
     WriteSources(*pch_files, input_deps, order_only_deps, module_dep_info,
-                 &obj_files, &other_files);
+                 &obj_files, &extra_files, &other_files);
   } else {
     stamp_files = &extra_files;  // Swift generates more than object files
     WriteSwiftSources(input_deps, order_only_deps, &obj_files, &extra_files);
@@ -146,7 +146,6 @@
     return;
 
   if (target_->output_type() == Target::SOURCE_SET) {
-    WriteSourceSetStamp(*stamp_files);
 #ifndef NDEBUG
     // Verify that the function that separately computes a source set's object
     // files match the object files just computed.
@@ -156,6 +155,12 @@
     for (const auto& obj : obj_files)
       DCHECK(computed_obj.Contains(obj));
 #endif
+
+    if (!target_->source_types_used().SwiftSourceUsed()) {
+      // Add extra files like pre compiled module to stamp files for phony targets.
+      stamp_files->insert(stamp_files->end(), extra_files.begin(), extra_files.end());
+    }
+    WriteSourceSetStamp(*stamp_files);
   } else {
     WriteLinkerStuff(obj_files, other_files, input_deps);
   }
@@ -380,6 +385,7 @@
     const std::vector<OutputFile>& order_only_deps,
     const std::vector<ClangModuleDep>& module_dep_info,
     std::vector<OutputFile>* object_files,
+    std::vector<OutputFile>* extra_files,
     std::vector<SourceFile>* other_files) {
   DCHECK(!target_->source_types_used().SwiftSourceUsed());
   object_files->reserve(object_files->size() + target_->sources().size());
@@ -444,6 +450,8 @@
     // output, but we'll only link to the first output.
     if (!source.IsModuleMapType()) {
       object_files->push_back(tool_outputs[0]);
+    } else {
+      extra_files->push_back(tool_outputs[0]);
     }
   }
 
diff --git a/src/gn/ninja_c_binary_target_writer.h b/src/gn/ninja_c_binary_target_writer.h
index 3707f0e..b81ff8c 100644
--- a/src/gn/ninja_c_binary_target_writer.h
+++ b/src/gn/ninja_c_binary_target_writer.h
@@ -82,6 +82,7 @@
                     const std::vector<OutputFile>& order_only_deps,
                     const std::vector<ClangModuleDep>& module_dep_info,
                     std::vector<OutputFile>* object_files,
+                    std::vector<OutputFile>* extra_files,
                     std::vector<SourceFile>* other_files);
   void WriteSwiftSources(const std::vector<OutputFile>& input_deps,
                          const std::vector<OutputFile>& order_only_deps,
diff --git a/src/gn/ninja_c_binary_target_writer_unittest.cc b/src/gn/ninja_c_binary_target_writer_unittest.cc
index f324d53..3ad3274 100644
--- a/src/gn/ninja_c_binary_target_writer_unittest.cc
+++ b/src/gn/ninja_c_binary_target_writer_unittest.cc
@@ -2336,7 +2336,7 @@
       "  source_file_part = bar.modulemap\n"
       "  source_name_part = bar\n"
       "\n"
-      "build phony/foo/bar: phony obj/foo/bar.bar.o\n";
+      "build phony/foo/bar: phony obj/foo/bar.bar.o obj/foo/bar.bar.pcm\n";
   std::string out_str = out.str();
   EXPECT_EQ(expected, out_str) << expected << "\n" << out_str;
 }