| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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/args.h" | 5 #include "tools/gn/args.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 #include "tools/gn/variables.h" | 8 #include "tools/gn/variables.h" |
| 9 | 9 |
| 10 #if defined(OS_WIN) | 10 #if defined(OS_WIN) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 "\n" | 43 "\n" |
| 44 "How build arguments are used\n" | 44 "How build arguments are used\n" |
| 45 "\n" | 45 "\n" |
| 46 " If you want to use an argument, you use declare_args() and specify\n" | 46 " If you want to use an argument, you use declare_args() and specify\n" |
| 47 " default values. These default values will apply if none of the steps\n" | 47 " default values. These default values will apply if none of the steps\n" |
| 48 " listed in the \"How build arguments are set\" section above apply to\n" | 48 " listed in the \"How build arguments are set\" section above apply to\n" |
| 49 " the given argument, but the defaults will not override any of these.\n" | 49 " the given argument, but the defaults will not override any of these.\n" |
| 50 "\n" | 50 "\n" |
| 51 " Often, the root build config file will declare global arguments that\n" | 51 " Often, the root build config file will declare global arguments that\n" |
| 52 " will be passed to all buildfiles. Individual build files can also\n" | 52 " will be passed to all buildfiles. Individual build files can also\n" |
| 53 " specify arguments that apply only to those files. It is also usedful\n" | 53 " specify arguments that apply only to those files. It is also useful\n" |
| 54 " to specify build args in an \"import\"-ed file if you want such\n" | 54 " to specify build args in an \"import\"-ed file if you want such\n" |
| 55 " arguments to apply to multiple buildfiles.\n"; | 55 " arguments to apply to multiple buildfiles.\n"; |
| 56 | 56 |
| 57 Args::Args() { | 57 Args::Args() { |
| 58 } | 58 } |
| 59 | 59 |
| 60 Args::Args(const Args& other) | 60 Args::Args(const Args& other) |
| 61 : overrides_(other.overrides_), | 61 : overrides_(other.overrides_), |
| 62 all_overrides_(other.all_overrides_), | 62 all_overrides_(other.all_overrides_), |
| 63 declared_arguments_(other.declared_arguments_) { | 63 declared_arguments_(other.declared_arguments_) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 // seen it before. Instead, we check that the location matches. We | 102 // seen it before. Instead, we check that the location matches. We |
| 103 // additionally check that the value matches to prevent people from | 103 // additionally check that the value matches to prevent people from |
| 104 // declaring defaults based on other parameters that may change. The | 104 // declaring defaults based on other parameters that may change. The |
| 105 // rationale is that you should have exactly one default value for each | 105 // rationale is that you should have exactly one default value for each |
| 106 // argument that we can display in the help. | 106 // argument that we can display in the help. |
| 107 Scope::KeyValueMap::iterator previously_declared = | 107 Scope::KeyValueMap::iterator previously_declared = |
| 108 declared_arguments_.find(i->first); | 108 declared_arguments_.find(i->first); |
| 109 if (previously_declared != declared_arguments_.end()) { | 109 if (previously_declared != declared_arguments_.end()) { |
| 110 if (previously_declared->second.origin() != i->second.origin()) { | 110 if (previously_declared->second.origin() != i->second.origin()) { |
| 111 // Declaration location mismatch. | 111 // Declaration location mismatch. |
| 112 *err = Err(i->second.origin(), "Duplicate build arg declaration.", | 112 *err = Err(i->second.origin(), "Duplicate build argument declaration.", |
| 113 "Here you're declaring an argument that was already declared " | 113 "Here you're declaring an argument that was already declared " |
| 114 "elsewhere.\nYou can only declare each argument once in the entire " | 114 "elsewhere.\nYou can only declare each argument once in the entire " |
| 115 "build so there is one\ncanonical place for documentation and the " | 115 "build so there is one\ncanonical place for documentation and the " |
| 116 "default value. Either move this\nargument to the build config " | 116 "default value. Either move this\nargument to the build config " |
| 117 "file (for visibility everywhere) or to a .gni file\nthat you " | 117 "file (for visibility everywhere) or to a .gni file\nthat you " |
| 118 "\"import\" from the files where you need it (preferred)."); | 118 "\"import\" from the files where you need it (preferred)."); |
| 119 err->AppendSubErr(Err(previously_declared->second.origin(), | 119 err->AppendSubErr(Err(previously_declared->second.origin(), |
| 120 "Previous declaration.", | 120 "Previous declaration.", |
| 121 "See also \"gn help buildargs\" for more on how " | 121 "See also \"gn help buildargs\" for more on how " |
| 122 "build args work.")); | 122 "build arguments work.")); |
| 123 return false; | 123 return false; |
| 124 } else if (previously_declared->second != i->second) { | 124 } else if (previously_declared->second != i->second) { |
| 125 // Default value mismatch. | 125 // Default value mismatch. |
| 126 *err = Err(i->second.origin(), | 126 *err = Err(i->second.origin(), |
| 127 "Non-constant default value for build arg.", | 127 "Non-constant default value for build argument.", |
| 128 "Each build arg should have one default value so we report it " | 128 "Each build argument should have one default value so we report " |
| 129 "nicely in the\n\"gn args\" command. Please make this value " | 129 "it nicely in the\n\"gn args\" command. Please make this value " |
| 130 "constant."); | 130 "constant."); |
| 131 return false; | 131 return false; |
| 132 } | 132 } |
| 133 } else { | 133 } else { |
| 134 declared_arguments_.insert(*i); | 134 declared_arguments_.insert(*i); |
| 135 } | 135 } |
| 136 | 136 |
| 137 // Only set on the current scope to the new value if it hasn't been already | 137 // Only set on the current scope to the new value if it hasn't been already |
| 138 // set. Mark the variable used so the build script can override it in | 138 // set. Mark the variable used so the build script can override it in |
| 139 // certain cases without getting unused value errors. | 139 // certain cases without getting unused value errors. |
| 140 if (!scope_to_set->GetValue(i->first)) { | 140 if (!scope_to_set->GetValue(i->first)) { |
| 141 scope_to_set->SetValue(i->first, i->second, i->second.origin()); | 141 scope_to_set->SetValue(i->first, i->second, i->second.origin()); |
| 142 scope_to_set->MarkUsed(i->first); | 142 scope_to_set->MarkUsed(i->first); |
| 143 } | 143 } |
| 144 } | 144 } |
| 145 | 145 |
| 146 return true; | 146 return true; |
| 147 } | 147 } |
| 148 | 148 |
| 149 bool Args::VerifyAllOverridesUsed(Err* err) const { | 149 bool Args::VerifyAllOverridesUsed(Err* err) const { |
| 150 base::AutoLock lock(lock_); | 150 base::AutoLock lock(lock_); |
| 151 | 151 |
| 152 for (Scope::KeyValueMap::const_iterator i = all_overrides_.begin(); | 152 for (Scope::KeyValueMap::const_iterator i = all_overrides_.begin(); |
| 153 i != all_overrides_.end(); ++i) { | 153 i != all_overrides_.end(); ++i) { |
| 154 if (declared_arguments_.find(i->first) == declared_arguments_.end()) { | 154 if (declared_arguments_.find(i->first) == declared_arguments_.end()) { |
| 155 *err = Err(i->second.origin(), "Build arg has no effect.", | 155 *err = Err(i->second.origin(), "Build argument has no effect.", |
| 156 "The value \"" + i->first.as_string() + "\" was set a build " | 156 "The variable \"" + i->first.as_string() + "\" was set as a build " |
| 157 "argument\nbut never appeared in a declare_args() block in any " | 157 "argument\nbut never appeared in a declare_args() block in any " |
| 158 "buildfile."); | 158 "buildfile."); |
| 159 return false; | 159 return false; |
| 160 } | 160 } |
| 161 } | 161 } |
| 162 return true; | 162 return true; |
| 163 } | 163 } |
| 164 | 164 |
| 165 void Args::SetSystemVars(Scope* dest) const { | 165 void Args::SetSystemVars(Scope* dest) const { |
| 166 // Host OS. | 166 // Host OS. |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 i != values.end(); ++i) | 240 i != values.end(); ++i) |
| 241 scope->SetValue(i->first, i->second, i->second.origin()); | 241 scope->SetValue(i->first, i->second, i->second.origin()); |
| 242 } | 242 } |
| 243 | 243 |
| 244 void Args::SaveOverrideRecord(const Scope::KeyValueMap& values) const { | 244 void Args::SaveOverrideRecord(const Scope::KeyValueMap& values) const { |
| 245 base::AutoLock lock(lock_); | 245 base::AutoLock lock(lock_); |
| 246 for (Scope::KeyValueMap::const_iterator i = values.begin(); | 246 for (Scope::KeyValueMap::const_iterator i = values.begin(); |
| 247 i != values.end(); ++i) | 247 i != values.end(); ++i) |
| 248 all_overrides_[i->first] = i->second; | 248 all_overrides_[i->first] = i->second; |
| 249 } | 249 } |
| OLD | NEW |