Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / tools / gn / command_gen.cc
blob5c0a5a94f7ae2664cd2042085adf299c66e58f47
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"
17 #include "tools/gn/switches.h"
18 #include "tools/gn/target.h"
20 namespace commands {
22 namespace {
24 const char kSwitchCheck[] = "check";
26 // Called on worker thread to write the ninja file.
27 void BackgroundDoWrite(const Target* target) {
28 NinjaTargetWriter::RunAndWriteFile(target);
29 g_scheduler->DecrementWorkCount();
32 // Called on the main thread.
33 void ItemResolvedCallback(base::subtle::Atomic32* write_counter,
34 scoped_refptr<Builder> builder,
35 const BuilderRecord* record) {
36 base::subtle::NoBarrier_AtomicIncrement(write_counter, 1);
38 const Item* item = record->item();
39 const Target* target = item->AsTarget();
40 if (target) {
41 g_scheduler->IncrementWorkCount();
42 g_scheduler->ScheduleWork(base::Bind(&BackgroundDoWrite, target));
46 } // namespace
48 const char kGen[] = "gen";
49 const char kGen_HelpShort[] =
50 "gen: Generate ninja files.";
51 const char kGen_Help[] =
52 "gn gen: Generate ninja files.\n"
53 "\n"
54 " gn gen <out_dir>\n"
55 "\n"
56 " Generates ninja files from the current tree and puts them in the given\n"
57 " output directory.\n"
58 "\n"
59 " The output directory can be a source-repo-absolute path name such as:\n"
60 " //out/foo\n"
61 " Or it can be a directory relative to the current directory such as:\n"
62 " out/foo\n"
63 "\n"
64 " See \"gn help\" for the common command-line switches.\n";
66 int RunGen(const std::vector<std::string>& args) {
67 base::ElapsedTimer timer;
69 if (args.size() != 1) {
70 Err(Location(), "Need exactly one build directory to generate.",
71 "I expected something more like \"gn gen out/foo\"\n"
72 "You can also see \"gn help gen\".").PrintToStdout();
73 return 1;
76 // Deliberately leaked to avoid expensive process teardown.
77 Setup* setup = new Setup();
78 if (!setup->DoSetup(args[0], true))
79 return 1;
81 if (base::CommandLine::ForCurrentProcess()->HasSwitch(kSwitchCheck))
82 setup->set_check_public_headers(true);
84 // Cause the load to also generate the ninja files for each target. We wrap
85 // the writing to maintain a counter.
86 base::subtle::Atomic32 write_counter = 0;
87 setup->builder()->set_resolved_callback(
88 base::Bind(&ItemResolvedCallback, &write_counter,
89 scoped_refptr<Builder>(setup->builder())));
91 // Do the actual load. This will also write out the target ninja files.
92 if (!setup->Run())
93 return 1;
95 Err err;
96 // Write the root ninja files.
97 if (!NinjaWriter::RunAndWriteFiles(&setup->build_settings(),
98 setup->builder(),
99 &err)) {
100 err.PrintToStdout();
101 return 1;
104 base::TimeDelta elapsed_time = timer.Elapsed();
106 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kQuiet)) {
107 OutputString("Done. ", DECORATION_GREEN);
109 std::string stats = "Wrote " +
110 base::IntToString(static_cast<int>(write_counter)) +
111 " targets from " +
112 base::IntToString(
113 setup->scheduler().input_file_manager()->GetInputFileCount()) +
114 " files in " +
115 base::Int64ToString(elapsed_time.InMilliseconds()) + "ms\n";
116 OutputString(stats);
119 return 0;
122 } // namespace commands