Chromium Code Reviews| 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 | |
| 87 if (!base::PathExists(build_ninja_d_file)) { | |
| 88 base::DeleteFile(build_dir, true); | |
|
brettw
2015/02/17 17:41:56
I don't think we want to do this. I think if we do
tfarina
2015/02/17 18:49:19
Done.
| |
| 89 return 1; | |
| 90 } | |
| 91 | |
| 92 // Erase everything but the args file, and write a dummy build.ninja file that | |
| 93 // will automatically rerun GN the next time Ninja is run. | |
| 94 base::FilePath build_ninja_file = build_dir.AppendASCII("build.ninja"); | |
| 95 std::string build_commands = ExtractGNBuildCommands(build_ninja_file); | |
| 96 | |
| 97 // Read the args.gn file, if any. Not all GN builds have one. | |
| 98 base::FilePath gn_args_file = build_dir.AppendASCII("args.gn"); | |
| 99 std::string args_contents; | |
| 100 base::ReadFileToString(gn_args_file, &args_contents); | |
| 101 | |
| 102 base::DeleteFile(build_dir, true); | |
| 103 | |
| 104 // Put back the args.gn file (if any). | |
| 105 base::CreateDirectory(build_dir); | |
| 106 if (!args_contents.empty()) { | |
| 107 if (base::WriteFile(gn_args_file, args_contents.data(), | |
| 108 static_cast<int>(args_contents.size())) == -1) { | |
| 109 Err(Location(), std::string("Failed to write args.gn.")).PrintToStdout(); | |
| 110 return 1; | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 // Write the build.ninja file sufficiently to regenerate itself. | |
| 115 if (!build_commands.empty()) { | |
| 116 if (base::WriteFile(build_ninja_file, build_commands.data(), | |
| 117 static_cast<int>(build_commands.size())) == -1) { | |
| 118 Err(Location(), std::string("Failed to write build.ninja.")) | |
| 119 .PrintToStdout(); | |
| 120 return 1; | |
| 121 } | |
| 122 } else { | |
| 123 // Couldn't parse the build.ninja file, write a default thing. | |
| 124 std::vector<base::FilePath::StringType> components; | |
| 125 build_ninja_file.GetComponents(&components); | |
| 126 std::string default_build_file = base::StringPrintf( | |
| 127 kDefaultNinjaFile, components[components.size() - 2].c_str()); | |
| 128 if (base::WriteFile(build_ninja_file, default_build_file.data(), | |
| 129 static_cast<int>(default_build_file.size())) == -1) { | |
| 130 Err(Location(), std::string("Failed to write build.ninja.")) | |
| 131 .PrintToStdout(); | |
| 132 return 1; | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 // Write a .d file for the build which references a nonexistant file. | |
| 137 // This will make Ninja always mark the build as dirty. | |
| 138 std::string dummy_content("build.ninja: nonexistant_file.gn\n"); | |
| 139 if (base::WriteFile(build_ninja_d_file, dummy_content.data(), | |
| 140 static_cast<int>(dummy_content.size())) == -1) { | |
| 141 Err(Location(), std::string("Failed to write build.ninja.d.")) | |
| 142 .PrintToStdout(); | |
| 143 return 1; | |
| 144 } | |
| 145 | |
| 146 return 0; | |
| 147 } | |
| 148 | |
| 149 } // namespace commands | |
| OLD | NEW |