OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "tools/gn/setup.h" | 5 #include "tools/gn/setup.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <sstream> | 9 #include <sstream> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
17 #include "base/process/launch.h" | 17 #include "base/process/launch.h" |
18 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
19 #include "base/strings/string_split.h" | 19 #include "base/strings/string_split.h" |
20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
21 #include "base/strings/sys_string_conversions.h" | 21 #include "base/strings/sys_string_conversions.h" |
22 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
23 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 24 #include "tools/gn/command_format.h" |
24 #include "tools/gn/commands.h" | 25 #include "tools/gn/commands.h" |
25 #include "tools/gn/filesystem_utils.h" | 26 #include "tools/gn/filesystem_utils.h" |
26 #include "tools/gn/input_file.h" | 27 #include "tools/gn/input_file.h" |
27 #include "tools/gn/parse_tree.h" | 28 #include "tools/gn/parse_tree.h" |
28 #include "tools/gn/parser.h" | 29 #include "tools/gn/parser.h" |
29 #include "tools/gn/source_dir.h" | 30 #include "tools/gn/source_dir.h" |
30 #include "tools/gn/source_file.h" | 31 #include "tools/gn/source_file.h" |
31 #include "tools/gn/standard_out.h" | 32 #include "tools/gn/standard_out.h" |
32 #include "tools/gn/switches.h" | 33 #include "tools/gn/switches.h" |
33 #include "tools/gn/tokenizer.h" | 34 #include "tools/gn/tokenizer.h" |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 Setup::Setup() | 255 Setup::Setup() |
255 : build_settings_(), | 256 : build_settings_(), |
256 loader_(new LoaderImpl(&build_settings_)), | 257 loader_(new LoaderImpl(&build_settings_)), |
257 builder_(loader_.get()), | 258 builder_(loader_.get()), |
258 root_build_file_("//BUILD.gn"), | 259 root_build_file_("//BUILD.gn"), |
259 check_public_headers_(false), | 260 check_public_headers_(false), |
260 dotfile_settings_(&build_settings_, std::string()), | 261 dotfile_settings_(&build_settings_, std::string()), |
261 dotfile_scope_(&dotfile_settings_), | 262 dotfile_scope_(&dotfile_settings_), |
262 fill_arguments_(true) { | 263 fill_arguments_(true) { |
263 dotfile_settings_.set_toolchain_label(Label()); | 264 dotfile_settings_.set_toolchain_label(Label()); |
| 265 |
264 build_settings_.set_item_defined_callback( | 266 build_settings_.set_item_defined_callback( |
265 base::Bind(&ItemDefinedCallback, scheduler_.task_runner(), &builder_)); | 267 base::Bind(&ItemDefinedCallback, scheduler_.task_runner(), &builder_)); |
266 | 268 |
267 loader_->set_complete_callback(base::Bind(&DecrementWorkCount)); | 269 loader_->set_complete_callback(base::Bind(&DecrementWorkCount)); |
268 // The scheduler's task runner wasn't created when the Loader was created, so | 270 // The scheduler's task runner wasn't created when the Loader was created, so |
269 // we need to set it now. | 271 // we need to set it now. |
270 loader_->set_task_runner(scheduler_.task_runner()); | 272 loader_->set_task_runner(scheduler_.task_runner()); |
271 } | 273 } |
272 | 274 |
273 Setup::~Setup() { | 275 Setup::~Setup() { |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 return false; | 435 return false; |
434 } | 436 } |
435 | 437 |
436 args_root_ = Parser::Parse(args_tokens_, &err); | 438 args_root_ = Parser::Parse(args_tokens_, &err); |
437 if (err.has_error()) { | 439 if (err.has_error()) { |
438 err.PrintToStdout(); | 440 err.PrintToStdout(); |
439 return false; | 441 return false; |
440 } | 442 } |
441 | 443 |
442 Scope arg_scope(&dotfile_settings_); | 444 Scope arg_scope(&dotfile_settings_); |
| 445 // Set soure dir so relative imports in args work. |
| 446 SourceDir root_source_dir = |
| 447 SourceDirForCurrentDirectory(build_settings_.root_path()); |
| 448 arg_scope.set_source_dir(root_source_dir); |
443 args_root_->Execute(&arg_scope, &err); | 449 args_root_->Execute(&arg_scope, &err); |
444 if (err.has_error()) { | 450 if (err.has_error()) { |
445 err.PrintToStdout(); | 451 err.PrintToStdout(); |
446 return false; | 452 return false; |
447 } | 453 } |
448 | 454 |
449 // Save the result of the command args. | 455 // Save the result of the command args. |
450 Scope::KeyValueMap overrides; | 456 Scope::KeyValueMap overrides; |
451 arg_scope.GetCurrentScopeValues(&overrides); | 457 arg_scope.GetCurrentScopeValues(&overrides); |
452 build_settings_.build_args().AddArgOverrides(overrides); | 458 build_settings_.build_args().AddArgOverrides(overrides); |
453 return true; | 459 return true; |
454 } | 460 } |
455 | 461 |
456 bool Setup::SaveArgsToFile() { | 462 bool Setup::SaveArgsToFile() { |
457 ScopedTrace setup_trace(TraceItem::TRACE_SETUP, "Save args file"); | 463 ScopedTrace setup_trace(TraceItem::TRACE_SETUP, "Save args file"); |
458 | 464 |
459 std::ostringstream stream; | |
460 for (const auto& pair : build_settings_.build_args().GetAllOverrides()) { | |
461 stream << pair.first.as_string() << " = " << pair.second.ToString(true); | |
462 stream << std::endl; | |
463 } | |
464 | |
465 // For the first run, the build output dir might not be created yet, so do | 465 // For the first run, the build output dir might not be created yet, so do |
466 // that so we can write a file into it. Ignore errors, we'll catch the error | 466 // that so we can write a file into it. Ignore errors, we'll catch the error |
467 // when we try to write a file to it below. | 467 // when we try to write a file to it below. |
468 base::FilePath build_arg_file = | 468 base::FilePath build_arg_file = |
469 build_settings_.GetFullPath(GetBuildArgFile()); | 469 build_settings_.GetFullPath(GetBuildArgFile()); |
470 base::CreateDirectory(build_arg_file.DirName()); | 470 base::CreateDirectory(build_arg_file.DirName()); |
471 | 471 |
472 std::string contents = stream.str(); | 472 std::string contents = args_input_file_->contents(); |
| 473 commands::FormatStringToString(contents, false, &contents); |
473 #if defined(OS_WIN) | 474 #if defined(OS_WIN) |
474 // Use Windows lineendings for this file since it will often open in | 475 // Use Windows lineendings for this file since it will often open in |
475 // Notepad which can't handle Unix ones. | 476 // Notepad which can't handle Unix ones. |
476 base::ReplaceSubstringsAfterOffset(&contents, 0, "\n", "\r\n"); | 477 base::ReplaceSubstringsAfterOffset(&contents, 0, "\n", "\r\n"); |
477 #endif | 478 #endif |
478 if (base::WriteFile(build_arg_file, contents.c_str(), | 479 if (base::WriteFile(build_arg_file, contents.c_str(), |
479 static_cast<int>(contents.size())) == -1) { | 480 static_cast<int>(contents.size())) == -1) { |
480 Err(Location(), "Args file could not be written.", | 481 Err(Location(), "Args file could not be written.", |
481 "The file is \"" + FilePathToUTF8(build_arg_file) + | 482 "The file is \"" + FilePathToUTF8(build_arg_file) + |
482 "\"").PrintToStdout(); | 483 "\"").PrintToStdout(); |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
737 if (err.has_error()) { | 738 if (err.has_error()) { |
738 err.PrintToStdout(); | 739 err.PrintToStdout(); |
739 return false; | 740 return false; |
740 } | 741 } |
741 } | 742 } |
742 build_settings_.set_exec_script_whitelist(std::move(whitelist)); | 743 build_settings_.set_exec_script_whitelist(std::move(whitelist)); |
743 } | 744 } |
744 | 745 |
745 return true; | 746 return true; |
746 } | 747 } |
OLD | NEW |