Home | History | Annotate | Download | only in gn
      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.
      4 
      5 #include "tools/gn/file_template.h"
      6 #include "tools/gn/functions.h"
      7 #include "tools/gn/parse_tree.h"
      8 
      9 namespace functions {
     10 
     11 const char kProcessFileTemplate[] = "process_file_template";
     12 const char kProcessFileTemplate_Help[] =
     13     "process_file_template: Do template expansion over a list of files.\n"
     14     "\n"
     15     "  process_file_template(source_list, template)\n"
     16     "\n"
     17     "  process_file_template applies a template list to a source file list,\n"
     18     "  returning the result of applying each template to each source. This is\n"
     19     "  typically used for computing output file names from input files.\n"
     20     "\n"
     21     "Arguments:\n"
     22     "\n"
     23     "  The source_list is a list of file names.\n"
     24     "\n"
     25     "  The template can be a string or a list. If it is a list, multiple\n"
     26     "  output strings are generated for each input.\n"
     27     "\n"
     28     "  The following template substrings are used in the template arguments\n"
     29     "  and are replaced with the corresponding part of the input file name:\n"
     30     "\n"
     31     "    {{source}}\n"
     32     "        The entire source name.\n"
     33     "\n"
     34     "    {{source_name_part}}\n"
     35     "        The source name with no path or extension.\n"
     36     "\n"
     37     "Example:\n"
     38     "\n"
     39     "  sources = [\n"
     40     "    \"foo.idl\",\n"
     41     "    \"bar.idl\",\n"
     42     "  ]\n"
     43     "  myoutputs = process_file_template(\n"
     44     "      sources,\n"
     45     "      [ \"$target_gen_dir/{{source_name_part}}.cc\",\n"
     46     "        \"$target_gen_dir/{{source_name_part}}.h\" ])\n"
     47     "\n"
     48     " The result in this case will be:\n"
     49     "    [ \"//out/Debug/foo.cc\"\n"
     50     "      \"//out/Debug/foo.h\"\n"
     51     "      \"//out/Debug/bar.cc\"\n"
     52     "      \"//out/Debug/bar.h\" ]\n";
     53 
     54 Value RunProcessFileTemplate(Scope* scope,
     55                              const FunctionCallNode* function,
     56                              const std::vector<Value>& args,
     57                              Err* err) {
     58   if (args.size() != 2) {
     59     *err = Err(function->function(), "Expected two arguments");
     60     return Value();
     61   }
     62 
     63   FileTemplate file_template(args[1], err);
     64   if (err->has_error())
     65     return Value();
     66 
     67   Value ret(function, Value::LIST);
     68   file_template.Apply(args[0], function, &ret.list_value(), err);
     69   return ret;
     70 }
     71 
     72 }  // namespace functions
     73