blob: 5290dd652535a752897496da537f286aa1f5044f [file] [log] [blame]
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "gn/commands.h"
#include "gn/err.h"
#include "gn/filesystem_utils.h"
#include "gn/setup.h"
namespace {
// Extracts from a the commands to run GN.
// The commands to run GN are the gn rule and build step at the top
// of the file. We want to keep these when deleting GN builds since
// we want to preserve the command-line flags to GN.
// On error, returns the empty string.
std::string ExtractGNBuildCommands(const base::FilePath& build_ninja_file) {
std::string file_contents;
if (!base::ReadFileToString(build_ninja_file, &file_contents))
return std::string();
std::vector<std::string_view> lines = base::SplitStringPiece(
file_contents, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
std::string result;
int num_blank_lines = 0;
for (const auto& line : lines) {
if (line.empty())
if (num_blank_lines == 3)
return result;
bool CleanOneDir(const std::string& dir) {
// Deliberately leaked to avoid expensive process teardown.
Setup* setup = new Setup;
if (!setup->DoSetup(dir, false))
return false;
base::FilePath build_dir(setup->build_settings().GetFullPath(
// NOTE: Not all GN builds have file hence we check here
// if a files exists instead.
base::FilePath build_ninja_d_file = build_dir.AppendASCII("");
if (!base::PathExists(build_ninja_d_file)) {
"%s does not look like a build directory.\n",
return false;
// Erase everything but the args file, and write a dummy file that
// will automatically rerun GN the next time Ninja is run.
base::FilePath build_ninja_file = build_dir.AppendASCII("");
std::string build_commands = ExtractGNBuildCommands(build_ninja_file);
if (build_commands.empty()) {
// Couldn't parse the file.
Err(Location(), "Couldn't read in this directory.",
"Try running \"gn gen\" on it and then re-running \"gn clean\".")
return false;
base::FileEnumerator traversal(
build_dir, false,
base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES);
for (base::FilePath current = traversal.Next(); !current.empty();
current = traversal.Next()) {
if (base::ToLowerASCII(current.BaseName().value()) !=
base::DeleteFile(current, true);
// Write the file sufficiently to regenerate itself.
if (base::WriteFile(build_ninja_file,,
static_cast<int>(build_commands.size())) == -1) {
Err(Location(), std::string("Failed to write"))
return false;
// Write a .d file for the build which references a nonexistent file.
// This will make Ninja always mark the build as dirty.
std::string dummy_content("\n");
if (base::WriteFile(build_ninja_d_file,,
static_cast<int>(dummy_content.size())) == -1) {
Err(Location(), std::string("Failed to write"))
return false;
return true;
} // namespace
namespace commands {
const char kClean[] = "clean";
const char kClean_HelpShort[] = "clean: Cleans the output directory.";
const char kClean_Help[] =
"gn clean <out_dir>...\n"
" Deletes the contents of the output directory except for and\n"
" creates a Ninja build environment sufficient to regenerate the build.\n";
int RunClean(const std::vector<std::string>& args) {
if (args.empty()) {
Err(Location(), "Missing argument.", "Usage: \"gn clean <out_dir>...\"")
return 1;
for (const auto& dir : args) {
if (!CleanOneDir(dir))
return 1;
return 0;
} // namespace commands