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

Unified Diff: tools/gn/function_write_file.cc

Issue 301973005: GN only writes files if they've changed (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Reviuew comments Created 6 years, 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/gn/BUILD.gn ('k') | tools/gn/function_write_file_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/gn/function_write_file.cc
diff --git a/tools/gn/function_write_file.cc b/tools/gn/function_write_file.cc
index 3a2458bfcf19facabb5ad63e855ca6ac56135653..36bcbc5ac6cd0684ca88c70cd8ab574b77d512b1 100644
--- a/tools/gn/function_write_file.cc
+++ b/tools/gn/function_write_file.cc
@@ -28,6 +28,10 @@ const char kWriteFile_Help[] =
" If data is a list, the list will be written one-item-per-line with no\n"
" quoting or brackets.\n"
"\n"
+ " If the file exists and the contents are identical to that being\n"
+ " written, the file will not be updated. This will prevent unnecessary\n"
+ " rebuilds of targets that depend on this file.\n"
+ "\n"
" TODO(brettw) we probably need an optional third argument to control\n"
" list formatting.\n"
"\n"
@@ -68,18 +72,25 @@ Value RunWriteFile(Scope* scope,
} else {
contents << args[1].ToString(false);
}
-
- // Write file, creating the directory if necessary.
+ const std::string& new_contents = contents.str();
base::FilePath file_path =
scope->settings()->build_settings()->GetFullPath(source_file);
- const std::string& contents_string = contents.str();
+
+ // Make sure we're not replacing the same contents.
+ std::string existing_contents;
+ if (base::ReadFileToString(file_path, &existing_contents) &&
+ existing_contents == new_contents)
+ return Value(); // Nothing to do.
+
+ // Write file, creating the directory if necessary.
if (!base::CreateDirectory(file_path.DirName())) {
*err = Err(function->function(), "Unable to create directory.",
"I was using \"" + FilePathToUTF8(file_path.DirName()) + "\".");
return Value();
}
- int int_size = static_cast<int>(contents_string.size());
- if (base::WriteFile(file_path, contents_string.c_str(), int_size)
+
+ int int_size = static_cast<int>(new_contents.size());
+ if (base::WriteFile(file_path, new_contents.c_str(), int_size)
!= int_size) {
*err = Err(function->function(), "Unable to write file.",
"I was writing \"" + FilePathToUTF8(file_path) + "\".");
« no previous file with comments | « tools/gn/BUILD.gn ('k') | tools/gn/function_write_file_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698