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

Unified Diff: tools/gn/command_clean.cc

Issue 909103003: gn: Add 'clean' command. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: remove debugging code - more Errs Created 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/gn/BUILD.gn ('k') | tools/gn/commands.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/gn/command_clean.cc
diff --git a/tools/gn/command_clean.cc b/tools/gn/command_clean.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1e2625233139bb3059b92b278cdb08370ba1162d
--- /dev/null
+++ b/tools/gn/command_clean.cc
@@ -0,0 +1,159 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/files/file_path.h"
+#include "base/files/file_util.h"
+#include "base/files/scoped_file.h"
+#include "base/strings/stringprintf.h"
+#include "tools/gn/commands.h"
+#include "tools/gn/err.h"
+#include "tools/gn/setup.h"
+
+namespace {
+
+const int kLineBufferLen = 65535;
+char line_buffer[kLineBufferLen];
+
+std::string ReadLine(FILE* file) {
+ const char* line = fgets(line_buffer, kLineBufferLen - 1, file);
+ if (!line)
+ return std::string();
+
+ std::string str(line);
+ return str;
+}
+
+// Extracts from a build.ninja the commands to run GN.
+//
+// The commands to run GN are the gn rule and build.ninja build step at the top
+// of the build.ninja file. We want to keep these when deleting GN builds since
+// we want to preserve the command-line flags to GN.
+//
+// On error, returns the empty string.
+std::string ExtractGNBuildCommands(const base::FilePath& build_ninja_file) {
+ std::string file_contents;
+ if (!base::ReadFileToString(build_ninja_file, &file_contents)) {
scottmg 2015/02/11 00:16:36 It looks like you meant to remove this. Alternati
tfarina 2015/02/11 12:29:26 Hey, that was nice. Done.
+ return std::string();
+ }
+
+ base::ScopedFILE fp(base::OpenFile(build_ninja_file, "r"));
+
+ std::string result;
+ std::string line;
+ int num_blank_lines = 0;
+ while (num_blank_lines < 2) {
+ std::string line = ReadLine(fp.get());
+ result += line;
+ if (line[0] == '\n') {
+ ++num_blank_lines;
+ }
+ }
+
+ return result;
+}
+
+const char kDefaultNinjaFile[] =
+ "rule gn\n"
+ " command = gn -q gen //out/%s/\n"
+ " description = Regenerating ninja files\n"
+ "\n"
+ "build build.ninja: gn\n"
+ " generator = 1\n"
+ " depfile = build.ninja.d\n";
+
+} // namespace
+
+namespace commands {
+
+const char kClean[] = "clean";
+const char kClean_HelpShort[] =
+ "clean: Cleans the output directory.";
+const char kClean_Help[] =
+ "gn clean <out_dir>\n"
+ "\n"
+ " Deletes the contents of the output directory except for args.gn.\n";
+
+int RunClean(const std::vector<std::string>& args) {
+ if (args.size() != 1) {
+ Err(Location(), "You're holding it wrong.",
+ "Usage: \"gn clean <out_dir> \"").PrintToStdout();
+ return 1;
+ }
+
+ Setup* setup = new Setup;
+ if (!setup->DoSetup(args[0], false) || !setup->Run())
+ return 1;
+
+ base::FilePath build_dir(setup->build_settings().GetFullPath(
+ SourceDir(setup->build_settings().build_dir().value())));
+
+ // GN writes a build.ninja.d file. Note that not all GN builds have args.gn.
scottmg 2015/02/11 00:16:37 This is GN, so it seems odd to say "GN writes a bu
tfarina 2015/02/11 12:29:26 Done.
+ base::FilePath build_ninja_d_file = build_dir.AppendASCII("build.ninja.d");
+
+ if (!base::PathExists(build_ninja_d_file)) {
+ base::DeleteFile(build_dir, true);
+ return 1;
+ }
+
+ // GN builds aren't automatically regenerated when you sync. To avoid
scottmg 2015/02/11 00:16:37 I guess this changes what the command does somewha
cjhopman 2015/02/11 01:59:42 I guess there's this weird issue that we treat gn
+ // messing with the GN workflow, erase everything but the args file, and
+ // write a dummy build.ninja file that will automatically rerun GN the next
+ // time Ninja is run.
+ base::FilePath build_ninja_file = build_dir.AppendASCII("build.ninja");
+ std::string build_commands = ExtractGNBuildCommands(build_ninja_file);
+
+ base::FilePath gn_args_file = build_dir.AppendASCII("args.gn");
+ std::string args_contents;
+ if (!base::ReadFileToString(gn_args_file, &args_contents)) {
+ // Not all GN builds have a args.gn file.
+ }
+
+ base::DeleteFile(build_dir, true);
+
+ // Put back the args.gn file (if any).
+ base::CreateDirectory(build_dir);
+ if (!args_contents.empty()) {
+ if (base::WriteFile(gn_args_file, args_contents.data(),
+ static_cast<int>(args_contents.size())) == -1) {
+ Err(Location(), std::string("Failed to write args.gn.")).PrintToStdout();
+ return 1;
+ }
+ }
+
+ // Write the build.ninja file sufficiently to regenerate itself.
+ if (!build_commands.empty()) {
+ if (base::WriteFile(build_ninja_file, build_commands.data(),
+ static_cast<int>(build_commands.size())) == -1) {
+ Err(Location(), std::string("Failed to write build.ninja."))
+ .PrintToStdout();
+ return 1;
+ }
+ } else {
+ // Couldn't parse the build.ninja file, write a default thing.
+ std::vector<base::FilePath::StringType> components;
+ build_ninja_file.GetComponents(&components);
+ std::string default_build_file = base::StringPrintf(
+ kDefaultNinjaFile, components[components.size() - 2].c_str());
+ if (base::WriteFile(build_ninja_file, default_build_file.data(),
+ static_cast<int>(default_build_file.size())) == -1) {
+ Err(Location(), std::string("Failed to write build.ninja."))
+ .PrintToStdout();
+ return 1;
+ }
+ }
+
+ // Write a .d file for the build which references a nonexistant file.
+ // This will make Ninja always mark the build as dirty.
+ std::string dummy_content("build.ninja: nonexistant_file.gn\n");
+ if (base::WriteFile(build_ninja_d_file, dummy_content.data(),
+ static_cast<int>(dummy_content.size())) == -1) {
+ Err(Location(), std::string("Failed to write build.ninja.d."))
+ .PrintToStdout();
+ return 1;
+ }
+
+ return 0;
+}
+
+} // namespace commands
« no previous file with comments | « tools/gn/BUILD.gn ('k') | tools/gn/commands.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698