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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « tools/gn/BUILD.gn ('k') | tools/gn/commands.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "base/files/file_path.h"
6 #include "base/files/file_util.h"
7 #include "base/files/scoped_file.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"
12
13 namespace {
14
15 const int kLineBufferLen = 65535;
16 char line_buffer[kLineBufferLen];
17
18 std::string ReadLine(FILE* file) {
19 const char* line = fgets(line_buffer, kLineBufferLen - 1, file);
20 if (!line)
21 return std::string();
22
23 std::string str(line);
24 return str;
25 }
26
27 // Extracts from a build.ninja the commands to run GN.
28 //
29 // The commands to run GN are the gn rule and build.ninja build step at the top
30 // of the build.ninja file. We want to keep these when deleting GN builds since
31 // we want to preserve the command-line flags to GN.
32 //
33 // On error, returns the empty string.
34 std::string ExtractGNBuildCommands(const base::FilePath& build_ninja_file) {
35 std::string file_contents;
36 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.
37 return std::string();
38 }
39
40 base::ScopedFILE fp(base::OpenFile(build_ninja_file, "r"));
41
42 std::string result;
43 std::string line;
44 int num_blank_lines = 0;
45 while (num_blank_lines < 2) {
46 std::string line = ReadLine(fp.get());
47 result += line;
48 if (line[0] == '\n') {
49 ++num_blank_lines;
50 }
51 }
52
53 return result;
54 }
55
56 const char kDefaultNinjaFile[] =
57 "rule gn\n"
58 " command = gn -q gen //out/%s/\n"
59 " description = Regenerating ninja files\n"
60 "\n"
61 "build build.ninja: gn\n"
62 " generator = 1\n"
63 " depfile = build.ninja.d\n";
64
65 } // namespace
66
67 namespace commands {
68
69 const char kClean[] = "clean";
70 const char kClean_HelpShort[] =
71 "clean: Cleans the output directory.";
72 const char kClean_Help[] =
73 "gn clean <out_dir>\n"
74 "\n"
75 " Deletes the contents of the output directory except for args.gn.\n";
76
77 int RunClean(const std::vector<std::string>& args) {
78 if (args.size() != 1) {
79 Err(Location(), "You're holding it wrong.",
80 "Usage: \"gn clean <out_dir> \"").PrintToStdout();
81 return 1;
82 }
83
84 Setup* setup = new Setup;
85 if (!setup->DoSetup(args[0], false) || !setup->Run())
86 return 1;
87
88 base::FilePath build_dir(setup->build_settings().GetFullPath(
89 SourceDir(setup->build_settings().build_dir().value())));
90
91 // 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.
92 base::FilePath build_ninja_d_file = build_dir.AppendASCII("build.ninja.d");
93
94 if (!base::PathExists(build_ninja_d_file)) {
95 base::DeleteFile(build_dir, true);
96 return 1;
97 }
98
99 // 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
100 // messing with the GN workflow, erase everything but the args file, and
101 // write a dummy build.ninja file that will automatically rerun GN the next
102 // time Ninja is run.
103 base::FilePath build_ninja_file = build_dir.AppendASCII("build.ninja");
104 std::string build_commands = ExtractGNBuildCommands(build_ninja_file);
105
106 base::FilePath gn_args_file = build_dir.AppendASCII("args.gn");
107 std::string args_contents;
108 if (!base::ReadFileToString(gn_args_file, &args_contents)) {
109 // Not all GN builds have a args.gn file.
110 }
111
112 base::DeleteFile(build_dir, true);
113
114 // Put back the args.gn file (if any).
115 base::CreateDirectory(build_dir);
116 if (!args_contents.empty()) {
117 if (base::WriteFile(gn_args_file, args_contents.data(),
118 static_cast<int>(args_contents.size())) == -1) {
119 Err(Location(), std::string("Failed to write args.gn.")).PrintToStdout();
120 return 1;
121 }
122 }
123
124 // Write the build.ninja file sufficiently to regenerate itself.
125 if (!build_commands.empty()) {
126 if (base::WriteFile(build_ninja_file, build_commands.data(),
127 static_cast<int>(build_commands.size())) == -1) {
128 Err(Location(), std::string("Failed to write build.ninja."))
129 .PrintToStdout();
130 return 1;
131 }
132 } else {
133 // Couldn't parse the build.ninja file, write a default thing.
134 std::vector<base::FilePath::StringType> components;
135 build_ninja_file.GetComponents(&components);
136 std::string default_build_file = base::StringPrintf(
137 kDefaultNinjaFile, components[components.size() - 2].c_str());
138 if (base::WriteFile(build_ninja_file, default_build_file.data(),
139 static_cast<int>(default_build_file.size())) == -1) {
140 Err(Location(), std::string("Failed to write build.ninja."))
141 .PrintToStdout();
142 return 1;
143 }
144 }
145
146 // Write a .d file for the build which references a nonexistant file.
147 // This will make Ninja always mark the build as dirty.
148 std::string dummy_content("build.ninja: nonexistant_file.gn\n");
149 if (base::WriteFile(build_ninja_d_file, dummy_content.data(),
150 static_cast<int>(dummy_content.size())) == -1) {
151 Err(Location(), std::string("Failed to write build.ninja.d."))
152 .PrintToStdout();
153 return 1;
154 }
155
156 return 0;
157 }
158
159 } // namespace commands
OLDNEW
« 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