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. |
| 189 Scope::KeyValueMap::const_iterator toolchain_override = |
| 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()); |
| 204 continue; |
| 205 } |
| 206 |
| 207 // Mark the variable used so the build script can override it in |
178 // certain cases without getting unused value errors. | 208 // certain cases without getting unused value errors. |
179 if (!scope_to_set->GetValue(arg.first)) { | 209 scope_to_set->SetValue(arg.first, arg.second, arg.second.origin()); |
180 scope_to_set->SetValue(arg.first, arg.second, arg.second.origin()); | 210 scope_to_set->MarkUsed(arg.first); |
181 scope_to_set->MarkUsed(arg.first); | |
182 } | |
183 } | 211 } |
184 | 212 |
185 return true; | 213 return true; |
186 } | 214 } |
187 | 215 |
188 bool Args::VerifyAllOverridesUsed(Err* err) const { | 216 bool Args::VerifyAllOverridesUsed(Err* err) const { |
189 base::AutoLock lock(lock_); | 217 base::AutoLock lock(lock_); |
190 Scope::KeyValueMap all_overrides(all_overrides_); | 218 Scope::KeyValueMap all_overrides(all_overrides_); |
191 for (const auto& map_pair : declared_arguments_per_toolchain_) | 219 for (const auto& map_pair : declared_arguments_per_toolchain_) |
192 RemoveDeclaredOverrides(map_pair.second, &all_overrides); | 220 RemoveDeclaredOverrides(map_pair.second, &all_overrides); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 dest->MarkUsed(variables::kCurrentCpu); | 307 dest->MarkUsed(variables::kCurrentCpu); |
280 dest->MarkUsed(variables::kTargetCpu); | 308 dest->MarkUsed(variables::kTargetCpu); |
281 dest->MarkUsed(variables::kHostOs); | 309 dest->MarkUsed(variables::kHostOs); |
282 dest->MarkUsed(variables::kCurrentOs); | 310 dest->MarkUsed(variables::kCurrentOs); |
283 dest->MarkUsed(variables::kTargetOs); | 311 dest->MarkUsed(variables::kTargetOs); |
284 } | 312 } |
285 | 313 |
286 void Args::ApplyOverridesLocked(const Scope::KeyValueMap& values, | 314 void Args::ApplyOverridesLocked(const Scope::KeyValueMap& values, |
287 Scope* scope) const { | 315 Scope* scope) const { |
288 lock_.AssertAcquired(); | 316 lock_.AssertAcquired(); |
289 for (const auto& val : values) | 317 |
| 318 const Scope::KeyValueMap& declared_arguments( |
| 319 DeclaredArgumentsForToolchainLocked(scope)); |
| 320 |
| 321 // Only set a value if it has been declared. |
| 322 for (const auto& val : values) { |
| 323 Scope::KeyValueMap::const_iterator declared = |
| 324 declared_arguments.find(val.first); |
| 325 |
| 326 if (declared == declared_arguments.end()) |
| 327 continue; |
| 328 |
290 scope->SetValue(val.first, val.second, val.second.origin()); | 329 scope->SetValue(val.first, val.second, val.second.origin()); |
| 330 } |
291 } | 331 } |
292 | 332 |
293 void Args::SaveOverrideRecordLocked(const Scope::KeyValueMap& values) const { | 333 void Args::SaveOverrideRecordLocked(const Scope::KeyValueMap& values) const { |
294 lock_.AssertAcquired(); | 334 lock_.AssertAcquired(); |
295 for (const auto& val : values) | 335 for (const auto& val : values) |
296 all_overrides_[val.first] = val.second; | 336 all_overrides_[val.first] = val.second; |
297 } | 337 } |
298 | 338 |
299 Scope::KeyValueMap& Args::DeclaredArgumentsForToolchainLocked( | 339 Scope::KeyValueMap& Args::DeclaredArgumentsForToolchainLocked( |
300 Scope* scope) const { | 340 Scope* scope) const { |
301 lock_.AssertAcquired(); | 341 lock_.AssertAcquired(); |
302 return declared_arguments_per_toolchain_[scope->settings()]; | 342 return declared_arguments_per_toolchain_[scope->settings()]; |
303 } | 343 } |
| 344 |
| 345 Scope::KeyValueMap& Args::OverridesForToolchainLocked( |
| 346 Scope* scope) const { |
| 347 lock_.AssertAcquired(); |
| 348 return toolchain_overrides_[scope->settings()]; |
| 349 } |
OLD | NEW |