• 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_TARGET_WRITER_H_
6 #define TOOLS_GN_NINJA_TARGET_WRITER_H_
7 
8 #include <iosfwd>
9 
10 #include "gn/path_output.h"
11 #include "gn/substitution_type.h"
12 
13 class OutputFile;
14 class Settings;
15 class Target;
16 struct SubstitutionBits;
17 
18 // Generates one target's ".ninja" file. The toplevel "build.ninja" file is
19 // generated by the NinjaBuildWriter.
20 class NinjaTargetWriter {
21  public:
22   NinjaTargetWriter(const Target* target, std::ostream& out);
23   virtual ~NinjaTargetWriter();
24 
25   // Returns the build line to be written to the toolchain build file.
26   //
27   // Some targets have their rules written to separate files, and some can have
28   // their rules coalesced in the main build file. For the coalesced case, this
29   // function will return the rules as a string. For the separate file case,
30   // the separate ninja file will be written and the return string will be the
31   // subninja command to load that file.
32   static std::string RunAndWriteFile(const Target* target);
33 
34   virtual void Run() = 0;
35 
36  protected:
37   // Writes out the substitution values that are shared between the different
38   // types of tools (target gen dir, target label, etc.). Only the substitutions
39   // identified by the given bits will be written.
40   void WriteSharedVars(const SubstitutionBits& bits);
41 
42   // Writes out the substitution values that are shared between C compiler tools
43   // and action tools. Only the substitutions identified by the given bits will
44   // be written.
45   // If respect_source_used is set, the generated substitution values will
46   // respect the types of source code used; otherwise they will respect the bits
47   // passed in.
48   void WriteCCompilerVars(const SubstitutionBits& bits,
49                           bool indent,
50                           bool respect_source_used);
51 
52   // Writes out the substitution values that are shared between Rust tools
53   // and action tools. Only the substitutions identified by the given bits will
54   // be written, unless 'always_write' is specified.
55   void WriteRustCompilerVars(const SubstitutionBits& bits,
56                              bool indent,
57                              bool always_write);
58 
59   // Writes to the output stream a stamp rule for input dependencies, and
60   // returns the file to be appended to source rules that encodes the
61   // order-only dependencies for the current target.
62   // If num_stamp_uses is small, this might return all input dependencies
63   // directly, without writing a stamp file.
64   // If there are no implicit dependencies and no additional target dependencies
65   // are passed in, this returns an empty vector.
66   std::vector<OutputFile> WriteInputDepsStampAndGetDep(
67       const std::vector<const Target*>& additional_hard_deps,
68       size_t num_stamp_uses) const;
69 
70   // Writes to the output file a final stamp rule for the target that stamps
71   // the given list of files. This function assumes the stamp is for the target
72   // as a whole so the stamp file is set as the target's dependency output.
73   void WriteStampForTarget(const std::vector<OutputFile>& deps,
74                            const std::vector<OutputFile>& order_only_deps);
75 
76   const Settings* settings_;  // Non-owning.
77   const Target* target_;      // Non-owning.
78   std::ostream& out_;
79   PathOutput path_output_;
80 
81  private:
82   void WriteCopyRules();
83   void WriteEscapedSubstitution(const Substitution* type);
84 
85   NinjaTargetWriter(const NinjaTargetWriter&) = delete;
86   NinjaTargetWriter& operator=(const NinjaTargetWriter&) = delete;
87 };
88 
89 #endif  // TOOLS_GN_NINJA_TARGET_WRITER_H_
90