OLD | NEW |
---|---|
(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/string_split.h" | |
9 #include "base/strings/stringprintf.h" | |
10 #include "tools/gn/commands.h" | |
11 #include "tools/gn/err.h" | |
12 #include "tools/gn/setup.h" | |
13 | |
14 namespace { | |
15 | |
16 // Extracts from a build.ninja the commands to run GN. | |
17 // | |
18 // The commands to run GN are the gn rule and build.ninja build step at the top | |
19 // of the build.ninja file. We want to keep these when deleting GN builds since | |
20 // we want to preserve the command-line flags to GN. | |
21 // | |
22 // On error, returns the empty string. | |
23 std::string ExtractGNBuildCommands(const base::FilePath& build_ninja_file) { | |
24 std::string file_contents; | |
25 if (!base::ReadFileToString(build_ninja_file, &file_contents)) { | |
26 return std::string(); | |
27 } | |
28 | |
29 std::vector<std::string> lines; | |
30 base::SplitString(file_contents, '\n', &lines); | |
31 | |
32 std::string result; | |
33 int num_blank_lines = 0; | |
34 for (size_t i = 0; i < lines.size(); ++i) { | |
35 result += lines[i]; | |
36 result += "\n"; | |
37 if (lines[i].empty()) { | |
38 ++num_blank_lines; | |
39 } | |
40 if (num_blank_lines == 2) | |
41 break; | |
42 } | |
43 | |
44 return result; | |
45 } | |
46 | |
47 const char kDefaultNinjaFile[] = | |
48 "rule gn\n" | |
49 " command = gn -q gen //out/%s/\n" | |
50 " description = Regenerating ninja files\n" | |
51 "\n" | |
52 "build build.ninja: gn\n" | |
53 " generator = 1\n" | |
54 " depfile = build.ninja.d\n"; | |
55 | |
56 } // namespace | |
57 | |
58 namespace commands { | |
59 | |
60 const char kClean[] = "clean"; | |
61 const char kClean_HelpShort[] = | |
62 "clean: Cleans the output directory."; | |
63 const char kClean_Help[] = | |
64 "gn clean <out_dir>\n" | |
65 "\n" | |
66 " Deletes the contents of the output directory except for args.gn.\n"; | |
brettw
2015/02/16 23:35:21
add "and creates a Ninja build environment suffici
tfarina
2015/02/17 02:31:41
Done.
| |
67 | |
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(); | |
brettw
2015/02/13 20:53:14
No space after <out_dir>
tfarina
2015/02/17 02:31:41
Done.
| |
72 return 1; | |
73 } | |
74 | |
75 Setup* setup = new Setup; | |
76 if (!setup->DoSetup(args[0], false) || !setup->Run()) | |
brettw
2015/02/13 20:53:14
We don't want to do this since this will read the
tfarina
2015/02/16 20:33:33
I need the DoSetup() call because it calls FillBui
tfarina
2015/02/17 02:31:41
Done.
| |
77 return 1; | |
78 | |
79 base::FilePath build_dir(setup->build_settings().GetFullPath( | |
80 SourceDir(setup->build_settings().build_dir().value()))); | |
81 | |
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 | |
86 if (!base::PathExists(build_ninja_d_file)) { | |
87 base::DeleteFile(build_dir, true); | |
brettw
2015/02/13 20:53:14
We don't want this. GN clean should only delete GN
tfarina
2015/02/14 01:46:39
Brett, what you mean by GN build directories? Do y
| |
88 return 1; | |
89 } | |
90 | |
91 // GN builds aren't automatically regenerated when you sync. To avoid | |
brettw
2015/02/16 23:35:21
This comment doesn't make sense in this context. J
tfarina
2015/02/17 02:31:41
Done.
| |
92 // messing with the GN workflow, erase everything but the args file, and | |
93 // write a dummy build.ninja file that will automatically rerun GN the next | |
94 // time Ninja is run. | |
95 base::FilePath build_ninja_file = build_dir.AppendASCII("build.ninja"); | |
96 std::string build_commands = ExtractGNBuildCommands(build_ninja_file); | |
97 | |
98 base::FilePath gn_args_file = build_dir.AppendASCII("args.gn"); | |
99 std::string args_contents; | |
100 if (!base::ReadFileToString(gn_args_file, &args_contents)) { | |
101 // Not all GN builds have a args.gn file. | |
brettw
2015/02/16 23:35:21
I'd just delete the conditional and put a comment
tfarina
2015/02/17 02:31:41
Done.
| |
102 } | |
103 | |
104 base::DeleteFile(build_dir, true); | |
105 | |
106 // Put back the args.gn file (if any). | |
107 base::CreateDirectory(build_dir); | |
108 if (!args_contents.empty()) { | |
109 if (base::WriteFile(gn_args_file, args_contents.data(), | |
110 static_cast<int>(args_contents.size())) == -1) { | |
111 Err(Location(), std::string("Failed to write args.gn.")).PrintToStdout(); | |
112 return 1; | |
113 } | |
114 } | |
115 | |
116 // Write the build.ninja file sufficiently to regenerate itself. | |
117 if (!build_commands.empty()) { | |
118 if (base::WriteFile(build_ninja_file, build_commands.data(), | |
119 static_cast<int>(build_commands.size())) == -1) { | |
120 Err(Location(), std::string("Failed to write build.ninja.")) | |
121 .PrintToStdout(); | |
122 return 1; | |
123 } | |
124 } else { | |
125 // Couldn't parse the build.ninja file, write a default thing. | |
126 std::vector<base::FilePath::StringType> components; | |
127 build_ninja_file.GetComponents(&components); | |
128 std::string default_build_file = base::StringPrintf( | |
129 kDefaultNinjaFile, components[components.size() - 2].c_str()); | |
130 if (base::WriteFile(build_ninja_file, default_build_file.data(), | |
131 static_cast<int>(default_build_file.size())) == -1) { | |
132 Err(Location(), std::string("Failed to write build.ninja.")) | |
133 .PrintToStdout(); | |
134 return 1; | |
135 } | |
136 } | |
137 | |
138 // Write a .d file for the build which references a nonexistant file. | |
139 // This will make Ninja always mark the build as dirty. | |
140 std::string dummy_content("build.ninja: nonexistant_file.gn\n"); | |
141 if (base::WriteFile(build_ninja_d_file, dummy_content.data(), | |
142 static_cast<int>(dummy_content.size())) == -1) { | |
143 Err(Location(), std::string("Failed to write build.ninja.d.")) | |
144 .PrintToStdout(); | |
145 return 1; | |
146 } | |
147 | |
148 return 0; | |
149 } | |
150 | |
151 } // namespace commands | |
OLD | NEW |