• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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_COMMAND_WRITER_H_
6 #define TOOLS_GN_NINJA_TARGET_COMMAND_WRITER_H_
7 
8 #include "base/json/string_escape.h"
9 #include "gn/config_values_extractors.h"
10 #include "gn/escape.h"
11 #include "gn/filesystem_utils.h"
12 #include "gn/frameworks_utils.h"
13 #include "gn/path_output.h"
14 #include "gn/target.h"
15 #include "gn/toolchain.h"
16 #include "gn/variables.h"
17 
18 struct DefineWriter {
DefineWriterDefineWriter19   DefineWriter() { options.mode = ESCAPE_NINJA_COMMAND; }
DefineWriterDefineWriter20   DefineWriter(EscapingMode mode, bool escape_strings)
21       : escape_strings(escape_strings) {
22     options.mode = mode;
23   }
24 
operatorDefineWriter25   void operator()(const std::string& s, std::ostream& out) const {
26     out << " ";
27     if (escape_strings) {
28       std::string dest;
29       base::EscapeJSONString(s, false, &dest);
30       EscapeStringToStream(out, "-D" + dest, options);
31       return;
32     }
33     EscapeStringToStream(out, "-D" + s, options);
34   }
35 
36   EscapeOptions options;
37   bool escape_strings = false;
38 };
39 
40 struct FrameworkDirsWriter {
FrameworkDirsWriterFrameworkDirsWriter41   FrameworkDirsWriter(PathOutput& path_output, const std::string& tool_switch)
42       : path_output_(path_output), tool_switch_(tool_switch) {}
43 
44   ~FrameworkDirsWriter() = default;
45 
operatorFrameworkDirsWriter46   void operator()(const SourceDir& d, std::ostream& out) const {
47     std::ostringstream path_out;
48     path_output_.WriteDir(path_out, d, PathOutput::DIR_NO_LAST_SLASH);
49     const std::string& path = path_out.str();
50     if (path[0] == '"')
51       out << " \"" << tool_switch_ << path.substr(1);
52     else
53       out << " " << tool_switch_ << path;
54   }
55 
56   PathOutput& path_output_;
57   std::string tool_switch_;
58 };
59 
60 struct FrameworksWriter {
FrameworksWriterFrameworksWriter61   explicit FrameworksWriter(const std::string& tool_switch)
62       : FrameworksWriter(ESCAPE_NINJA_COMMAND, false, tool_switch) {}
FrameworksWriterFrameworksWriter63   FrameworksWriter(EscapingMode mode,
64                    bool escape_strings,
65                    const std::string& tool_switch)
66       : escape_strings_(escape_strings), tool_switch_(tool_switch) {
67     options_.mode = mode;
68   }
69 
operatorFrameworksWriter70   void operator()(const std::string& s, std::ostream& out) const {
71     out << " " << tool_switch_;
72     std::string_view framework_name = GetFrameworkName(s);
73 
74     if (escape_strings_) {
75       std::string dest;
76       base::EscapeJSONString(framework_name, false, &dest);
77       EscapeStringToStream(out, dest, options_);
78       return;
79     }
80     EscapeStringToStream(out, framework_name, options_);
81   }
82 
83   EscapeOptions options_;
84   bool escape_strings_;
85   std::string tool_switch_;
86 };
87 
88 struct IncludeWriter {
IncludeWriterIncludeWriter89   explicit IncludeWriter(PathOutput& path_output) : path_output_(path_output) {}
90   ~IncludeWriter() = default;
91 
operatorIncludeWriter92   void operator()(const SourceDir& d, std::ostream& out) const {
93     std::ostringstream path_out;
94     path_output_.WriteDir(path_out, d, PathOutput::DIR_NO_LAST_SLASH);
95     const std::string& path = path_out.str();
96     if (path[0] == '"')
97       out << " \"-I" << path.substr(1);
98     else
99       out << " -I" << path;
100   }
101 
102   PathOutput& path_output_;
103 };
104 
105 // has_precompiled_headers is set when this substitution matches a tool type
106 // that supports precompiled headers, and this target supports precompiled
107 // headers. It doesn't indicate if the tool has precompiled headers (this
108 // will be looked up by this function).
109 //
110 // The tool_type indicates the corresponding tool for flags that are
111 // tool-specific (e.g. "cflags_c"). For non-tool-specific flags (e.g.
112 // "defines") tool_type should be TYPE_NONE.
113 void WriteOneFlag(const Target* target,
114                   const Substitution* subst_enum,
115                   bool has_precompiled_headers,
116                   const char* tool_name,
117                   const std::vector<std::string>& (ConfigValues::*getter)()
118                       const,
119                   EscapeOptions flag_escape_options,
120                   PathOutput& path_output,
121                   std::ostream& out,
122                   bool write_substitution = true);
123 
124 // Fills |outputs| with the object or gch file for the precompiled header of the
125 // given type (flag type and tool type must match).
126 void GetPCHOutputFiles(const Target* target,
127                        const char* tool_name,
128                        std::vector<OutputFile>* outputs);
129 
130 std::string GetGCCPCHOutputExtension(const char* tool_name);
131 std::string GetWindowsPCHObjectExtension(const char* tool_name,
132                                          const std::string& obj_extension);
133 
134 #endif  // TOOLS_GN_NINJA_TARGET_COMMAND_WRITER_H_
135