Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef TOOLS_GN_FILE_TEMPLATE_H_ | 5 #ifndef TOOLS_GN_FILE_TEMPLATE_H_ |
| 6 #define TOOLS_GN_FILE_TEMPLATE_H_ | 6 #define TOOLS_GN_FILE_TEMPLATE_H_ |
| 7 | 7 |
| 8 #include <iosfwd> | 8 #include <iosfwd> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/containers/stack_container.h" | 11 #include "base/containers/stack_container.h" |
| 12 #include "tools/gn/err.h" | 12 #include "tools/gn/err.h" |
| 13 #include "tools/gn/source_dir.h" | |
| 13 #include "tools/gn/value.h" | 14 #include "tools/gn/value.h" |
| 14 | 15 |
| 15 struct EscapeOptions; | 16 struct EscapeOptions; |
| 16 class ParseNode; | 17 class ParseNode; |
| 17 class Settings; | 18 class Settings; |
| 18 class SourceFile; | 19 class SourceFile; |
| 19 class Target; | 20 class Target; |
| 20 | 21 |
| 21 extern const char kSourceExpansion_Help[]; | 22 extern const char kSourceExpansion_Help[]; |
| 22 | 23 |
| 23 // A FileTemplate object implements source expansion for a given "template" | 24 // A FileTemplate object implements source expansion for a given "template" |
| 24 // (either outputs or args, depending on the target type). | 25 // (either outputs or args, depending on the target type). |
| 25 // | 26 // |
| 26 // There are two ways you can use this. You can make a template and then | 27 // There are two ways you can use this. You can make a template and then |
| 27 // apply a source to it to get a list of outputs manually. Or you can do the | 28 // apply a source to it to get a list of outputs manually. Or you can do the |
| 28 // actual substitution in Ninja, writing the arguments in a rule and using | 29 // actual substitution in Ninja, writing the arguments in a rule and using |
| 29 // variables in build statements to invoke the rule with the right | 30 // variables in build statements to invoke the rule with the right |
| 30 // substitutions. | 31 // substitutions. |
| 31 class FileTemplate { | 32 class FileTemplate { |
| 32 public: | 33 public: |
| 34 enum OutputStyle { | |
| 35 OUTPUT_ABSOLUTE, // Dirs will be absolute "//foo/bar". | |
| 36 OUTPUT_RELATIVE, // Dirs will be relative to a given directory. | |
| 37 }; | |
| 38 | |
| 33 struct Subrange { | 39 struct Subrange { |
| 34 // See the help in the .cc file for what these mean. | 40 // See the help in the .cc file for what these mean. |
| 35 enum Type { | 41 enum Type { |
| 36 LITERAL = 0, | 42 LITERAL = 0, |
| 37 | 43 |
| 38 SOURCE, // {{source}} | 44 SOURCE, // {{source}} |
| 39 NAME_PART, // {{source_name_part}} | 45 NAME_PART, // {{source_name_part}} |
| 40 FILE_PART, // {{source_file_part}} | 46 FILE_PART, // {{source_file_part}} |
| 41 SOURCE_DIR, // {{source_dir}} | 47 SOURCE_DIR, // {{source_dir}} |
| 42 ROOT_RELATIVE_DIR, // {{root_relative_dir}} | 48 ROOT_RELATIVE_DIR, // {{root_relative_dir}} |
| 43 SOURCE_GEN_DIR, // {{source_gen_dir}} | 49 SOURCE_GEN_DIR, // {{source_gen_dir}} |
| 44 SOURCE_OUT_DIR, // {{source_out_dir}} | 50 SOURCE_OUT_DIR, // {{source_out_dir}} |
| 45 | 51 |
| 46 NUM_TYPES // Must be last | 52 NUM_TYPES // Must be last |
| 47 }; | 53 }; |
| 48 Subrange(Type t, const std::string& l = std::string()) | 54 Subrange(Type t, const std::string& l = std::string()) |
| 49 : type(t), | 55 : type(t), |
| 50 literal(l) { | 56 literal(l) { |
| 51 } | 57 } |
| 52 | 58 |
| 53 Type type; | 59 Type type; |
| 54 | 60 |
| 55 // When type_ == LITERAL, this specifies the literal. | 61 // When type_ == LITERAL, this specifies the literal. |
| 56 std::string literal; | 62 std::string literal; |
| 57 }; | 63 }; |
| 58 | 64 |
| 59 // Constructs a template from the given value. On error, the err will be | 65 // Constructs a template from the given value. On error, the err will be |
| 60 // set. In this case you should not use this object. | 66 // set. In this case you should not use this object. |
| 61 FileTemplate(const Settings* settings, const Value& t, Err* err); | 67 FileTemplate(const Settings* settings, |
| 62 FileTemplate(const Settings* settings, const std::vector<std::string>& t); | 68 const Value& t, |
| 63 FileTemplate(const Settings* settings, const std::vector<SourceFile>& t); | 69 OutputStyle output_style, |
| 70 const SourceDir& relative_to, | |
| 71 Err* err); | |
| 72 FileTemplate(const Settings* settings, | |
| 73 const std::vector<std::string>& t, | |
| 74 OutputStyle output_style, | |
| 75 const SourceDir& relative_to); | |
| 76 FileTemplate(const Settings* settings, | |
| 77 const std::vector<SourceFile>& t, | |
| 78 OutputStyle output_style, | |
| 79 const SourceDir& relative_to); | |
| 64 | 80 |
| 65 ~FileTemplate(); | 81 ~FileTemplate(); |
| 66 | 82 |
| 67 // Returns an output template representing the given target's script | 83 // Returns an output template representing the given target's script |
| 68 // outputs. | 84 // outputs. |
| 69 static FileTemplate GetForTargetOutputs(const Target* target); | 85 static FileTemplate GetForTargetOutputs(const Target* target); |
| 70 | 86 |
| 71 // Returns true if the given substitution type is used by this template. | 87 // Returns true if the given substitution type is used by this template. |
| 72 bool IsTypeUsed(Subrange::Type type) const; | 88 bool IsTypeUsed(Subrange::Type type) const; |
| 73 | 89 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 98 | 114 |
| 99 // Writes to the given stream the variable declarations for extracting the | 115 // Writes to the given stream the variable declarations for extracting the |
| 100 // required parts of the given source file string. The results will be | 116 // required parts of the given source file string. The results will be |
| 101 // indented two spaces. | 117 // indented two spaces. |
| 102 // | 118 // |
| 103 // This is used to set up a build statement to invoke a rule where the rule | 119 // This is used to set up a build statement to invoke a rule where the rule |
| 104 // contains a representation of this file template to be expanded by Ninja | 120 // contains a representation of this file template to be expanded by Ninja |
| 105 // (see GetWithNinjaExpansions). | 121 // (see GetWithNinjaExpansions). |
| 106 void WriteNinjaVariablesForSubstitution( | 122 void WriteNinjaVariablesForSubstitution( |
| 107 std::ostream& out, | 123 std::ostream& out, |
| 108 const Settings* settings, | |
| 109 const SourceFile& source, | 124 const SourceFile& source, |
| 110 const EscapeOptions& escape_options) const; | 125 const EscapeOptions& escape_options) const; |
| 111 | 126 |
| 112 // Returns the Ninja variable name used by the above Ninja functions to | 127 // Returns the Ninja variable name used by the above Ninja functions to |
| 113 // substitute for the given type. | 128 // substitute for the given type. |
| 114 static const char* GetNinjaVariableNameForType(Subrange::Type type); | 129 static const char* GetNinjaVariableNameForType(Subrange::Type type); |
| 115 | 130 |
| 116 // Extracts the given type of substitution from the given source. The source | 131 // Extracts the given type of substitution from the given source file. |
| 117 // should be the file name relative to the output directory. | 132 // If output_style is RELATIVE, relative_to indicates the directory that the |
|
viettrungluu
2014/07/10 23:10:39
nit: RELATIVE -> OUTPUT_RELATIVE?
| |
| 133 // relative directories should be relative to, otherwise it is ignored. | |
| 118 static std::string GetSubstitution(const Settings* settings, | 134 static std::string GetSubstitution(const Settings* settings, |
| 119 const SourceFile& source, | 135 const SourceFile& source, |
| 120 Subrange::Type type); | 136 Subrange::Type type, |
| 137 OutputStyle output_style, | |
| 138 const SourceDir& relative_to); | |
| 121 | 139 |
| 122 // Known template types, these include the "{{ }}" | 140 // Known template types, these include the "{{ }}". |
| 141 // IF YOU ADD NEW ONES: If the expansion expands to something inside the | |
| 142 // output directory, also update EnsureStringIsInOutputDir. | |
| 123 static const char kSource[]; | 143 static const char kSource[]; |
| 124 static const char kSourceNamePart[]; | 144 static const char kSourceNamePart[]; |
| 125 static const char kSourceFilePart[]; | 145 static const char kSourceFilePart[]; |
| 126 static const char kSourceDir[]; | 146 static const char kSourceDir[]; |
| 127 static const char kRootRelDir[]; | 147 static const char kRootRelDir[]; |
| 128 static const char kSourceGenDir[]; | 148 static const char kSourceGenDir[]; |
| 129 static const char kSourceOutDir[]; | 149 static const char kSourceOutDir[]; |
| 130 | 150 |
| 131 private: | 151 private: |
| 132 typedef base::StackVector<Subrange, 8> Template; | 152 typedef base::StackVector<Subrange, 8> Template; |
| 133 typedef base::StackVector<Template, 8> TemplateVector; | 153 typedef base::StackVector<Template, 8> TemplateVector; |
| 134 | 154 |
| 135 void ParseInput(const Value& value, Err* err); | 155 void ParseInput(const Value& value, Err* err); |
| 136 | 156 |
| 137 // Parses a template string and adds it to the templates_ list. | 157 // Parses a template string and adds it to the templates_ list. |
| 138 void ParseOneTemplateString(const std::string& str); | 158 void ParseOneTemplateString(const std::string& str); |
| 139 | 159 |
| 140 const Settings* settings_; | 160 const Settings* settings_; |
| 161 OutputStyle output_style_; | |
| 162 SourceDir relative_to_; | |
| 141 | 163 |
| 142 TemplateVector templates_; | 164 TemplateVector templates_; |
| 143 | 165 |
| 144 // The corresponding value is set to true if the given subrange type is | 166 // The corresponding value is set to true if the given subrange type is |
| 145 // required. This allows us to precompute these types whem applying them | 167 // required. This allows us to precompute these types whem applying them |
| 146 // to a given source file. | 168 // to a given source file. |
| 147 bool types_required_[Subrange::NUM_TYPES]; | 169 bool types_required_[Subrange::NUM_TYPES]; |
| 148 | 170 |
| 149 // Set when any of the types_required_ is true. Otherwise, everythins is a | 171 // Set when any of the types_required_ is true. Otherwise, everythins is a |
| 150 // literal (a common case so we can optimize some code paths). | 172 // literal (a common case so we can optimize some code paths). |
| 151 bool has_substitutions_; | 173 bool has_substitutions_; |
| 152 }; | 174 }; |
| 153 | 175 |
| 154 #endif // TOOLS_GN_FILE_TEMPLATE_H_ | 176 #endif // TOOLS_GN_FILE_TEMPLATE_H_ |
| OLD | NEW |