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/err.h"
6 #include "tools/gn/functions.h"
7 #include "tools/gn/parse_tree.h"
8 #include "tools/gn/scope.h"
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 "set_defaults: Set default values for a target type.\n"
18 " set_defaults(<target_type_name>) { <values...> }\n"
20 " Sets the default values for a given target type. Whenever\n"
21 " target_type_name is seen in the future, the values specified in\n"
22 " set_default's block will be copied into the current scope.\n"
24 " When the target type is used, the variable copying is very strict.\n"
25 " If a variable with that name is already in scope, the build will fail\n"
28 " set_defaults can be used for built-in target types (\"executable\",\n"
29 " \"shared_library\", etc.) and custom ones defined via the \"template\"\n"
33 " set_defaults(\"static_library\") {\n"
34 " configs = [ \"//tools/mything:settings\" ]\n"
37 " static_library(\"mylib\")\n"
38 " # The configs will be auto-populated as above. You can remove it if\n"
39 " # you don't want the default for a particular default:\n"
40 " configs -= \"//tools/mything:settings\"\n"
43 Value
RunSetDefaults(Scope
* scope
,
44 const FunctionCallNode
* function
,
45 const std::vector
<Value
>& args
,
48 if (!EnsureSingleStringArg(function
, args
, err
))
50 const std::string
& target_type(args
[0].string_value());
52 // Ensure there aren't defaults already set.
54 // It might be nice to allow multiple calls set mutate the defaults. The
55 // main case for this is where some local portions of the code want
56 // additional defaults they specify in an imported file.
58 // Currently, we don't allow imports to clobber anything, so this wouldn't
59 // work. Additionally, allowing this would be undesirable since we don't
60 // want multiple imports to each try to set defaults, since it might look
61 // like the defaults are modified by each one in sequence, while in fact
62 // imports would always clobber previous values and it would be confusing.
64 // If we wanted this, the solution would be to allow imports to overwrite
65 // target defaults set up by the default build config only. That way there
66 // are no ordering issues, but this would be more work.
67 if (scope
->GetTargetDefaults(target_type
)) {
68 *err
= Err(function
->function(),
69 "This target type defaults were already set.");
74 FillNeedsBlockError(function
, err
);
78 // Run the block for the rule invocation.
79 Scope
block_scope(scope
);
80 block
->Execute(&block_scope
, err
);
84 // Now copy the values set on the scope we made into the free-floating one
85 // (with no containing scope) used to hold the target defaults.
86 Scope
* dest
= scope
->MakeTargetDefaults(target_type
);
87 block_scope
.NonRecursiveMergeTo(dest
, Scope::MergeOptions(), function
,
88 "<SHOULD NOT FAIL>", err
);
92 } // namespace functions