Fix BundleData::GetBundleRootDirOutput() for non-default toolchain.

The "create_bundle" targets defines in build/config/{mac,ios}/rules.gni
put their outputs in "$root_out_dir". This directory is different from
the top-level output directory from BuildSettings and instead correspond
to the Settings toolchain output directory.

Fix BundleData::GetBundleRootDirOutput() to compute the bundle root
directory output relative to the toolchain output directory to fix
dependencies when a "create_bundle" target is defined in the non-default
toolchain.

Fix TestWithScope constructor to ensure that BuildSettings::SetBuildDir
call is made before the call to Settings constructor (as the toolchain
output directory is initialized from the BuildSettings build_dir).

Remove unnecessary calls to setup.build_settings().SetBuildDir() as
the value is already set in TestWithScope constructor.

BUG=603180

Review-Url: https://codereview.chromium.org/2101923006
Cr-Original-Commit-Position: refs/heads/master@{#403446}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 3fed8056522b525d1ee49acc5a04c6b3c3c229bb
diff --git a/tools/gn/bundle_data.cc b/tools/gn/bundle_data.cc
index 2908ddf..448eac9 100644
--- a/tools/gn/bundle_data.cc
+++ b/tools/gn/bundle_data.cc
@@ -148,7 +148,7 @@
 }
 
 SourceFile BundleData::GetBundleRootDirOutput(const Settings* settings) const {
-  const SourceDir& build_dir = settings->build_settings()->build_dir();
+  const SourceDir& build_dir = settings->toolchain_output_dir();
   std::string bundle_root_relative = RebasePath(root_dir().value(), build_dir);
 
   size_t first_component = bundle_root_relative.find('/');
diff --git a/tools/gn/function_rebase_path_unittest.cc b/tools/gn/function_rebase_path_unittest.cc
index 456d4fb..4b09790 100644
--- a/tools/gn/function_rebase_path_unittest.cc
+++ b/tools/gn/function_rebase_path_unittest.cc
@@ -32,7 +32,6 @@
 
 TEST(RebasePath, Strings) {
   TestWithScope setup;
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
   Scope* scope = setup.scope();
   scope->set_source_dir(SourceDir("//tools/gn/"));
 
@@ -151,7 +150,6 @@
 // Test list input.
 TEST(RebasePath, List) {
   TestWithScope setup;
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
   setup.scope()->set_source_dir(SourceDir("//tools/gn/"));
 
   std::vector<Value> args;
@@ -175,7 +173,6 @@
 
 TEST(RebasePath, Errors) {
   TestWithScope setup;
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
 
   // No arg input should issue an error.
   Err err;
diff --git a/tools/gn/ninja_action_target_writer_unittest.cc b/tools/gn/ninja_action_target_writer_unittest.cc
index 21a7ea5..ab871d1 100644
--- a/tools/gn/ninja_action_target_writer_unittest.cc
+++ b/tools/gn/ninja_action_target_writer_unittest.cc
@@ -13,10 +13,8 @@
 #include "tools/gn/test_with_scope.h"
 
 TEST(NinjaActionTargetWriter, WriteOutputFilesForBuildLine) {
-  TestWithScope setup;
   Err err;
-
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
+  TestWithScope setup;
 
   Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
   target.set_output_type(Target::ACTION_FOREACH);
@@ -39,10 +37,9 @@
 
 // Tests an action with no sources.
 TEST(NinjaActionTargetWriter, ActionNoSources) {
-  TestWithScope setup;
   Err err;
+  TestWithScope setup;
 
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
   Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
   target.set_output_type(Target::ACTION);
 
@@ -79,10 +76,9 @@
 
 // Tests an action with no sources and console = true
 TEST(NinjaActionTargetWriter, ActionNoSourcesConsole) {
-  TestWithScope setup;
   Err err;
+  TestWithScope setup;
 
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
   Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
   target.set_output_type(Target::ACTION);
 
@@ -121,10 +117,9 @@
 // Makes sure that we write sources as input dependencies for actions with
 // both sources and inputs (ACTION_FOREACH treats the sources differently).
 TEST(NinjaActionTargetWriter, ActionWithSources) {
-  TestWithScope setup;
   Err err;
+  TestWithScope setup;
 
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
   Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
   target.set_output_type(Target::ACTION);
 
@@ -161,10 +156,8 @@
 }
 
 TEST(NinjaActionTargetWriter, ForEach) {
-  TestWithScope setup;
   Err err;
-
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
+  TestWithScope setup;
 
   // Some dependencies that the action can depend on. Use actions for these
   // so they have a nice platform-independent stamp file that can appear in the
@@ -243,10 +236,9 @@
 }
 
 TEST(NinjaActionTargetWriter, ForEachWithDepfile) {
-  TestWithScope setup;
   Err err;
+  TestWithScope setup;
 
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
   Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
   target.set_output_type(Target::ACTION_FOREACH);
 
@@ -306,10 +298,9 @@
 }
 
 TEST(NinjaActionTargetWriter, ForEachWithResponseFile) {
-  TestWithScope setup;
   Err err;
+  TestWithScope setup;
 
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
   Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
   target.set_output_type(Target::ACTION_FOREACH);
 
diff --git a/tools/gn/ninja_binary_target_writer_unittest.cc b/tools/gn/ninja_binary_target_writer_unittest.cc
index 1cfdb56..381eb39 100644
--- a/tools/gn/ninja_binary_target_writer_unittest.cc
+++ b/tools/gn/ninja_binary_target_writer_unittest.cc
@@ -13,10 +13,8 @@
 #include "tools/gn/test_with_scope.h"
 
 TEST(NinjaBinaryTargetWriter, SourceSet) {
-  TestWithScope setup;
   Err err;
-
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
+  TestWithScope setup;
 
   Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
   target.set_output_type(Target::SOURCE_SET);
@@ -257,10 +255,8 @@
 // This tests that output extension and output dir overrides apply, and input
 // dependencies are applied.
 TEST(NinjaBinaryTargetWriter, OutputExtensionAndInputDeps) {
-  TestWithScope setup;
   Err err;
-
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
+  TestWithScope setup;
 
   // An action for our library to depend on.
   Target action(setup.settings(), Label(SourceDir("//foo/"), "action"));
@@ -314,10 +310,8 @@
 
 // Tests libs are applied.
 TEST(NinjaBinaryTargetWriter, LibsAndLibDirs) {
-  TestWithScope setup;
   Err err;
-
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
+  TestWithScope setup;
 
   // A shared library w/ libs and lib_dirs.
   Target target(setup.settings(), Label(SourceDir("//foo/"), "shlib"));
@@ -351,10 +345,8 @@
 }
 
 TEST(NinjaBinaryTargetWriter, EmptyOutputExtension) {
-  TestWithScope setup;
   Err err;
-
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
+  TestWithScope setup;
 
   // This test is the same as OutputExtensionAndInputDeps, except that we call
   // set_output_extension("") and ensure that we get an empty one and override
@@ -397,10 +389,8 @@
 }
 
 TEST(NinjaBinaryTargetWriter, SourceSetDataDeps) {
-  TestWithScope setup;
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
-
   Err err;
+  TestWithScope setup;
 
   // This target is a data (runtime) dependency of the intermediate target.
   Target data(setup.settings(), Label(SourceDir("//foo/"), "data_target"));
@@ -479,16 +469,14 @@
 }
 
 TEST(NinjaBinaryTargetWriter, SharedLibraryModuleDefinitionFile) {
+  Err err;
   TestWithScope setup;
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
 
   Target shared_lib(setup.settings(), Label(SourceDir("//foo/"), "bar"));
   shared_lib.set_output_type(Target::SHARED_LIBRARY);
   shared_lib.SetToolchain(setup.toolchain());
   shared_lib.sources().push_back(SourceFile("//foo/sources.cc"));
   shared_lib.sources().push_back(SourceFile("//foo/bar.def"));
-
-  Err err;
   ASSERT_TRUE(shared_lib.OnResolved(&err));
 
   std::ostringstream out;
@@ -515,16 +503,14 @@
 }
 
 TEST(NinjaBinaryTargetWriter, LoadableModule) {
+  Err err;
   TestWithScope setup;
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
 
   Target loadable_module(setup.settings(), Label(SourceDir("//foo/"), "bar"));
   loadable_module.set_output_type(Target::LOADABLE_MODULE);
   loadable_module.visibility().SetPublic();
   loadable_module.SetToolchain(setup.toolchain());
   loadable_module.sources().push_back(SourceFile("//foo/sources.cc"));
-
-  Err err;
   ASSERT_TRUE(loadable_module.OnResolved(&err)) << err.message();
 
   std::ostringstream out;
@@ -858,10 +844,8 @@
 // This tests that output extension and output dir overrides apply, and input
 // dependencies are applied.
 TEST(NinjaBinaryTargetWriter, InputFiles) {
-  TestWithScope setup;
   Err err;
-
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
+  TestWithScope setup;
 
   // This target has one input.
   {
diff --git a/tools/gn/ninja_bundle_data_target_writer_unittest.cc b/tools/gn/ninja_bundle_data_target_writer_unittest.cc
index b4725c7..1cbf83e 100644
--- a/tools/gn/ninja_bundle_data_target_writer_unittest.cc
+++ b/tools/gn/ninja_bundle_data_target_writer_unittest.cc
@@ -13,9 +13,7 @@
 
 TEST(NinjaBundleDataTargetWriter, Run) {
   Err err;
-
   TestWithScope setup;
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
 
   Target bundle_data(setup.settings(), Label(SourceDir("//foo/"), "data"));
   bundle_data.set_output_type(Target::BUNDLE_DATA);
diff --git a/tools/gn/ninja_copy_target_writer_unittest.cc b/tools/gn/ninja_copy_target_writer_unittest.cc
index dd8c211..44973a7 100644
--- a/tools/gn/ninja_copy_target_writer_unittest.cc
+++ b/tools/gn/ninja_copy_target_writer_unittest.cc
@@ -12,10 +12,9 @@
 
 // Tests multiple files with an output pattern and no toolchain dependency.
 TEST(NinjaCopyTargetWriter, Run) {
-  TestWithScope setup;
   Err err;
+  TestWithScope setup;
 
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
   Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
   target.set_output_type(Target::COPY_FILES);
 
@@ -43,10 +42,9 @@
 
 // Tests a single file with no output pattern.
 TEST(NinjaCopyTargetWriter, ToolchainDeps) {
-  TestWithScope setup;
   Err err;
+  TestWithScope setup;
 
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
   Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
   target.set_output_type(Target::COPY_FILES);
 
@@ -71,10 +69,8 @@
 }
 
 TEST(NinjaCopyTargetWriter, OrderOnlyDeps) {
-  TestWithScope setup;
   Err err;
-
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
+  TestWithScope setup;
 
   Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
   target.set_output_type(Target::COPY_FILES);
diff --git a/tools/gn/ninja_create_bundle_target_writer_unittest.cc b/tools/gn/ninja_create_bundle_target_writer_unittest.cc
index 876aeda..dee99c4 100644
--- a/tools/gn/ninja_create_bundle_target_writer_unittest.cc
+++ b/tools/gn/ninja_create_bundle_target_writer_unittest.cc
@@ -26,9 +26,7 @@
 // Tests multiple files with an output pattern.
 TEST(NinjaCreateBundleTargetWriter, Run) {
   Err err;
-
   TestWithScope setup;
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
 
   Target bundle_data(setup.settings(), Label(SourceDir("//foo/"), "data"));
   bundle_data.set_output_type(Target::BUNDLE_DATA);
@@ -70,9 +68,7 @@
 // Tests multiple files from asset catalog.
 TEST(NinjaCreateBundleTargetWriter, AssetCatalog) {
   Err err;
-
   TestWithScope setup;
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
 
   Target bundle_data(setup.settings(), Label(SourceDir("//foo/"), "data"));
   bundle_data.set_output_type(Target::BUNDLE_DATA);
@@ -120,9 +116,7 @@
 // correctly.
 TEST(NinjaCreateBundleTargetWriter, PhonyTarget) {
   Err err;
-
   TestWithScope setup;
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
 
   Target create_bundle(
       setup.settings(),
@@ -148,9 +142,7 @@
 // some asset catalog.
 TEST(NinjaCreateBundleTargetWriter, Complex) {
   Err err;
-
   TestWithScope setup;
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
 
   Target bundle_data0(setup.settings(),
                       Label(SourceDir("//qux/"), "info_plist"));
@@ -251,9 +243,7 @@
 // Tests code signing steps.
 TEST(NinjaCreateBundleTargetWriter, CodeSigning) {
   Err err;
-
   TestWithScope setup;
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
 
   Target executable(setup.settings(), Label(SourceDir("//baz/"), "quz"));
   executable.set_output_type(Target::EXECUTABLE);
diff --git a/tools/gn/ninja_group_target_writer_unittest.cc b/tools/gn/ninja_group_target_writer_unittest.cc
index c9d0ab4..9feb4bd 100644
--- a/tools/gn/ninja_group_target_writer_unittest.cc
+++ b/tools/gn/ninja_group_target_writer_unittest.cc
@@ -8,12 +8,10 @@
 #include "tools/gn/test_with_scope.h"
 
 TEST(NinjaGroupTargetWriter, Run) {
-  TestWithScope setup;
   Err err;
+  TestWithScope setup;
 
-  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
   Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
-
   target.set_output_type(Target::GROUP);
   target.visibility().SetPublic();
 
diff --git a/tools/gn/test_with_scope.cc b/tools/gn/test_with_scope.cc
index 0ddd463..b239c64 100644
--- a/tools/gn/test_with_scope.cc
+++ b/tools/gn/test_with_scope.cc
@@ -10,13 +10,22 @@
 #include "tools/gn/parser.h"
 #include "tools/gn/tokenizer.h"
 
+namespace {
+
+BuildSettings CreateBuildSettingsForTest() {
+  BuildSettings build_settings;
+  build_settings.SetBuildDir(SourceDir("//out/Debug/"));
+  return build_settings;
+}
+
+}  // namespace
+
 TestWithScope::TestWithScope()
-    : build_settings_(),
+    : build_settings_(CreateBuildSettingsForTest()),
       settings_(&build_settings_, std::string()),
       toolchain_(&settings_, Label(SourceDir("//toolchain/"), "default")),
       scope_(&settings_),
       scope_progammatic_provider_(&scope_, true) {
-  build_settings_.SetBuildDir(SourceDir("//out/Debug/"));
   build_settings_.set_print_callback(
       base::Bind(&TestWithScope::AppendPrintOutput, base::Unretained(this)));