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"
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"
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,
31 const FunctionCallNode* rule,
33 if (!EnsureNotProcessingImport(invocation, scope, err))
36 Scope block_scope(scope);
37 if (!FillTargetBlockScope(scope, invocation,
38 invocation->function().value().as_string(),
39 block, args, &block_scope, err))
42 // Run the block for the rule invocation.
43 block->ExecuteBlockInScope(&block_scope, err);
47 // Now run the rule itself with that block as the current scope.
48 rule->block()->ExecuteBlockInScope(&block_scope, err);
52 block_scope.CheckForUnusedVars(err);
58 // ----------------------------------------------------------------------------
60 bool EnsureNotProcessingImport(const ParseNode* node,
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"
73 bool EnsureNotProcessingBuildConfig(const ParseNode* node,
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.");
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,
93 FillNeedsBlockError(function, err);
97 // Copy the target defaults, if any, into the scope we're going to execute
99 const Scope* default_scope = scope->GetTargetDefaults(target_type);
101 if (!default_scope->NonRecursiveMergeTo(block_scope, function,
102 "target defaults", err))
106 // The name is the single argument to the target function.
107 if (!EnsureSingleStringArg(function, args, err))
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()),
115 block_scope->MarkUsed(target_name);
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 "
125 bool EnsureSingleStringArg(const FunctionCallNode* function,
126 const std::vector<Value>& args,
128 if (args.size() != 1) {
129 *err = Err(function->function(), "Incorrect arguments.",
130 "This function requires a single string argument.");
133 return args[0].VerifyTypeIs(Value::STRING, err);
136 const Label& ToolchainLabelForScope(const Scope* scope) {
137 return scope->settings()->toolchain_label();
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());
148 namespace functions {
150 // assert ----------------------------------------------------------------------
152 const char kAssert[] = "assert";
153 const char kAssert_Help[] =
154 "assert: Assert an expression is true at generation time.\n"
156 " assert(<condition> [, <error string>])\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"
164 " assert(defined(sources), \"Sources must be defined\")\n";
166 Value RunAssert(Scope* scope,
167 const FunctionCallNode* function,
168 const std::vector<Value>& args,
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>>>");
183 *err = Err(function->function(), "Assertion failed.",
184 args[1].string_value());
187 *err = Err(function->function(), "Assertion failed.");
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.
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."));
212 // config ----------------------------------------------------------------------
214 const char kConfig[] = "config";
215 const char kConfig_Help[] =
216 "config: Defines a configuration object.\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"
222 " A config is referenced by its label just like a target.\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"
228 " 1. The values specified directly on the target (rather than using a\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"
236 "Variables valid in a config definition:\n"
237 CONFIG_VALUES_VARS_HELP
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"
244 " config(\"myconfig\") {\n"
245 " includes = [ \"include/common\" ]\n"
246 " defines = [ \"ENABLE_DOOM_MELON\" ]\n"
249 " executable(\"mything\") {\n"
250 " configs = [ \":myconfig\" ]\n"
253 Value RunConfig(const FunctionCallNode* function,
254 const std::vector<Value>& args,
257 if (!EnsureSingleStringArg(function, args, err) ||
258 !EnsureNotProcessingImport(function, scope, err))
261 Label label(MakeLabelForScope(scope, function, args[0].string_value()));
263 if (g_scheduler->verbose_logging())
264 g_scheduler->Log("Generating config", label.GetUserVisibleName(true));
266 // Create the empty config object.
267 ItemTree* tree = &scope->settings()->build_settings()->item_tree();
268 Config* config = Config::GetConfig(scope->settings(), function->GetRange(),
270 if (err->has_error())
274 const SourceDir& input_dir = scope->GetSourceDir();
275 ConfigValuesGenerator gen(&config->config_values(), scope,
276 function->function(), input_dir, err);
278 if (err->has_error())
283 base::AutoLock lock(tree->lock());
284 tree->MarkItemDefinedLocked(scope->settings()->build_settings(), label,
290 // declare_args ----------------------------------------------------------------
292 const char kDeclareArgs[] = "declare_args";
293 const char kDeclareArgs_Help[] =
294 "declare_args: Declare build arguments used by this file.\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"
301 " See also \"gn help buildargs\" for an overview.\n"
304 " declare_args() {\n"
305 " enable_teleporter = true\n"
306 " enable_doom_melon = false\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";
314 Value RunDeclareArgs(Scope* scope,
315 const FunctionCallNode* function,
316 const std::vector<Value>& args,
319 Scope block_scope(scope);
320 block->ExecuteBlockInScope(&block_scope, err);
321 if (err->has_error())
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(
334 // defined ---------------------------------------------------------------------
336 const char kDefined[] = "defined";
337 const char kDefined_Help[] =
338 "defined: Returns whether an identifier is defined.\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"
345 " template(\"mytemplate\") {\n"
346 " # To help users call this template properly...\n"
347 " assert(defined(sources), \"Sources must be defined\")\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"
356 Value RunDefined(Scope* scope,
357 const FunctionCallNode* function,
358 const ListNode* args_list,
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.");
369 if (scope->GetValue(identifier->value().value()))
370 return Value(function, true);
371 return Value(function, false);
374 // import ----------------------------------------------------------------------
376 const char kImport[] = "import";
377 const char kImport_Help[] =
378 "import: Import a file into the current scope.\n"
380 " The import command loads the rules and variables resulting from\n"
381 " executing the given file into the current scope.\n"
383 " By convention, imported files are named with a .gni extension.\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"
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"
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"
402 " import(\"//build/rules/idl_compilation_rule.gni\")\n"
404 " # Looks in the current directory.\n"
405 " import(\"my_vars.gni\")\n";
407 Value RunImport(Scope* scope,
408 const FunctionCallNode* function,
409 const std::vector<Value>& args,
411 if (!EnsureSingleStringArg(function, args, err) ||
412 !EnsureNotProcessingImport(function, scope, err))
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,
423 // set_sources_assignment_filter -----------------------------------------------
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"
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"
436 " See \"gn help patterns\" for specifics on patterns.\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"
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"
447 " # Filter out all _win files.\n"
448 " set_sources_assignment_filter([ \"*_win.cc\", \"*_win.h\" ])\n";
450 Value RunSetSourcesAssignmentFilter(Scope* scope,
451 const FunctionCallNode* function,
452 const std::vector<Value>& args,
454 if (args.size() != 1) {
455 *err = Err(function, "set_sources_assignment_filter takes one argument.");
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());
465 // print -----------------------------------------------------------------------
467 const char kPrint[] = "print";
468 const char kPrint_Help[] =
470 " Prints all arguments to the console separated by spaces. A newline is\n"
471 " automatically appended to the end.\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"
480 " print(\"Hello world\")\n"
482 " print(sources, deps)\n";
484 Value RunPrint(Scope* scope,
485 const FunctionCallNode* function,
486 const std::vector<Value>& args,
488 for (size_t i = 0; i < args.size(); i++) {
491 std::cout << args[i].ToString(false);
493 std::cout << std::endl;
497 // -----------------------------------------------------------------------------
499 FunctionInfo::FunctionInfo()
500 : self_evaluating_args_runner(NULL),
501 generic_block_runner(NULL),
502 executed_block_runner(NULL),
503 no_block_runner(NULL),
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),
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),
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),
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),
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
544 struct FunctionInfoInitializer {
547 FunctionInfoInitializer() {
548 #define INSERT_FUNCTION(command) \
549 map[k##command] = FunctionInfo(&Run##command, k##command##_Help);
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)
579 #undef INSERT_FUNCTION
582 const FunctionInfoInitializer function_info;
584 const FunctionInfoMap& GetFunctions() {
585 return function_info.map;
588 Value RunFunction(Scope* scope,
589 const FunctionCallNode* function,
590 const ListNode* args_list,
593 const Token& name = function->function();
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());
603 Value args = args_list->Execute(scope, err);
604 if (err->has_error())
606 return RunTemplateInvocation(scope, function, args.list_value(), block,
610 *err = Err(name, "Unknown function.");
614 if (found_function->second.self_evaluating_args_runner) {
615 return found_function->second.self_evaluating_args_runner(
616 scope, function, args_list, err);
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())
624 if (found_function->second.generic_block_runner) {
626 FillNeedsBlockError(function, err);
629 return found_function->second.generic_block_runner(
630 scope, function, args.list_value(), block, err);
633 if (found_function->second.executed_block_runner) {
635 FillNeedsBlockError(function, err);
639 Scope block_scope(scope);
640 block->ExecuteBlockInScope(&block_scope, err);
641 if (err->has_error())
643 return found_function->second.executed_block_runner(
644 function, args.list_value(), &block_scope, err);
647 // Otherwise it's a no-block function.
648 return found_function->second.no_block_runner(scope, function,
649 args.list_value(), err);
652 } // namespace functions