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 and\n" |
| 67 " creates a Ninja build environment sufficient to regenerate the build.\n"; |
| 68 |
| 69 int RunClean(const std::vector<std::string>& args) { |
| 70 if (args.size() != 1) { |
| 71 Err(Location(), "You're holding it wrong.", |
| 72 "Usage: \"gn clean <out_dir>\"").PrintToStdout(); |
| 73 return 1; |
| 74 } |
| 75 |
| 76 Setup* setup = new Setup; |
| 77 if (!setup->DoSetup(args[0], false)) |
| 78 return 1; |
| 79 |
| 80 base::FilePath build_dir(setup->build_settings().GetFullPath( |
| 81 SourceDir(setup->build_settings().build_dir().value()))); |
| 82 |
| 83 // NOTE: Not all GN builds have args.gn file hence we check here |
| 84 // if a build.ninja.d files exists instead. |
| 85 base::FilePath build_ninja_d_file = build_dir.AppendASCII("build.ninja.d"); |
| 86 if (!base::PathExists(build_ninja_d_file)) { |
| 87 Err(Location(), |
| 88 base::StringPrintf("%s does not look like a build directory.\n", |
| 89 build_ninja_d_file.DirName().value().c_str())) |
| 90 .PrintToStdout(); |
| 91 return 1; |
| 92 } |
| 93 |
| 94 // Erase everything but the args file, and write a dummy build.ninja file that |
| 95 // will automatically rerun GN the next time Ninja is run. |
| 96 base::FilePath build_ninja_file = build_dir.AppendASCII("build.ninja"); |
| 97 std::string build_commands = ExtractGNBuildCommands(build_ninja_file); |
| 98 |
| 99 // Read the args.gn file, if any. Not all GN builds have one. |
| 100 base::FilePath gn_args_file = build_dir.AppendASCII("args.gn"); |
| 101 std::string args_contents; |
| 102 base::ReadFileToString(gn_args_file, &args_contents); |
| 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 |