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 "base/sys_info.h" | 7 #include "base/sys_info.h" |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 #include "tools/gn/variables.h" | 9 #include "tools/gn/variables.h" |
10 | 10 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 | 82 |
83 } // namespace | 83 } // namespace |
84 | 84 |
85 Args::Args() { | 85 Args::Args() { |
86 } | 86 } |
87 | 87 |
88 Args::Args(const Args& other) | 88 Args::Args(const Args& other) |
89 : overrides_(other.overrides_), | 89 : overrides_(other.overrides_), |
90 all_overrides_(other.all_overrides_), | 90 all_overrides_(other.all_overrides_), |
91 declared_arguments_per_toolchain_( | 91 declared_arguments_per_toolchain_( |
92 other.declared_arguments_per_toolchain_) { | 92 other.declared_arguments_per_toolchain_), |
93 toolchain_overrides_(other.toolchain_overrides_) { | |
93 } | 94 } |
94 | 95 |
95 Args::~Args() { | 96 Args::~Args() { |
96 } | 97 } |
97 | 98 |
98 void Args::AddArgOverride(const char* name, const Value& value) { | 99 void Args::AddArgOverride(const char* name, const Value& value) { |
99 base::AutoLock lock(lock_); | 100 base::AutoLock lock(lock_); |
100 | 101 |
101 overrides_[base::StringPiece(name)] = value; | 102 overrides_[base::StringPiece(name)] = value; |
102 all_overrides_[base::StringPiece(name)] = value; | 103 all_overrides_[base::StringPiece(name)] = value; |
(...skipping 21 matching lines...) Expand all Loading... | |
124 Scope::KeyValueMap Args::GetAllOverrides() const { | 125 Scope::KeyValueMap Args::GetAllOverrides() const { |
125 base::AutoLock lock(lock_); | 126 base::AutoLock lock(lock_); |
126 return all_overrides_; | 127 return all_overrides_; |
127 } | 128 } |
128 | 129 |
129 void Args::SetupRootScope(Scope* dest, | 130 void Args::SetupRootScope(Scope* dest, |
130 const Scope::KeyValueMap& toolchain_overrides) const { | 131 const Scope::KeyValueMap& toolchain_overrides) const { |
131 base::AutoLock lock(lock_); | 132 base::AutoLock lock(lock_); |
132 | 133 |
133 SetSystemVarsLocked(dest); | 134 SetSystemVarsLocked(dest); |
135 | |
136 // Apply overrides for already declared args. | |
137 // (i.e. the system vars we set above) | |
134 ApplyOverridesLocked(overrides_, dest); | 138 ApplyOverridesLocked(overrides_, dest); |
135 ApplyOverridesLocked(toolchain_overrides, dest); | 139 ApplyOverridesLocked(toolchain_overrides, dest); |
140 | |
141 OverridesForToolchainLocked(dest) = toolchain_overrides; | |
142 | |
136 SaveOverrideRecordLocked(toolchain_overrides); | 143 SaveOverrideRecordLocked(toolchain_overrides); |
137 } | 144 } |
138 | 145 |
139 bool Args::DeclareArgs(const Scope::KeyValueMap& args, | 146 bool Args::DeclareArgs(const Scope::KeyValueMap& args, |
140 Scope* scope_to_set, | 147 Scope* scope_to_set, |
141 Err* err) const { | 148 Err* err) const { |
142 base::AutoLock lock(lock_); | 149 base::AutoLock lock(lock_); |
143 | 150 |
144 Scope::KeyValueMap& declared_arguments( | 151 Scope::KeyValueMap& declared_arguments( |
145 DeclaredArgumentsForToolchainLocked(scope_to_set)); | 152 DeclaredArgumentsForToolchainLocked(scope_to_set)); |
153 | |
154 const Scope::KeyValueMap& toolchain_overrides( | |
155 OverridesForToolchainLocked(scope_to_set)); | |
156 | |
146 for (const auto& arg : args) { | 157 for (const auto& arg : args) { |
147 // Verify that the value hasn't already been declared. We want each value | 158 // Verify that the value hasn't already been declared. We want each value |
148 // to be declared only once. | 159 // to be declared only once. |
149 // | 160 // |
150 // The tricky part is that a buildfile can be interpreted multiple times | 161 // The tricky part is that a buildfile can be interpreted multiple times |
151 // when used from different toolchains, so we can't just check that we've | 162 // when used from different toolchains, so we can't just check that we've |
152 // seen it before. Instead, we check that the location matches. | 163 // seen it before. Instead, we check that the location matches. |
153 Scope::KeyValueMap::iterator previously_declared = | 164 Scope::KeyValueMap::iterator previously_declared = |
154 declared_arguments.find(arg.first); | 165 declared_arguments.find(arg.first); |
155 if (previously_declared != declared_arguments.end()) { | 166 if (previously_declared != declared_arguments.end()) { |
(...skipping 10 matching lines...) Expand all Loading... | |
166 err->AppendSubErr(Err(previously_declared->second.origin(), | 177 err->AppendSubErr(Err(previously_declared->second.origin(), |
167 "Previous declaration.", | 178 "Previous declaration.", |
168 "See also \"gn help buildargs\" for more on how " | 179 "See also \"gn help buildargs\" for more on how " |
169 "build arguments work.")); | 180 "build arguments work.")); |
170 return false; | 181 return false; |
171 } | 182 } |
172 } else { | 183 } else { |
173 declared_arguments.insert(arg); | 184 declared_arguments.insert(arg); |
174 } | 185 } |
175 | 186 |
176 // Only set on the current scope to the new value if it hasn't been already | 187 // Check whether this argument has been overridden on the toolchain level |
177 // set. Mark the variable used so the build script can override it in | 188 // and use the override instead. |
178 // certain cases without getting unused value errors. | 189 Scope::KeyValueMap::const_iterator toolchain_override = |
179 if (!scope_to_set->GetValue(arg.first)) { | 190 toolchain_overrides.find(arg.first); |
191 if (toolchain_override != toolchain_overrides.end()) { | |
192 scope_to_set->SetValue(toolchain_override->first, | |
193 toolchain_override->second, | |
194 toolchain_override->second.origin()); | |
195 continue; | |
196 } | |
197 | |
198 // Check whether this argument has been overridden and use the override | |
199 // instead. | |
200 Scope::KeyValueMap::const_iterator override = overrides_.find(arg.first); | |
201 if (override != overrides_.end()) { | |
202 scope_to_set->SetValue(override->first, override->second, | |
203 override->second.origin()); | |
brettw
2016/07/11 19:42:30
I feel like these the toolchain overrides and regu
| |
204 } else { | |
205 // Mark the variable used so the build script can override it in | |
206 // certain cases without getting unused value errors. | |
180 scope_to_set->SetValue(arg.first, arg.second, arg.second.origin()); | 207 scope_to_set->SetValue(arg.first, arg.second, arg.second.origin()); |
181 scope_to_set->MarkUsed(arg.first); | 208 scope_to_set->MarkUsed(arg.first); |
182 } | 209 } |
183 } | 210 } |
184 | 211 |
185 return true; | 212 return true; |
186 } | 213 } |
187 | 214 |
188 bool Args::VerifyAllOverridesUsed(Err* err) const { | 215 bool Args::VerifyAllOverridesUsed(Err* err) const { |
189 base::AutoLock lock(lock_); | 216 base::AutoLock lock(lock_); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
279 dest->MarkUsed(variables::kCurrentCpu); | 306 dest->MarkUsed(variables::kCurrentCpu); |
280 dest->MarkUsed(variables::kTargetCpu); | 307 dest->MarkUsed(variables::kTargetCpu); |
281 dest->MarkUsed(variables::kHostOs); | 308 dest->MarkUsed(variables::kHostOs); |
282 dest->MarkUsed(variables::kCurrentOs); | 309 dest->MarkUsed(variables::kCurrentOs); |
283 dest->MarkUsed(variables::kTargetOs); | 310 dest->MarkUsed(variables::kTargetOs); |
284 } | 311 } |
285 | 312 |
286 void Args::ApplyOverridesLocked(const Scope::KeyValueMap& values, | 313 void Args::ApplyOverridesLocked(const Scope::KeyValueMap& values, |
287 Scope* scope) const { | 314 Scope* scope) const { |
288 lock_.AssertAcquired(); | 315 lock_.AssertAcquired(); |
289 for (const auto& val : values) | 316 |
317 const Scope::KeyValueMap& declared_arguments( | |
318 DeclaredArgumentsForToolchainLocked(scope)); | |
319 | |
320 // Only set a value if it has been declared. | |
321 for (const auto& val : values) { | |
322 Scope::KeyValueMap::const_iterator declared = | |
323 declared_arguments.find(val.first); | |
324 | |
325 if (declared == declared_arguments.end()) | |
326 continue; | |
327 | |
290 scope->SetValue(val.first, val.second, val.second.origin()); | 328 scope->SetValue(val.first, val.second, val.second.origin()); |
329 } | |
291 } | 330 } |
292 | 331 |
293 void Args::SaveOverrideRecordLocked(const Scope::KeyValueMap& values) const { | 332 void Args::SaveOverrideRecordLocked(const Scope::KeyValueMap& values) const { |
294 lock_.AssertAcquired(); | 333 lock_.AssertAcquired(); |
295 for (const auto& val : values) | 334 for (const auto& val : values) |
296 all_overrides_[val.first] = val.second; | 335 all_overrides_[val.first] = val.second; |
297 } | 336 } |
298 | 337 |
299 Scope::KeyValueMap& Args::DeclaredArgumentsForToolchainLocked( | 338 Scope::KeyValueMap& Args::DeclaredArgumentsForToolchainLocked( |
300 Scope* scope) const { | 339 Scope* scope) const { |
301 lock_.AssertAcquired(); | 340 lock_.AssertAcquired(); |
302 return declared_arguments_per_toolchain_[scope->settings()]; | 341 return declared_arguments_per_toolchain_[scope->settings()]; |
303 } | 342 } |
343 | |
344 Scope::KeyValueMap& Args::OverridesForToolchainLocked( | |
345 Scope* scope) const { | |
346 lock_.AssertAcquired(); | |
347 return toolchain_overrides_[scope->settings()]; | |
348 } | |
OLD | NEW |