• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef TOOLS_GN_NINJA_BUILD_WRITER_H_
6 #define TOOLS_GN_NINJA_BUILD_WRITER_H_
7 
8 #include <iosfwd>
9 #include <map>
10 #include <string_view>
11 #include <unordered_map>
12 #include <vector>
13 
14 #include "gn/path_output.h"
15 
16 class Builder;
17 class BuildSettings;
18 class Err;
19 class Settings;
20 class Target;
21 class Toolchain;
22 
23 namespace base {
24 class CommandLine;
25 }  // namespace base
26 
27 // Generates the toplevel "build.ninja" file. This references the individual
28 // toolchain files and lists all input .gn files as dependencies of the
29 // build itself.
30 class NinjaBuildWriter {
31  public:
32   NinjaBuildWriter(const BuildSettings* settings,
33                    const std::unordered_map<const Settings*, const Toolchain*>&
34                        used_toolchains,
35                    const std::vector<const Target*>& all_targets,
36                    const Toolchain* default_toolchain,
37                    const std::vector<const Target*>& default_toolchain_targets,
38                    std::ostream& out,
39                    std::ostream& dep_out);
40   ~NinjaBuildWriter();
41 
42   // The design of this class is that this static factory function takes the
43   // Builder, extracts the relevant information, and passes it to the class
44   // constructor. The class itself doesn't depend on the Builder at all which
45   // makes testing much easier (tests integrating various functions along with
46   // the Builder get very complicated).
47   static bool RunAndWriteFile(const BuildSettings* settings,
48                               const Builder& builder,
49                               Err* err);
50 
51   bool Run(Err* err);
52 
53  private:
54   void WriteNinjaRules();
55   void WriteAllPools();
56   bool WriteSubninjas(Err* err);
57   bool WritePhonyAndAllRules(Err* err);
58 
59   void WritePhonyRule(const Target* target, std::string_view phony_name);
60 
61   const BuildSettings* build_settings_;
62 
63   const std::unordered_map<const Settings*, const Toolchain*>& used_toolchains_;
64   const std::vector<const Target*>& all_targets_;
65   const Toolchain* default_toolchain_;
66   const std::vector<const Target*>& default_toolchain_targets_;
67 
68   std::ostream& out_;
69   std::ostream& dep_out_;
70   PathOutput path_output_;
71 
72   NinjaBuildWriter(const NinjaBuildWriter&) = delete;
73   NinjaBuildWriter& operator=(const NinjaBuildWriter&) = delete;
74 };
75 
76 extern const char kNinjaRules_Help[];
77 
78 // Exposed for testing.
79 base::CommandLine GetSelfInvocationCommandLine(
80     const BuildSettings* build_settings);
81 
82 #endif  // TOOLS_GN_NINJA_BUILD_WRITER_H_
83