This source file includes following definitions.
- InvokeFileLoadCallback
- sync_invocation
- AsyncLoadFile
- SyncLoadFile
- GetInputFileCount
- GetAllPhysicalInputFileNames
- BackgroundLoadFile
- LoadFile
#include "tools/gn/input_file_manager.h"
#include "base/bind.h"
#include "base/stl_util.h"
#include "tools/gn/filesystem_utils.h"
#include "tools/gn/parser.h"
#include "tools/gn/scheduler.h"
#include "tools/gn/scope_per_file_provider.h"
#include "tools/gn/tokenizer.h"
#include "tools/gn/trace.h"
namespace {
void InvokeFileLoadCallback(const InputFileManager::FileLoadCallback& cb,
const ParseNode* node) {
cb.Run(node);
}
}
InputFileManager::InputFileData::InputFileData(const SourceFile& file_name)
: file(file_name),
loaded(false),
sync_invocation(false) {
}
InputFileManager::InputFileData::~InputFileData() {
}
InputFileManager::InputFileManager() {
}
InputFileManager::~InputFileManager() {
STLDeleteContainerPairSecondPointers(input_files_.begin(),
input_files_.end());
}
bool InputFileManager::AsyncLoadFile(const LocationRange& origin,
const BuildSettings* build_settings,
const SourceFile& file_name,
const FileLoadCallback& callback,
Err* err) {
base::Closure schedule_this;
{
base::AutoLock lock(lock_);
InputFileMap::const_iterator found = input_files_.find(file_name);
if (found == input_files_.end()) {
InputFileData* data = new InputFileData(file_name);
data->scheduled_callbacks.push_back(callback);
input_files_[file_name] = data;
schedule_this = base::Bind(&InputFileManager::BackgroundLoadFile,
this,
origin,
build_settings,
file_name,
&data->file);
} else {
InputFileData* data = found->second;
if (data->sync_invocation) {
g_scheduler->FailWithError(Err(
origin, "Load type mismatch.",
"The file \"" + file_name.value() + "\" was previously loaded\n"
"synchronously (via an import) and now you're trying to load it "
"asynchronously\n(via a deps rule). This is a class 2 misdemeanor: "
"a single input file must\nbe loaded the same way each time to "
"avoid blowing my tiny, tiny mind."));
return false;
}
if (data->loaded) {
schedule_this = base::Bind(&InvokeFileLoadCallback, callback,
data->parsed_root.get());
} else {
data->scheduled_callbacks.push_back(callback);
return true;
}
}
}
g_scheduler->pool()->PostWorkerTaskWithShutdownBehavior(
FROM_HERE, schedule_this,
base::SequencedWorkerPool::BLOCK_SHUTDOWN);
return true;
}
const ParseNode* InputFileManager::SyncLoadFile(
const LocationRange& origin,
const BuildSettings* build_settings,
const SourceFile& file_name,
Err* err) {
base::AutoLock lock(lock_);
InputFileData* data = NULL;
InputFileMap::iterator found = input_files_.find(file_name);
if (found == input_files_.end()) {
data = new InputFileData(file_name);
data->sync_invocation = true;
input_files_[file_name] = data;
base::AutoUnlock unlock(lock_);
if (!LoadFile(origin, build_settings, file_name, &data->file, err))
return NULL;
} else {
data = found->second;
if (!data->sync_invocation) {
*err = Err(
origin, "Load type mismatch.",
"The file \"" + file_name.value() + "\" was previously loaded\n"
"asynchronously (via a deps rule) and now you're trying to load it "
"synchronously.\nThis is a class 2 misdemeanor: a single input file "
"must be loaded the same way\neach time to avoid blowing my tiny, "
"tiny mind.");
return NULL;
}
if (!data->loaded) {
if (!data->completion_event)
data->completion_event.reset(new base::WaitableEvent(false, false));
{
base::AutoUnlock unlock(lock_);
data->completion_event->Wait();
}
data->completion_event->Signal();
}
}
if (!data->parsed_root)
*err = Err(origin, "File parse failed");
return data->parsed_root.get();
}
int InputFileManager::GetInputFileCount() const {
base::AutoLock lock(lock_);
return static_cast<int>(input_files_.size());
}
void InputFileManager::GetAllPhysicalInputFileNames(
std::vector<base::FilePath>* result) const {
base::AutoLock lock(lock_);
result->reserve(input_files_.size());
for (InputFileMap::const_iterator i = input_files_.begin();
i != input_files_.end(); ++i) {
if (!i->second->file.physical_name().empty())
result->push_back(i->second->file.physical_name());
}
}
void InputFileManager::BackgroundLoadFile(const LocationRange& origin,
const BuildSettings* build_settings,
const SourceFile& name,
InputFile* file) {
Err err;
if (!LoadFile(origin, build_settings, name, file, &err))
g_scheduler->FailWithError(err);
}
bool InputFileManager::LoadFile(const LocationRange& origin,
const BuildSettings* build_settings,
const SourceFile& name,
InputFile* file,
Err* err) {
if (g_scheduler->verbose_logging()) {
std::string logmsg = name.value();
if (origin.begin().file())
logmsg += " (referenced from " + origin.begin().Describe(false) + ")";
g_scheduler->Log("Loading", logmsg);
}
base::FilePath primary_path = build_settings->GetFullPath(name);
ScopedTrace load_trace(TraceItem::TRACE_FILE_LOAD, name.value());
if (!file->Load(primary_path)) {
if (!build_settings->secondary_source_path().empty()) {
base::FilePath secondary_path =
build_settings->GetFullPathSecondary(name);
if (!file->Load(secondary_path)) {
*err = Err(origin, "Can't load input file.",
"Unable to load either \n" +
FilePathToUTF8(primary_path) + " or \n" +
FilePathToUTF8(secondary_path));
return false;
}
} else {
*err = Err(origin,
"Unable to load \"" + FilePathToUTF8(primary_path) + "\".");
return false;
}
}
load_trace.Done();
ScopedTrace exec_trace(TraceItem::TRACE_FILE_PARSE, name.value());
std::vector<Token> tokens = Tokenizer::Tokenize(file, err);
if (err->has_error())
return false;
scoped_ptr<ParseNode> root = Parser::Parse(tokens, err);
if (err->has_error())
return false;
ParseNode* unowned_root = root.get();
exec_trace.Done();
std::vector<FileLoadCallback> callbacks;
{
base::AutoLock lock(lock_);
DCHECK(input_files_.find(name) != input_files_.end());
InputFileData* data = input_files_[name];
data->loaded = true;
data->tokens.swap(tokens);
data->parsed_root = root.Pass();
if (data->completion_event)
data->completion_event->Signal();
callbacks.swap(data->scheduled_callbacks);
}
for (size_t i = 0; i < callbacks.size(); i++)
callbacks[i].Run(unowned_root);
return true;
}