exclude PluginsFieldTrialTest.NoPrefLeftBehind from valgrind bot
[chromium-blink-merge.git] / tools / gn / command_clean.cc
blobd14b22d84a39e7f3949c097c180f03bc9bade75e
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/files/file_path.h"
6 #include "base/files/file_util.h"
7 #include "base/strings/string_split.h"
8 #include "base/strings/stringprintf.h"
9 #include "tools/gn/commands.h"
10 #include "tools/gn/err.h"
11 #include "tools/gn/setup.h"
13 namespace {
15 // Extracts from a build.ninja the commands to run GN.
17 // The commands to run GN are the gn rule and build.ninja build step at the top
18 // of the build.ninja file. We want to keep these when deleting GN builds since
19 // we want to preserve the command-line flags to GN.
21 // On error, returns the empty string.
22 std::string ExtractGNBuildCommands(const base::FilePath& build_ninja_file) {
23 std::string file_contents;
24 if (!base::ReadFileToString(build_ninja_file, &file_contents)) {
25 return std::string();
28 std::vector<std::string> lines = base::SplitString(
29 file_contents, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
31 std::string result;
32 int num_blank_lines = 0;
33 for (const auto& line : lines) {
34 result += line;
35 result += "\n";
36 if (line.empty()) {
37 ++num_blank_lines;
39 if (num_blank_lines == 2)
40 break;
43 return result;
46 const char kDefaultNinjaFile[] =
47 "rule gn\n"
48 " command = gn -q gen //out/%s/\n"
49 " description = Regenerating ninja files\n"
50 "\n"
51 "build build.ninja: gn\n"
52 " generator = 1\n"
53 " depfile = build.ninja.d\n";
55 } // namespace
57 namespace commands {
59 const char kClean[] = "clean";
60 const char kClean_HelpShort[] =
61 "clean: Cleans the output directory.";
62 const char kClean_Help[] =
63 "gn clean <out_dir>\n"
64 "\n"
65 " Deletes the contents of the output directory except for args.gn and\n"
66 " creates a Ninja build environment sufficient to regenerate the build.\n";
68 int RunClean(const std::vector<std::string>& args) {
69 if (args.size() != 1) {
70 Err(Location(), "You're holding it wrong.",
71 "Usage: \"gn clean <out_dir>\"").PrintToStdout();
72 return 1;
75 Setup* setup = new Setup;
76 if (!setup->DoSetup(args[0], false))
77 return 1;
79 base::FilePath build_dir(setup->build_settings().GetFullPath(
80 SourceDir(setup->build_settings().build_dir().value())));
82 // NOTE: Not all GN builds have args.gn file hence we check here
83 // if a build.ninja.d files exists instead.
84 base::FilePath build_ninja_d_file = build_dir.AppendASCII("build.ninja.d");
85 if (!base::PathExists(build_ninja_d_file)) {
86 Err(Location(),
87 base::StringPrintf("%s does not look like a build directory.\n",
88 build_ninja_d_file.DirName().value().c_str()))
89 .PrintToStdout();
90 return 1;
93 // Erase everything but the args file, and write a dummy build.ninja file that
94 // will automatically rerun GN the next time Ninja is run.
95 base::FilePath build_ninja_file = build_dir.AppendASCII("build.ninja");
96 std::string build_commands = ExtractGNBuildCommands(build_ninja_file);
98 // Read the args.gn file, if any. Not all GN builds have one.
99 base::FilePath gn_args_file = build_dir.AppendASCII("args.gn");
100 std::string args_contents;
101 base::ReadFileToString(gn_args_file, &args_contents);
103 base::DeleteFile(build_dir, true);
105 // Put back the args.gn file (if any).
106 base::CreateDirectory(build_dir);
107 if (!args_contents.empty()) {
108 if (base::WriteFile(gn_args_file, args_contents.data(),
109 static_cast<int>(args_contents.size())) == -1) {
110 Err(Location(), std::string("Failed to write args.gn.")).PrintToStdout();
111 return 1;
115 // Write the build.ninja file sufficiently to regenerate itself.
116 if (!build_commands.empty()) {
117 if (base::WriteFile(build_ninja_file, build_commands.data(),
118 static_cast<int>(build_commands.size())) == -1) {
119 Err(Location(), std::string("Failed to write build.ninja."))
120 .PrintToStdout();
121 return 1;
123 } else {
124 // Couldn't parse the build.ninja file, write a default thing.
125 std::vector<base::FilePath::StringType> components;
126 build_ninja_file.GetComponents(&components);
127 std::string default_build_file = base::StringPrintf(
128 kDefaultNinjaFile, components[components.size() - 2].c_str());
129 if (base::WriteFile(build_ninja_file, default_build_file.data(),
130 static_cast<int>(default_build_file.size())) == -1) {
131 Err(Location(), std::string("Failed to write build.ninja."))
132 .PrintToStdout();
133 return 1;
137 // Write a .d file for the build which references a nonexistant file.
138 // This will make Ninja always mark the build as dirty.
139 std::string dummy_content("build.ninja: nonexistant_file.gn\n");
140 if (base::WriteFile(build_ninja_d_file, dummy_content.data(),
141 static_cast<int>(dummy_content.size())) == -1) {
142 Err(Location(), std::string("Failed to write build.ninja.d."))
143 .PrintToStdout();
144 return 1;
147 return 0;
150 } // namespace commands