Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / tools / gn / function_process_file_template.cc
blobb7828a6b8aa724c21433923d78f44a5e08b283f3
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
3 // found in the LICENSE file.
5 #include "tools/gn/functions.h"
6 #include "tools/gn/parse_tree.h"
7 #include "tools/gn/scope.h"
8 #include "tools/gn/settings.h"
9 #include "tools/gn/substitution_list.h"
10 #include "tools/gn/substitution_writer.h"
11 #include "tools/gn/target.h"
12 #include "tools/gn/value_extractors.h"
14 namespace functions {
16 const char kProcessFileTemplate[] = "process_file_template";
17 const char kProcessFileTemplate_HelpShort[] =
18 "process_file_template: Do template expansion over a list of files.";
19 const char kProcessFileTemplate_Help[] =
20 "process_file_template: Do template expansion over a list of files.\n"
21 "\n"
22 " process_file_template(source_list, template)\n"
23 "\n"
24 " process_file_template applies a template list to a source file list,\n"
25 " returning the result of applying each template to each source. This is\n"
26 " typically used for computing output file names from input files.\n"
27 "\n"
28 " In most cases, get_target_outputs() will give the same result with\n"
29 " shorter, more maintainable code. This function should only be used\n"
30 " when that function can't be used (like there's no target or the target\n"
31 " is defined in another build file).\n"
32 "\n"
33 "Arguments:\n"
34 "\n"
35 " The source_list is a list of file names.\n"
36 "\n"
37 " The template can be a string or a list. If it is a list, multiple\n"
38 " output strings are generated for each input.\n"
39 "\n"
40 " The template should contain source expansions to which each name in\n"
41 " the source list is applied. See \"gn help source_expansion\".\n"
42 "\n"
43 "Example:\n"
44 "\n"
45 " sources = [\n"
46 " \"foo.idl\",\n"
47 " \"bar.idl\",\n"
48 " ]\n"
49 " myoutputs = process_file_template(\n"
50 " sources,\n"
51 " [ \"$target_gen_dir/{{source_name_part}}.cc\",\n"
52 " \"$target_gen_dir/{{source_name_part}}.h\" ])\n"
53 "\n"
54 " The result in this case will be:\n"
55 " [ \"//out/Debug/foo.cc\"\n"
56 " \"//out/Debug/foo.h\"\n"
57 " \"//out/Debug/bar.cc\"\n"
58 " \"//out/Debug/bar.h\" ]\n";
60 Value RunProcessFileTemplate(Scope* scope,
61 const FunctionCallNode* function,
62 const std::vector<Value>& args,
63 Err* err) {
64 if (args.size() != 2) {
65 *err = Err(function->function(), "Expected two arguments");
66 return Value();
69 // Source list.
70 Target::FileList input_files;
71 if (!ExtractListOfRelativeFiles(scope->settings()->build_settings(), args[0],
72 scope->GetSourceDir(), &input_files, err))
73 return Value();
75 std::vector<std::string> result_files;
76 SubstitutionList subst;
78 // Template.
79 const Value& template_arg = args[1];
80 if (template_arg.type() == Value::STRING) {
81 // Convert the string to a SubstitutionList with one pattern in it to
82 // simplify the code below.
83 std::vector<std::string> list;
84 list.push_back(template_arg.string_value());
85 if (!subst.Parse(list, template_arg.origin(), err))
86 return Value();
87 } else if (template_arg.type() == Value::LIST) {
88 if (!subst.Parse(template_arg, err))
89 return Value();
90 } else {
91 *err = Err(template_arg, "Not a string or a list.");
92 return Value();
95 SubstitutionWriter::ApplyListToSourcesAsString(
96 scope->settings(), subst, input_files, &result_files);
98 // Convert the list of strings to the return Value.
99 Value ret(function, Value::LIST);
100 ret.list_value().reserve(result_files.size());
101 for (const auto& file : result_files)
102 ret.list_value().push_back(Value(function, file));
104 return ret;
107 } // namespace functions