<webview>: Define API endpoints for webview.contextMenus API.
[chromium-blink-merge.git] / tools / gn / command_gen.cc
blob5be018aa4eee131d60004ef509b99030f2ca98e8
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 "base/atomicops.h"
6 #include "base/bind.h"
7 #include "base/command_line.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/timer/elapsed_timer.h"
10 #include "tools/gn/build_settings.h"
11 #include "tools/gn/commands.h"
12 #include "tools/gn/ninja_target_writer.h"
13 #include "tools/gn/ninja_writer.h"
14 #include "tools/gn/scheduler.h"
15 #include "tools/gn/setup.h"
16 #include "tools/gn/standard_out.h"
18 namespace commands {
20 namespace {
22 // Suppress output on success.
23 const char kSwitchQuiet[] = "q";
25 void BackgroundDoWrite(const Target* target, const Toolchain* toolchain) {
26 NinjaTargetWriter::RunAndWriteFile(target, toolchain);
27 g_scheduler->DecrementWorkCount();
30 // Called on the main thread.
31 void ItemResolvedCallback(base::subtle::Atomic32* write_counter,
32 scoped_refptr<Builder> builder,
33 const Item* item) {
34 base::subtle::NoBarrier_AtomicIncrement(write_counter, 1);
36 const Target* target = item->AsTarget();
37 if (target) {
38 const Toolchain* toolchain =
39 builder->GetToolchain(target->settings()->toolchain_label());
40 DCHECK(toolchain);
41 g_scheduler->IncrementWorkCount();
42 g_scheduler->ScheduleWork(
43 base::Bind(&BackgroundDoWrite, target, toolchain));
47 } // namespace
49 const char kGen[] = "gen";
50 const char kGen_HelpShort[] =
51 "gen: Generate ninja files.";
52 const char kGen_Help[] =
53 "gn gen: Generate ninja files.\n"
54 "\n"
55 " gn gen <output_directory>\n"
56 "\n"
57 " Generates ninja files from the current tree and puts them in the given\n"
58 " output directory.\n"
59 "\n"
60 " The output directory can be a source-repo-absolute path name such as:\n"
61 " //out/foo\n"
62 " Or it can be a directory relative to the current directory such as:\n"
63 " out/foo\n"
64 "\n"
65 " See \"gn help\" for the common command-line switches.\n";
67 // Note: partially duplicated in command_gyp.cc.
68 int RunGen(const std::vector<std::string>& args) {
69 base::ElapsedTimer timer;
71 if (args.size() != 1) {
72 Err(Location(), "Need exactly one build directory to generate.",
73 "I expected something more like \"gn gen out/foo\"\n"
74 "You can also see \"gn help gen\".").PrintToStdout();
75 return 1;
78 // Deliberately leaked to avoid expensive process teardown.
79 Setup* setup = new Setup();
80 if (!setup->DoSetup(args[0]))
81 return 1;
83 // Cause the load to also generate the ninja files for each target. We wrap
84 // the writing to maintain a counter.
85 base::subtle::Atomic32 write_counter = 0;
86 setup->builder()->set_resolved_callback(
87 base::Bind(&ItemResolvedCallback, &write_counter,
88 scoped_refptr<Builder>(setup->builder())));
90 // Do the actual load. This will also write out the target ninja files.
91 if (!setup->Run())
92 return 1;
94 // Write the root ninja files.
95 if (!NinjaWriter::RunAndWriteFiles(&setup->build_settings(),
96 setup->builder()))
97 return 1;
99 base::TimeDelta elapsed_time = timer.Elapsed();
101 if (!CommandLine::ForCurrentProcess()->HasSwitch(kSwitchQuiet)) {
102 OutputString("Done. ", DECORATION_GREEN);
104 std::string stats = "Wrote " +
105 base::IntToString(static_cast<int>(write_counter)) +
106 " targets from " +
107 base::IntToString(
108 setup->scheduler().input_file_manager()->GetInputFileCount()) +
109 " files in " +
110 base::IntToString(elapsed_time.InMilliseconds()) + "ms\n";
111 OutputString(stats);
114 return 0;
117 } // namespace commands