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

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

Issue 1494883002: GN: Makes GN output deterministic (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Tweaked some comments for clarity Created 5 years 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
OLDNEW
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/ninja_target_writer.h" 5 #include "tools/gn/ninja_target_writer.h"
6 6
7 #include <sstream> 7 #include <sstream>
8 8
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 path_output_.WriteFile(out_, input); 200 path_output_.WriteFile(out_, input);
201 } 201 }
202 if (list_sources_as_input_deps) { 202 if (list_sources_as_input_deps) {
203 for (const auto& source : target_->sources()) { 203 for (const auto& source : target_->sources()) {
204 out_ << " "; 204 out_ << " ";
205 path_output_.WriteFile(out_, source); 205 path_output_.WriteFile(out_, source);
206 } 206 }
207 } 207 }
208 208
209 // The different souces of input deps may duplicate some targets, so uniquify 209 // The different souces of input deps may duplicate some targets, so uniquify
210 // them (ordering doesn't matter for this case). 210 // them. Note that the order matters, or the output is non-deterministic.
211 std::set<const Target*> unique_deps; 211 std::set<const Target*> unique_deps;
212 212
213 // Hard dependencies that are direct or indirect dependencies. 213 // Hard dependencies that are direct or indirect dependencies.
214 const std::set<const Target*>& hard_deps = target_->recursive_hard_deps(); 214 const std::set<const Target*>& hard_deps = target_->recursive_hard_deps();
M-A Ruel 2015/12/07 15:09:50 Use the copy-constructor to save an empty initiali
brettw 2015/12/07 18:48:54 We don't have to micro-optimize, but in the GN cod
Zachary Forman 2015/12/08 09:40:42 Nice approach, and probably scales better too. Pre
215 for (const auto& dep : hard_deps)
216 unique_deps.insert(dep);
217 215
216 unique_deps.insert(hard_deps.begin(), hard_deps.end());
218 // Extra hard dependencies passed in. 217 // Extra hard dependencies passed in.
219 unique_deps.insert(extra_hard_deps.begin(), extra_hard_deps.end()); 218 unique_deps.insert(extra_hard_deps.begin(), extra_hard_deps.end());
220 219
221 // Toolchain dependencies. These must be resolved before doing anything. 220 // Toolchain dependencies. These must be resolved before doing anything.
222 // This just writs all toolchain deps for simplicity. If we find that 221 // This just writs all toolchain deps for simplicity. If we find that
M-A Ruel 2015/12/07 15:09:50 writes (while at it)
Zachary Forman 2015/12/08 09:40:42 Done.
223 // toolchains often have more than one dependency, we could consider writing 222 // toolchains often have more than one dependency, we could consider writing
224 // a toolchain-specific stamp file and only include the stamp here. 223 // a toolchain-specific stamp file and only include the stamp here.
225 const LabelTargetVector& toolchain_deps = target_->toolchain()->deps(); 224 const LabelTargetVector& toolchain_deps = target_->toolchain()->deps();
226 for (const auto& toolchain_dep : toolchain_deps) 225 for (const auto& toolchain_dep : toolchain_deps)
227 unique_deps.insert(toolchain_dep.ptr); 226 unique_deps.insert(toolchain_dep.ptr);
228 227
229 for (const auto& dep : unique_deps) { 228 // Sort the dependencies so they're in deterministic order.
229 std::vector<const Target*> sorted_deps(unique_deps.begin(),
230 unique_deps.end());
231
232 std::sort(
233 sorted_deps.begin(), sorted_deps.end(),
234 [](const Target* a, const Target* b) { return a->label() < b->label(); });
235
236 for (const auto& dep : sorted_deps) {
230 DCHECK(!dep->dependency_output_file().value().empty()); 237 DCHECK(!dep->dependency_output_file().value().empty());
231 out_ << " "; 238 out_ << " ";
232 path_output_.WriteFile(out_, dep->dependency_output_file()); 239 path_output_.WriteFile(out_, dep->dependency_output_file());
233 } 240 }
234 241
235 out_ << "\n"; 242 out_ << "\n";
236 return input_stamp_file; 243 return input_stamp_file;
237 } 244 }
238 245
239 void NinjaTargetWriter::WriteStampForTarget( 246 void NinjaTargetWriter::WriteStampForTarget(
(...skipping 15 matching lines...) Expand all
255 << GetNinjaRulePrefixForToolchain(settings_) 262 << GetNinjaRulePrefixForToolchain(settings_)
256 << Toolchain::ToolTypeToName(Toolchain::TYPE_STAMP); 263 << Toolchain::ToolTypeToName(Toolchain::TYPE_STAMP);
257 path_output_.WriteFiles(out_, files); 264 path_output_.WriteFiles(out_, files);
258 265
259 if (!order_only_deps.empty()) { 266 if (!order_only_deps.empty()) {
260 out_ << " ||"; 267 out_ << " ||";
261 path_output_.WriteFiles(out_, order_only_deps); 268 path_output_.WriteFiles(out_, order_only_deps);
262 } 269 }
263 out_ << std::endl; 270 out_ << std::endl;
264 } 271 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698