| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "tools/gn/err.h" | 5 #include "tools/gn/err.h" |
| 6 #include "tools/gn/functions.h" | 6 #include "tools/gn/functions.h" |
| 7 #include "tools/gn/parse_tree.h" | 7 #include "tools/gn/parse_tree.h" |
| 8 #include "tools/gn/scope.h" | 8 #include "tools/gn/scope.h" |
| 9 | 9 |
| 10 namespace functions { | 10 namespace functions { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 } | 74 } |
| 75 } | 75 } |
| 76 } | 76 } |
| 77 | 77 |
| 78 } // namespace | 78 } // namespace |
| 79 | 79 |
| 80 const char kForwardVariablesFrom[] = "forward_variables_from"; | 80 const char kForwardVariablesFrom[] = "forward_variables_from"; |
| 81 const char kForwardVariablesFrom_HelpShort[] = | 81 const char kForwardVariablesFrom_HelpShort[] = |
| 82 "forward_variables_from: Copies variables from a different scope."; | 82 "forward_variables_from: Copies variables from a different scope."; |
| 83 const char kForwardVariablesFrom_Help[] = | 83 const char kForwardVariablesFrom_Help[] = |
| 84 "forward_variables_from: Copies variables from a different scope.\n" | 84 R"(forward_variables_from: Copies variables from a different scope. |
| 85 "\n" | 85 |
| 86 " forward_variables_from(from_scope, variable_list_or_star,\n" | 86 forward_variables_from(from_scope, variable_list_or_star, |
| 87 " variable_to_not_forward_list = [])\n" | 87 variable_to_not_forward_list = []) |
| 88 "\n" | 88 |
| 89 " Copies the given variables from the given scope to the local scope\n" | 89 Copies the given variables from the given scope to the local scope if they |
| 90 " if they exist. This is normally used in the context of templates to\n" | 90 exist. This is normally used in the context of templates to use the values of |
| 91 " use the values of variables defined in the template invocation to\n" | 91 variables defined in the template invocation to a template-defined target. |
| 92 " a template-defined target.\n" | 92 |
| 93 "\n" | 93 The variables in the given variable_list will be copied if they exist in the |
| 94 " The variables in the given variable_list will be copied if they exist\n" | 94 given scope or any enclosing scope. If they do not exist, nothing will happen |
| 95 " in the given scope or any enclosing scope. If they do not exist,\n" | 95 and they be left undefined in the current scope. |
| 96 " nothing will happen and they be left undefined in the current scope.\n" | 96 |
| 97 "\n" | 97 As a special case, if the variable_list is a string with the value of "*", |
| 98 " As a special case, if the variable_list is a string with the value of\n" | 98 all variables from the given scope will be copied. "*" only copies variables |
| 99 " \"*\", all variables from the given scope will be copied. \"*\" only\n" | 99 set directly on the from_scope, not enclosing ones. Otherwise it would |
| 100 " copies variables set directly on the from_scope, not enclosing ones.\n" | 100 duplicate all global variables. |
| 101 " Otherwise it would duplicate all global variables.\n" | 101 |
| 102 "\n" | 102 When an explicit list of variables is supplied, if the variable exists in the |
| 103 " When an explicit list of variables is supplied, if the variable exists\n" | 103 current (destination) scope already, an error will be thrown. If "*" is |
| 104 " in the current (destination) scope already, an error will be thrown.\n" | 104 specified, variables in the current scope will be clobbered (the latter is |
| 105 " If \"*\" is specified, variables in the current scope will be\n" | 105 important because most targets have an implicit configs list, which means it |
| 106 " clobbered (the latter is important because most targets have an\n" | 106 wouldn't work at all if it didn't clobber). |
| 107 " implicit configs list, which means it wouldn't work at all if it\n" | 107 |
| 108 " didn't clobber).\n" | 108 The sources assignment filter (see "gn help " |
| 109 "\n" | 109 "set_sources_assignment_filter") |
| 110 " The sources assignment filter (see \"gn help " | 110 is never applied by this function. It's assumed than any desired filtering |
| 111 "set_sources_assignment_filter\")\n" | 111 was already done when sources was set on the from_scope. |
| 112 " is never applied by this function. It's assumed than any desired\n" | 112 |
| 113 " filtering was already done when sources was set on the from_scope.\n" | 113 If variables_to_not_forward_list is non-empty, then it must contains a list |
| 114 "\n" | 114 of variable names that will not be forwarded. This is mostly useful when |
| 115 " If variables_to_not_forward_list is non-empty, then it must contains\n" | 115 variable_list_or_star has a value of "*". |
| 116 " a list of variable names that will not be forwarded. This is mostly\n" | 116 |
| 117 " useful when variable_list_or_star has a value of \"*\".\n" | 117 Examples |
| 118 "\n" | 118 |
| 119 "Examples\n" | 119 # This is a common action template. It would invoke a script with some given |
| 120 "\n" | 120 # parameters, and wants to use the various types of deps and the visibility |
| 121 " # This is a common action template. It would invoke a script with\n" | 121 # from the invoker if it's defined. It also injects an additional dependency |
| 122 " # some given parameters, and wants to use the various types of deps\n" | 122 # to all targets. |
| 123 " # and the visibility from the invoker if it's defined. It also injects\n" | 123 template("my_test") { |
| 124 " # an additional dependency to all targets.\n" | 124 action(target_name) { |
| 125 " template(\"my_test\") {\n" | 125 forward_variables_from(invoker, [ "data_deps", "deps", |
| 126 " action(target_name) {\n" | 126 "public_deps", "visibility" " |
| 127 " forward_variables_from(invoker, [ \"data_deps\", \"deps\",\n" | 127 "]) |
| 128 " \"public_deps\", \"visibility\" " | 128 # Add our test code to the dependencies. |
| 129 "])\n" | 129 # "deps" may or may not be defined at this point. |
| 130 " # Add our test code to the dependencies.\n" | 130 if (defined(deps)) { |
| 131 " # \"deps\" may or may not be defined at this point.\n" | 131 deps += [ "//tools/doom_melon" ] |
| 132 " if (defined(deps)) {\n" | 132 } else { |
| 133 " deps += [ \"//tools/doom_melon\" ]\n" | 133 deps = [ "//tools/doom_melon" ] |
| 134 " } else {\n" | 134 } |
| 135 " deps = [ \"//tools/doom_melon\" ]\n" | 135 } |
| 136 " }\n" | 136 } |
| 137 " }\n" | 137 |
| 138 " }\n" | 138 # This is a template around either a target whose type depends on a global |
| 139 "\n" | 139 # variable. It forwards all values from the invoker. |
| 140 " # This is a template around either a target whose type depends on a\n" | 140 template("my_wrapper") { |
| 141 " # global variable. It forwards all values from the invoker.\n" | 141 target(my_wrapper_target_type, target_name) { |
| 142 " template(\"my_wrapper\") {\n" | 142 forward_variables_from(invoker, "*") |
| 143 " target(my_wrapper_target_type, target_name) {\n" | 143 } |
| 144 " forward_variables_from(invoker, \"*\")\n" | 144 } |
| 145 " }\n" | 145 |
| 146 " }\n" | 146 # A template that wraps another. It adds behavior based on one |
| 147 "\n" | 147 # variable, and forwards all others to the nested target. |
| 148 " # A template that wraps another. It adds behavior based on one \n" | 148 template("my_ios_test_app") { |
| 149 " # variable, and forwards all others to the nested target.\n" | 149 ios_test_app(target_name) { |
| 150 " template(\"my_ios_test_app\") {\n" | 150 forward_variables_from(invoker, "*", ["test_bundle_name"]) |
| 151 " ios_test_app(target_name) {\n" | 151 if (!defined(extra_substitutions)) { |
| 152 " forward_variables_from(invoker, \"*\", [\"test_bundle_name\"])\n" | 152 extra_substitutions = [] |
| 153 " if (!defined(extra_substitutions)) {\n" | 153 } |
| 154 " extra_substitutions = []\n" | 154 extra_substitutions += [ "BUNDLE_ID_TEST_NAME=$test_bundle_name" ] |
| 155 " }\n" | 155 } |
| 156 " extra_substitutions += [ \"BUNDLE_ID_TEST_NAME=$test_bundle_name\" " | 156 } |
| 157 "]\n" | 157 )"; |
| 158 " }\n" | |
| 159 " }\n"; | |
| 160 | 158 |
| 161 // This function takes a ListNode rather than a resolved vector of values | 159 // This function takes a ListNode rather than a resolved vector of values |
| 162 // both avoid copying the potentially-large source scope, and so the variables | 160 // both avoid copying the potentially-large source scope, and so the variables |
| 163 // in the source scope can be marked as used. | 161 // in the source scope can be marked as used. |
| 164 Value RunForwardVariablesFrom(Scope* scope, | 162 Value RunForwardVariablesFrom(Scope* scope, |
| 165 const FunctionCallNode* function, | 163 const FunctionCallNode* function, |
| 166 const ListNode* args_list, | 164 const ListNode* args_list, |
| 167 Err* err) { | 165 Err* err) { |
| 168 const auto& args_vector = args_list->contents(); | 166 const auto& args_vector = args_list->contents(); |
| 169 if (args_vector.size() != 2 && args_vector.size() != 3) { | 167 if (args_vector.size() != 2 && args_vector.size() != 3) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 } | 233 } |
| 236 } | 234 } |
| 237 | 235 |
| 238 // Not the right type of argument. | 236 // Not the right type of argument. |
| 239 *err = Err(what_value, "Not a valid list of variables to copy.", | 237 *err = Err(what_value, "Not a valid list of variables to copy.", |
| 240 "Expecting either the string \"*\" or a list of strings."); | 238 "Expecting either the string \"*\" or a list of strings."); |
| 241 return Value(); | 239 return Value(); |
| 242 } | 240 } |
| 243 | 241 |
| 244 } // namespace functions | 242 } // namespace functions |
| OLD | NEW |