Change next_proto member type.
[chromium-blink-merge.git] / tools / gn / setup.cc
blobf2eb846b7ccfea7343b0e627e8234d22ef0c0d28
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
3 // found in the LICENSE file.
5 #include "tools/gn/setup.h"
7 #include <stdlib.h>
9 #include <algorithm>
10 #include <sstream>
12 #include "base/bind.h"
13 #include "base/command_line.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/process/launch.h"
17 #include "base/strings/string_split.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "build/build_config.h"
21 #include "tools/gn/commands.h"
22 #include "tools/gn/filesystem_utils.h"
23 #include "tools/gn/input_file.h"
24 #include "tools/gn/parse_tree.h"
25 #include "tools/gn/parser.h"
26 #include "tools/gn/source_dir.h"
27 #include "tools/gn/source_file.h"
28 #include "tools/gn/standard_out.h"
29 #include "tools/gn/switches.h"
30 #include "tools/gn/tokenizer.h"
31 #include "tools/gn/trace.h"
32 #include "tools/gn/value.h"
34 #if defined(OS_WIN)
35 #include <windows.h>
36 #endif
38 extern const char kDotfile_Help[] =
39 ".gn file\n"
40 "\n"
41 " When gn starts, it will search the current directory and parent\n"
42 " directories for a file called \".gn\". This indicates the source root.\n"
43 " You can override this detection by using the --root command-line\n"
44 " argument\n"
45 "\n"
46 " The .gn file in the source root will be executed. The syntax is the\n"
47 " same as a buildfile, but with very limited build setup-specific\n"
48 " meaning.\n"
49 "\n"
50 " If you specify --root, by default GN will look for the file .gn in\n"
51 " that directory. If you want to specify a different file, you can\n"
52 " additionally pass --dotfile:\n"
53 "\n"
54 " gn gen out/Debug --root=/home/build --dotfile=/home/my_gn_file.gn\n"
55 "\n"
56 "Variables\n"
57 "\n"
58 " buildconfig [required]\n"
59 " Label of the build config file. This file will be used to set up\n"
60 " the build file execution environment for each toolchain.\n"
61 "\n"
62 " check_targets [optional]\n"
63 " A list of labels and label patterns that should be checked when\n"
64 " running \"gn check\" or \"gn gen --check\". If unspecified, all\n"
65 " targets will be checked. If it is the empty list, no targets will\n"
66 " be checked.\n"
67 "\n"
68 " The format of this list is identical to that of \"visibility\"\n"
69 " so see \"gn help visibility\" for examples.\n"
70 "\n"
71 " root [optional]\n"
72 " Label of the root build target. The GN build will start by loading\n"
73 " the build file containing this target name. This defaults to\n"
74 " \"//:\" which will cause the file //BUILD.gn to be loaded.\n"
75 "\n"
76 " secondary_source [optional]\n"
77 " Label of an alternate directory tree to find input files. When\n"
78 " searching for a BUILD.gn file (or the build config file discussed\n"
79 " above), the file will first be looked for in the source root.\n"
80 " If it's not found, the secondary source root will be checked\n"
81 " (which would contain a parallel directory hierarchy).\n"
82 "\n"
83 " This behavior is intended to be used when BUILD.gn files can't be\n"
84 " checked in to certain source directories for whatever reason.\n"
85 "\n"
86 " The secondary source root must be inside the main source tree.\n"
87 "\n"
88 "Example .gn file contents\n"
89 "\n"
90 " buildconfig = \"//build/config/BUILDCONFIG.gn\"\n"
91 "\n"
92 " check_targets = [\n"
93 " \"//doom_melon/*\", # Check everything in this subtree.\n"
94 " \"//tools:mind_controlling_ant\", # Check this specific target.\n"
95 " ]\n"
96 "\n"
97 " root = \"//:root\"\n"
98 "\n"
99 " secondary_source = \"//build/config/temporary_buildfiles/\"\n";
101 namespace {
103 const base::FilePath::CharType kGnFile[] = FILE_PATH_LITERAL(".gn");
105 base::FilePath FindDotFile(const base::FilePath& current_dir) {
106 base::FilePath try_this_file = current_dir.Append(kGnFile);
107 if (base::PathExists(try_this_file))
108 return try_this_file;
110 base::FilePath with_no_slash = current_dir.StripTrailingSeparators();
111 base::FilePath up_one_dir = with_no_slash.DirName();
112 if (up_one_dir == current_dir)
113 return base::FilePath(); // Got to the top.
115 return FindDotFile(up_one_dir);
118 // Called on any thread. Post the item to the builder on the main thread.
119 void ItemDefinedCallback(base::MessageLoop* main_loop,
120 scoped_refptr<Builder> builder,
121 scoped_ptr<Item> item) {
122 DCHECK(item);
123 main_loop->PostTask(FROM_HERE, base::Bind(&Builder::ItemDefined, builder,
124 base::Passed(&item)));
127 void DecrementWorkCount() {
128 g_scheduler->DecrementWorkCount();
131 } // namespace
133 // CommonSetup -----------------------------------------------------------------
135 const char CommonSetup::kBuildArgFileName[] = "args.gn";
137 CommonSetup::CommonSetup()
138 : build_settings_(),
139 loader_(new LoaderImpl(&build_settings_)),
140 builder_(new Builder(loader_.get())),
141 root_build_file_("//BUILD.gn"),
142 check_for_bad_items_(true),
143 check_for_unused_overrides_(true),
144 check_public_headers_(false) {
145 loader_->set_complete_callback(base::Bind(&DecrementWorkCount));
148 CommonSetup::CommonSetup(const CommonSetup& other)
149 : build_settings_(other.build_settings_),
150 loader_(new LoaderImpl(&build_settings_)),
151 builder_(new Builder(loader_.get())),
152 root_build_file_(other.root_build_file_),
153 check_for_bad_items_(other.check_for_bad_items_),
154 check_for_unused_overrides_(other.check_for_unused_overrides_),
155 check_public_headers_(other.check_public_headers_) {
156 loader_->set_complete_callback(base::Bind(&DecrementWorkCount));
159 CommonSetup::~CommonSetup() {
162 void CommonSetup::RunPreMessageLoop() {
163 // Load the root build file.
164 loader_->Load(root_build_file_, LocationRange(), Label());
166 // Will be decremented with the loader is drained.
167 g_scheduler->IncrementWorkCount();
170 bool CommonSetup::RunPostMessageLoop() {
171 Err err;
172 if (check_for_bad_items_) {
173 if (!builder_->CheckForBadItems(&err)) {
174 err.PrintToStdout();
175 return false;
179 if (check_for_unused_overrides_) {
180 if (!build_settings_.build_args().VerifyAllOverridesUsed(&err)) {
181 // TODO(brettw) implement a system of warnings. Until we have a better
182 // system, print the error but don't return failure.
183 err.PrintToStdout();
184 return true;
188 if (check_public_headers_) {
189 std::vector<const Target*> all_targets = builder_->GetAllResolvedTargets();
190 std::vector<const Target*> to_check;
191 if (check_patterns()) {
192 commands::FilterTargetsByPatterns(all_targets, *check_patterns(),
193 &to_check);
194 } else {
195 to_check = all_targets;
198 if (!commands::CheckPublicHeaders(&build_settings_, all_targets,
199 to_check, false)) {
200 return false;
204 // Write out tracing and timing if requested.
205 const CommandLine* cmdline = CommandLine::ForCurrentProcess();
206 if (cmdline->HasSwitch(switches::kTime))
207 PrintLongHelp(SummarizeTraces());
208 if (cmdline->HasSwitch(switches::kTracelog))
209 SaveTraces(cmdline->GetSwitchValuePath(switches::kTracelog));
211 return true;
214 // Setup -----------------------------------------------------------------------
216 Setup::Setup()
217 : CommonSetup(),
218 empty_settings_(&empty_build_settings_, std::string()),
219 dotfile_scope_(&empty_settings_),
220 fill_arguments_(true) {
221 empty_settings_.set_toolchain_label(Label());
222 build_settings_.set_item_defined_callback(
223 base::Bind(&ItemDefinedCallback, scheduler_.main_loop(), builder_));
225 // The scheduler's main loop wasn't created when the Loader was created, so
226 // we need to set it now.
227 loader_->set_main_loop(scheduler_.main_loop());
230 Setup::~Setup() {
233 bool Setup::DoSetup(const std::string& build_dir, bool force_create) {
234 CommandLine* cmdline = CommandLine::ForCurrentProcess();
236 scheduler_.set_verbose_logging(cmdline->HasSwitch(switches::kVerbose));
237 if (cmdline->HasSwitch(switches::kTime) ||
238 cmdline->HasSwitch(switches::kTracelog))
239 EnableTracing();
241 ScopedTrace setup_trace(TraceItem::TRACE_SETUP, "DoSetup");
243 if (!FillSourceDir(*cmdline))
244 return false;
245 if (!RunConfigFile())
246 return false;
247 if (!FillOtherConfig(*cmdline))
248 return false;
250 // Must be after FillSourceDir to resolve.
251 if (!FillBuildDir(build_dir, !force_create))
252 return false;
254 // Check for unused variables in the .gn file.
255 Err err;
256 if (!dotfile_scope_.CheckForUnusedVars(&err)) {
257 err.PrintToStdout();
258 return false;
261 if (fill_arguments_) {
262 if (!FillArguments(*cmdline))
263 return false;
265 FillPythonPath();
267 return true;
270 bool Setup::Run() {
271 RunPreMessageLoop();
272 if (!scheduler_.Run())
273 return false;
274 return RunPostMessageLoop();
277 Scheduler* Setup::GetScheduler() {
278 return &scheduler_;
281 SourceFile Setup::GetBuildArgFile() const {
282 return SourceFile(build_settings_.build_dir().value() + kBuildArgFileName);
285 bool Setup::FillArguments(const CommandLine& cmdline) {
286 // Use the args on the command line if specified, and save them. Do this even
287 // if the list is empty (this means clear any defaults).
288 if (cmdline.HasSwitch(switches::kArgs)) {
289 if (!FillArgsFromCommandLine(cmdline.GetSwitchValueASCII(switches::kArgs)))
290 return false;
291 SaveArgsToFile();
292 return true;
295 // No command line args given, use the arguments from the build dir (if any).
296 return FillArgsFromFile();
299 bool Setup::FillArgsFromCommandLine(const std::string& args) {
300 args_input_file_.reset(new InputFile(SourceFile()));
301 args_input_file_->SetContents(args);
302 args_input_file_->set_friendly_name("the command-line \"--args\"");
303 return FillArgsFromArgsInputFile();
306 bool Setup::FillArgsFromFile() {
307 ScopedTrace setup_trace(TraceItem::TRACE_SETUP, "Load args file");
309 SourceFile build_arg_source_file = GetBuildArgFile();
310 base::FilePath build_arg_file =
311 build_settings_.GetFullPath(build_arg_source_file);
313 std::string contents;
314 if (!base::ReadFileToString(build_arg_file, &contents))
315 return true; // File doesn't exist, continue with default args.
317 // Add a dependency on the build arguments file. If this changes, we want
318 // to re-generate the build.
319 g_scheduler->AddGenDependency(build_arg_file);
321 if (contents.empty())
322 return true; // Empty file, do nothing.
324 args_input_file_.reset(new InputFile(build_arg_source_file));
325 args_input_file_->SetContents(contents);
326 args_input_file_->set_friendly_name(
327 "build arg file (use \"gn args <out_dir>\" to edit)");
329 setup_trace.Done(); // Only want to count the load as part of the trace.
330 return FillArgsFromArgsInputFile();
333 bool Setup::FillArgsFromArgsInputFile() {
334 ScopedTrace setup_trace(TraceItem::TRACE_SETUP, "Parse args");
336 Err err;
337 args_tokens_ = Tokenizer::Tokenize(args_input_file_.get(), &err);
338 if (err.has_error()) {
339 err.PrintToStdout();
340 return false;
343 args_root_ = Parser::Parse(args_tokens_, &err);
344 if (err.has_error()) {
345 err.PrintToStdout();
346 return false;
349 Scope arg_scope(&empty_settings_);
350 args_root_->AsBlock()->ExecuteBlockInScope(&arg_scope, &err);
351 if (err.has_error()) {
352 err.PrintToStdout();
353 return false;
356 // Save the result of the command args.
357 Scope::KeyValueMap overrides;
358 arg_scope.GetCurrentScopeValues(&overrides);
359 build_settings_.build_args().AddArgOverrides(overrides);
360 return true;
363 bool Setup::SaveArgsToFile() {
364 ScopedTrace setup_trace(TraceItem::TRACE_SETUP, "Save args file");
366 std::ostringstream stream;
367 for (const auto& pair : build_settings_.build_args().GetAllOverrides()) {
368 stream << pair.first.as_string() << " = " << pair.second.ToString(true);
369 stream << std::endl;
372 // For the first run, the build output dir might not be created yet, so do
373 // that so we can write a file into it. Ignore errors, we'll catch the error
374 // when we try to write a file to it below.
375 base::FilePath build_arg_file =
376 build_settings_.GetFullPath(GetBuildArgFile());
377 base::CreateDirectory(build_arg_file.DirName());
379 std::string contents = stream.str();
380 #if defined(OS_WIN)
381 // Use Windows lineendings for this file since it will often open in
382 // Notepad which can't handle Unix ones.
383 ReplaceSubstringsAfterOffset(&contents, 0, "\n", "\r\n");
384 #endif
385 if (base::WriteFile(build_arg_file, contents.c_str(),
386 static_cast<int>(contents.size())) == -1) {
387 Err(Location(), "Args file could not be written.",
388 "The file is \"" + FilePathToUTF8(build_arg_file) +
389 "\"").PrintToStdout();
390 return false;
393 // Add a dependency on the build arguments file. If this changes, we want
394 // to re-generate the build.
395 g_scheduler->AddGenDependency(build_arg_file);
397 return true;
400 bool Setup::FillSourceDir(const CommandLine& cmdline) {
401 // Find the .gn file.
402 base::FilePath root_path;
404 // Prefer the command line args to the config file.
405 base::FilePath relative_root_path =
406 cmdline.GetSwitchValuePath(switches::kRoot);
407 if (!relative_root_path.empty()) {
408 root_path = base::MakeAbsoluteFilePath(relative_root_path);
409 if (root_path.empty()) {
410 Err(Location(), "Root source path not found.",
411 "The path \"" + FilePathToUTF8(relative_root_path) +
412 "\" doesn't exist.").PrintToStdout();
413 return false;
416 // When --root is specified, an alternate --dotfile can also be set.
417 // --dotfile should be a real file path and not a "//foo" source-relative
418 // path.
419 base::FilePath dot_file_path =
420 cmdline.GetSwitchValuePath(switches::kDotfile);
421 if (dot_file_path.empty()) {
422 dotfile_name_ = root_path.Append(kGnFile);
423 } else {
424 dotfile_name_ = base::MakeAbsoluteFilePath(dot_file_path);
425 if (dotfile_name_.empty()) {
426 Err(Location(), "Could not load dotfile.",
427 "The file \"" + FilePathToUTF8(dot_file_path) +
428 "\" cound't be loaded.").PrintToStdout();
429 return false;
432 } else {
433 // In the default case, look for a dotfile and that also tells us where the
434 // source root is.
435 base::FilePath cur_dir;
436 base::GetCurrentDirectory(&cur_dir);
437 dotfile_name_ = FindDotFile(cur_dir);
438 if (dotfile_name_.empty()) {
439 Err(Location(), "Can't find source root.",
440 "I could not find a \".gn\" file in the current directory or any "
441 "parent,\nand the --root command-line argument was not specified.")
442 .PrintToStdout();
443 return false;
445 root_path = dotfile_name_.DirName();
448 if (scheduler_.verbose_logging())
449 scheduler_.Log("Using source root", FilePathToUTF8(root_path));
450 build_settings_.SetRootPath(root_path);
452 return true;
455 bool Setup::FillBuildDir(const std::string& build_dir, bool require_exists) {
456 SourceDir resolved =
457 SourceDirForCurrentDirectory(build_settings_.root_path()).
458 ResolveRelativeDir(build_dir, build_settings_.root_path_utf8());
459 if (resolved.is_null()) {
460 Err(Location(), "Couldn't resolve build directory.",
461 "The build directory supplied (\"" + build_dir + "\") was not valid.").
462 PrintToStdout();
463 return false;
466 if (scheduler_.verbose_logging())
467 scheduler_.Log("Using build dir", resolved.value());
469 if (require_exists) {
470 base::FilePath build_dir_path = build_settings_.GetFullPath(resolved);
471 if (!base::PathExists(build_dir_path.Append(
472 FILE_PATH_LITERAL("build.ninja")))) {
473 Err(Location(), "Not a build directory.",
474 "This command requires an existing build directory. I interpreted "
475 "your input\n\"" + build_dir + "\" as:\n " +
476 FilePathToUTF8(build_dir_path) +
477 "\nwhich doesn't seem to contain a previously-generated build.")
478 .PrintToStdout();
479 return false;
483 build_settings_.SetBuildDir(resolved);
484 return true;
487 void Setup::FillPythonPath() {
488 // Trace this since it tends to be a bit slow on Windows.
489 ScopedTrace setup_trace(TraceItem::TRACE_SETUP, "Fill Python Path");
490 #if defined(OS_WIN)
491 // Find Python on the path so we can use the absolute path in the build.
492 const base::char16 kGetPython[] =
493 L"cmd.exe /c python -c \"import sys; print sys.executable\"";
494 std::string python_path;
495 if (base::GetAppOutput(kGetPython, &python_path)) {
496 base::TrimWhitespaceASCII(python_path, base::TRIM_ALL, &python_path);
497 if (scheduler_.verbose_logging())
498 scheduler_.Log("Found python", python_path);
499 } else {
500 scheduler_.Log("WARNING", "Could not find python on path, using "
501 "just \"python.exe\"");
502 python_path = "python.exe";
504 build_settings_.set_python_path(base::FilePath(base::UTF8ToUTF16(python_path))
505 .NormalizePathSeparatorsTo('/'));
506 #else
507 build_settings_.set_python_path(base::FilePath("python"));
508 #endif
511 bool Setup::RunConfigFile() {
512 if (scheduler_.verbose_logging())
513 scheduler_.Log("Got dotfile", FilePathToUTF8(dotfile_name_));
515 dotfile_input_file_.reset(new InputFile(SourceFile("//.gn")));
516 if (!dotfile_input_file_->Load(dotfile_name_)) {
517 Err(Location(), "Could not load dotfile.",
518 "The file \"" + FilePathToUTF8(dotfile_name_) + "\" cound't be loaded")
519 .PrintToStdout();
520 return false;
523 Err err;
524 dotfile_tokens_ = Tokenizer::Tokenize(dotfile_input_file_.get(), &err);
525 if (err.has_error()) {
526 err.PrintToStdout();
527 return false;
530 dotfile_root_ = Parser::Parse(dotfile_tokens_, &err);
531 if (err.has_error()) {
532 err.PrintToStdout();
533 return false;
536 dotfile_root_->AsBlock()->ExecuteBlockInScope(&dotfile_scope_, &err);
537 if (err.has_error()) {
538 err.PrintToStdout();
539 return false;
542 return true;
545 bool Setup::FillOtherConfig(const CommandLine& cmdline) {
546 Err err;
548 // Secondary source path, read from the config file if present.
549 // Read from the config file if present.
550 const Value* secondary_value =
551 dotfile_scope_.GetValue("secondary_source", true);
552 if (secondary_value) {
553 if (!secondary_value->VerifyTypeIs(Value::STRING, &err)) {
554 err.PrintToStdout();
555 return false;
557 build_settings_.SetSecondarySourcePath(
558 SourceDir(secondary_value->string_value()));
561 // Root build file.
562 const Value* root_value = dotfile_scope_.GetValue("root", true);
563 if (root_value) {
564 if (!root_value->VerifyTypeIs(Value::STRING, &err)) {
565 err.PrintToStdout();
566 return false;
569 Label root_target_label =
570 Label::Resolve(SourceDir("//"), Label(), *root_value, &err);
571 if (err.has_error()) {
572 err.PrintToStdout();
573 return false;
576 root_build_file_ = Loader::BuildFileForLabel(root_target_label);
579 // Build config file.
580 const Value* build_config_value =
581 dotfile_scope_.GetValue("buildconfig", true);
582 if (!build_config_value) {
583 Err(Location(), "No build config file.",
584 "Your .gn file (\"" + FilePathToUTF8(dotfile_name_) + "\")\n"
585 "didn't specify a \"buildconfig\" value.").PrintToStdout();
586 return false;
587 } else if (!build_config_value->VerifyTypeIs(Value::STRING, &err)) {
588 err.PrintToStdout();
589 return false;
591 build_settings_.set_build_config_file(
592 SourceFile(build_config_value->string_value()));
594 // Targets to check.
595 const Value* check_targets_value =
596 dotfile_scope_.GetValue("check_targets", true);
597 if (check_targets_value) {
598 check_patterns_.reset(new std::vector<LabelPattern>);
600 // Fill the list of targets to check.
601 if (!check_targets_value->VerifyTypeIs(Value::LIST, &err)) {
602 err.PrintToStdout();
603 return false;
605 SourceDir current_dir("//");
606 for (const auto& item : check_targets_value->list_value()) {
607 check_patterns_->push_back(
608 LabelPattern::GetPattern(current_dir, item, &err));
609 if (err.has_error()) {
610 err.PrintToStdout();
611 return false;
616 return true;
619 // DependentSetup --------------------------------------------------------------
621 DependentSetup::DependentSetup(Setup* derive_from)
622 : CommonSetup(*derive_from),
623 scheduler_(derive_from->GetScheduler()) {
624 build_settings_.set_item_defined_callback(
625 base::Bind(&ItemDefinedCallback, scheduler_->main_loop(), builder_));
628 DependentSetup::DependentSetup(DependentSetup* derive_from)
629 : CommonSetup(*derive_from),
630 scheduler_(derive_from->GetScheduler()) {
631 build_settings_.set_item_defined_callback(
632 base::Bind(&ItemDefinedCallback, scheduler_->main_loop(), builder_));
635 DependentSetup::~DependentSetup() {
638 Scheduler* DependentSetup::GetScheduler() {
639 return scheduler_;
642 void DependentSetup::RunPreMessageLoop() {
643 CommonSetup::RunPreMessageLoop();
646 bool DependentSetup::RunPostMessageLoop() {
647 return CommonSetup::RunPostMessageLoop();