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"
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"
38 extern const char kDotfile_Help
[] =
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"
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"
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"
54 " gn gen out/Debug --root=/home/build --dotfile=/home/my_gn_file.gn\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"
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"
68 " The format of this list is identical to that of \"visibility\"\n"
69 " so see \"gn help visibility\" for examples.\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"
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"
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"
86 " The secondary source root must be inside the main source tree.\n"
88 "Example .gn file contents\n"
90 " buildconfig = \"//build/config/BUILDCONFIG.gn\"\n"
92 " check_targets = [\n"
93 " \"//doom_melon/*\", # Check everything in this subtree.\n"
94 " \"//tools:mind_controlling_ant\", # Check this specific target.\n"
97 " root = \"//:root\"\n"
99 " secondary_source = \"//build/config/temporary_buildfiles/\"\n";
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
) {
123 main_loop
->PostTask(FROM_HERE
, base::Bind(&Builder::ItemDefined
, builder
,
124 base::Passed(&item
)));
127 void DecrementWorkCount() {
128 g_scheduler
->DecrementWorkCount();
133 // CommonSetup -----------------------------------------------------------------
135 const char CommonSetup::kBuildArgFileName
[] = "args.gn";
137 CommonSetup::CommonSetup()
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() {
172 if (check_for_bad_items_
) {
173 if (!builder_
->CheckForBadItems(&err
)) {
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.
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(),
195 to_check
= all_targets
;
198 if (!commands::CheckPublicHeaders(&build_settings_
, all_targets
,
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
));
214 // Setup -----------------------------------------------------------------------
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());
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
))
241 ScopedTrace
setup_trace(TraceItem::TRACE_SETUP
, "DoSetup");
243 if (!FillSourceDir(*cmdline
))
245 if (!RunConfigFile())
247 if (!FillOtherConfig(*cmdline
))
250 // Must be after FillSourceDir to resolve.
251 if (!FillBuildDir(build_dir
, !force_create
))
254 // Check for unused variables in the .gn file.
256 if (!dotfile_scope_
.CheckForUnusedVars(&err
)) {
261 if (fill_arguments_
) {
262 if (!FillArguments(*cmdline
))
272 if (!scheduler_
.Run())
274 return RunPostMessageLoop();
277 Scheduler
* Setup::GetScheduler() {
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
)))
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");
337 args_tokens_
= Tokenizer::Tokenize(args_input_file_
.get(), &err
);
338 if (err
.has_error()) {
343 args_root_
= Parser::Parse(args_tokens_
, &err
);
344 if (err
.has_error()) {
349 Scope
arg_scope(&empty_settings_
);
350 args_root_
->AsBlock()->ExecuteBlockInScope(&arg_scope
, &err
);
351 if (err
.has_error()) {
356 // Save the result of the command args.
357 Scope::KeyValueMap overrides
;
358 arg_scope
.GetCurrentScopeValues(&overrides
);
359 build_settings_
.build_args().AddArgOverrides(overrides
);
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);
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();
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");
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();
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
);
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();
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
419 base::FilePath dot_file_path
=
420 cmdline
.GetSwitchValuePath(switches::kDotfile
);
421 if (dot_file_path
.empty()) {
422 dotfile_name_
= root_path
.Append(kGnFile
);
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();
433 // In the default case, look for a dotfile and that also tells us where the
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.")
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
);
455 bool Setup::FillBuildDir(const std::string
& build_dir
, bool require_exists
) {
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.").
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.")
483 build_settings_
.SetBuildDir(resolved
);
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");
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
);
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('/'));
507 build_settings_
.set_python_path(base::FilePath("python"));
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")
524 dotfile_tokens_
= Tokenizer::Tokenize(dotfile_input_file_
.get(), &err
);
525 if (err
.has_error()) {
530 dotfile_root_
= Parser::Parse(dotfile_tokens_
, &err
);
531 if (err
.has_error()) {
536 dotfile_root_
->AsBlock()->ExecuteBlockInScope(&dotfile_scope_
, &err
);
537 if (err
.has_error()) {
545 bool Setup::FillOtherConfig(const CommandLine
& cmdline
) {
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
)) {
557 build_settings_
.SetSecondarySourcePath(
558 SourceDir(secondary_value
->string_value()));
562 const Value
* root_value
= dotfile_scope_
.GetValue("root", true);
564 if (!root_value
->VerifyTypeIs(Value::STRING
, &err
)) {
569 Label root_target_label
=
570 Label::Resolve(SourceDir("//"), Label(), *root_value
, &err
);
571 if (err
.has_error()) {
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();
587 } else if (!build_config_value
->VerifyTypeIs(Value::STRING
, &err
)) {
591 build_settings_
.set_build_config_file(
592 SourceFile(build_config_value
->string_value()));
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
)) {
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()) {
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() {
642 void DependentSetup::RunPreMessageLoop() {
643 CommonSetup::RunPreMessageLoop();
646 bool DependentSetup::RunPostMessageLoop() {
647 return CommonSetup::RunPostMessageLoop();