Write a phony target for the top-level directory of a create_bundle target.

This is needed so that if a create_bundle is used as a bundle_data in another
bundle, the dependent bundle can be specified as a sources.

BUG=297668
R=brettw@chromium.org

Review URL: https://codereview.chromium.org/1892243002

Cr-Original-Commit-Position: refs/heads/master@{#389101}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: a23feb50e97c016318d5a859609f8f6a139f0ed5
diff --git a/tools/gn/bundle_data.cc b/tools/gn/bundle_data.cc
index 7faccbd..f1b60c0 100644
--- a/tools/gn/bundle_data.cc
+++ b/tools/gn/bundle_data.cc
@@ -5,6 +5,7 @@
 #include "tools/gn/bundle_data.h"
 
 #include "base/logging.h"
+#include "tools/gn/filesystem_utils.h"
 #include "tools/gn/output_file.h"
 #include "tools/gn/settings.h"
 #include "tools/gn/target.h"
@@ -103,6 +104,9 @@
 
   if (!asset_catalog_sources_.empty())
     outputs_as_source->push_back(GetCompiledAssetCatalogPath());
+
+  if (!root_dir_.empty())
+    outputs_as_source->push_back(GetBundleRootDirOutput(settings));
 }
 
 SourceFile BundleData::GetCompiledAssetCatalogPath() const {
@@ -110,3 +114,18 @@
   std::string assets_car_path = resources_dir_ + "/Assets.car";
   return SourceFile(SourceFile::SWAP_IN, &assets_car_path);
 }
+
+SourceFile BundleData::GetBundleRootDirOutput(const Settings* settings) const {
+  const SourceDir& build_dir = settings->build_settings()->build_dir();
+  std::string bundle_root_relative = RebasePath(root_dir(), build_dir);
+
+  size_t first_component = bundle_root_relative.find('/');
+  if (first_component != std::string::npos) {
+    base::StringPiece outermost_bundle_dir =
+        base::StringPiece(bundle_root_relative).substr(0, first_component);
+    std::string return_value(build_dir.value());
+    outermost_bundle_dir.AppendToString(&return_value);
+    return SourceFile(SourceFile::SWAP_IN, &return_value);
+  }
+  return SourceFile(root_dir());
+}
diff --git a/tools/gn/bundle_data.h b/tools/gn/bundle_data.h
index e78589e..aaf5e33 100644
--- a/tools/gn/bundle_data.h
+++ b/tools/gn/bundle_data.h
@@ -64,6 +64,18 @@
   // asset_catalog_sources() is not empty.
   SourceFile GetCompiledAssetCatalogPath() const;
 
+  // Returns the path to the top-level directory of the bundle. This is
+  // based on root_dir(), but since that can be Bundle.app/Contents/ or
+  // any other subpath, this is just the most top-level directory (e.g.,
+  // just Bundle.app/).
+  //
+  // Note that this is a SourceFile instead of a SourceDir. This is because
+  // the output of a create_bundle rule is a single logical unit, even though
+  // it is really a directory containing many outputs. This allows other
+  // targets to treat the bundle as a single unit, rather than a collection
+  // of its contents.
+  SourceFile GetBundleRootDirOutput(const Settings* settings) const;
+
   // Returns the list of inputs for the compilation of the asset catalog.
   SourceFiles& asset_catalog_sources() { return asset_catalog_sources_; }
   const SourceFiles& asset_catalog_sources() const {
diff --git a/tools/gn/ninja_create_bundle_target_writer.cc b/tools/gn/ninja_create_bundle_target_writer.cc
index d85f240..84da2ff 100644
--- a/tools/gn/ninja_create_bundle_target_writer.cc
+++ b/tools/gn/ninja_create_bundle_target_writer.cc
@@ -116,4 +116,15 @@
   for (const auto& pair : target_->data_deps())
     order_only_deps.push_back(pair.ptr->dependency_output_file());
   WriteStampForTarget(output_files, order_only_deps);
+
+  // Write a phony target for the outer bundle directory. This allows other
+  // targets to treat the entire bundle as a single unit, even though it is
+  // a directory, so that it can be depended upon as a discrete build edge.
+  out_ << "build ";
+  path_output_.WriteFile(
+      out_,
+      OutputFile(settings_->build_settings(),
+                 target_->bundle_data().GetBundleRootDirOutput(settings_)));
+  out_ << ": phony " << target_->dependency_output_file().value();
+  out_ << std::endl;
 }
diff --git a/tools/gn/ninja_create_bundle_target_writer_unittest.cc b/tools/gn/ninja_create_bundle_target_writer_unittest.cc
index 88e6fba..44dd894 100644
--- a/tools/gn/ninja_create_bundle_target_writer_unittest.cc
+++ b/tools/gn/ninja_create_bundle_target_writer_unittest.cc
@@ -55,7 +55,8 @@
       "\n"
       "build obj/baz/bar.stamp: stamp "
           "bar.bundle/Resources/input1.txt "
-          "bar.bundle/Resources/input2.txt\n";
+          "bar.bundle/Resources/input2.txt\n"
+      "build bar.bundle: phony obj/baz/bar.stamp\n";
   std::string out_str = out.str();
   EXPECT_EQ(expected, out_str);
 }
@@ -97,7 +98,52 @@
           "../../foo/Foo.xcassets/foo.imageset/FooIcon-29@2x.png "
           "../../foo/Foo.xcassets/foo.imageset/FooIcon-29@3x.png\n"
       "\n"
-      "build obj/baz/bar.stamp: stamp bar.bundle/Resources/Assets.car\n";
+      "build obj/baz/bar.stamp: stamp bar.bundle/Resources/Assets.car\n"
+      "build bar.bundle: phony obj/baz/bar.stamp\n";
+  std::string out_str = out.str();
+  EXPECT_EQ(expected, out_str);
+}
+
+// Tests that the phony target for the top-level bundle directory is generated
+// correctly.
+TEST(NinjaCreateBundleTargetWriter, BundleRootDirOutput) {
+  TestWithScope setup;
+  Err err;
+
+  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
+  Target target(setup.settings(), Label(SourceDir("//baz/"), "bar"));
+  target.set_output_type(Target::CREATE_BUNDLE);
+
+  const std::string bundle_root_dir("//out/Debug/bar.bundle/Contents");
+  target.bundle_data().root_dir().assign(bundle_root_dir);
+  target.bundle_data().resources_dir().assign(bundle_root_dir + "/Resources");
+  target.bundle_data().executable_dir().assign(bundle_root_dir + "/MacOS");
+  target.bundle_data().plugins_dir().assign(bundle_root_dir + "/Plug Ins");
+
+  std::vector<SourceFile> sources;
+  sources.push_back(SourceFile("//foo/input1.txt"));
+  sources.push_back(SourceFile("//foo/input2.txt"));
+  target.bundle_data().file_rules().push_back(BundleFileRule(
+      sources, SubstitutionPattern::MakeForTest(
+                   "{{bundle_resources_dir}}/{{source_file_part}}")));
+
+  target.SetToolchain(setup.toolchain());
+  ASSERT_TRUE(target.OnResolved(&err));
+
+  std::ostringstream out;
+  NinjaCreateBundleTargetWriter writer(&target, out);
+  writer.Run();
+
+  const char expected[] =
+      "build bar.bundle/Contents/Resources/input1.txt: copy_bundle_data "
+          "../../foo/input1.txt\n"
+      "build bar.bundle/Contents/Resources/input2.txt: copy_bundle_data "
+          "../../foo/input2.txt\n"
+      "\n"
+      "build obj/baz/bar.stamp: stamp "
+          "bar.bundle/Contents/Resources/input1.txt "
+          "bar.bundle/Contents/Resources/input2.txt\n"
+      "build bar.bundle: phony obj/baz/bar.stamp\n";
   std::string out_str = out.str();
   EXPECT_EQ(expected, out_str);
 }
@@ -167,7 +213,8 @@
           "bar.bundle/Resources/input1.txt "
           "bar.bundle/Resources/input2.txt "
           "bar.bundle/Info.plist "
-          "bar.bundle/Resources/Assets.car\n";
+          "bar.bundle/Resources/Assets.car\n"
+      "build bar.bundle: phony obj/baz/bar.stamp\n";
   std::string out_str = out.str();
   EXPECT_EQ(expected, out_str);
 }