2013-11-22 07:54:13 +08:00
|
|
|
//===- lib/Driver/WinLinkInputGraph.cpp -----------------------------------===//
|
2013-11-21 07:51:41 +08:00
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lld/Driver/WinLinkInputGraph.h"
|
|
|
|
|
2013-12-03 12:18:55 +08:00
|
|
|
using llvm::sys::fs::file_magic;
|
|
|
|
using llvm::sys::fs::identify_magic;
|
|
|
|
|
2013-11-21 07:51:41 +08:00
|
|
|
namespace lld {
|
|
|
|
|
|
|
|
/// \brief Parse the input file to lld::File.
|
|
|
|
error_code PECOFFFileNode::parse(const LinkingContext &ctx,
|
|
|
|
raw_ostream &diagnostics) {
|
|
|
|
ErrorOr<StringRef> filePath = getPath(ctx);
|
2013-12-03 08:57:19 +08:00
|
|
|
if (!filePath) {
|
|
|
|
diagnostics << "File not found: " << _path << "\n";
|
2013-11-21 07:51:41 +08:00
|
|
|
return error_code(filePath);
|
2013-12-03 08:57:19 +08:00
|
|
|
}
|
2013-11-21 07:51:41 +08:00
|
|
|
|
2013-12-03 08:57:19 +08:00
|
|
|
if (error_code ec = getBuffer(*filePath)) {
|
|
|
|
diagnostics << "Cannot open file: " << *filePath << "\n";
|
2013-11-21 07:51:41 +08:00
|
|
|
return ec;
|
2013-12-03 08:57:19 +08:00
|
|
|
}
|
2013-11-21 07:51:41 +08:00
|
|
|
|
|
|
|
if (ctx.logInputFiles())
|
|
|
|
diagnostics << *filePath << "\n";
|
|
|
|
|
|
|
|
if (filePath->endswith(".objtxt"))
|
|
|
|
return ctx.getYAMLReader().parseFile(_buffer, _files);
|
2013-12-03 12:18:55 +08:00
|
|
|
if (identify_magic(_buffer->getBuffer()) == file_magic::archive) {
|
2013-11-21 07:51:41 +08:00
|
|
|
// Archive File
|
|
|
|
error_code ec;
|
2013-12-03 12:18:55 +08:00
|
|
|
_files.push_back(std::unique_ptr<File>(
|
|
|
|
new FileArchive(ctx, std::move(_buffer), ec, false)));
|
2013-11-21 07:51:41 +08:00
|
|
|
return ec;
|
|
|
|
}
|
2013-12-03 12:18:55 +08:00
|
|
|
return _ctx.getDefaultReader().parseFile(_buffer, _files);
|
2013-11-21 07:51:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<File &> PECOFFFileNode::getNextFile() {
|
|
|
|
if (_nextFileIndex == _files.size())
|
|
|
|
return make_error_code(InputGraphError::no_more_files);
|
|
|
|
return *_files[_nextFileIndex++];
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<StringRef> PECOFFFileNode::getPath(const LinkingContext &) const {
|
|
|
|
if (_path.endswith_lower(".lib"))
|
|
|
|
return _ctx.searchLibraryFile(_path);
|
|
|
|
if (llvm::sys::path::extension(_path).empty())
|
2013-11-21 08:17:31 +08:00
|
|
|
return _ctx.allocate(_path.str() + ".obj");
|
2013-11-21 07:51:41 +08:00
|
|
|
return _path;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<StringRef> PECOFFLibraryNode::getPath(const LinkingContext &) const {
|
2013-11-21 07:54:52 +08:00
|
|
|
if (_path.endswith_lower(".lib"))
|
|
|
|
return _ctx.searchLibraryFile(_path);
|
2013-11-21 08:17:31 +08:00
|
|
|
return _ctx.searchLibraryFile(_ctx.allocate(_path.str() + ".lib"));
|
2013-11-21 07:51:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end anonymous namespace
|