OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_SUBSTITUTION_WRITER_H_ | 5 #ifndef TOOLS_GN_SUBSTITUTION_WRITER_H_ |
6 #define TOOLS_GN_SUBSTITUTION_WRITER_H_ | 6 #define TOOLS_GN_SUBSTITUTION_WRITER_H_ |
7 | 7 |
8 #include <iosfwd> | 8 #include <iosfwd> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "tools/gn/substitution_type.h" | 12 #include "tools/gn/substitution_type.h" |
13 | 13 |
14 struct EscapeOptions; | 14 struct EscapeOptions; |
15 class OutputFile; | 15 class OutputFile; |
16 class Settings; | 16 class Settings; |
17 class SourceDir; | 17 class SourceDir; |
18 class SourceFile; | 18 class SourceFile; |
19 class SubstitutionList; | 19 class SubstitutionList; |
20 class SubstitutionPattern; | 20 class SubstitutionPattern; |
| 21 class Target; |
| 22 class Tool; |
21 | 23 |
22 // Help text for script source expansion. | 24 // Help text for script source expansion. |
23 extern const char kSourceExpansion_Help[]; | 25 extern const char kSourceExpansion_Help[]; |
24 | 26 |
25 // This class handles writing or applying substitution patterns to strings. | 27 // This class handles writing or applying substitution patterns to strings. |
| 28 // |
| 29 // There are several different uses: |
| 30 // |
| 31 // - Source substitutions: These are used to compute action_foreach |
| 32 // outputs and arguments. Functions are provided to expand these in terms |
| 33 // of both OutputFiles (for writing Ninja files) as well as SourceFiles |
| 34 // (for computing lists used by code). |
| 35 // |
| 36 // - Target substitutions: These are specific to the target+tool combination |
| 37 // and are shared between the compiler and linker ones. It includes things |
| 38 // like the target_gen_dir. |
| 39 // |
| 40 // - Compiler substitutions: These are used to compute compiler outputs. |
| 41 // It includes all source substitutions (since they depend on the various |
| 42 // parts of the source file) as well as the target substitutions. |
| 43 // |
| 44 // - Linker substitutions: These are used to compute linker outputs. It |
| 45 // includes the target substitutions. |
| 46 // |
| 47 // The compiler and linker specific substitutions do NOT include the various |
| 48 // cflags, ldflags, libraries, etc. These are written by the ninja target |
| 49 // writer since they depend on traversing the dependency tree. |
26 class SubstitutionWriter { | 50 class SubstitutionWriter { |
27 public: | 51 public: |
28 enum OutputStyle { | 52 enum OutputStyle { |
29 OUTPUT_ABSOLUTE, // Dirs will be absolute "//foo/bar". | 53 OUTPUT_ABSOLUTE, // Dirs will be absolute "//foo/bar". |
30 OUTPUT_RELATIVE, // Dirs will be relative to a given directory. | 54 OUTPUT_RELATIVE, // Dirs will be relative to a given directory. |
31 }; | 55 }; |
32 | 56 |
33 SubstitutionWriter(); | 57 // Writes the pattern to the given stream with no special handling, and with |
34 ~SubstitutionWriter(); | 58 // Ninja variables replacing the patterns. |
| 59 static void WriteWithNinjaVariables( |
| 60 const SubstitutionPattern& pattern, |
| 61 const EscapeOptions& escape_options, |
| 62 std::ostream& out); |
| 63 |
| 64 // NOP substitutions --------------------------------------------------------- |
35 | 65 |
36 // Converts the given SubstitutionList to OutputFiles assuming there are | 66 // Converts the given SubstitutionList to OutputFiles assuming there are |
37 // no substitutions (it will assert if there are). This is used for cases | 67 // no substitutions (it will assert if there are). This is used for cases |
38 // like actions where the outputs are explicit, but the list is stored as | 68 // like actions where the outputs are explicit, but the list is stored as |
39 // a SubstitutionList. | 69 // a SubstitutionList. |
40 static void GetListAsSourceFiles( | 70 static void GetListAsSourceFiles( |
41 const Settings* settings, | |
42 const SubstitutionList& list, | 71 const SubstitutionList& list, |
43 std::vector<SourceFile>* output); | 72 std::vector<SourceFile>* output); |
44 static void GetListAsOutputFiles( | 73 static void GetListAsOutputFiles( |
45 const Settings* settings, | 74 const Settings* settings, |
46 const SubstitutionList& list, | 75 const SubstitutionList& list, |
47 std::vector<OutputFile>* output); | 76 std::vector<OutputFile>* output); |
48 | 77 |
| 78 // Source substitutions ----------------------------------------------------- |
| 79 |
49 // Applies the substitution pattern to a source file, returning the result | 80 // Applies the substitution pattern to a source file, returning the result |
50 // as either a string, a SourceFile or an OutputFile. If the result is | 81 // as either a string, a SourceFile or an OutputFile. If the result is |
51 // expected to be a SourceFile or an OutputFile, this will CHECK if the | 82 // expected to be a SourceFile or an OutputFile, this will CHECK if the |
52 // result isn't in the correct directory. The caller should validate this | 83 // result isn't in the correct directory. The caller should validate this |
53 // first (see for example IsFileInOuputDir). | 84 // first (see for example IsFileInOuputDir). |
54 static SourceFile ApplyPatternToSource( | 85 static SourceFile ApplyPatternToSource( |
55 const Settings* settings, | 86 const Settings* settings, |
56 const SubstitutionPattern& pattern, | 87 const SubstitutionPattern& pattern, |
57 const SourceFile& source); | 88 const SourceFile& source); |
58 static std::string ApplyPatternToSourceAsString( | 89 static std::string ApplyPatternToSourceAsString( |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 // Ninja files, paths will be relative to the build dir, and no definition | 139 // Ninja files, paths will be relative to the build dir, and no definition |
109 // for {{source}} will be written since that maps to Ninja's implicit $in | 140 // for {{source}} will be written since that maps to Ninja's implicit $in |
110 // variable. | 141 // variable. |
111 static void WriteNinjaVariablesForSource( | 142 static void WriteNinjaVariablesForSource( |
112 const Settings* settings, | 143 const Settings* settings, |
113 const SourceFile& source, | 144 const SourceFile& source, |
114 const std::vector<SubstitutionType>& types, | 145 const std::vector<SubstitutionType>& types, |
115 const EscapeOptions& escape_options, | 146 const EscapeOptions& escape_options, |
116 std::ostream& out); | 147 std::ostream& out); |
117 | 148 |
118 // Writes the pattern to the given stream with no special handling, and with | |
119 // Ninja variables replacing the patterns. | |
120 static void WriteWithNinjaVariables( | |
121 const SubstitutionPattern& pattern, | |
122 const EscapeOptions& escape_options, | |
123 std::ostream& out); | |
124 | |
125 // Extracts the given type of substitution related to a source file from the | 149 // Extracts the given type of substitution related to a source file from the |
126 // given source file. If output_style is OUTPUT_RELATIVE, relative_to | 150 // given source file. If output_style is OUTPUT_RELATIVE, relative_to |
127 // indicates the directory that the relative directories should be relative | 151 // indicates the directory that the relative directories should be relative |
128 // to, otherwise it is ignored. | 152 // to, otherwise it is ignored. |
129 static std::string GetSourceSubstitution( | 153 static std::string GetSourceSubstitution( |
130 const Settings* settings, | 154 const Settings* settings, |
131 const SourceFile& source, | 155 const SourceFile& source, |
132 SubstitutionType type, | 156 SubstitutionType type, |
133 OutputStyle output_style, | 157 OutputStyle output_style, |
134 const SourceDir& relative_to); | 158 const SourceDir& relative_to); |
| 159 |
| 160 // Target substitutions ------------------------------------------------------ |
| 161 // |
| 162 // Handles the target substitutions that apply to both compiler and linker |
| 163 // tools. |
| 164 static OutputFile ApplyPatternToTargetAsOutputFile( |
| 165 const Target* target, |
| 166 const Tool* tool, |
| 167 const SubstitutionPattern& pattern); |
| 168 static void ApplyListToTargetAsOutputFile( |
| 169 const Target* target, |
| 170 const Tool* tool, |
| 171 const SubstitutionList& list, |
| 172 std::vector<OutputFile>* output); |
| 173 |
| 174 // This function is slightly different than the other substitution getters |
| 175 // since it can handle failure (since it is designed to be used by the |
| 176 // compiler and linker ones which will fall through if it's not a common tool |
| 177 // one). |
| 178 static bool GetTargetSubstitution( |
| 179 const Target* target, |
| 180 SubstitutionType type, |
| 181 std::string* result); |
| 182 static std::string GetTargetSubstitution( |
| 183 const Target* target, |
| 184 SubstitutionType type); |
| 185 |
| 186 // Compiler substitutions ---------------------------------------------------- |
| 187 // |
| 188 // A compiler substitution allows both source and tool substitutions. These |
| 189 // are used to compute output names for compiler tools. |
| 190 |
| 191 static OutputFile ApplyPatternToCompilerAsOutputFile( |
| 192 const Target* target, |
| 193 const SourceFile& source, |
| 194 const SubstitutionPattern& pattern); |
| 195 static void ApplyListToCompilerAsOutputFile( |
| 196 const Target* target, |
| 197 const SourceFile& source, |
| 198 const SubstitutionList& list, |
| 199 std::vector<OutputFile>* output); |
| 200 |
| 201 // Like GetSourceSubstitution but for strings based on the target or |
| 202 // toolchain. This type of result will always be relative to the build |
| 203 // directory. |
| 204 static std::string GetCompilerSubstitution( |
| 205 const Target* target, |
| 206 const SourceFile& source, |
| 207 SubstitutionType type); |
| 208 |
| 209 // Linker substitutions ------------------------------------------------------ |
| 210 |
| 211 static OutputFile ApplyPatternToLinkerAsOutputFile( |
| 212 const Target* target, |
| 213 const Tool* tool, |
| 214 const SubstitutionPattern& pattern); |
| 215 static void ApplyListToLinkerAsOutputFile( |
| 216 const Target* target, |
| 217 const Tool* tool, |
| 218 const SubstitutionList& list, |
| 219 std::vector<OutputFile>* output); |
| 220 |
| 221 // Like GetSourceSubstitution but for strings based on the target or |
| 222 // toolchain. This type of result will always be relative to the build |
| 223 // directory. |
| 224 static std::string GetLinkerSubstitution( |
| 225 const Target* target, |
| 226 const Tool* tool, |
| 227 SubstitutionType type); |
135 }; | 228 }; |
136 | 229 |
137 #endif // TOOLS_GN_SUBSTITUTION_WRITER_H_ | 230 #endif // TOOLS_GN_SUBSTITUTION_WRITER_H_ |
OLD | NEW |