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/switches.h"
18 #include "tools/gn/target.h"
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();
41 g_scheduler
->IncrementWorkCount();
42 g_scheduler
->ScheduleWork(base::Bind(&BackgroundDoWrite
, target
));
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"
56 " Generates ninja files from the current tree and puts them in the given\n"
57 " output directory.\n"
59 " The output directory can be a source-repo-absolute path name such as:\n"
61 " Or it can be a directory relative to the current directory such as:\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();
76 // Deliberately leaked to avoid expensive process teardown.
77 Setup
* setup
= new Setup();
78 if (!setup
->DoSetup(args
[0], true))
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.
96 // Write the root ninja files.
97 if (!NinjaWriter::RunAndWriteFiles(&setup
->build_settings(),
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
)) +
113 setup
->scheduler().input_file_manager()->GetInputFileCount()) +
115 base::Int64ToString(elapsed_time
.InMilliseconds()) + "ms\n";
122 } // namespace commands