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.
4
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/time/time.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
18namespace commands {
19
20namespace {
21
22// Suppress output on success.
23const char kSwitchQuiet[] = "q";
24
25void TargetResolvedCallback(base::subtle::Atomic32* write_counter,
26                            const Target* target) {
27  base::subtle::NoBarrier_AtomicIncrement(write_counter, 1);
28  NinjaTargetWriter::RunAndWriteFile(target);
29}
30
31}  // namespace
32
33const char kGen[] = "gen";
34const char kGen_HelpShort[] =
35    "gen: Generate ninja files.";
36const char kGen_Help[] =
37    "gn gen\n"
38    "  Generates ninja files from the current tree.\n"
39    "\n"
40    "  See \"gn help\" for the common command-line switches.\n";
41
42int RunGen(const std::vector<std::string>& args) {
43  base::TimeTicks begin_time = base::TimeTicks::Now();
44
45  // Deliberately leaked to avoid expensive process teardown.
46  Setup* setup = new Setup;
47  if (!setup->DoSetup())
48    return 1;
49
50  // Cause the load to also generate the ninja files for each target. We wrap
51  // the writing to maintain a counter.
52  base::subtle::Atomic32 write_counter = 0;
53  setup->build_settings().set_target_resolved_callback(
54      base::Bind(&TargetResolvedCallback, &write_counter));
55
56  // Do the actual load. This will also write out the target ninja files.
57  if (!setup->Run())
58    return 1;
59
60  // Write the root ninja files.
61  if (!NinjaWriter::RunAndWriteFiles(&setup->build_settings()))
62    return 1;
63
64  base::TimeTicks end_time = base::TimeTicks::Now();
65
66  if (!CommandLine::ForCurrentProcess()->HasSwitch(kSwitchQuiet)) {
67    OutputString("Done. ", DECORATION_GREEN);
68
69    std::string stats = "Wrote " +
70        base::IntToString(static_cast<int>(write_counter)) +
71        " targets from " +
72        base::IntToString(
73            setup->scheduler().input_file_manager()->GetInputFileCount()) +
74        " files in " +
75        base::IntToString((end_time - begin_time).InMilliseconds()) + "ms\n";
76    OutputString(stats);
77  }
78
79  return 0;
80}
81
82}  // namespace commands
83