- add sources.
[platform/framework/web/crosswalk.git] / src / tools / gn / functions.cc
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/functions.h"
6
7 #include <iostream>
8
9 #include "base/strings/string_util.h"
10 #include "tools/gn/config.h"
11 #include "tools/gn/config_values_generator.h"
12 #include "tools/gn/err.h"
13 #include "tools/gn/input_file.h"
14 #include "tools/gn/item_tree.h"
15 #include "tools/gn/parse_tree.h"
16 #include "tools/gn/scheduler.h"
17 #include "tools/gn/scope.h"
18 #include "tools/gn/settings.h"
19 #include "tools/gn/target_manager.h"
20 #include "tools/gn/token.h"
21 #include "tools/gn/value.h"
22
23 namespace {
24
25 // This is called when a template is invoked. When we see a template
26 // declaration, that funciton is RunTemplate.
27 Value RunTemplateInvocation(Scope* scope,
28                             const FunctionCallNode* invocation,
29                             const std::vector<Value>& args,
30                             BlockNode* block,
31                             const FunctionCallNode* rule,
32                             Err* err) {
33   if (!EnsureNotProcessingImport(invocation, scope, err))
34     return Value();
35
36   Scope block_scope(scope);
37   if (!FillTargetBlockScope(scope, invocation,
38                             invocation->function().value().as_string(),
39                             block, args, &block_scope, err))
40     return Value();
41
42   // Run the block for the rule invocation.
43   block->ExecuteBlockInScope(&block_scope, err);
44   if (err->has_error())
45     return Value();
46
47   // Now run the rule itself with that block as the current scope.
48   rule->block()->ExecuteBlockInScope(&block_scope, err);
49   if (err->has_error())
50     return Value();
51
52   block_scope.CheckForUnusedVars(err);
53   return Value();
54 }
55
56 }  // namespace
57
58 // ----------------------------------------------------------------------------
59
60 bool EnsureNotProcessingImport(const ParseNode* node,
61                                const Scope* scope,
62                                Err* err) {
63   if (scope->IsProcessingImport()) {
64     *err = Err(node, "Not valid from an import.",
65         "Imports are for defining defaults, variables, and rules. The\n"
66         "appropriate place for this kind of thing is really in a normal\n"
67         "BUILD file.");
68     return false;
69   }
70   return true;
71 }
72
73 bool EnsureNotProcessingBuildConfig(const ParseNode* node,
74                                     const Scope* scope,
75                                     Err* err) {
76   if (scope->IsProcessingBuildConfig()) {
77     *err = Err(node, "Not valid from the build config.",
78         "You can't do this kind of thing from the build config script, "
79         "silly!\nPut it in a regular BUILD file.");
80     return false;
81   }
82   return true;
83 }
84
85 bool FillTargetBlockScope(const Scope* scope,
86                           const FunctionCallNode* function,
87                           const std::string& target_type,
88                           const BlockNode* block,
89                           const std::vector<Value>& args,
90                           Scope* block_scope,
91                           Err* err) {
92   if (!block) {
93     FillNeedsBlockError(function, err);
94     return false;
95   }
96
97   // Copy the target defaults, if any, into the scope we're going to execute
98   // the block in.
99   const Scope* default_scope = scope->GetTargetDefaults(target_type);
100   if (default_scope) {
101     if (!default_scope->NonRecursiveMergeTo(block_scope, function,
102                                             "target defaults", err))
103       return false;
104   }
105
106   // The name is the single argument to the target function.
107   if (!EnsureSingleStringArg(function, args, err))
108     return false;
109
110   // Set the target name variable to the current target, and mark it used
111   // because we don't want to issue an error if the script ignores it.
112   const base::StringPiece target_name("target_name");
113   block_scope->SetValue(target_name, Value(function, args[0].string_value()),
114                         function);
115   block_scope->MarkUsed(target_name);
116   return true;
117 }
118
119 void FillNeedsBlockError(const FunctionCallNode* function, Err* err) {
120   *err = Err(function->function(), "This function call requires a block.",
121       "The block's \"{\" must be on the same line as the function "
122       "call's \")\".");
123 }
124
125 bool EnsureSingleStringArg(const FunctionCallNode* function,
126                            const std::vector<Value>& args,
127                            Err* err) {
128   if (args.size() != 1) {
129     *err = Err(function->function(), "Incorrect arguments.",
130                "This function requires a single string argument.");
131     return false;
132   }
133   return args[0].VerifyTypeIs(Value::STRING, err);
134 }
135
136 const Label& ToolchainLabelForScope(const Scope* scope) {
137   return scope->settings()->toolchain_label();
138 }
139
140 Label MakeLabelForScope(const Scope* scope,
141                         const FunctionCallNode* function,
142                         const std::string& name) {
143   const Label& toolchain_label = ToolchainLabelForScope(scope);
144   return Label(scope->GetSourceDir(), name, toolchain_label.dir(),
145                toolchain_label.name());
146 }
147
148 namespace functions {
149
150 // assert ----------------------------------------------------------------------
151
152 const char kAssert[] = "assert";
153 const char kAssert_Help[] =
154     "assert: Assert an expression is true at generation time.\n"
155     "\n"
156     "  assert(<condition> [, <error string>])\n"
157     "\n"
158     "  If the condition is false, the build will fail with an error. If the\n"
159     "  optional second argument is provided, that string will be printed\n"
160     "  with the error message.\n"
161     "\n"
162     "Examples:\n"
163     "  assert(is_win)\n"
164     "  assert(defined(sources), \"Sources must be defined\")\n";
165
166 Value RunAssert(Scope* scope,
167                 const FunctionCallNode* function,
168                 const std::vector<Value>& args,
169                 Err* err) {
170   if (args.size() != 1 && args.size() != 2) {
171     *err = Err(function->function(), "Wrong number of arguments.",
172                "assert() takes one or two argument, "
173                "were you expecting somethig else?");
174   } else if (args[0].type() != Value::BOOLEAN) {
175     *err = Err(function->function(), "Assertion value not a bool.");
176   } else if (!args[0].boolean_value()) {
177     if (args.size() == 2) {
178       // Optional string message.
179       if (args[1].type() != Value::STRING) {
180         *err = Err(function->function(), "Assertion failed.",
181             "<<<ERROR MESSAGE IS NOT A STRING>>>");
182       } else {
183         *err = Err(function->function(), "Assertion failed.",
184             args[1].string_value());
185       }
186     } else {
187       *err = Err(function->function(), "Assertion failed.");
188     }
189
190     if (args[0].origin()) {
191       // If you do "assert(foo)" we'd ideally like to show you where foo was
192       // set, and in this case the origin of the args will tell us that.
193       // However, if you do "assert(foo && bar)" the source of the value will
194       // be the assert like, which isn't so helpful.
195       //
196       // So we try to see if the args are from the same line or not. This will
197       // break if you do "assert(\nfoo && bar)" and we may show the second line
198       // as the source, oh well. The way around this is to check to see if the
199       // origin node is inside our function call block.
200       Location origin_location = args[0].origin()->GetRange().begin();
201       if (origin_location.file() != function->function().location().file() ||
202           origin_location.line_number() !=
203               function->function().location().line_number()) {
204         err->AppendSubErr(Err(args[0].origin()->GetRange(), "",
205                               "This is where it was set."));
206       }
207     }
208   }
209   return Value();
210 }
211
212 // config ----------------------------------------------------------------------
213
214 const char kConfig[] = "config";
215 const char kConfig_Help[] =
216     "config: Defines a configuration object.\n"
217     "\n"
218     "  Configuration objects can be applied to targets and specify sets of\n"
219     "  compiler flags, includes, defines, etc. They provide a way to\n"
220     "  conveniently group sets of this configuration information.\n"
221     "\n"
222     "  A config is referenced by its label just like a target.\n"
223     "\n"
224     "  The values in a config are additive only. If you want to remove a flag\n"
225     "  you need to remove the corresponding config that sets it. The final\n"
226     "  set of flags, defines, etc. for a target is generated in this order:\n"
227     "\n"
228     "   1. The values specified directly on the target (rather than using a\n"
229     "      config.\n"
230     "   2. The configs specified in the target's \"configs\" list, in order.\n"
231     "   3. Direct dependent configs from a breadth-first traversal of the\n"
232     "      dependency tree in the order that the targets appear in \"deps\".\n"
233     "   4. All dependent configs from a breadth-first traversal of the\n"
234     "      dependency tree in the order that the targets appear in \"deps\".\n"
235     "\n"
236     "Variables valid in a config definition:\n"
237     CONFIG_VALUES_VARS_HELP
238     "\n"
239     "Variables on a target used to apply configs:\n"
240     "  all_dependent_configs, configs, direct_dependent_configs,\n"
241     "  forward_dependent_configs_from\n"
242     "\n"
243     "Example:\n"
244     "  config(\"myconfig\") {\n"
245     "    includes = [ \"include/common\" ]\n"
246     "    defines = [ \"ENABLE_DOOM_MELON\" ]\n"
247     "  }\n"
248     "\n"
249     "  executable(\"mything\") {\n"
250     "    configs = [ \":myconfig\" ]\n"
251     "  }\n";
252
253 Value RunConfig(const FunctionCallNode* function,
254                 const std::vector<Value>& args,
255                 Scope* scope,
256                 Err* err) {
257   if (!EnsureSingleStringArg(function, args, err) ||
258       !EnsureNotProcessingImport(function, scope, err))
259     return Value();
260
261   Label label(MakeLabelForScope(scope, function, args[0].string_value()));
262
263   if (g_scheduler->verbose_logging())
264     g_scheduler->Log("Generating config", label.GetUserVisibleName(true));
265
266   // Create the empty config object.
267   ItemTree* tree = &scope->settings()->build_settings()->item_tree();
268   Config* config = Config::GetConfig(scope->settings(), function->GetRange(),
269                                      label, NULL, err);
270   if (err->has_error())
271     return Value();
272
273   // Fill it.
274   const SourceDir& input_dir = scope->GetSourceDir();
275   ConfigValuesGenerator gen(&config->config_values(), scope,
276                             function->function(), input_dir, err);
277   gen.Run();
278   if (err->has_error())
279     return Value();
280
281   // Mark as complete.
282   {
283     base::AutoLock lock(tree->lock());
284     tree->MarkItemDefinedLocked(scope->settings()->build_settings(), label,
285                                 err);
286   }
287   return Value();
288 }
289
290 // declare_args ----------------------------------------------------------------
291
292 const char kDeclareArgs[] = "declare_args";
293 const char kDeclareArgs_Help[] =
294     "declare_args: Declare build arguments used by this file.\n"
295     "\n"
296     "  Introduces the given arguments into the current scope. If they are\n"
297     "  not specified on the command line or in a toolchain's arguments,\n"
298     "  the default values given in the declare_args block will be used.\n"
299     "  However, these defaults will not override command-line values.\n"
300     "\n"
301     "  See also \"gn help buildargs\" for an overview.\n"
302     "\n"
303     "Example:\n"
304     "  declare_args() {\n"
305     "    enable_teleporter = true\n"
306     "    enable_doom_melon = false\n"
307     "  }\n"
308     "\n"
309     "  If you want to override the (default disabled) Doom Melon:\n"
310     "    gn --args=\"enable_doom_melon=true enable_teleporter=false\"\n"
311     "  This also sets the teleporter, but it's already defaulted to on so\n"
312     "  it will have no effect.\n";
313
314 Value RunDeclareArgs(Scope* scope,
315                      const FunctionCallNode* function,
316                      const std::vector<Value>& args,
317                      BlockNode* block,
318                      Err* err) {
319   Scope block_scope(scope);
320   block->ExecuteBlockInScope(&block_scope, err);
321   if (err->has_error())
322     return Value();
323
324   // Pass the values from our scope into the Args object for adding to the
325   // scope with the proper values (taking into account the defaults given in
326   // the block_scope, and arguments passed into the build).
327   Scope::KeyValueMap values;
328   block_scope.GetCurrentScopeValues(&values);
329   scope->settings()->build_settings()->build_args().DeclareArgs(
330       values, scope, err);
331   return Value();
332 }
333
334 // defined ---------------------------------------------------------------------
335
336 const char kDefined[] = "defined";
337 const char kDefined_Help[] =
338     "defined: Returns whether an identifier is defined.\n"
339     "\n"
340     "  Returns true if the given argument is defined. This is most useful in\n"
341     "  templates to assert that the caller set things up properly.\n"
342     "\n"
343     "Example:\n"
344     "\n"
345     "  template(\"mytemplate\") {\n"
346     "    # To help users call this template properly...\n"
347     "    assert(defined(sources), \"Sources must be defined\")\n"
348     "\n"
349     "    # If we want to accept an optional \"values\" argument, we don't\n"
350     "    # want to dereference something that may not be defined.\n"
351     "    if (!defined(outputs)) {\n"
352     "      outputs = []\n"
353     "    }\n"
354     "  }\n";
355
356 Value RunDefined(Scope* scope,
357                  const FunctionCallNode* function,
358                  const ListNode* args_list,
359                  Err* err) {
360   const std::vector<const ParseNode*>& args_vector = args_list->contents();
361   const IdentifierNode* identifier = NULL;
362   if (args_vector.size() != 1 ||
363       !(identifier = args_vector[0]->AsIdentifier())) {
364     *err = Err(function, "Bad argument to defined().",
365         "defined() takes one argument which should be an identifier.");
366     return Value();
367   }
368
369   if (scope->GetValue(identifier->value().value()))
370     return Value(function, true);
371   return Value(function, false);
372 }
373
374 // import ----------------------------------------------------------------------
375
376 const char kImport[] = "import";
377 const char kImport_Help[] =
378     "import: Import a file into the current scope.\n"
379     "\n"
380     "  The import command loads the rules and variables resulting from\n"
381     "  executing the given file into the current scope.\n"
382     "\n"
383     "  By convention, imported files are named with a .gni extension.\n"
384     "\n"
385     "  It does not do an \"include\". The imported file is executed in a\n"
386     "  standalone environment from the caller of the import command. The\n"
387     "  results of this execution are cached for other files that import the\n"
388     "  same .gni file.\n"
389     "\n"
390     "  Note that you can not import a BUILD.gn file that's otherwise used\n"
391     "  in the build. Files must either be imported or implicitly loaded as\n"
392     "  a result of deps rules, but not both.\n"
393     "\n"
394     "  The imported file's scope will be merged with the scope at the point\n"
395     "  import was called. If there is a conflict (both the current scope and\n"
396     "  the imported file define some variable or rule with the same name)\n"
397     "  a runtime error will be thrown. Therefore, it's good practice to\n"
398     "  minimize the stuff that an imported file defines.\n"
399     "\n"
400     "Examples:\n"
401     "\n"
402     "  import(\"//build/rules/idl_compilation_rule.gni\")\n"
403     "\n"
404     "  # Looks in the current directory.\n"
405     "  import(\"my_vars.gni\")\n";
406
407 Value RunImport(Scope* scope,
408                 const FunctionCallNode* function,
409                 const std::vector<Value>& args,
410                 Err* err) {
411   if (!EnsureSingleStringArg(function, args, err) ||
412       !EnsureNotProcessingImport(function, scope, err))
413     return Value();
414
415   const SourceDir& input_dir = scope->GetSourceDir();
416   SourceFile import_file =
417       input_dir.ResolveRelativeFile(args[0].string_value());
418   scope->settings()->import_manager().DoImport(import_file, function,
419                                                scope, err);
420   return Value();
421 }
422
423 // set_sources_assignment_filter -----------------------------------------------
424
425 const char kSetSourcesAssignmentFilter[] = "set_sources_assignment_filter";
426 const char kSetSourcesAssignmentFilter_Help[] =
427     "set_sources_assignment_filter: Set a pattern to filter source files.\n"
428     "\n"
429     "  The sources assignment filter is a list of patterns that remove files\n"
430     "  from the list implicitly whenever the \"sources\" variable is\n"
431     "  assigned to. This is intended to be used to globally filter out files\n"
432     "  with platform-specific naming schemes when they don't apply, for\n"
433     "  example, you may want to filter out all \"*_win.cc\" files on non-\n"
434     "  Windows platforms.\n"
435     "\n"
436     "  See \"gn help patterns\" for specifics on patterns.\n"
437     "\n"
438     "  Typically this will be called once in the master build config script\n"
439     "  to set up the filter for the current platform. Subsequent calls will\n"
440     "  overwrite the previous values.\n"
441     "\n"
442     "  If you want to bypass the filter and add a file even if it might\n"
443     "  be filtered out, call set_sources_assignment_filter([]) to clear the\n"
444     "  list of filters. This will apply until the current scope exits\n"
445     "\n"
446     "Example:\n"
447     "  # Filter out all _win files.\n"
448     "  set_sources_assignment_filter([ \"*_win.cc\", \"*_win.h\" ])\n";
449
450 Value RunSetSourcesAssignmentFilter(Scope* scope,
451                                     const FunctionCallNode* function,
452                                     const std::vector<Value>& args,
453                                     Err* err) {
454   if (args.size() != 1) {
455     *err = Err(function, "set_sources_assignment_filter takes one argument.");
456   } else {
457     scoped_ptr<PatternList> f(new PatternList);
458     f->SetFromValue(args[0], err);
459     if (!err->has_error())
460       scope->set_sources_assignment_filter(f.Pass());
461   }
462   return Value();
463 }
464
465 // print -----------------------------------------------------------------------
466
467 const char kPrint[] = "print";
468 const char kPrint_Help[] =
469     "print(...)\n"
470     "  Prints all arguments to the console separated by spaces. A newline is\n"
471     "  automatically appended to the end.\n"
472     "\n"
473     "  This function is intended for debugging. Note that build files are run\n"
474     "  in parallel so you may get interleaved prints. A buildfile may also\n"
475     "  be executed more than once in parallel in the context of different\n"
476     "  toolchains so the prints from one file may be duplicated or\n"
477     "  interleaved with itself.\n"
478     "\n"
479     "Examples:\n"
480     "  print(\"Hello world\")\n"
481     "\n"
482     "  print(sources, deps)\n";
483
484 Value RunPrint(Scope* scope,
485                const FunctionCallNode* function,
486                const std::vector<Value>& args,
487                Err* err) {
488   for (size_t i = 0; i < args.size(); i++) {
489     if (i != 0)
490       std::cout << " ";
491     std::cout << args[i].ToString(false);
492   }
493   std::cout << std::endl;
494   return Value();
495 }
496
497 // -----------------------------------------------------------------------------
498
499 FunctionInfo::FunctionInfo()
500     : self_evaluating_args_runner(NULL),
501       generic_block_runner(NULL),
502       executed_block_runner(NULL),
503       no_block_runner(NULL),
504       help(NULL) {
505 }
506
507 FunctionInfo::FunctionInfo(SelfEvaluatingArgsFunction seaf, const char* in_help)
508     : self_evaluating_args_runner(seaf),
509       generic_block_runner(NULL),
510       executed_block_runner(NULL),
511       no_block_runner(NULL),
512       help(in_help) {
513 }
514
515 FunctionInfo::FunctionInfo(GenericBlockFunction gbf, const char* in_help)
516     : self_evaluating_args_runner(NULL),
517       generic_block_runner(gbf),
518       executed_block_runner(NULL),
519       no_block_runner(NULL),
520       help(in_help) {
521 }
522
523 FunctionInfo::FunctionInfo(ExecutedBlockFunction ebf, const char* in_help)
524     : self_evaluating_args_runner(NULL),
525       generic_block_runner(NULL),
526       executed_block_runner(ebf),
527       no_block_runner(NULL),
528       help(in_help) {
529 }
530
531 FunctionInfo::FunctionInfo(NoBlockFunction nbf, const char* in_help)
532     : self_evaluating_args_runner(NULL),
533       generic_block_runner(NULL),
534       executed_block_runner(NULL),
535       no_block_runner(nbf),
536       help(in_help) {
537 }
538
539 // Setup the function map via a static initializer. We use this because it
540 // avoids race conditions without having to do some global setup function or
541 // locking-heavy singleton checks at runtime. In practice, we always need this
542 // before we can do anything interesting, so it's OK to wait for the
543 // initializer.
544 struct FunctionInfoInitializer {
545   FunctionInfoMap map;
546
547   FunctionInfoInitializer() {
548     #define INSERT_FUNCTION(command) \
549         map[k##command] = FunctionInfo(&Run##command, k##command##_Help);
550
551     INSERT_FUNCTION(Assert)
552     INSERT_FUNCTION(Component)
553     INSERT_FUNCTION(Config)
554     INSERT_FUNCTION(Copy)
555     INSERT_FUNCTION(Custom)
556     INSERT_FUNCTION(DeclareArgs)
557     INSERT_FUNCTION(Defined)
558     INSERT_FUNCTION(ExecScript)
559     INSERT_FUNCTION(Executable)
560     INSERT_FUNCTION(Group)
561     INSERT_FUNCTION(Import)
562     INSERT_FUNCTION(Print)
563     INSERT_FUNCTION(ProcessFileTemplate)
564     INSERT_FUNCTION(ReadFile)
565     INSERT_FUNCTION(RebasePath)
566     INSERT_FUNCTION(SetDefaults)
567     INSERT_FUNCTION(SetDefaultToolchain)
568     INSERT_FUNCTION(SetSourcesAssignmentFilter)
569     INSERT_FUNCTION(SharedLibrary)
570     INSERT_FUNCTION(SourceSet)
571     INSERT_FUNCTION(StaticLibrary)
572     INSERT_FUNCTION(Template)
573     INSERT_FUNCTION(Test)
574     INSERT_FUNCTION(Tool)
575     INSERT_FUNCTION(Toolchain)
576     INSERT_FUNCTION(ToolchainArgs)
577     INSERT_FUNCTION(WriteFile)
578
579     #undef INSERT_FUNCTION
580   }
581 };
582 const FunctionInfoInitializer function_info;
583
584 const FunctionInfoMap& GetFunctions() {
585   return function_info.map;
586 }
587
588 Value RunFunction(Scope* scope,
589                   const FunctionCallNode* function,
590                   const ListNode* args_list,
591                   BlockNode* block,
592                   Err* err) {
593   const Token& name = function->function();
594
595   const FunctionInfoMap& function_map = GetFunctions();
596   FunctionInfoMap::const_iterator found_function =
597       function_map.find(name.value());
598   if (found_function == function_map.end()) {
599     // No build-in function matching this, check for a template.
600     const FunctionCallNode* rule =
601         scope->GetTemplate(function->function().value().as_string());
602     if (rule) {
603       Value args = args_list->Execute(scope, err);
604       if (err->has_error())
605         return Value();
606       return RunTemplateInvocation(scope, function, args.list_value(), block,
607                                    rule, err);
608     }
609
610     *err = Err(name, "Unknown function.");
611     return Value();
612   }
613
614   if (found_function->second.self_evaluating_args_runner) {
615     return found_function->second.self_evaluating_args_runner(
616         scope, function, args_list, err);
617   }
618
619   // All other function types take a pre-executed set of args.
620   Value args = args_list->Execute(scope, err);
621   if (err->has_error())
622     return Value();
623
624   if (found_function->second.generic_block_runner) {
625     if (!block) {
626       FillNeedsBlockError(function, err);
627       return Value();
628     }
629     return found_function->second.generic_block_runner(
630         scope, function, args.list_value(), block, err);
631   }
632
633   if (found_function->second.executed_block_runner) {
634     if (!block) {
635       FillNeedsBlockError(function, err);
636       return Value();
637     }
638
639     Scope block_scope(scope);
640     block->ExecuteBlockInScope(&block_scope, err);
641     if (err->has_error())
642       return Value();
643     return found_function->second.executed_block_runner(
644         function, args.list_value(), &block_scope, err);
645   }
646
647   // Otherwise it's a no-block function.
648   return found_function->second.no_block_runner(scope, function,
649                                                 args.list_value(), err);
650 }
651
652 }  // namespace functions