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"
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/target.h"
23 // Suppress output on success.
24 const char kSwitchQuiet
[] = "q";
26 const char kSwitchCheck
[] = "check";
28 // Called on worker thread to write the ninja file.
29 void BackgroundDoWrite(const Target
* target
) {
30 NinjaTargetWriter::RunAndWriteFile(target
);
31 g_scheduler
->DecrementWorkCount();
34 // Called on the main thread.
35 void ItemResolvedCallback(base::subtle::Atomic32
* write_counter
,
36 scoped_refptr
<Builder
> builder
,
37 const BuilderRecord
* record
) {
38 base::subtle::NoBarrier_AtomicIncrement(write_counter
, 1);
40 const Item
* item
= record
->item();
41 const Target
* target
= item
->AsTarget();
43 g_scheduler
->IncrementWorkCount();
44 g_scheduler
->ScheduleWork(base::Bind(&BackgroundDoWrite
, target
));
50 const char kGen
[] = "gen";
51 const char kGen_HelpShort
[] =
52 "gen: Generate ninja files.";
53 const char kGen_Help
[] =
54 "gn gen: Generate ninja files.\n"
56 " gn gen <output_directory>\n"
58 " Generates ninja files from the current tree and puts them in the given\n"
59 " output directory.\n"
61 " The output directory can be a source-repo-absolute path name such as:\n"
63 " Or it can be a directory relative to the current directory such as:\n"
66 " See \"gn help\" for the common command-line switches.\n";
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();
78 // Deliberately leaked to avoid expensive process teardown.
79 Setup
* setup
= new Setup();
80 if (!setup
->DoSetup(args
[0], true))
83 if (CommandLine::ForCurrentProcess()->HasSwitch(kSwitchCheck
))
84 setup
->set_check_public_headers(true);
86 // Cause the load to also generate the ninja files for each target. We wrap
87 // the writing to maintain a counter.
88 base::subtle::Atomic32 write_counter
= 0;
89 setup
->builder()->set_resolved_callback(
90 base::Bind(&ItemResolvedCallback
, &write_counter
,
91 scoped_refptr
<Builder
>(setup
->builder())));
93 // Do the actual load. This will also write out the target ninja files.
98 // Write the root ninja files.
99 if (!NinjaWriter::RunAndWriteFiles(&setup
->build_settings(),
106 base::TimeDelta elapsed_time
= timer
.Elapsed();
108 if (!CommandLine::ForCurrentProcess()->HasSwitch(kSwitchQuiet
)) {
109 OutputString("Done. ", DECORATION_GREEN
);
111 std::string stats
= "Wrote " +
112 base::IntToString(static_cast<int>(write_counter
)) +
115 setup
->scheduler().input_file_manager()->GetInputFileCount()) +
117 base::Int64ToString(elapsed_time
.InMilliseconds()) + "ms\n";
124 } // namespace commands