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"
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
,
34 base::subtle::NoBarrier_AtomicIncrement(write_counter
, 1);
36 const Target
* target
= item
->AsTarget();
38 const Toolchain
* toolchain
=
39 builder
->GetToolchain(target
->settings()->toolchain_label());
41 g_scheduler
->IncrementWorkCount();
42 g_scheduler
->ScheduleWork(
43 base::Bind(&BackgroundDoWrite
, target
, toolchain
));
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"
55 " gn gen <output_directory>\n"
57 " Generates ninja files from the current tree and puts them in the given\n"
58 " output directory.\n"
60 " The output directory can be a source-repo-absolute path name such as:\n"
62 " Or it can be a directory relative to the current directory such as:\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();
78 // Deliberately leaked to avoid expensive process teardown.
79 Setup
* setup
= new Setup();
80 if (!setup
->DoSetup(args
[0]))
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.
94 // Write the root ninja files.
95 if (!NinjaWriter::RunAndWriteFiles(&setup
->build_settings(),
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
)) +
108 setup
->scheduler().input_file_manager()->GetInputFileCount()) +
110 base::IntToString(elapsed_time
.InMilliseconds()) + "ms\n";
117 } // namespace commands