• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 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_C_BINARY_TARGET_WRITER_H_
6 #define TOOLS_GN_NINJA_C_BINARY_TARGET_WRITER_H_
7 
8 #include "base/macros.h"
9 #include "gn/config_values.h"
10 #include "gn/ninja_binary_target_writer.h"
11 #include "gn/toolchain.h"
12 #include "gn/unique_vector.h"
13 
14 struct EscapeOptions;
15 
16 // Writes a .ninja file for a binary target type (an executable, a shared
17 // library, or a static library).
18 class NinjaCBinaryTargetWriter : public NinjaBinaryTargetWriter {
19  public:
20   NinjaCBinaryTargetWriter(const Target* target, std::ostream& out);
21   ~NinjaCBinaryTargetWriter() override;
22 
23   void Run() override;
24 
25  protected:
26   // Adds source_set files to the list of object files.
27   void AddSourceSetFiles(const Target* source_set,
28                          UniqueVector<OutputFile>* obj_files) const override;
29 
30  private:
31   using OutputFileSet = std::set<OutputFile>;
32 
33   // Writes all flags for the compiler: includes, defines, cflags, etc.
34   void WriteCompilerVars();
35 
36   // Writes build lines required for precompiled headers. Any generated
37   // object files will be appended to the |object_files|. Any generated
38   // non-object files (for instance, .gch files from a GCC toolchain, are
39   // appended to |other_files|).
40   //
41   // input_dep is the stamp file collecting the dependencies required before
42   // compiling this target. It will be empty if there are no input deps.
43   void WritePCHCommands(const OutputFile& input_dep,
44                         const std::vector<OutputFile>& order_only_deps,
45                         std::vector<OutputFile>* object_files,
46                         std::vector<OutputFile>* other_files);
47 
48   // Writes a .pch compile build line for a language type.
49   void WritePCHCommand(const Substitution* flag_type,
50                        const char* tool_name,
51                        CTool::PrecompiledHeaderType header_type,
52                        const OutputFile& input_dep,
53                        const std::vector<OutputFile>& order_only_deps,
54                        std::vector<OutputFile>* object_files,
55                        std::vector<OutputFile>* other_files);
56 
57   void WriteGCCPCHCommand(const Substitution* flag_type,
58                           const char* tool_name,
59                           const OutputFile& input_dep,
60                           const std::vector<OutputFile>& order_only_deps,
61                           std::vector<OutputFile>* gch_files);
62 
63   void WriteWindowsPCHCommand(const Substitution* flag_type,
64                               const char* tool_name,
65                               const OutputFile& input_dep,
66                               const std::vector<OutputFile>& order_only_deps,
67                               std::vector<OutputFile>* object_files);
68 
69   // pch_deps are additional dependencies to run before the rule. They are
70   // expected to abide by the naming conventions specified by GetPCHOutputFiles.
71   //
72   // order_only_dep are the dependencies that must be run before doing any
73   // compiles.
74   //
75   // The files produced by the compiler will be added to two output vectors.
76   void WriteSources(const std::vector<OutputFile>& pch_deps,
77                     const OutputFile& input_dep,
78                     const std::vector<OutputFile>& order_only_deps,
79                     std::vector<OutputFile>* object_files,
80                     std::vector<SourceFile>* other_files);
81 
82   void WriteLinkerStuff(const std::vector<OutputFile>& object_files,
83                         const std::vector<SourceFile>& other_files,
84                         const OutputFile& input_dep);
85   void WriteOutputSubstitutions();
86   void WriteSolibs(const std::vector<OutputFile>& solibs);
87 
88   // Writes the implicit dependencies for the link or stamp line. This is
89   // the "||" and everything following it on the ninja line.
90   //
91   // The order-only dependencies are the non-linkable deps passed in as an
92   // argument, plus the data file depdencies in the target.
93   void WriteOrderOnlyDependencies(
94       const UniqueVector<const Target*>& non_linkable_deps);
95 
96   // Checks for duplicates in the given list of output files. If any duplicates
97   // are found, throws an error and return false.
98   bool CheckForDuplicateObjectFiles(const std::vector<OutputFile>& files) const;
99 
100   const CTool* tool_;
101 
102   DISALLOW_COPY_AND_ASSIGN(NinjaCBinaryTargetWriter);
103 };
104 
105 #endif  // TOOLS_GN_NINJA_C_BINARY_TARGET_WRITER_H_
106