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 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 Err* err) const { | 113 Err* err) const { |
114 base::AutoLock lock(lock_); | 114 base::AutoLock lock(lock_); |
115 | 115 |
116 for (Scope::KeyValueMap::const_iterator i = args.begin(); | 116 for (Scope::KeyValueMap::const_iterator i = args.begin(); |
117 i != args.end(); ++i) { | 117 i != args.end(); ++i) { |
118 // Verify that the value hasn't already been declared. We want each value | 118 // Verify that the value hasn't already been declared. We want each value |
119 // to be declared only once. | 119 // to be declared only once. |
120 // | 120 // |
121 // The tricky part is that a buildfile can be interpreted multiple times | 121 // The tricky part is that a buildfile can be interpreted multiple times |
122 // when used from different toolchains, so we can't just check that we've | 122 // when used from different toolchains, so we can't just check that we've |
123 // seen it before. Instead, we check that the location matches. We | 123 // seen it before. Instead, we check that the location matches. |
124 // additionally check that the value matches to prevent people from | |
125 // declaring defaults based on other parameters that may change. The | |
126 // rationale is that you should have exactly one default value for each | |
127 // argument that we can display in the help. | |
128 Scope::KeyValueMap::iterator previously_declared = | 124 Scope::KeyValueMap::iterator previously_declared = |
129 declared_arguments_.find(i->first); | 125 declared_arguments_.find(i->first); |
130 if (previously_declared != declared_arguments_.end()) { | 126 if (previously_declared != declared_arguments_.end()) { |
131 if (previously_declared->second.origin() != i->second.origin()) { | 127 if (previously_declared->second.origin() != i->second.origin()) { |
132 // Declaration location mismatch. | 128 // Declaration location mismatch. |
133 *err = Err(i->second.origin(), "Duplicate build argument declaration.", | 129 *err = Err(i->second.origin(), "Duplicate build argument declaration.", |
134 "Here you're declaring an argument that was already declared " | 130 "Here you're declaring an argument that was already declared " |
135 "elsewhere.\nYou can only declare each argument once in the entire " | 131 "elsewhere.\nYou can only declare each argument once in the entire " |
136 "build so there is one\ncanonical place for documentation and the " | 132 "build so there is one\ncanonical place for documentation and the " |
137 "default value. Either move this\nargument to the build config " | 133 "default value. Either move this\nargument to the build config " |
138 "file (for visibility everywhere) or to a .gni file\nthat you " | 134 "file (for visibility everywhere) or to a .gni file\nthat you " |
139 "\"import\" from the files where you need it (preferred)."); | 135 "\"import\" from the files where you need it (preferred)."); |
140 err->AppendSubErr(Err(previously_declared->second.origin(), | 136 err->AppendSubErr(Err(previously_declared->second.origin(), |
141 "Previous declaration.", | 137 "Previous declaration.", |
142 "See also \"gn help buildargs\" for more on how " | 138 "See also \"gn help buildargs\" for more on how " |
143 "build arguments work.")); | 139 "build arguments work.")); |
144 return false; | 140 return false; |
145 } else if (previously_declared->second != i->second) { | |
146 // Default value mismatch. | |
147 *err = Err(i->second.origin(), | |
148 "Non-constant default value for build argument.", | |
149 "Each build argument should have one default value so we report " | |
150 "it nicely in the\n\"gn args\" command. Please make this value " | |
151 "constant."); | |
152 return false; | |
153 } | 141 } |
154 } else { | 142 } else { |
155 declared_arguments_.insert(*i); | 143 declared_arguments_.insert(*i); |
156 } | 144 } |
157 | 145 |
158 // Only set on the current scope to the new value if it hasn't been already | 146 // Only set on the current scope to the new value if it hasn't been already |
159 // set. Mark the variable used so the build script can override it in | 147 // set. Mark the variable used so the build script can override it in |
160 // certain cases without getting unused value errors. | 148 // certain cases without getting unused value errors. |
161 if (!scope_to_set->GetValue(i->first)) { | 149 if (!scope_to_set->GetValue(i->first)) { |
162 scope_to_set->SetValue(i->first, i->second, i->second.origin()); | 150 scope_to_set->SetValue(i->first, i->second, i->second.origin()); |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 i != values.end(); ++i) | 278 i != values.end(); ++i) |
291 scope->SetValue(i->first, i->second, i->second.origin()); | 279 scope->SetValue(i->first, i->second, i->second.origin()); |
292 } | 280 } |
293 | 281 |
294 void Args::SaveOverrideRecordLocked(const Scope::KeyValueMap& values) const { | 282 void Args::SaveOverrideRecordLocked(const Scope::KeyValueMap& values) const { |
295 lock_.AssertAcquired(); | 283 lock_.AssertAcquired(); |
296 for (Scope::KeyValueMap::const_iterator i = values.begin(); | 284 for (Scope::KeyValueMap::const_iterator i = values.begin(); |
297 i != values.end(); ++i) | 285 i != values.end(); ++i) |
298 all_overrides_[i->first] = i->second; | 286 all_overrides_[i->first] = i->second; |
299 } | 287 } |
OLD | NEW |