#include "llvm/Linker.h"
#include "llvm/Module.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/Config/config.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Streams.h"
using namespace llvm;
Linker::Linker(const std::string& progname, const std::string& modname,
unsigned flags)
: Composite(0)
, LibPaths()
, Flags(flags)
, Error()
, ProgramName(progname)
{
Composite = new Module(modname);
}
Linker::Linker(const std::string& progname, Module* aModule, unsigned flags)
: Composite(aModule)
, LibPaths()
, Flags(flags)
, Error()
, ProgramName(progname)
{
}
Linker::~Linker() {
delete Composite;
}
bool
Linker::error(const std::string& message) {
Error = message;
if (!(Flags&QuietErrors))
cerr << ProgramName << ": error: " << message << "\n";
return true;
}
bool
Linker::warning(const std::string& message) {
Error = message;
if (!(Flags&QuietWarnings))
cerr << ProgramName << ": warning: " << message << "\n";
return false;
}
void
Linker::verbose(const std::string& message) {
if (Flags&Verbose)
cerr << " " << message << "\n";
}
void
Linker::addPath(const sys::Path& path) {
LibPaths.push_back(path);
}
void
Linker::addPaths(const std::vector<std::string>& paths) {
for (unsigned i = 0; i != paths.size(); ++i) {
sys::Path aPath;
aPath.set(paths[i]);
LibPaths.push_back(aPath);
}
}
void
Linker::addSystemPaths() {
sys::Path::GetBitcodeLibraryPaths(LibPaths);
LibPaths.insert(LibPaths.begin(),sys::Path("./"));
}
Module*
Linker::releaseModule() {
Module* result = Composite;
LibPaths.clear();
Error.clear();
Composite = 0;
Flags = 0;
return result;
}
std::auto_ptr<Module>
Linker::LoadObject(const sys::Path &FN) {
std::string ParseErrorMessage;
Module *Result = 0;
const std::string &FNS = FN.toString();
std::auto_ptr<MemoryBuffer> Buffer(MemoryBuffer::getFileOrSTDIN(FNS.c_str()));
if (Buffer.get())
Result = ParseBitcodeFile(Buffer.get(), &ParseErrorMessage);
else
ParseErrorMessage = "Error reading file '" + FNS + "'";
if (Result)
return std::auto_ptr<Module>(Result);
Error = "Bitcode file '" + FN.toString() + "' could not be loaded";
if (ParseErrorMessage.size())
Error += ": " + ParseErrorMessage;
return std::auto_ptr<Module>();
}
static inline sys::Path IsLibrary(const std::string& Name,
const sys::Path& Directory) {
sys::Path FullPath(Directory);
FullPath.appendComponent("lib" + Name);
FullPath.appendSuffix("a");
if (FullPath.isArchive())
return FullPath;
FullPath.eraseSuffix();
FullPath.appendSuffix("bca");
if (FullPath.isArchive())
return FullPath;
FullPath.eraseSuffix();
FullPath.appendSuffix(&(LTDL_SHLIB_EXT[1]));
if (FullPath.isDynamicLibrary()) return FullPath;
if (FullPath.isBitcodeFile()) return FullPath;
FullPath.clear();
return FullPath;
}
sys::Path
Linker::FindLib(const std::string &Filename) {
sys::Path FilePath(Filename);
if (FilePath.canRead() &&
(FilePath.isArchive() || FilePath.isDynamicLibrary()))
return FilePath;
for (unsigned Index = 0; Index != LibPaths.size(); ++Index) {
sys::Path Directory(LibPaths[Index]);
sys::Path FullPath = IsLibrary(Filename,Directory);
if (!FullPath.isEmpty())
return FullPath;
}
return sys::Path();
}