• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "gn/functions.h"
6 
7 #include <stddef.h>
8 #include <cctype>
9 #include <memory>
10 #include <utility>
11 
12 #include "base/environment.h"
13 #include "base/strings/string_util.h"
14 #include "gn/build_settings.h"
15 #include "gn/config.h"
16 #include "gn/config_values_generator.h"
17 #include "gn/err.h"
18 #include "gn/input_file.h"
19 #include "gn/parse_node_value_adapter.h"
20 #include "gn/parse_tree.h"
21 #include "gn/pool.h"
22 #include "gn/scheduler.h"
23 #include "gn/scope.h"
24 #include "gn/settings.h"
25 #include "gn/template.h"
26 #include "gn/token.h"
27 #include "gn/value.h"
28 #include "gn/value_extractors.h"
29 #include "gn/variables.h"
30 
31 namespace {
32 
33 // Some functions take a {} following them, and some don't. For the ones that
34 // don't, this is used to verify that the given block node is null and will
35 // set the error accordingly if it's not. Returns true if the block is null.
VerifyNoBlockForFunctionCall(const FunctionCallNode * function,const BlockNode * block,Err * err)36 bool VerifyNoBlockForFunctionCall(const FunctionCallNode* function,
37                                   const BlockNode* block,
38                                   Err* err) {
39   if (!block)
40     return true;
41 
42   *err =
43       Err(block, "Unexpected '{'.",
44           "This function call doesn't take a {} block following it, and you\n"
45           "can't have a {} block that's not connected to something like an if\n"
46           "statement or a target declaration.");
47   err->AppendRange(function->function().range());
48   return false;
49 }
50 
51 // This key is set as a scope property on the scope of a declare_args() block,
52 // in order to prevent reading a variable defined earlier in the same call
53 // (see `gn help declare_args` for more).
54 const void* kInDeclareArgsKey = nullptr;
55 
56 }  // namespace
57 
EnsureNotReadingFromSameDeclareArgs(const ParseNode * node,const Scope * cur_scope,const Scope * val_scope,Err * err)58 bool EnsureNotReadingFromSameDeclareArgs(const ParseNode* node,
59                                          const Scope* cur_scope,
60                                          const Scope* val_scope,
61                                          Err* err) {
62   // If the value didn't come from a scope at all, we're safe.
63   if (!val_scope)
64     return true;
65 
66   const Scope* val_args_scope = nullptr;
67   val_scope->GetProperty(&kInDeclareArgsKey, &val_args_scope);
68 
69   const Scope* cur_args_scope = nullptr;
70   cur_scope->GetProperty(&kInDeclareArgsKey, &cur_args_scope);
71   if (!val_args_scope || !cur_args_scope || (val_args_scope != cur_args_scope))
72     return true;
73 
74   *err =
75       Err(node,
76           "Reading a variable defined in the same declare_args() call.\n"
77           "\n"
78           "If you need to set the value of one arg based on another, put\n"
79           "them in two separate declare_args() calls, one after the other.\n");
80   return false;
81 }
82 
EnsureNotProcessingImport(const ParseNode * node,const Scope * scope,Err * err)83 bool EnsureNotProcessingImport(const ParseNode* node,
84                                const Scope* scope,
85                                Err* err) {
86   if (scope->IsProcessingImport()) {
87     *err =
88         Err(node, "Not valid from an import.",
89             "Imports are for defining defaults, variables, and rules. The\n"
90             "appropriate place for this kind of thing is really in a normal\n"
91             "BUILD file.");
92     return false;
93   }
94   return true;
95 }
96 
EnsureNotProcessingBuildConfig(const ParseNode * node,const Scope * scope,Err * err)97 bool EnsureNotProcessingBuildConfig(const ParseNode* node,
98                                     const Scope* scope,
99                                     Err* err) {
100   if (scope->IsProcessingBuildConfig()) {
101     *err = Err(node, "Not valid from the build config.",
102                "You can't do this kind of thing from the build config script, "
103                "silly!\nPut it in a regular BUILD file.");
104     return false;
105   }
106   return true;
107 }
108 
FillTargetBlockScope(const Scope * scope,const FunctionCallNode * function,const std::string & target_type,const BlockNode * block,const std::vector<Value> & args,Scope * block_scope,Err * err)109 bool FillTargetBlockScope(const Scope* scope,
110                           const FunctionCallNode* function,
111                           const std::string& target_type,
112                           const BlockNode* block,
113                           const std::vector<Value>& args,
114                           Scope* block_scope,
115                           Err* err) {
116   if (!block) {
117     FillNeedsBlockError(function, err);
118     return false;
119   }
120 
121   // Copy the target defaults, if any, into the scope we're going to execute
122   // the block in.
123   const Scope* default_scope = scope->GetTargetDefaults(target_type);
124   if (default_scope) {
125     Scope::MergeOptions merge_options;
126     merge_options.skip_private_vars = true;
127     if (!default_scope->NonRecursiveMergeTo(block_scope, merge_options,
128                                             function, "target defaults", err))
129       return false;
130   }
131 
132   // The name is the single argument to the target function.
133   if (!EnsureSingleStringArg(function, args, err))
134     return false;
135 
136   // Set the target name variable to the current target, and mark it used
137   // because we don't want to issue an error if the script ignores it.
138   const std::string_view target_name(variables::kTargetName);
139   block_scope->SetValue(target_name, Value(function, args[0].string_value()),
140                         function);
141   block_scope->MarkUsed(target_name);
142   return true;
143 }
144 
FillNeedsBlockError(const FunctionCallNode * function,Err * err)145 void FillNeedsBlockError(const FunctionCallNode* function, Err* err) {
146   *err = Err(function->function(), "This function call requires a block.",
147              "The block's \"{\" must be on the same line as the function "
148              "call's \")\".");
149 }
150 
EnsureSingleStringArg(const FunctionCallNode * function,const std::vector<Value> & args,Err * err)151 bool EnsureSingleStringArg(const FunctionCallNode* function,
152                            const std::vector<Value>& args,
153                            Err* err) {
154   if (args.size() != 1) {
155     *err = Err(function->function(), "Incorrect arguments.",
156                "This function requires a single string argument.");
157     return false;
158   }
159   return args[0].VerifyTypeIs(Value::STRING, err);
160 }
161 
ToolchainLabelForScope(const Scope * scope)162 const Label& ToolchainLabelForScope(const Scope* scope) {
163   return scope->settings()->toolchain_label();
164 }
165 
MakeLabelForScope(const Scope * scope,const FunctionCallNode * function,const std::string & name)166 Label MakeLabelForScope(const Scope* scope,
167                         const FunctionCallNode* function,
168                         const std::string& name) {
169   const Label& toolchain_label = ToolchainLabelForScope(scope);
170   return Label(scope->GetSourceDir(), name, toolchain_label.dir(),
171                toolchain_label.name());
172 }
173 
174 // static
175 const int NonNestableBlock::kKey = 0;
176 
NonNestableBlock(Scope * scope,const FunctionCallNode * function,const char * type_description)177 NonNestableBlock::NonNestableBlock(Scope* scope,
178                                    const FunctionCallNode* function,
179                                    const char* type_description)
180     : scope_(scope),
181       function_(function),
182       type_description_(type_description),
183       key_added_(false) {}
184 
~NonNestableBlock()185 NonNestableBlock::~NonNestableBlock() {
186   if (key_added_)
187     scope_->SetProperty(&kKey, nullptr);
188 }
189 
Enter(Err * err)190 bool NonNestableBlock::Enter(Err* err) {
191   void* scope_value = scope_->GetProperty(&kKey, nullptr);
192   if (scope_value) {
193     // Existing block.
194     const NonNestableBlock* existing =
195         reinterpret_cast<const NonNestableBlock*>(scope_value);
196     *err = Err(function_, "Can't nest these things.",
197                std::string("You are trying to nest a ") + type_description_ +
198                    " inside a " + existing->type_description_ + ".");
199     err->AppendSubErr(Err(existing->function_, "The enclosing block."));
200     return false;
201   }
202 
203   scope_->SetProperty(&kKey, this);
204   key_added_ = true;
205   return true;
206 }
207 
208 namespace functions {
209 
210 // assert ----------------------------------------------------------------------
211 
212 const char kAssert[] = "assert";
213 const char kAssert_HelpShort[] =
214     "assert: Assert an expression is true at generation time.";
215 const char kAssert_Help[] =
216     R"(assert: Assert an expression is true at generation time.
217 
218   assert(<condition> [, <error string>])
219 
220   If the condition is false, the build will fail with an error. If the
221   optional second argument is provided, that string will be printed
222   with the error message.
223 
224 Examples
225 
226   assert(is_win)
227   assert(defined(sources), "Sources must be defined");
228 )";
229 
RunAssert(Scope * scope,const FunctionCallNode * function,const std::vector<Value> & args,Err * err)230 Value RunAssert(Scope* scope,
231                 const FunctionCallNode* function,
232                 const std::vector<Value>& args,
233                 Err* err) {
234   // Check usage: Assert takes 1 or 2 arguments.
235   if (args.size() != 1 && args.size() != 2) {
236     *err = Err(function->function(), "Wrong number of arguments.",
237                "assert() takes one or two arguments, "
238                "were you expecting something else?");
239     return Value();
240   }
241 
242   // Check usage: The first argument must be a boolean.
243   if (args[0].type() != Value::BOOLEAN) {
244     *err = Err(function->function(), "Assertion value not a bool.");
245     return Value();
246   }
247   bool assertion_passed = args[0].boolean_value();
248 
249   // Check usage: The second argument, if present, must be a string.
250   if (args.size() == 2 && args[1].type() != Value::STRING) {
251     *err = Err(function->function(), "Assertion message is not a string.");
252     return Value();
253   }
254 
255   // Assertion passed: there is nothing to do, so return an empty value.
256   if (assertion_passed) {
257     return Value();
258   }
259 
260   // Assertion failed; try to make a useful message and report it.
261   if (args.size() == 2) {
262     *err =
263         Err(function->function(), "Assertion failed.", args[1].string_value());
264   } else {
265     *err = Err(function->function(), "Assertion failed.");
266   }
267   if (args[0].origin()) {
268     // If you do "assert(foo)" we'd ideally like to show you where foo was
269     // set, and in this case the origin of the args will tell us that.
270     // However, if you do "assert(foo && bar)" the source of the value will
271     // be the assert like, which isn't so helpful.
272     //
273     // So we try to see if the args are from the same line or not. This will
274     // break if you do "assert(\nfoo && bar)" and we may show the second line
275     // as the source, oh well. The way around this is to check to see if the
276     // origin node is inside our function call block.
277     Location origin_location = args[0].origin()->GetRange().begin();
278     if (origin_location.file() != function->function().location().file() ||
279         origin_location.line_number() !=
280             function->function().location().line_number()) {
281       err->AppendSubErr(
282           Err(args[0].origin()->GetRange(), "", "This is where it was set."));
283     }
284   }
285   return Value();
286 }
287 
288 // config ----------------------------------------------------------------------
289 
290 const char kConfig[] = "config";
291 const char kConfig_HelpShort[] = "config: Defines a configuration object.";
292 const char kConfig_Help[] =
293     R"(config: Defines a configuration object.
294 
295   Configuration objects can be applied to targets and specify sets of compiler
296   flags, includes, defines, etc. They provide a way to conveniently group sets
297   of this configuration information.
298 
299   A config is referenced by its label just like a target.
300 
301   The values in a config are additive only. If you want to remove a flag you
302   need to remove the corresponding config that sets it. The final set of flags,
303   defines, etc. for a target is generated in this order:
304 
305    1. The values specified directly on the target (rather than using a config).
306    2. The configs specified in the target's "configs" list, in order.
307    3. Public_configs from a breadth-first traversal of the dependency tree in
308       the order that the targets appear in "deps".
309    4. All dependent configs from a breadth-first traversal of the dependency
310       tree in the order that the targets appear in "deps".
311 
312 More background
313 
314   Configs solve a problem where the build system needs to have a higher-level
315   understanding of various compiler settings. For example, some compiler flags
316   have to appear in a certain order relative to each other, some settings like
317   defines and flags logically go together, and the build system needs to
318   de-duplicate flags even though raw command-line parameters can't always be
319   operated on in that way.
320 
321   The config gives a name to a group of settings that can then be reasoned
322   about by GN. GN can know that configs with the same label are the same thing
323   so can be de-duplicated. It allows related settings to be grouped so they
324   are added or removed as a unit. And it allows targets to refer to settings
325   with conceptual names ("no_rtti", "enable_exceptions", etc.) rather than
326   having to hard-coding every compiler's flags each time they are referred to.
327 
328 Variables valid in a config definition
329 
330 )"
331 
332     CONFIG_VALUES_VARS_HELP
333 
334     R"(  Nested configs: configs
335   General: visibility
336 
337 Variables on a target used to apply configs
338 
339   all_dependent_configs, configs, public_configs
340 
341 Example
342 
343   config("myconfig") {
344     include_dirs = [ "include/common" ]
345     defines = [ "ENABLE_DOOM_MELON" ]
346   }
347 
348   executable("mything") {
349     configs = [ ":myconfig" ]
350   }
351 )";
352 
RunConfig(const FunctionCallNode * function,const std::vector<Value> & args,Scope * scope,Err * err)353 Value RunConfig(const FunctionCallNode* function,
354                 const std::vector<Value>& args,
355                 Scope* scope,
356                 Err* err) {
357   NonNestableBlock non_nestable(scope, function, "config");
358   if (!non_nestable.Enter(err))
359     return Value();
360 
361   if (!EnsureSingleStringArg(function, args, err) ||
362       !EnsureNotProcessingImport(function, scope, err))
363     return Value();
364 
365   Label label(MakeLabelForScope(scope, function, args[0].string_value()));
366 
367   if (g_scheduler->verbose_logging())
368     g_scheduler->Log("Defining config", label.GetUserVisibleName(true));
369 
370   // Create the new config.
371   std::unique_ptr<Config> config = std::make_unique<Config>(
372       scope->settings(), label, scope->build_dependency_files());
373   config->set_defined_from(function);
374   if (!Visibility::FillItemVisibility(config.get(), scope, err))
375     return Value();
376 
377   // Fill the flags and such.
378   const SourceDir& input_dir = scope->GetSourceDir();
379   ConfigValuesGenerator gen(&config->own_values(), scope, input_dir, err);
380   gen.Run();
381   if (err->has_error())
382     return Value();
383 
384   // Read sub-configs.
385   const Value* configs_value = scope->GetValue(variables::kConfigs, true);
386   if (configs_value) {
387     ExtractListOfUniqueLabels(scope->settings()->build_settings(),
388                               *configs_value, scope->GetSourceDir(),
389                               ToolchainLabelForScope(scope), &config->configs(),
390                               err);
391   }
392   if (err->has_error())
393     return Value();
394 
395   // Save the generated item.
396   Scope::ItemVector* collector = scope->GetItemCollector();
397   if (!collector) {
398     *err = Err(function, "Can't define a config in this context.");
399     return Value();
400   }
401   collector->push_back(std::move(config));
402 
403   return Value();
404 }
405 
406 // declare_args ----------------------------------------------------------------
407 
408 const char kDeclareArgs[] = "declare_args";
409 const char kDeclareArgs_HelpShort[] = "declare_args: Declare build arguments.";
410 const char kDeclareArgs_Help[] =
411     R"(declare_args: Declare build arguments.
412 
413   Introduces the given arguments into the current scope. If they are not
414   specified on the command line or in a toolchain's arguments, the default
415   values given in the declare_args block will be used. However, these defaults
416   will not override command-line values.
417 
418   See also "gn help buildargs" for an overview.
419 
420   The precise behavior of declare args is:
421 
422    1. The declare_args() block executes. Any variable defined in the enclosing
423       scope is available for reading, but any variable defined earlier in
424       the current scope is not (since the overrides haven't been applied yet).
425 
426    2. At the end of executing the block, any variables set within that scope
427       are saved, with the values specified in the block used as the "default value"
428       for that argument. Once saved, these variables are available for override
429       via args.gn.
430 
431    3. User-defined overrides are applied. Anything set in "gn args" now
432       overrides any default values. The resulting set of variables is promoted
433       to be readable from the following code in the file.
434 
435   This has some ramifications that may not be obvious:
436 
437     - You should not perform difficult work inside a declare_args block since
438       this only sets a default value that may be discarded. In particular,
439       don't use the result of exec_script() to set the default value. If you
440       want to have a script-defined default, set some default "undefined" value
441       like [], "", or -1, and after the declare_args block, call exec_script if
442       the value is unset by the user.
443 
444     - Because you cannot read the value of a variable defined in the same
445       block, if you need to make the default value of one arg depend
446       on the possibly-overridden value of another, write two separate
447       declare_args() blocks:
448 
449         declare_args() {
450           enable_foo = true
451         }
452         declare_args() {
453           # Bar defaults to same user-overridden state as foo.
454           enable_bar = enable_foo
455         }
456 
457 Example
458 
459   declare_args() {
460     enable_teleporter = true
461     enable_doom_melon = false
462   }
463 
464   If you want to override the (default disabled) Doom Melon:
465     gn --args="enable_doom_melon=true enable_teleporter=true"
466   This also sets the teleporter, but it's already defaulted to on so it will
467   have no effect.
468 )";
469 
RunDeclareArgs(Scope * scope,const FunctionCallNode * function,const std::vector<Value> & args,BlockNode * block,Err * err)470 Value RunDeclareArgs(Scope* scope,
471                      const FunctionCallNode* function,
472                      const std::vector<Value>& args,
473                      BlockNode* block,
474                      Err* err) {
475   NonNestableBlock non_nestable(scope, function, "declare_args");
476   if (!non_nestable.Enter(err))
477     return Value();
478 
479   Scope block_scope(scope);
480   block_scope.SetProperty(&kInDeclareArgsKey, &block_scope);
481   block->Execute(&block_scope, err);
482   if (err->has_error())
483     return Value();
484 
485   // Pass the values from our scope into the Args object for adding to the
486   // scope with the proper values (taking into account the defaults given in
487   // the block_scope, and arguments passed into the build).
488   Scope::KeyValueMap values;
489   block_scope.GetCurrentScopeValues(&values);
490   scope->settings()->build_settings()->build_args().DeclareArgs(values, scope,
491                                                                 err);
492   return Value();
493 }
494 
495 // defined ---------------------------------------------------------------------
496 
497 const char kDefined[] = "defined";
498 const char kDefined_HelpShort[] =
499     "defined: Returns whether an identifier is defined.";
500 const char kDefined_Help[] =
501     R"(defined: Returns whether an identifier is defined.
502 
503   Returns true if the given argument is defined. This is most useful in
504   templates to assert that the caller set things up properly.
505 
506   You can pass an identifier:
507     defined(foo)
508   which will return true or false depending on whether foo is defined in the
509   current scope.
510 
511   You can also check a named scope:
512     defined(foo.bar)
513   which will return true or false depending on whether bar is defined in the
514   named scope foo. It will throw an error if foo is not defined or is not a
515   scope.
516 
517 Example
518 
519   template("mytemplate") {
520     # To help users call this template properly...
521     assert(defined(invoker.sources), "Sources must be defined")
522 
523     # If we want to accept an optional "values" argument, we don't
524     # want to dereference something that may not be defined.
525     if (defined(invoker.values)) {
526       values = invoker.values
527     } else {
528       values = "some default value"
529     }
530   }
531 )";
532 
RunDefined(Scope * scope,const FunctionCallNode * function,const ListNode * args_list,Err * err)533 Value RunDefined(Scope* scope,
534                  const FunctionCallNode* function,
535                  const ListNode* args_list,
536                  Err* err) {
537   const auto& args_vector = args_list->contents();
538   if (args_vector.size() != 1) {
539     *err = Err(function, "Wrong number of arguments to defined().",
540                "Expecting exactly one.");
541     return Value();
542   }
543 
544   const IdentifierNode* identifier = args_vector[0]->AsIdentifier();
545   if (identifier) {
546     // Passed an identifier "defined(foo)".
547     if (scope->GetValue(identifier->value().value()))
548       return Value(function, true);
549     return Value(function, false);
550   }
551 
552   const AccessorNode* accessor = args_vector[0]->AsAccessor();
553   if (accessor) {
554     // Passed an accessor "defined(foo.bar)".
555     if (accessor->member()) {
556       // The base of the accessor must be a scope if it's defined.
557       const Value* base = scope->GetValue(accessor->base().value());
558       if (!base) {
559         *err = Err(accessor, "Undefined identifier");
560         return Value();
561       }
562       if (!base->VerifyTypeIs(Value::SCOPE, err))
563         return Value();
564 
565       // Check the member inside the scope to see if its defined.
566       if (base->scope_value()->GetValue(accessor->member()->value().value()))
567         return Value(function, true);
568       return Value(function, false);
569     }
570   }
571 
572   // Argument is invalid.
573   *err = Err(function, "Bad thing passed to defined().",
574              "It should be of the form defined(foo) or defined(foo.bar).");
575   return Value();
576 }
577 
578 // getenv ----------------------------------------------------------------------
579 
580 const char kGetEnv[] = "getenv";
581 const char kGetEnv_HelpShort[] = "getenv: Get an environment variable.";
582 const char kGetEnv_Help[] =
583     R"(getenv: Get an environment variable.
584 
585   value = getenv(env_var_name)
586 
587   Returns the value of the given environment variable. If the value is not
588   found, it will try to look up the variable with the "opposite" case (based on
589   the case of the first letter of the variable), but is otherwise
590   case-sensitive.
591 
592   If the environment variable is not found, the empty string will be returned.
593   Note: it might be nice to extend this if we had the concept of "none" in the
594   language to indicate lookup failure.
595 
596 Example
597 
598   home_dir = getenv("HOME")
599 )";
600 
RunGetEnv(Scope * scope,const FunctionCallNode * function,const std::vector<Value> & args,Err * err)601 Value RunGetEnv(Scope* scope,
602                 const FunctionCallNode* function,
603                 const std::vector<Value>& args,
604                 Err* err) {
605   if (!EnsureSingleStringArg(function, args, err))
606     return Value();
607 
608   std::unique_ptr<base::Environment> env(base::Environment::Create());
609 
610   std::string result;
611   if (!env->GetVar(args[0].string_value().c_str(), &result))
612     return Value(function, "");  // Not found, return empty string.
613   return Value(function, result);
614 }
615 
616 // import ----------------------------------------------------------------------
617 
618 const char kImport[] = "import";
619 const char kImport_HelpShort[] =
620     "import: Import a file into the current scope.";
621 const char kImport_Help[] =
622     R"(import: Import a file into the current scope.
623 
624   The import command loads the rules and variables resulting from executing the
625   given file into the current scope.
626 
627   By convention, imported files are named with a .gni extension.
628 
629   An import is different than a C++ "include". The imported file is executed in
630   a standalone environment from the caller of the import command. The results
631   of this execution are cached for other files that import the same .gni file.
632 
633   Note that you can not import a BUILD.gn file that's otherwise used in the
634   build. Files must either be imported or implicitly loaded as a result of deps
635   rules, but not both.
636 
637   The imported file's scope will be merged with the scope at the point import
638   was called. If there is a conflict (both the current scope and the imported
639   file define some variable or rule with the same name but different value), a
640   runtime error will be thrown. Therefore, it's good practice to minimize the
641   stuff that an imported file defines.
642 
643   Variables and templates beginning with an underscore '_' are considered
644   private and will not be imported. Imported files can use such variables for
645   internal computation without affecting other files.
646 
647 Examples
648 
649   import("//build/rules/idl_compilation_rule.gni")
650 
651   # Looks in the current directory.
652   import("my_vars.gni")
653 )";
654 
RunImport(Scope * scope,const FunctionCallNode * function,const std::vector<Value> & args,Err * err)655 Value RunImport(Scope* scope,
656                 const FunctionCallNode* function,
657                 const std::vector<Value>& args,
658                 Err* err) {
659   if (!EnsureSingleStringArg(function, args, err))
660     return Value();
661 
662   const SourceDir& input_dir = scope->GetSourceDir();
663   SourceFile import_file = input_dir.ResolveRelativeFile(
664       args[0], err, scope->settings()->build_settings()->root_path_utf8());
665   scope->AddBuildDependencyFile(import_file);
666   if (!err->has_error()) {
667     scope->settings()->import_manager().DoImport(import_file, function, scope,
668                                                  err);
669   }
670   return Value();
671 }
672 
673 // not_needed -----------------------------------------------------------------
674 
675 const char kNotNeeded[] = "not_needed";
676 const char kNotNeeded_HelpShort[] =
677     "not_needed: Mark variables from scope as not needed.";
678 const char kNotNeeded_Help[] =
679     R"(not_needed: Mark variables from scope as not needed.
680 
681   not_needed(variable_list_or_star, variable_to_ignore_list = [])
682   not_needed(from_scope, variable_list_or_star,
683              variable_to_ignore_list = [])
684 
685   Mark the variables in the current or given scope as not needed, which means
686   you will not get an error about unused variables for these. The
687   variable_to_ignore_list allows excluding variables from "all matches" if
688   variable_list_or_star is "*".
689 
690 Example
691 
692   not_needed("*", [ "config" ])
693   not_needed([ "data_deps", "deps" ])
694   not_needed(invoker, "*", [ "config" ])
695   not_needed(invoker, [ "data_deps", "deps" ])
696 )";
697 
698 Value RunNotNeeded(Scope* scope,
699                    const FunctionCallNode* function,
700                    const ListNode* args_list,
701                    Err* err) {
702   const auto& args_vector = args_list->contents();
703   if (args_vector.size() < 1 || args_vector.size() > 3) {
704     *err = Err(function, "Wrong number of arguments.",
705                "Expecting one, two or three arguments.");
706     return Value();
707   }
708   auto args_cur = args_vector.begin();
709 
710   Value* value = nullptr;  // Value to use, may point to result_value.
711   Value result_value;      // Storage for the "evaluate" case.
712   Value scope_value;       // Storage for an evaluated scope.
713   const IdentifierNode* identifier = (*args_cur)->AsIdentifier();
714   if (identifier) {
715     // Optimize the common case where the input scope is an identifier. This
716     // prevents a copy of a potentially large Scope object.
717     value = scope->GetMutableValue(identifier->value().value(),
718                                    Scope::SEARCH_NESTED, true);
719     if (!value) {
720       *err = Err(identifier, "Undefined identifier.");
721       return Value();
722     }
723   } else {
724     // Non-optimized case, just evaluate the argument.
725     result_value = (*args_cur)->Execute(scope, err);
726     if (err->has_error())
727       return Value();
728     value = &result_value;
729   }
730   args_cur++;
731 
732   // Extract the source scope if different from current one.
733   Scope* source = scope;
734   if (value->type() == Value::SCOPE) {
735     if (args_cur == args_vector.end()) {
736       *err = Err(
737           function, "Wrong number of arguments.",
738           "The first argument is a scope, expecting two or three arguments.");
739       return Value();
740     }
741     // Copy the scope value if it will be overridden.
742     if (value == &result_value) {
743       scope_value = Value(nullptr, value->scope_value()->MakeClosure());
744       source = scope_value.scope_value();
745     } else {
746       source = value->scope_value();
747     }
748     result_value = (*args_cur)->Execute(scope, err);
749     if (err->has_error())
750       return Value();
751     value = &result_value;
752     args_cur++;
753   } else if (args_vector.size() > 2) {
754     *err = Err(
755         function, "Wrong number of arguments.",
756         "The first argument is not a scope, expecting one or two arguments.");
757     return Value();
758   }
759 
760   // Extract the exclusion list if defined.
761   Value exclusion_value;
762   std::set<std::string> exclusion_set;
763   if (args_cur != args_vector.end()) {
764     exclusion_value = (*args_cur)->Execute(source, err);
765     if (err->has_error())
766       return Value();
767 
768     if (exclusion_value.type() != Value::LIST) {
769       *err = Err(exclusion_value, "Not a valid list of variables to exclude.",
770                  "Expecting a list of strings.");
771       return Value();
772     }
773 
774     for (const Value& cur : exclusion_value.list_value()) {
775       if (!cur.VerifyTypeIs(Value::STRING, err))
776         return Value();
777 
778       exclusion_set.insert(cur.string_value());
779     }
780   }
781 
782   if (value->type() == Value::STRING) {
783     if (value->string_value() == "*") {
784       source->MarkAllUsed(exclusion_set);
785       return Value();
786     }
787   } else if (value->type() == Value::LIST) {
788     if (exclusion_value.type() != Value::NONE) {
789       *err = Err(exclusion_value, "Not supported with a variable list.",
790                  "Exclusion list can only be used with the string \"*\".");
791       return Value();
792     }
793     for (const Value& cur : value->list_value()) {
794       if (!cur.VerifyTypeIs(Value::STRING, err))
795         return Value();
796       // We don't need the return value, we invoke scope::GetValue only to mark
797       // the value as used. Note that we cannot use Scope::MarkUsed because we
798       // want to also search in the parent scope.
799       (void)source->GetValue(cur.string_value(), true);
800     }
801     return Value();
802   }
803 
804   // Not the right type of argument.
805   *err = Err(*value, "Not a valid list of variables.",
806              "Expecting either the string \"*\" or a list of strings.");
807   return Value();
808 }
809 
810 // pool ------------------------------------------------------------------------
811 
812 const char kPool[] = "pool";
813 const char kPool_HelpShort[] = "pool: Defines a pool object.";
814 const char kPool_Help[] =
815     R"*(pool: Defines a pool object.
816 
817   Pool objects can be applied to a tool to limit the parallelism of the
818   build. This object has a single property "depth" corresponding to
819   the number of tasks that may run simultaneously.
820 
821   As the file containing the pool definition may be executed in the
822   context of more than one toolchain it is recommended to specify an
823   explicit toolchain when defining and referencing a pool.
824 
825   A pool named "console" defined in the root build file represents Ninja's
826   console pool. Targets using this pool will have access to the console's
827   stdin and stdout, and output will not be buffered. This special pool must
828   have a depth of 1. Pools not defined in the root must not be named "console".
829   The console pool can only be defined for the default toolchain.
830   Refer to the Ninja documentation on the console pool for more info.
831 
832   A pool is referenced by its label just like a target.
833 
834 Variables
835 
836   depth*
837   * = required
838 
839 Example
840 
841   if (current_toolchain == default_toolchain) {
842     pool("link_pool") {
843       depth = 1
844     }
845   }
846 
847   toolchain("toolchain") {
848     tool("link") {
849       command = "..."
850       pool = ":link_pool($default_toolchain)"
851     }
852   }
853 )*";
854 
855 const char kDepth[] = "depth";
856 
RunPool(const FunctionCallNode * function,const std::vector<Value> & args,Scope * scope,Err * err)857 Value RunPool(const FunctionCallNode* function,
858               const std::vector<Value>& args,
859               Scope* scope,
860               Err* err) {
861   NonNestableBlock non_nestable(scope, function, "pool");
862   if (!non_nestable.Enter(err))
863     return Value();
864 
865   if (!EnsureSingleStringArg(function, args, err) ||
866       !EnsureNotProcessingImport(function, scope, err))
867     return Value();
868 
869   Label label(MakeLabelForScope(scope, function, args[0].string_value()));
870 
871   if (g_scheduler->verbose_logging())
872     g_scheduler->Log("Defining pool", label.GetUserVisibleName(true));
873 
874   // Get the pool depth. It is an error to define a pool without a depth,
875   // so check first for the presence of the value.
876   const Value* depth = scope->GetValue(kDepth, true);
877   if (!depth) {
878     *err = Err(function, "Can't define a pool without depth.");
879     return Value();
880   }
881 
882   if (!depth->VerifyTypeIs(Value::INTEGER, err))
883     return Value();
884 
885   if (depth->int_value() < 0) {
886     *err = Err(*depth, "depth must be positive or 0.");
887     return Value();
888   }
889 
890   // Create the new pool.
891   std::unique_ptr<Pool> pool = std::make_unique<Pool>(
892       scope->settings(), label, scope->build_dependency_files());
893 
894   if (label.name() == "console") {
895     const Settings* settings = scope->settings();
896     if (!settings->is_default()) {
897       *err = Err(
898           function,
899           "\"console\" pool must be defined only in the default toolchain.");
900       return Value();
901     }
902     if (label.dir() != settings->build_settings()->root_target_label().dir()) {
903       *err = Err(function, "\"console\" pool must be defined in the root //.");
904       return Value();
905     }
906     if (depth->int_value() != 1) {
907       *err = Err(*depth, "\"console\" pool must have depth 1.");
908       return Value();
909     }
910   }
911   pool->set_depth(depth->int_value());
912 
913   // Save the generated item.
914   Scope::ItemVector* collector = scope->GetItemCollector();
915   if (!collector) {
916     *err = Err(function, "Can't define a pool in this context.");
917     return Value();
918   }
919   collector->push_back(std::move(pool));
920 
921   return Value();
922 }
923 
924 // print -----------------------------------------------------------------------
925 
926 const char kPrint[] = "print";
927 const char kPrint_HelpShort[] = "print: Prints to the console.";
928 const char kPrint_Help[] =
929     R"(print: Prints to the console.
930 
931   Prints all arguments to the console separated by spaces. A newline is
932   automatically appended to the end.
933 
934   This function is intended for debugging. Note that build files are run in
935   parallel so you may get interleaved prints. A buildfile may also be executed
936   more than once in parallel in the context of different toolchains so the
937   prints from one file may be duplicated or
938   interleaved with itself.
939 
940 Examples
941 
942   print("Hello world")
943 
944   print(sources, deps)
945 )";
946 
RunPrint(Scope * scope,const FunctionCallNode * function,const std::vector<Value> & args,Err * err)947 Value RunPrint(Scope* scope,
948                const FunctionCallNode* function,
949                const std::vector<Value>& args,
950                Err* err) {
951   std::string output;
952   for (size_t i = 0; i < args.size(); i++) {
953     if (i != 0)
954       output.push_back(' ');
955     output.append(args[i].ToString(false));
956   }
957   output.push_back('\n');
958 
959   const BuildSettings::PrintCallback& cb =
960       scope->settings()->build_settings()->print_callback();
961   if (cb) {
962     cb(output);
963   } else {
964     printf("%s", output.c_str());
965     fflush(stdout);
966   }
967 
968   return Value();
969 }
970 
971 // split_list ------------------------------------------------------------------
972 
973 const char kSplitList[] = "split_list";
974 const char kSplitList_HelpShort[] =
975     "split_list: Splits a list into N different sub-lists.";
976 const char kSplitList_Help[] =
977     R"(split_list: Splits a list into N different sub-lists.
978 
979   result = split_list(input, n)
980 
981   Given a list and a number N, splits the list into N sub-lists of
982   approximately equal size. The return value is a list of the sub-lists. The
983   result will always be a list of size N. If N is greater than the number of
984   elements in the input, it will be padded with empty lists.
985 
986   The expected use is to divide source files into smaller uniform chunks.
987 
988 Example
989 
990   The code:
991     mylist = [1, 2, 3, 4, 5, 6]
992     print(split_list(mylist, 3))
993 
994   Will print:
995     [[1, 2], [3, 4], [5, 6]
996 )";
RunSplitList(Scope * scope,const FunctionCallNode * function,const ListNode * args_list,Err * err)997 Value RunSplitList(Scope* scope,
998                    const FunctionCallNode* function,
999                    const ListNode* args_list,
1000                    Err* err) {
1001   const auto& args_vector = args_list->contents();
1002   if (args_vector.size() != 2) {
1003     *err = Err(function, "Wrong number of arguments to split_list().",
1004                "Expecting exactly two.");
1005     return Value();
1006   }
1007 
1008   ParseNodeValueAdapter list_adapter;
1009   if (!list_adapter.InitForType(scope, args_vector[0].get(), Value::LIST, err))
1010     return Value();
1011   const std::vector<Value>& input = list_adapter.get().list_value();
1012 
1013   ParseNodeValueAdapter count_adapter;
1014   if (!count_adapter.InitForType(scope, args_vector[1].get(), Value::INTEGER,
1015                                  err))
1016     return Value();
1017   int64_t count = count_adapter.get().int_value();
1018   if (count <= 0) {
1019     *err = Err(function, "Requested result size is not positive.");
1020     return Value();
1021   }
1022 
1023   Value result(function, Value::LIST);
1024   result.list_value().resize(count);
1025 
1026   // Every result list gets at least this many items in it.
1027   int64_t min_items_per_list = static_cast<int64_t>(input.size()) / count;
1028 
1029   // This many result lists get an extra item which is the remainder from above.
1030   int64_t extra_items = static_cast<int64_t>(input.size()) % count;
1031 
1032   // Allocate all lists that have a remainder assigned to them (max items).
1033   int64_t max_items_per_list = min_items_per_list + 1;
1034   auto last_item_end = input.begin();
1035   for (int64_t i = 0; i < extra_items; i++) {
1036     result.list_value()[i] = Value(function, Value::LIST);
1037 
1038     auto begin_add = last_item_end;
1039     last_item_end += max_items_per_list;
1040     result.list_value()[i].list_value().assign(begin_add, last_item_end);
1041   }
1042 
1043   // Allocate all smaller items that don't have a remainder.
1044   for (int64_t i = extra_items; i < count; i++) {
1045     result.list_value()[i] = Value(function, Value::LIST);
1046 
1047     auto begin_add = last_item_end;
1048     last_item_end += min_items_per_list;
1049     result.list_value()[i].list_value().assign(begin_add, last_item_end);
1050   }
1051 
1052   return result;
1053 }
1054 
1055 // string_join -----------------------------------------------------------------
1056 
1057 const char kStringJoin[] = "string_join";
1058 const char kStringJoin_HelpShort[] =
1059     "string_join: Concatenates a list of strings with a separator.";
1060 const char kStringJoin_Help[] =
1061     R"(string_join: Concatenates a list of strings with a separator.
1062 
1063   result = string_join(separator, strings)
1064 
1065   Concatenate a list of strings with intervening occurrences of separator.
1066 
1067 Examples
1068 
1069     string_join("", ["a", "b", "c"])    --> "abc"
1070     string_join("|", ["a", "b", "c"])   --> "a|b|c"
1071     string_join(", ", ["a", "b", "c"])  --> "a, b, c"
1072     string_join("s", ["", ""])          --> "s"
1073 )";
1074 
RunStringJoin(Scope * scope,const FunctionCallNode * function,const std::vector<Value> & args,Err * err)1075 Value RunStringJoin(Scope* scope,
1076                     const FunctionCallNode* function,
1077                     const std::vector<Value>& args,
1078                     Err* err) {
1079   // Check usage: Number of arguments.
1080   if (args.size() != 2) {
1081     *err = Err(function, "Wrong number of arguments to string_join().",
1082                "Expecting exactly two. usage: string_join(separator, strings)");
1083     return Value();
1084   }
1085 
1086   // Check usage: separator is a string.
1087   if (!args[0].VerifyTypeIs(Value::STRING, err)) {
1088     *err = Err(function,
1089                "separator in string_join(separator, strings) is not "
1090                "a string",
1091                "Expecting separator argument to be a string.");
1092     return Value();
1093   }
1094   const std::string separator = args[0].string_value();
1095 
1096   // Check usage: strings is a list.
1097   if (!args[1].VerifyTypeIs(Value::LIST, err)) {
1098     *err = Err(function,
1099                "strings in string_join(separator, strings) "
1100                "is not a list",
1101                "Expecting strings argument to be a list.");
1102     return Value();
1103   }
1104   const std::vector<Value> strings = args[1].list_value();
1105 
1106   // Arguments looks good; do the join.
1107   std::stringstream stream;
1108   for (size_t i = 0; i < strings.size(); ++i) {
1109     if (!strings[i].VerifyTypeIs(Value::STRING, err)) {
1110       return Value();
1111     }
1112     if (i != 0) {
1113       stream << separator;
1114     }
1115     stream << strings[i].string_value();
1116   }
1117   return Value(function, stream.str());
1118 }
1119 
1120 // string_replace --------------------------------------------------------------
1121 
1122 const char kStringReplace[] = "string_replace";
1123 const char kStringReplace_HelpShort[] =
1124     "string_replace: Replaces substring in the given string.";
1125 const char kStringReplace_Help[] =
1126     R"(string_replace: Replaces substring in the given string.
1127 
1128   result = string_replace(str, old, new[, max])
1129 
1130   Returns a copy of the string str in which the occurrences of old have been
1131   replaced with new, optionally restricting the number of replacements. The
1132   replacement is performed sequentially, so if new contains old, it won't be
1133   replaced.
1134 
1135 Example
1136 
1137   The code:
1138     mystr = "Hello, world!"
1139     print(string_replace(mystr, "world", "GN"))
1140 
1141   Will print:
1142     Hello, GN!
1143 )";
1144 
RunStringReplace(Scope * scope,const FunctionCallNode * function,const std::vector<Value> & args,Err * err)1145 Value RunStringReplace(Scope* scope,
1146                        const FunctionCallNode* function,
1147                        const std::vector<Value>& args,
1148                        Err* err) {
1149   if (args.size() < 3 || args.size() > 4) {
1150     *err = Err(function, "Wrong number of arguments to string_replace().");
1151     return Value();
1152   }
1153 
1154   if (!args[0].VerifyTypeIs(Value::STRING, err))
1155     return Value();
1156   const std::string str = args[0].string_value();
1157 
1158   if (!args[1].VerifyTypeIs(Value::STRING, err))
1159     return Value();
1160   const std::string& old = args[1].string_value();
1161 
1162   if (!args[2].VerifyTypeIs(Value::STRING, err))
1163     return Value();
1164   const std::string& new_ = args[2].string_value();
1165 
1166   int64_t max = INT64_MAX;
1167   if (args.size() > 3) {
1168     if (!args[3].VerifyTypeIs(Value::INTEGER, err))
1169       return Value();
1170     max = args[3].int_value();
1171     if (max <= 0) {
1172       *err = Err(function, "Requested number of replacements is not positive.");
1173       return Value();
1174     }
1175   }
1176 
1177   int64_t n = 0;
1178   std::string val(str);
1179   size_t start_pos = 0;
1180   while ((start_pos = val.find(old, start_pos)) != std::string::npos) {
1181     val.replace(start_pos, old.length(), new_);
1182     start_pos += new_.length();
1183     if (++n >= max)
1184       break;
1185   }
1186   return Value(function, std::move(val));
1187 }
1188 
1189 // string_split ----------------------------------------------------------------
1190 
1191 const char kStringSplit[] = "string_split";
1192 const char kStringSplit_HelpShort[] =
1193     "string_split: Split string into a list of strings.";
1194 const char kStringSplit_Help[] =
1195     R"(string_split: Split string into a list of strings.
1196 
1197   result = string_split(str[, sep])
1198 
1199   Split string into all substrings separated by separator and returns a list
1200   of the substrings between those separators.
1201 
1202   If the separator argument is omitted, the split is by any whitespace, and
1203   any leading/trailing whitespace is ignored; similar to Python's str.split().
1204 
1205 Examples without a separator (split on whitespace):
1206 
1207   string_split("")          --> []
1208   string_split("a")         --> ["a"]
1209   string_split(" aa  bb")   --> ["aa", "bb"]
1210 
1211 Examples with a separator (split on separators):
1212 
1213   string_split("", "|")           --> [""]
1214   string_split("  a b  ", " ")    --> ["", "", "a", "b", "", ""]
1215   string_split("aa+-bb+-c", "+-") --> ["aa", "bb", "c"]
1216 )";
1217 
RunStringSplit(Scope * scope,const FunctionCallNode * function,const std::vector<Value> & args,Err * err)1218 Value RunStringSplit(Scope* scope,
1219                      const FunctionCallNode* function,
1220                      const std::vector<Value>& args,
1221                      Err* err) {
1222   // Check usage: argument count.
1223   if (args.size() != 1 && args.size() != 2) {
1224     *err = Err(function, "Wrong number of arguments to string_split().",
1225                "Usage: string_split(str[, sep])");
1226     return Value();
1227   }
1228 
1229   // Check usage: str is a string.
1230   if (!args[0].VerifyTypeIs(Value::STRING, err)) {
1231     return Value();
1232   }
1233   const std::string str = args[0].string_value();
1234 
1235   // Check usage: separator is a non-empty string.
1236   std::string separator;
1237   if (args.size() == 2) {
1238     if (!args[1].VerifyTypeIs(Value::STRING, err)) {
1239       return Value();
1240     }
1241     separator = args[1].string_value();
1242     if (separator.empty()) {
1243       *err = Err(function,
1244                  "Separator argument to string_split() "
1245                  "cannot be empty string",
1246                  "Usage: string_split(str[, sep])");
1247       return Value();
1248     }
1249   }
1250 
1251   // Split the string into a std::vector.
1252   std::vector<std::string> strings;
1253   if (!separator.empty()) {
1254     // Case: Explicit separator argument.
1255     // Note: split_string("", "x") --> [""] like Python.
1256     size_t pos = 0;
1257     size_t next_pos = 0;
1258     while ((next_pos = str.find(separator, pos)) != std::string::npos) {
1259       strings.push_back(str.substr(pos, next_pos - pos));
1260       pos = next_pos + separator.length();
1261     }
1262     strings.push_back(str.substr(pos, std::string::npos));
1263   } else {
1264     // Case: Split on any whitespace and strip ends.
1265     // Note: split_string("") --> [] like Python.
1266     std::string::const_iterator pos = str.cbegin();
1267     while (pos != str.end()) {
1268       // Advance past spaces. After this, pos is pointing to non-whitespace.
1269       pos = find_if(pos, str.end(), [](char x) { return !std::isspace(x); });
1270       if (pos == str.end()) {
1271         // Tail is all whitespace, so we're done.
1272         break;
1273       }
1274       // Advance past non-whitespace to get next chunk.
1275       std::string::const_iterator next_whitespace_position =
1276           find_if(pos, str.end(), [](char x) { return std::isspace(x); });
1277       strings.push_back(std::string(pos, next_whitespace_position));
1278       pos = next_whitespace_position;
1279     }
1280   }
1281 
1282   // Convert vector of std::strings to list of GN strings.
1283   Value result(function, Value::LIST);
1284   result.list_value().resize(strings.size());
1285   for (size_t i = 0; i < strings.size(); ++i) {
1286     result.list_value()[i] = Value(function, strings[i]);
1287   }
1288   return result;
1289 }
1290 
1291 // -----------------------------------------------------------------------------
1292 
FunctionInfo()1293 FunctionInfo::FunctionInfo()
1294     : self_evaluating_args_runner(nullptr),
1295       generic_block_runner(nullptr),
1296       executed_block_runner(nullptr),
1297       no_block_runner(nullptr),
1298       help_short(nullptr),
1299       help(nullptr),
1300       is_target(false) {}
1301 
FunctionInfo(SelfEvaluatingArgsFunction seaf,const char * in_help_short,const char * in_help,bool in_is_target)1302 FunctionInfo::FunctionInfo(SelfEvaluatingArgsFunction seaf,
1303                            const char* in_help_short,
1304                            const char* in_help,
1305                            bool in_is_target)
1306     : self_evaluating_args_runner(seaf),
1307       generic_block_runner(nullptr),
1308       executed_block_runner(nullptr),
1309       no_block_runner(nullptr),
1310       help_short(in_help_short),
1311       help(in_help),
1312       is_target(in_is_target) {}
1313 
FunctionInfo(GenericBlockFunction gbf,const char * in_help_short,const char * in_help,bool in_is_target)1314 FunctionInfo::FunctionInfo(GenericBlockFunction gbf,
1315                            const char* in_help_short,
1316                            const char* in_help,
1317                            bool in_is_target)
1318     : self_evaluating_args_runner(nullptr),
1319       generic_block_runner(gbf),
1320       executed_block_runner(nullptr),
1321       no_block_runner(nullptr),
1322       help_short(in_help_short),
1323       help(in_help),
1324       is_target(in_is_target) {}
1325 
FunctionInfo(ExecutedBlockFunction ebf,const char * in_help_short,const char * in_help,bool in_is_target)1326 FunctionInfo::FunctionInfo(ExecutedBlockFunction ebf,
1327                            const char* in_help_short,
1328                            const char* in_help,
1329                            bool in_is_target)
1330     : self_evaluating_args_runner(nullptr),
1331       generic_block_runner(nullptr),
1332       executed_block_runner(ebf),
1333       no_block_runner(nullptr),
1334       help_short(in_help_short),
1335       help(in_help),
1336       is_target(in_is_target) {}
1337 
FunctionInfo(NoBlockFunction nbf,const char * in_help_short,const char * in_help,bool in_is_target)1338 FunctionInfo::FunctionInfo(NoBlockFunction nbf,
1339                            const char* in_help_short,
1340                            const char* in_help,
1341                            bool in_is_target)
1342     : self_evaluating_args_runner(nullptr),
1343       generic_block_runner(nullptr),
1344       executed_block_runner(nullptr),
1345       no_block_runner(nbf),
1346       help_short(in_help_short),
1347       help(in_help),
1348       is_target(in_is_target) {}
1349 
1350 // Setup the function map via a static initializer. We use this because it
1351 // avoids race conditions without having to do some global setup function or
1352 // locking-heavy singleton checks at runtime. In practice, we always need this
1353 // before we can do anything interesting, so it's OK to wait for the
1354 // initializer.
1355 struct FunctionInfoInitializer {
1356   FunctionInfoMap map;
1357 
FunctionInfoInitializerfunctions::FunctionInfoInitializer1358   FunctionInfoInitializer() {
1359 #define INSERT_FUNCTION(command, is_target)                             \
1360   map[k##command] = FunctionInfo(&Run##command, k##command##_HelpShort, \
1361                                  k##command##_Help, is_target);
1362 
1363     INSERT_FUNCTION(Action, true)
1364     INSERT_FUNCTION(ActionForEach, true)
1365     INSERT_FUNCTION(BundleData, true)
1366     INSERT_FUNCTION(CreateBundle, true)
1367     INSERT_FUNCTION(Copy, true)
1368     INSERT_FUNCTION(Executable, true)
1369     INSERT_FUNCTION(Group, true)
1370     INSERT_FUNCTION(LoadableModule, true)
1371     INSERT_FUNCTION(SharedLibrary, true)
1372     INSERT_FUNCTION(SourceSet, true)
1373     INSERT_FUNCTION(StaticLibrary, true)
1374     INSERT_FUNCTION(Target, true)
1375     INSERT_FUNCTION(GeneratedFile, true)
1376     INSERT_FUNCTION(RustLibrary, true)
1377     INSERT_FUNCTION(RustProcMacro, true)
1378 
1379     INSERT_FUNCTION(Assert, false)
1380     INSERT_FUNCTION(Config, false)
1381     INSERT_FUNCTION(DeclareArgs, false)
1382     INSERT_FUNCTION(Defined, false)
1383     INSERT_FUNCTION(ExecScript, false)
1384     INSERT_FUNCTION(FilterExclude, false)
1385     INSERT_FUNCTION(FilterInclude, false)
1386     INSERT_FUNCTION(ForEach, false)
1387     INSERT_FUNCTION(ForwardVariablesFrom, false)
1388     INSERT_FUNCTION(GetEnv, false)
1389     INSERT_FUNCTION(GetLabelInfo, false)
1390     INSERT_FUNCTION(GetPathInfo, false)
1391     INSERT_FUNCTION(GetTargetOutputs, false)
1392     INSERT_FUNCTION(Import, false)
1393     INSERT_FUNCTION(NotNeeded, false)
1394     INSERT_FUNCTION(Pool, false)
1395     INSERT_FUNCTION(Print, false)
1396     INSERT_FUNCTION(ProcessFileTemplate, false)
1397     INSERT_FUNCTION(ReadFile, false)
1398     INSERT_FUNCTION(RebasePath, false)
1399     INSERT_FUNCTION(SetDefaults, false)
1400     INSERT_FUNCTION(SetDefaultToolchain, false)
1401     INSERT_FUNCTION(SplitList, false)
1402     INSERT_FUNCTION(StringJoin, false)
1403     INSERT_FUNCTION(StringReplace, false)
1404     INSERT_FUNCTION(StringSplit, false)
1405     INSERT_FUNCTION(Template, false)
1406     INSERT_FUNCTION(Tool, false)
1407     INSERT_FUNCTION(Toolchain, false)
1408     INSERT_FUNCTION(WriteFile, false)
1409 
1410 #undef INSERT_FUNCTION
1411   }
1412 };
1413 const FunctionInfoInitializer function_info;
1414 
GetFunctions()1415 const FunctionInfoMap& GetFunctions() {
1416   return function_info.map;
1417 }
1418 
RunFunction(Scope * scope,const FunctionCallNode * function,const ListNode * args_list,BlockNode * block,Err * err)1419 Value RunFunction(Scope* scope,
1420                   const FunctionCallNode* function,
1421                   const ListNode* args_list,
1422                   BlockNode* block,
1423                   Err* err) {
1424   const Token& name = function->function();
1425 
1426   std::string template_name(function->function().value());
1427   const Template* templ = scope->GetTemplate(template_name);
1428   if (templ) {
1429     Value args = args_list->Execute(scope, err);
1430     if (err->has_error())
1431       return Value();
1432     return templ->Invoke(scope, function, template_name, args.list_value(),
1433                          block, err);
1434   }
1435 
1436   // No template matching this, check for a built-in function.
1437   const FunctionInfoMap& function_map = GetFunctions();
1438   FunctionInfoMap::const_iterator found_function =
1439       function_map.find(name.value());
1440   if (found_function == function_map.end()) {
1441     *err = Err(name, "Unknown function.");
1442     return Value();
1443   }
1444 
1445   if (found_function->second.self_evaluating_args_runner) {
1446     // Self evaluating args functions are special weird built-ins like foreach.
1447     // Rather than force them all to check that they have a block or no block
1448     // and risk bugs for new additions, check a whitelist here.
1449     if (found_function->second.self_evaluating_args_runner != &RunForEach) {
1450       if (!VerifyNoBlockForFunctionCall(function, block, err))
1451         return Value();
1452     }
1453     return found_function->second.self_evaluating_args_runner(scope, function,
1454                                                               args_list, err);
1455   }
1456 
1457   // All other function types take a pre-executed set of args.
1458   Value args = args_list->Execute(scope, err);
1459   if (err->has_error())
1460     return Value();
1461 
1462   if (found_function->second.generic_block_runner) {
1463     if (!block) {
1464       FillNeedsBlockError(function, err);
1465       return Value();
1466     }
1467     return found_function->second.generic_block_runner(
1468         scope, function, args.list_value(), block, err);
1469   }
1470 
1471   if (found_function->second.executed_block_runner) {
1472     if (!block) {
1473       FillNeedsBlockError(function, err);
1474       return Value();
1475     }
1476 
1477     Scope block_scope(scope);
1478     block->Execute(&block_scope, err);
1479     if (err->has_error())
1480       return Value();
1481 
1482     Value result = found_function->second.executed_block_runner(
1483         function, args.list_value(), &block_scope, err);
1484     if (err->has_error())
1485       return Value();
1486 
1487     if (!block_scope.CheckForUnusedVars(err))
1488       return Value();
1489     return result;
1490   }
1491 
1492   // Otherwise it's a no-block function.
1493   if (!VerifyNoBlockForFunctionCall(function, block, err))
1494     return Value();
1495   return found_function->second.no_block_runner(scope, function,
1496                                                 args.list_value(), err);
1497 }
1498 
1499 }  // namespace functions
1500