Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: tools/gn/analyzer.cc

Issue 2350963006: Fix an issue in `gn analyze` when building all. (Closed)
Patch Set: initial patch for review Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | tools/gn/analyzer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/analyzer.h" 5 #include "tools/gn/analyzer.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 #include <set> 9 #include <set>
10 #include <vector> 10 #include <vector>
(...skipping 25 matching lines...) Expand all
36 std::vector<Label> test_vec; 36 std::vector<Label> test_vec;
37 bool compile_included_all; 37 bool compile_included_all;
38 SourceFileSet source_files; 38 SourceFileSet source_files;
39 LabelSet compile_labels; 39 LabelSet compile_labels;
40 LabelSet test_labels; 40 LabelSet test_labels;
41 }; 41 };
42 42
43 struct Outputs { 43 struct Outputs {
44 std::string status; 44 std::string status;
45 std::string error; 45 std::string error;
46 bool compile_includes_all;
46 LabelSet compile_labels; 47 LabelSet compile_labels;
47 LabelSet test_labels; 48 LabelSet test_labels;
48 LabelSet invalid_labels; 49 LabelSet invalid_labels;
49 }; 50 };
50 51
51 LabelSet LabelsFor(const TargetSet& targets) { 52 LabelSet LabelsFor(const TargetSet& targets) {
52 LabelSet labels; 53 LabelSet labels;
53 for (const auto& target : targets) 54 for (const auto& target : targets)
54 labels.insert(target->label()); 55 labels.insert(target->label());
55 return labels; 56 return labels;
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 const Label& default_toolchain, Err *err) { 197 const Label& default_toolchain, Err *err) {
197 std::string output; 198 std::string output;
198 auto value = base::MakeUnique<base::DictionaryValue>(); 199 auto value = base::MakeUnique<base::DictionaryValue>();
199 200
200 if (outputs.error.size()) { 201 if (outputs.error.size()) {
201 WriteString(*value, "error", outputs.error); 202 WriteString(*value, "error", outputs.error);
202 WriteLabels(default_toolchain, *value, "invalid_targets", 203 WriteLabels(default_toolchain, *value, "invalid_targets",
203 outputs.invalid_labels); 204 outputs.invalid_labels);
204 } else { 205 } else {
205 WriteString(*value, "status", outputs.status); 206 WriteString(*value, "status", outputs.status);
206 WriteLabels(default_toolchain, *value, "compile_targets", 207 if (outputs.compile_includes_all) {
207 outputs.compile_labels); 208 auto compile_targets = base::WrapUnique(new base::ListValue());
Nico 2016/09/20 19:20:44 For better or worse, I think we have decided to pr
Dirk Pranke 2016/09/20 19:24:17 I think we mostly use WrapUnique() elsewhere, stil
209 compile_targets->AppendString("all");
210 value->Set("compile_targets", std::move(compile_targets));
211 } else {
212 WriteLabels(default_toolchain, *value, "compile_targets",
213 outputs.compile_labels);
214 }
208 WriteLabels(default_toolchain, *value, "test_targets", outputs.test_labels); 215 WriteLabels(default_toolchain, *value, "test_targets", outputs.test_labels);
209 } 216 }
210 217
211 if (!base::JSONWriter::Write(*value.get(), &output)) 218 if (!base::JSONWriter::Write(*value.get(), &output))
212 *err = Err(Location(), "Failed to marshal JSON value for output"); 219 *err = Err(Location(), "Failed to marshal JSON value for output");
213 return output; 220 return output;
214 } 221 }
215 222
216 } // namespace 223 } // namespace
217 224
218 Analyzer::Analyzer(const Builder& builder) 225 Analyzer::Analyzer(const Builder& builder)
219 : all_targets_(builder.GetAllResolvedTargets()), 226 : all_targets_(builder.GetAllResolvedTargets()),
220 default_toolchain_(builder.loader()->GetDefaultToolchain()) { 227 default_toolchain_(builder.loader()->GetDefaultToolchain()) {
221 for (const auto* target : all_targets_) { 228 for (const auto* target : all_targets_) {
222 labels_to_targets_[target->label()] = target; 229 labels_to_targets_[target->label()] = target;
223 for (const auto& dep_pair : target->GetDeps(Target::DEPS_ALL)) 230 for (const auto& dep_pair : target->GetDeps(Target::DEPS_ALL))
224 dep_map_.insert(std::make_pair(dep_pair.ptr, target)); 231 dep_map_.insert(std::make_pair(dep_pair.ptr, target));
225 } 232 }
226 for (const auto* target : all_targets_) { 233 for (const auto* target : all_targets_) {
227 if (dep_map_.find(target) == dep_map_.end()) 234 if (dep_map_.find(target) == dep_map_.end())
228 roots_.insert(target); 235 roots_.insert(target);
229 } 236 }
230 } 237 }
231 238
232 Analyzer::~Analyzer() {} 239 Analyzer::~Analyzer() {}
233 240
234 std::string Analyzer::Analyze(const std::string& input, Err* err) const { 241 std::string Analyzer::Analyze(const std::string& input, Err* err) const {
235 Inputs inputs; 242 Inputs inputs = Inputs();
236 Outputs outputs; 243 Outputs outputs = Outputs();
Dirk Pranke 2016/09/20 18:54:23 Is this the best way to make sure everything is ze
Nico 2016/09/20 19:16:59 The best way is to give your thing a ctor, eg by d
Dirk Pranke 2016/09/20 19:24:17 Ah, I was wondering if you could do something like
237 244
238 Err local_err = JSONToInputs(default_toolchain_, input, &inputs); 245 Err local_err = JSONToInputs(default_toolchain_, input, &inputs);
239 if (local_err.has_error()) { 246 if (local_err.has_error()) {
240 outputs.error = local_err.message(); 247 outputs.error = local_err.message();
241 return OutputsToJSON(outputs, default_toolchain_, err); 248 return OutputsToJSON(outputs, default_toolchain_, err);
242 } 249 }
243 250
244 LabelSet invalid_labels; 251 LabelSet invalid_labels;
245 for (const auto& label : InvalidLabels(inputs.compile_labels)) 252 for (const auto& label : InvalidLabels(inputs.compile_labels))
246 invalid_labels.insert(label); 253 invalid_labels.insert(label);
247 for (const auto& label : InvalidLabels(inputs.test_labels)) 254 for (const auto& label : InvalidLabels(inputs.test_labels))
248 invalid_labels.insert(label); 255 invalid_labels.insert(label);
249 if (!invalid_labels.empty()) { 256 if (!invalid_labels.empty()) {
250 outputs.error = "Invalid targets"; 257 outputs.error = "Invalid targets";
251 outputs.invalid_labels = invalid_labels; 258 outputs.invalid_labels = invalid_labels;
252 return OutputsToJSON(outputs, default_toolchain_, err); 259 return OutputsToJSON(outputs, default_toolchain_, err);
253 } 260 }
254 261
255 // TODO(crbug.com/555273): We can do smarter things when we detect changes 262 // TODO(crbug.com/555273): We can do smarter things when we detect changes
256 // to build files. For example, if all of the ninja files are unchanged, 263 // to build files. For example, if all of the ninja files are unchanged,
257 // we know that we can ignore changes to these files. Also, for most .gn 264 // we know that we can ignore changes to these files. Also, for most .gn
258 // files, we can treat a change as simply affecting every target, config, 265 // files, we can treat a change as simply affecting every target, config,
259 // or toolchain defined in that file. 266 // or toolchain defined in that file.
260 if (AnyBuildFilesWereModified(inputs.source_files)) { 267 if (AnyBuildFilesWereModified(inputs.source_files)) {
261 outputs.status = "Found dependency (all)"; 268 outputs.status = "Found dependency (all)";
262 outputs.compile_labels = inputs.compile_labels; 269 if (inputs.compile_included_all) {
270 outputs.compile_includes_all = true;
271 } else {
272 for (const auto label: inputs.compile_labels)
273 outputs.compile_labels.insert(label);
274 for (const auto label: inputs.test_labels)
275 outputs.compile_labels.insert(label);
Dirk Pranke 2016/09/20 18:54:23 This is the thing that is equivalent to a set_unio
Nico 2016/09/20 19:20:44 I haven't looked at the docs of std::set, but I'd'
Dirk Pranke 2016/09/20 19:24:17 Yeah, that might also work, though I'm not sure th
brettw 2016/09/20 19:28:53 Just do what Nico suggested. Since you already hav
Dirk Pranke 2016/09/20 19:36:20 ok, done.
276 }
263 outputs.test_labels = inputs.test_labels; 277 outputs.test_labels = inputs.test_labels;
264 return OutputsToJSON(outputs, default_toolchain_, err); 278 return OutputsToJSON(outputs, default_toolchain_, err);
265 } 279 }
266 280
267 TargetSet affected_targets = AllAffectedTargets(inputs.source_files); 281 TargetSet affected_targets = AllAffectedTargets(inputs.source_files);
268 if (affected_targets.empty()) { 282 if (affected_targets.empty()) {
269 outputs.status = "No dependency"; 283 outputs.status = "No dependency";
270 return OutputsToJSON(outputs, default_toolchain_, err); 284 return OutputsToJSON(outputs, default_toolchain_, err);
271 } 285 }
272 286
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 void Analyzer::AddAllRefsTo(const Target* target, TargetSet* results) const { 402 void Analyzer::AddAllRefsTo(const Target* target, TargetSet* results) const {
389 if (results->find(target) != results->end()) 403 if (results->find(target) != results->end())
390 return; // Already found this target. 404 return; // Already found this target.
391 results->insert(target); 405 results->insert(target);
392 406
393 auto dep_begin = dep_map_.lower_bound(target); 407 auto dep_begin = dep_map_.lower_bound(target);
394 auto dep_end = dep_map_.upper_bound(target); 408 auto dep_end = dep_map_.upper_bound(target);
395 for (auto cur_dep = dep_begin; cur_dep != dep_end; cur_dep++) 409 for (auto cur_dep = dep_begin; cur_dep != dep_end; cur_dep++)
396 AddAllRefsTo(cur_dep->second, results); 410 AddAllRefsTo(cur_dep->second, results);
397 } 411 }
OLDNEW
« no previous file with comments | « no previous file | tools/gn/analyzer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698