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 #include "tools/gn/substitution_writer.h" | 5 #include "tools/gn/substitution_writer.h" |
6 | 6 |
7 #include "tools/gn/build_settings.h" | 7 #include "tools/gn/build_settings.h" |
8 #include "tools/gn/escape.h" | 8 #include "tools/gn/escape.h" |
9 #include "tools/gn/filesystem_utils.h" | 9 #include "tools/gn/filesystem_utils.h" |
10 #include "tools/gn/output_file.h" | 10 #include "tools/gn/output_file.h" |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
98 " than the BUILD.gn file.\n" | 98 " than the BUILD.gn file.\n" |
99 " \"//foo/bar/baz.txt\" => \"gen/foo/bar\"\n" | 99 " \"//foo/bar/baz.txt\" => \"gen/foo/bar\"\n" |
100 "\n" | 100 "\n" |
101 " {{source_out_dir}}\n" | 101 " {{source_out_dir}}\n" |
102 " The object file directory (*) corresponding to the source file's\n" | 102 " The object file directory (*) corresponding to the source file's\n" |
103 " path, relative to the build directory. this us be different than\n" | 103 " path, relative to the build directory. this us be different than\n" |
104 " the target's out directory if the source file is in a different\n" | 104 " the target's out directory if the source file is in a different\n" |
105 " directory than the build.gn file.\n" | 105 " directory than the build.gn file.\n" |
106 " \"//foo/bar/baz.txt\" => \"obj/foo/bar\"\n" | 106 " \"//foo/bar/baz.txt\" => \"obj/foo/bar\"\n" |
107 "\n" | 107 "\n" |
108 " {{source_target_relative}}\n" | |
109 " The path to the source file relative to the target's directory.\n" | |
110 " This will generally be used for replicating the source directory\n" | |
111 " layout in the output directory.\n" | |
112 " \"//foo/bar/baz.txt\" => \"baz.txt\"\n" | |
brettw
2016/10/05 19:47:50
Can this comment mention this only works in action
Petr Hosek
2016/10/11 00:59:49
Done.
| |
113 "\n" | |
108 "(*) Note on directories\n" | 114 "(*) Note on directories\n" |
109 "\n" | 115 "\n" |
110 " Paths containing directories (except the source_root_relative_dir)\n" | 116 " Paths containing directories (except the source_root_relative_dir)\n" |
111 " will be different depending on what context the expansion is evaluated\n" | 117 " will be different depending on what context the expansion is evaluated\n" |
112 " in. Generally it should \"just work\" but it means you can't\n" | 118 " in. Generally it should \"just work\" but it means you can't\n" |
113 " concatenate strings containing these values with reasonable results.\n" | 119 " concatenate strings containing these values with reasonable results.\n" |
114 "\n" | 120 "\n" |
115 " Details: source expansions can be used in the \"outputs\" variable,\n" | 121 " Details: source expansions can be used in the \"outputs\" variable,\n" |
116 " the \"args\" variable, and in calls to \"process_file_template\". The\n" | 122 " the \"args\" variable, and in calls to \"process_file_template\". The\n" |
117 " \"args\" are passed to a script which is run from the build directory,\n" | 123 " \"args\" are passed to a script which is run from the build directory,\n" |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
196 const SubstitutionList& list, | 202 const SubstitutionList& list, |
197 std::vector<OutputFile>* output) { | 203 std::vector<OutputFile>* output) { |
198 std::vector<SourceFile> output_as_sources; | 204 std::vector<SourceFile> output_as_sources; |
199 GetListAsSourceFiles(list, &output_as_sources); | 205 GetListAsSourceFiles(list, &output_as_sources); |
200 for (const auto& file : output_as_sources) | 206 for (const auto& file : output_as_sources) |
201 output->push_back(OutputFile(settings->build_settings(), file)); | 207 output->push_back(OutputFile(settings->build_settings(), file)); |
202 } | 208 } |
203 | 209 |
204 // static | 210 // static |
205 SourceFile SubstitutionWriter::ApplyPatternToSource( | 211 SourceFile SubstitutionWriter::ApplyPatternToSource( |
212 const Target* target, | |
206 const Settings* settings, | 213 const Settings* settings, |
207 const SubstitutionPattern& pattern, | 214 const SubstitutionPattern& pattern, |
208 const SourceFile& source) { | 215 const SourceFile& source) { |
209 std::string result_value = ApplyPatternToSourceAsString( | 216 std::string result_value = ApplyPatternToSourceAsString( |
210 settings, pattern, source); | 217 target, settings, pattern, source); |
211 CHECK(!result_value.empty() && result_value[0] == '/') | 218 CHECK(!result_value.empty() && result_value[0] == '/') |
212 << "The result of the pattern \"" | 219 << "The result of the pattern \"" |
213 << pattern.AsString() | 220 << pattern.AsString() |
214 << "\" was not a path beginning in \"/\" or \"//\"."; | 221 << "\" was not a path beginning in \"/\" or \"//\"."; |
215 return SourceFile(SourceFile::SWAP_IN, &result_value); | 222 return SourceFile(SourceFile::SWAP_IN, &result_value); |
216 } | 223 } |
217 | 224 |
218 // static | 225 // static |
219 std::string SubstitutionWriter::ApplyPatternToSourceAsString( | 226 std::string SubstitutionWriter::ApplyPatternToSourceAsString( |
227 const Target* target, | |
220 const Settings* settings, | 228 const Settings* settings, |
221 const SubstitutionPattern& pattern, | 229 const SubstitutionPattern& pattern, |
222 const SourceFile& source) { | 230 const SourceFile& source) { |
223 std::string result_value; | 231 std::string result_value; |
224 for (const auto& subrange : pattern.ranges()) { | 232 for (const auto& subrange : pattern.ranges()) { |
225 if (subrange.type == SUBSTITUTION_LITERAL) { | 233 if (subrange.type == SUBSTITUTION_LITERAL) { |
226 result_value.append(subrange.literal); | 234 result_value.append(subrange.literal); |
227 } else { | 235 } else { |
228 result_value.append( | 236 result_value.append( |
229 GetSourceSubstitution(settings, source, subrange.type, | 237 GetSourceSubstitution(target, settings, source, subrange.type, |
230 OUTPUT_ABSOLUTE, SourceDir())); | 238 OUTPUT_ABSOLUTE, SourceDir())); |
231 } | 239 } |
232 } | 240 } |
233 return result_value; | 241 return result_value; |
234 } | 242 } |
235 | 243 |
236 // static | 244 // static |
237 OutputFile SubstitutionWriter::ApplyPatternToSourceAsOutputFile( | 245 OutputFile SubstitutionWriter::ApplyPatternToSourceAsOutputFile( |
246 const Target* target, | |
238 const Settings* settings, | 247 const Settings* settings, |
239 const SubstitutionPattern& pattern, | 248 const SubstitutionPattern& pattern, |
240 const SourceFile& source) { | 249 const SourceFile& source) { |
241 SourceFile result_as_source = ApplyPatternToSource(settings, pattern, source); | 250 SourceFile result_as_source = ApplyPatternToSource( |
251 target, settings, pattern, source); | |
242 return OutputFile(settings->build_settings(), result_as_source); | 252 return OutputFile(settings->build_settings(), result_as_source); |
243 } | 253 } |
244 | 254 |
245 // static | 255 // static |
246 void SubstitutionWriter::ApplyListToSource( | 256 void SubstitutionWriter::ApplyListToSource( |
257 const Target* target, | |
247 const Settings* settings, | 258 const Settings* settings, |
248 const SubstitutionList& list, | 259 const SubstitutionList& list, |
249 const SourceFile& source, | 260 const SourceFile& source, |
250 std::vector<SourceFile>* output) { | 261 std::vector<SourceFile>* output) { |
251 for (const auto& item : list.list()) | 262 for (const auto& item : list.list()) |
252 output->push_back(ApplyPatternToSource(settings, item, source)); | 263 output->push_back(ApplyPatternToSource(target, settings, item, source)); |
253 } | 264 } |
254 | 265 |
255 // static | 266 // static |
256 void SubstitutionWriter::ApplyListToSourceAsString( | 267 void SubstitutionWriter::ApplyListToSourceAsString( |
268 const Target* target, | |
257 const Settings* settings, | 269 const Settings* settings, |
258 const SubstitutionList& list, | 270 const SubstitutionList& list, |
259 const SourceFile& source, | 271 const SourceFile& source, |
260 std::vector<std::string>* output) { | 272 std::vector<std::string>* output) { |
261 for (const auto& item : list.list()) | 273 for (const auto& item : list.list()) |
262 output->push_back(ApplyPatternToSourceAsString(settings, item, source)); | 274 output->push_back(ApplyPatternToSourceAsString( |
275 target, settings, item, source)); | |
263 } | 276 } |
264 | 277 |
265 // static | 278 // static |
266 void SubstitutionWriter::ApplyListToSourceAsOutputFile( | 279 void SubstitutionWriter::ApplyListToSourceAsOutputFile( |
280 const Target* target, | |
267 const Settings* settings, | 281 const Settings* settings, |
268 const SubstitutionList& list, | 282 const SubstitutionList& list, |
269 const SourceFile& source, | 283 const SourceFile& source, |
270 std::vector<OutputFile>* output) { | 284 std::vector<OutputFile>* output) { |
271 for (const auto& item : list.list()) | 285 for (const auto& item : list.list()) |
272 output->push_back(ApplyPatternToSourceAsOutputFile(settings, item, source)); | 286 output->push_back(ApplyPatternToSourceAsOutputFile( |
287 target, settings, item, source)); | |
273 } | 288 } |
274 | 289 |
275 // static | 290 // static |
276 void SubstitutionWriter::ApplyListToSources( | 291 void SubstitutionWriter::ApplyListToSources( |
292 const Target* target, | |
277 const Settings* settings, | 293 const Settings* settings, |
278 const SubstitutionList& list, | 294 const SubstitutionList& list, |
279 const std::vector<SourceFile>& sources, | 295 const std::vector<SourceFile>& sources, |
280 std::vector<SourceFile>* output) { | 296 std::vector<SourceFile>* output) { |
281 output->clear(); | 297 output->clear(); |
282 for (const auto& source : sources) | 298 for (const auto& source : sources) |
283 ApplyListToSource(settings, list, source, output); | 299 ApplyListToSource(target, settings, list, source, output); |
284 } | 300 } |
285 | 301 |
286 // static | 302 // static |
287 void SubstitutionWriter::ApplyListToSourcesAsString( | 303 void SubstitutionWriter::ApplyListToSourcesAsString( |
304 const Target* target, | |
288 const Settings* settings, | 305 const Settings* settings, |
289 const SubstitutionList& list, | 306 const SubstitutionList& list, |
290 const std::vector<SourceFile>& sources, | 307 const std::vector<SourceFile>& sources, |
291 std::vector<std::string>* output) { | 308 std::vector<std::string>* output) { |
292 output->clear(); | 309 output->clear(); |
293 for (const auto& source : sources) | 310 for (const auto& source : sources) |
294 ApplyListToSourceAsString(settings, list, source, output); | 311 ApplyListToSourceAsString(target, settings, list, source, output); |
295 } | 312 } |
296 | 313 |
297 // static | 314 // static |
298 void SubstitutionWriter::ApplyListToSourcesAsOutputFile( | 315 void SubstitutionWriter::ApplyListToSourcesAsOutputFile( |
316 const Target* target, | |
299 const Settings* settings, | 317 const Settings* settings, |
300 const SubstitutionList& list, | 318 const SubstitutionList& list, |
301 const std::vector<SourceFile>& sources, | 319 const std::vector<SourceFile>& sources, |
302 std::vector<OutputFile>* output) { | 320 std::vector<OutputFile>* output) { |
303 output->clear(); | 321 output->clear(); |
304 for (const auto& source : sources) | 322 for (const auto& source : sources) |
305 ApplyListToSourceAsOutputFile(settings, list, source, output); | 323 ApplyListToSourceAsOutputFile(target, settings, list, source, output); |
306 } | 324 } |
307 | 325 |
308 // static | 326 // static |
309 void SubstitutionWriter::WriteNinjaVariablesForSource( | 327 void SubstitutionWriter::WriteNinjaVariablesForSource( |
328 const Target* target, | |
310 const Settings* settings, | 329 const Settings* settings, |
311 const SourceFile& source, | 330 const SourceFile& source, |
312 const std::vector<SubstitutionType>& types, | 331 const std::vector<SubstitutionType>& types, |
313 const EscapeOptions& escape_options, | 332 const EscapeOptions& escape_options, |
314 std::ostream& out) { | 333 std::ostream& out) { |
315 for (const auto& type : types) { | 334 for (const auto& type : types) { |
316 // Don't write SOURCE since that just maps to Ninja's $in variable, which | 335 // Don't write SOURCE since that just maps to Ninja's $in variable, which |
317 // is implicit in the rule. RESPONSE_FILE_NAME is written separately | 336 // is implicit in the rule. RESPONSE_FILE_NAME is written separately |
318 // only when writing target rules since it can never be used in any | 337 // only when writing target rules since it can never be used in any |
319 // other context (like process_file_template). | 338 // other context (like process_file_template). |
320 if (type != SUBSTITUTION_SOURCE && type != SUBSTITUTION_RSP_FILE_NAME) { | 339 if (type != SUBSTITUTION_SOURCE && type != SUBSTITUTION_RSP_FILE_NAME) { |
321 out << " " << kSubstitutionNinjaNames[type] << " = "; | 340 out << " " << kSubstitutionNinjaNames[type] << " = "; |
322 EscapeStringToStream( | 341 EscapeStringToStream( |
323 out, | 342 out, |
324 GetSourceSubstitution(settings, source, type, OUTPUT_RELATIVE, | 343 GetSourceSubstitution(target, settings, source, type, |
344 OUTPUT_RELATIVE, | |
325 settings->build_settings()->build_dir()), | 345 settings->build_settings()->build_dir()), |
326 escape_options); | 346 escape_options); |
327 out << std::endl; | 347 out << std::endl; |
328 } | 348 } |
329 } | 349 } |
330 } | 350 } |
331 | 351 |
332 // static | 352 // static |
333 std::string SubstitutionWriter::GetSourceSubstitution( | 353 std::string SubstitutionWriter::GetSourceSubstitution( |
354 const Target* target, | |
334 const Settings* settings, | 355 const Settings* settings, |
335 const SourceFile& source, | 356 const SourceFile& source, |
336 SubstitutionType type, | 357 SubstitutionType type, |
337 OutputStyle output_style, | 358 OutputStyle output_style, |
338 const SourceDir& relative_to) { | 359 const SourceDir& relative_to) { |
339 std::string to_rebase; | 360 std::string to_rebase; |
340 switch (type) { | 361 switch (type) { |
341 case SUBSTITUTION_SOURCE: | 362 case SUBSTITUTION_SOURCE: |
342 if (source.is_system_absolute()) | 363 if (source.is_system_absolute()) |
343 return source.value(); | 364 return source.value(); |
(...skipping 22 matching lines...) Expand all Loading... | |
366 case SUBSTITUTION_SOURCE_GEN_DIR: | 387 case SUBSTITUTION_SOURCE_GEN_DIR: |
367 to_rebase = DirectoryWithNoLastSlash(GetSubBuildDirAsSourceDir( | 388 to_rebase = DirectoryWithNoLastSlash(GetSubBuildDirAsSourceDir( |
368 BuildDirContext(settings), source.GetDir(), BuildDirType::GEN)); | 389 BuildDirContext(settings), source.GetDir(), BuildDirType::GEN)); |
369 break; | 390 break; |
370 | 391 |
371 case SUBSTITUTION_SOURCE_OUT_DIR: | 392 case SUBSTITUTION_SOURCE_OUT_DIR: |
372 to_rebase = DirectoryWithNoLastSlash(GetSubBuildDirAsSourceDir( | 393 to_rebase = DirectoryWithNoLastSlash(GetSubBuildDirAsSourceDir( |
373 BuildDirContext(settings), source.GetDir(), BuildDirType::OBJ)); | 394 BuildDirContext(settings), source.GetDir(), BuildDirType::OBJ)); |
374 break; | 395 break; |
375 | 396 |
397 case SUBSTITUTION_SOURCE_TARGET_RELATIVE: | |
398 if (target) | |
brettw
2016/10/05 19:47:50
Chrome-style says this condition needs {}
Petr Hosek
2016/10/11 00:59:49
Done.
| |
399 return RebasePath(source.value(), target->label().dir(), | |
400 settings->build_settings()->root_path_utf8()); | |
401 to_rebase = source.value(); | |
brettw
2016/10/05 19:47:50
In the "else" case here, I think it would be bette
Petr Hosek
2016/10/11 00:59:49
Done.
| |
402 break; | |
403 | |
376 default: | 404 default: |
377 NOTREACHED() | 405 NOTREACHED() |
378 << "Unsupported substitution for this function: " | 406 << "Unsupported substitution for this function: " |
379 << kSubstitutionNames[type]; | 407 << kSubstitutionNames[type]; |
380 return std::string(); | 408 return std::string(); |
381 } | 409 } |
382 | 410 |
383 // If we get here, the result is a path that should be made relative or | 411 // If we get here, the result is a path that should be made relative or |
384 // absolute according to the output_style. Other cases (just file name or | 412 // absolute according to the output_style. Other cases (just file name or |
385 // extension extraction) will have been handled via early return above. | 413 // extension extraction) will have been handled via early return above. |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
502 const Target* target, | 530 const Target* target, |
503 const SourceFile& source, | 531 const SourceFile& source, |
504 SubstitutionType type) { | 532 SubstitutionType type) { |
505 // First try the common tool ones. | 533 // First try the common tool ones. |
506 std::string result; | 534 std::string result; |
507 if (GetTargetSubstitution(target, type, &result)) | 535 if (GetTargetSubstitution(target, type, &result)) |
508 return result; | 536 return result; |
509 | 537 |
510 // Fall-through to the source ones. | 538 // Fall-through to the source ones. |
511 return GetSourceSubstitution( | 539 return GetSourceSubstitution( |
512 target->settings(), source, type, OUTPUT_RELATIVE, | 540 target, target->settings(), source, type, OUTPUT_RELATIVE, |
513 target->settings()->build_settings()->build_dir()); | 541 target->settings()->build_settings()->build_dir()); |
514 } | 542 } |
515 | 543 |
516 // static | 544 // static |
517 OutputFile SubstitutionWriter::ApplyPatternToLinkerAsOutputFile( | 545 OutputFile SubstitutionWriter::ApplyPatternToLinkerAsOutputFile( |
518 const Target* target, | 546 const Target* target, |
519 const Tool* tool, | 547 const Tool* tool, |
520 const SubstitutionPattern& pattern) { | 548 const SubstitutionPattern& pattern) { |
521 OutputFile result; | 549 OutputFile result; |
522 for (const auto& subrange : pattern.ranges()) { | 550 for (const auto& subrange : pattern.ranges()) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
576 return tool->default_output_extension(); | 604 return tool->default_output_extension(); |
577 if (target->output_extension().empty()) | 605 if (target->output_extension().empty()) |
578 return std::string(); // Explicitly set to no extension. | 606 return std::string(); // Explicitly set to no extension. |
579 return std::string(".") + target->output_extension(); | 607 return std::string(".") + target->output_extension(); |
580 | 608 |
581 default: | 609 default: |
582 NOTREACHED(); | 610 NOTREACHED(); |
583 return std::string(); | 611 return std::string(); |
584 } | 612 } |
585 } | 613 } |
OLD | NEW |