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

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

Issue 1656253003: [GN] Don't rewrite files with the same contents (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Restore original stream types Created 4 years, 10 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
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/visual_studio_writer.h" 5 #include "tools/gn/visual_studio_writer.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <map> 8 #include <map>
9 #include <set> 9 #include <set>
10 #include <string> 10 #include <string>
11 11
12 #include "base/files/file_util.h"
13 #include "base/logging.h" 12 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
15 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
17 #include "tools/gn/builder.h" 16 #include "tools/gn/builder.h"
18 #include "tools/gn/config.h" 17 #include "tools/gn/config.h"
19 #include "tools/gn/config_values_extractors.h" 18 #include "tools/gn/config_values_extractors.h"
20 #include "tools/gn/filesystem_utils.h" 19 #include "tools/gn/filesystem_utils.h"
21 #include "tools/gn/parse_tree.h" 20 #include "tools/gn/parse_tree.h"
22 #include "tools/gn/path_output.h" 21 #include "tools/gn/path_output.h"
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 // outlive the output. 145 // outlive the output.
147 base::StringPiece FindParentDir(const std::string* path) { 146 base::StringPiece FindParentDir(const std::string* path) {
148 DCHECK(path && !path->empty()); 147 DCHECK(path && !path->empty());
149 for (int i = static_cast<int>(path->size()) - 2; i >= 0; --i) { 148 for (int i = static_cast<int>(path->size()) - 2; i >= 0; --i) {
150 if (IsSlash((*path)[i])) 149 if (IsSlash((*path)[i]))
151 return base::StringPiece(path->data(), i); 150 return base::StringPiece(path->data(), i);
152 } 151 }
153 return base::StringPiece(); 152 return base::StringPiece();
154 } 153 }
155 154
156 bool HasSameContent(std::stringstream& data_1, const base::FilePath& data_2) {
157 // Compare file sizes first. Quick and will save us some time if they are
158 // different sizes.
159 int64_t data_1_len = data_1.tellp();
160
161 int64_t data_2_len;
162 if (!base::GetFileSize(data_2, &data_2_len) || data_1_len != data_2_len)
163 return false;
164
165 std::string data_2_data;
166 data_2_data.resize(data_2_len);
167 if (!base::ReadFileToString(data_2, &data_2_data))
168 return false;
169
170 std::string data_1_data;
171 data_1_data = data_1.str();
172
173 return data_1_data == data_2_data;
174 }
175
176 } // namespace 155 } // namespace
177 156
178 VisualStudioWriter::SolutionEntry::SolutionEntry(const std::string& _name, 157 VisualStudioWriter::SolutionEntry::SolutionEntry(const std::string& _name,
179 const std::string& _path, 158 const std::string& _path,
180 const std::string& _guid) 159 const std::string& _guid)
181 : name(_name), path(_path), guid(_guid), parent_folder(nullptr) {} 160 : name(_name), path(_path), guid(_guid), parent_folder(nullptr) {}
182 161
183 VisualStudioWriter::SolutionEntry::~SolutionEntry() = default; 162 VisualStudioWriter::SolutionEntry::~SolutionEntry() = default;
184 163
185 VisualStudioWriter::VisualStudioWriter(const BuildSettings* build_settings) 164 VisualStudioWriter::VisualStudioWriter(const BuildSettings* build_settings)
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 std::stringstream vcxproj_string_out; 237 std::stringstream vcxproj_string_out;
259 if (!WriteProjectFileContents(vcxproj_string_out, *projects_.back(), target, 238 if (!WriteProjectFileContents(vcxproj_string_out, *projects_.back(), target,
260 err)) { 239 err)) {
261 projects_.pop_back(); 240 projects_.pop_back();
262 return false; 241 return false;
263 } 242 }
264 243
265 // Only write the content to the file if it's different. That is 244 // Only write the content to the file if it's different. That is
266 // both a performance optimization and more importantly, prevents 245 // both a performance optimization and more importantly, prevents
267 // Visual Studio from reloading the projects. 246 // Visual Studio from reloading the projects.
268 if (!HasSameContent(vcxproj_string_out, vcxproj_path)) { 247 if (!WriteFileIfChanged(vcxproj_path, vcxproj_string_out.str(), err))
269 std::string content = vcxproj_string_out.str(); 248 return false;
270 int size = static_cast<int>(content.size());
271 if (base::WriteFile(vcxproj_path, content.c_str(), size) != size) {
272 *err = Err(Location(), "Couldn't open " + target->label().name() +
273 ".vcxproj for writing");
274 return false;
275 }
276 }
277 249
278 base::FilePath filters_path = UTF8ToFilePath(vcxproj_path_str + ".filters"); 250 base::FilePath filters_path = UTF8ToFilePath(vcxproj_path_str + ".filters");
279
280 std::stringstream filters_string_out; 251 std::stringstream filters_string_out;
281 WriteFiltersFileContents(filters_string_out, target); 252 WriteFiltersFileContents(filters_string_out, target);
282 253 return WriteFileIfChanged(filters_path, filters_string_out.str(), err);
283 if (!HasSameContent(filters_string_out, filters_path)) {
284 std::string content = filters_string_out.str();
285 int size = static_cast<int>(content.size());
286 if (base::WriteFile(filters_path, content.c_str(), size) != size) {
287 *err = Err(Location(), "Couldn't open " + target->label().name() +
288 ".vcxproj.filters for writing");
289 return false;
290 }
291 }
292
293 return true;
294 } 254 }
295 255
296 bool VisualStudioWriter::WriteProjectFileContents( 256 bool VisualStudioWriter::WriteProjectFileContents(
297 std::ostream& out, 257 std::ostream& out,
298 const SolutionEntry& solution_project, 258 const SolutionEntry& solution_project,
299 const Target* target, 259 const Target* target,
300 Err* err) { 260 Err* err) {
301 PathOutput path_output(GetTargetOutputDir(target), 261 PathOutput path_output(GetTargetOutputDir(target),
302 build_settings_->root_path_utf8(), 262 build_settings_->root_path_utf8(),
303 EscapingMode::ESCAPE_NONE); 263 EscapingMode::ESCAPE_NONE);
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
579 return false; 539 return false;
580 540
581 base::FilePath sln_path = build_settings_->GetFullPath(sln_file); 541 base::FilePath sln_path = build_settings_->GetFullPath(sln_file);
582 542
583 std::stringstream string_out; 543 std::stringstream string_out;
584 WriteSolutionFileContents(string_out, sln_path.DirName()); 544 WriteSolutionFileContents(string_out, sln_path.DirName());
585 545
586 // Only write the content to the file if it's different. That is 546 // Only write the content to the file if it's different. That is
587 // both a performance optimization and more importantly, prevents 547 // both a performance optimization and more importantly, prevents
588 // Visual Studio from reloading the projects. 548 // Visual Studio from reloading the projects.
589 if (HasSameContent(string_out, sln_path)) 549 return WriteFileIfChanged(sln_path, string_out.str(), err);
590 return true;
591
592 std::string content = string_out.str();
593 int size = static_cast<int>(content.size());
594 if (base::WriteFile(sln_path, content.c_str(), size) != size) {
595 *err = Err(Location(), "Couldn't open all.sln for writing");
596 return false;
597 }
598
599 return true;
600 } 550 }
601 551
602 void VisualStudioWriter::WriteSolutionFileContents( 552 void VisualStudioWriter::WriteSolutionFileContents(
603 std::ostream& out, 553 std::ostream& out,
604 const base::FilePath& solution_dir_path) { 554 const base::FilePath& solution_dir_path) {
605 out << "Microsoft Visual Studio Solution File, Format Version 12.00" 555 out << "Microsoft Visual Studio Solution File, Format Version 12.00"
606 << std::endl; 556 << std::endl;
607 out << "# Visual Studio 2015" << std::endl; 557 out << "# Visual Studio 2015" << std::endl;
608 558
609 SourceDir solution_dir(FilePathToUTF8(solution_dir_path)); 559 SourceDir solution_dir(FilePathToUTF8(solution_dir_path));
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
745 base::CompareCase::SENSITIVE)) { 695 base::CompareCase::SENSITIVE)) {
746 folder->parent_folder = parents.back(); 696 folder->parent_folder = parents.back();
747 break; 697 break;
748 } else { 698 } else {
749 parents.pop_back(); 699 parents.pop_back();
750 } 700 }
751 } 701 }
752 parents.push_back(folder); 702 parents.push_back(folder);
753 } 703 }
754 } 704 }
OLDNEW
« tools/gn/function_write_file.cc ('K') | « tools/gn/ninja_target_writer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698