• 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/err.h"
6 #include "gn/functions.h"
7 #include "gn/parse_tree.h"
8 #include "gn/scope.h"
9 
10 namespace functions {
11 
12 const char kSetDefaults[] = "set_defaults";
13 const char kSetDefaults_HelpShort[] =
14     "set_defaults: Set default values for a target type.";
15 const char kSetDefaults_Help[] =
16     R"(set_defaults: Set default values for a target type.
17 
18   set_defaults(<target_type_name>) { <values...> }
19 
20   Sets the default values for a given target type. Whenever target_type_name is
21   seen in the future, the values specified in set_default's block will be
22   copied into the current scope.
23 
24   When the target type is used, the variable copying is very strict. If a
25   variable with that name is already in scope, the build will fail with an
26   error.
27 
28   set_defaults can be used for built-in target types ("executable",
29   "shared_library", etc.) and custom ones defined via the "template" command.
30   It can be called more than once and the most recent call in any scope will
31   apply, but there is no way to refer to the previous defaults and modify them
32   (each call to set_defaults must supply a complete list of all defaults it
33   wants). If you want to share defaults, store them in a separate variable.
34 
35 Example
36 
37   set_defaults("static_library") {
38     configs = [ "//tools/mything:settings" ]
39   }
40 
41   static_library("mylib") {
42     # The configs will be auto-populated as above. You can remove it if
43     # you don't want the default for a particular default:
44     configs -= [ "//tools/mything:settings" ]
45   }
46 )";
47 
RunSetDefaults(Scope * scope,const FunctionCallNode * function,const std::vector<Value> & args,BlockNode * block,Err * err)48 Value RunSetDefaults(Scope* scope,
49                      const FunctionCallNode* function,
50                      const std::vector<Value>& args,
51                      BlockNode* block,
52                      Err* err) {
53   if (!EnsureSingleStringArg(function, args, err))
54     return Value();
55   const std::string& target_type(args[0].string_value());
56 
57   if (!block) {
58     FillNeedsBlockError(function, err);
59     return Value();
60   }
61 
62   // Run the block for the rule invocation.
63   Scope block_scope(scope);
64   block->Execute(&block_scope, err);
65   if (err->has_error())
66     return Value();
67 
68   // Now copy the values set on the scope we made into the free-floating one
69   // (with no containing scope) used to hold the target defaults.
70   Scope* dest = scope->MakeTargetDefaults(target_type);
71   block_scope.NonRecursiveMergeTo(dest, Scope::MergeOptions(), function,
72                                   "<SHOULD NOT FAIL>", err);
73   return Value();
74 }
75 
76 }  // namespace functions
77