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 <string_view> 9 10 #include "base/json/string_escape.h" 11 #include "gn/config_values_extractors.h" 12 #include "gn/escape.h" 13 #include "gn/filesystem_utils.h" 14 #include "gn/frameworks_utils.h" 15 #include "gn/path_output.h" 16 #include "gn/target.h" 17 #include "gn/toolchain.h" 18 #include "gn/variables.h" 19 20 struct DefineWriter { DefineWriterDefineWriter21 DefineWriter() { options.mode = ESCAPE_NINJA_COMMAND; } DefineWriterDefineWriter22 DefineWriter(EscapingMode mode) { options.mode = mode; } 23 operatorDefineWriter24 void operator()(const std::string& s, std::ostream& out) const { 25 out << " "; 26 EscapeStringToStream(out, "-D" + s, options); 27 } 28 29 EscapeOptions options; 30 }; 31 32 struct FrameworkDirsWriter { FrameworkDirsWriterFrameworkDirsWriter33 FrameworkDirsWriter(PathOutput& path_output, const std::string& tool_switch) 34 : path_output_(path_output), tool_switch_(tool_switch) {} 35 36 ~FrameworkDirsWriter() = default; 37 operatorFrameworkDirsWriter38 void operator()(const SourceDir& d, std::ostream& out) const { 39 std::ostringstream path_out; 40 path_output_.WriteDir(path_out, d, PathOutput::DIR_NO_LAST_SLASH); 41 const std::string& path = path_out.str(); 42 if (path[0] == '"') 43 out << " \"" << tool_switch_ << path.substr(1); 44 else 45 out << " " << tool_switch_ << path; 46 } 47 48 PathOutput& path_output_; 49 std::string tool_switch_; 50 }; 51 52 struct FrameworksWriter { FrameworksWriterFrameworksWriter53 explicit FrameworksWriter(const std::string& tool_switch) 54 : FrameworksWriter(ESCAPE_NINJA_COMMAND, tool_switch) {} FrameworksWriterFrameworksWriter55 FrameworksWriter(EscapingMode mode, const std::string& tool_switch) 56 : tool_switch_(tool_switch) { 57 options_.mode = mode; 58 } 59 operatorFrameworksWriter60 void operator()(const std::string& s, std::ostream& out) const { 61 out << " " << tool_switch_; 62 std::string_view framework_name = GetFrameworkName(s); 63 EscapeStringToStream(out, framework_name, options_); 64 } 65 66 EscapeOptions options_; 67 std::string tool_switch_; 68 }; 69 70 struct IncludeWriter { IncludeWriterIncludeWriter71 explicit IncludeWriter(PathOutput& path_output) : path_output_(path_output) {} 72 ~IncludeWriter() = default; 73 operatorIncludeWriter74 void operator()(const SourceDir& d, std::ostream& out) const { 75 std::ostringstream path_out; 76 path_output_.WriteDir(path_out, d, PathOutput::DIR_NO_LAST_SLASH); 77 const std::string& path = path_out.str(); 78 if (path[0] == '"') 79 out << " \"-I" << path.substr(1); 80 else 81 out << " -I" << path; 82 } 83 84 PathOutput& path_output_; 85 }; 86 87 // has_precompiled_headers is set when this substitution matches a tool type 88 // that supports precompiled headers, and this target supports precompiled 89 // headers. It doesn't indicate if the tool has precompiled headers (this 90 // will be looked up by this function). 91 // 92 // The tool_type indicates the corresponding tool for flags that are 93 // tool-specific (e.g. "cflags_c"). For non-tool-specific flags (e.g. 94 // "defines") tool_type should be TYPE_NONE. 95 void WriteOneFlag(RecursiveWriterConfig config, 96 const Target* target, 97 const Substitution* subst_enum, 98 bool has_precompiled_headers, 99 const char* tool_name, 100 const std::vector<std::string>& (ConfigValues::*getter)() 101 const, 102 EscapeOptions flag_escape_options, 103 PathOutput& path_output, 104 std::ostream& out, 105 bool write_substitution = true, 106 bool indent = false); 107 108 // Fills |outputs| with the object or gch file for the precompiled header of the 109 // given type (flag type and tool type must match). 110 void GetPCHOutputFiles(const Target* target, 111 const char* tool_name, 112 std::vector<OutputFile>* outputs); 113 114 std::string GetGCCPCHOutputExtension(const char* tool_name); 115 std::string GetWindowsPCHObjectExtension(const char* tool_name, 116 const std::string& obj_extension); 117 118 #endif // TOOLS_GN_NINJA_TARGET_COMMAND_WRITER_H_ 119