This source file includes following definitions.
- BackgroundDoWrite
- ItemResolvedCallback
- RunGen
#include "base/atomicops.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/strings/string_number_conversions.h"
#include "base/timer/elapsed_timer.h"
#include "tools/gn/build_settings.h"
#include "tools/gn/commands.h"
#include "tools/gn/ninja_target_writer.h"
#include "tools/gn/ninja_writer.h"
#include "tools/gn/scheduler.h"
#include "tools/gn/setup.h"
#include "tools/gn/standard_out.h"
namespace commands {
namespace {
const char kSwitchQuiet[] = "q";
void BackgroundDoWrite(const Target* target,
const Toolchain* toolchain,
const std::vector<const Item*>& deps_for_visibility) {
Err err;
for (size_t i = 0; i < deps_for_visibility.size(); i++) {
if (!Visibility::CheckItemVisibility(target, deps_for_visibility[i],
&err)) {
g_scheduler->FailWithError(err);
break;
}
}
if (!err.has_error())
NinjaTargetWriter::RunAndWriteFile(target, toolchain);
g_scheduler->DecrementWorkCount();
}
void ItemResolvedCallback(base::subtle::Atomic32* write_counter,
scoped_refptr<Builder> builder,
const BuilderRecord* record) {
base::subtle::NoBarrier_AtomicIncrement(write_counter, 1);
const Item* item = record->item();
const Target* target = item->AsTarget();
if (target) {
const Toolchain* toolchain =
builder->GetToolchain(target->settings()->toolchain_label());
DCHECK(toolchain);
std::vector<const Item*> deps;
for (BuilderRecord::BuilderRecordSet::const_iterator iter =
record->all_deps().begin();
iter != record->all_deps().end();
++iter)
deps.push_back((*iter)->item());
g_scheduler->IncrementWorkCount();
g_scheduler->ScheduleWork(
base::Bind(&BackgroundDoWrite, target, toolchain, deps));
}
}
}
const char kGen[] = "gen";
const char kGen_HelpShort[] =
"gen: Generate ninja files.";
const char kGen_Help[] =
"gn gen: Generate ninja files.\n"
"\n"
" gn gen <output_directory>\n"
"\n"
" Generates ninja files from the current tree and puts them in the given\n"
" output directory.\n"
"\n"
" The output directory can be a source-repo-absolute path name such as:\n"
" //out/foo\n"
" Or it can be a directory relative to the current directory such as:\n"
" out/foo\n"
"\n"
" See \"gn help\" for the common command-line switches.\n";
int RunGen(const std::vector<std::string>& args) {
base::ElapsedTimer timer;
if (args.size() != 1) {
Err(Location(), "Need exactly one build directory to generate.",
"I expected something more like \"gn gen out/foo\"\n"
"You can also see \"gn help gen\".").PrintToStdout();
return 1;
}
Setup* setup = new Setup();
if (!setup->DoSetup(args[0]))
return 1;
base::subtle::Atomic32 write_counter = 0;
setup->builder()->set_resolved_callback(
base::Bind(&ItemResolvedCallback, &write_counter,
scoped_refptr<Builder>(setup->builder())));
if (!setup->Run())
return 1;
if (!NinjaWriter::RunAndWriteFiles(&setup->build_settings(),
setup->builder()))
return 1;
base::TimeDelta elapsed_time = timer.Elapsed();
if (!CommandLine::ForCurrentProcess()->HasSwitch(kSwitchQuiet)) {
OutputString("Done. ", DECORATION_GREEN);
std::string stats = "Wrote " +
base::IntToString(static_cast<int>(write_counter)) +
" targets from " +
base::IntToString(
setup->scheduler().input_file_manager()->GetInputFileCount()) +
" files in " +
base::IntToString(elapsed_time.InMilliseconds()) + "ms\n";
OutputString(stats);
}
return 0;
}
}