1 //===- lib/Linker/Linker.cpp - Basic Linker functionality ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains basic Linker functionality that all usages will need.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Linker.h"
15 #include "llvm/Module.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/Config/config.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/Streams.h"
22 Linker::Linker(const std::string& progname, const std::string& modname,
23 LLVMContext& C, unsigned flags):
25 Composite(new Module(modname, C)),
29 ProgramName(progname) { }
31 Linker::Linker(const std::string& progname, Module* aModule, unsigned flags) :
32 Context(aModule->getContext()),
37 ProgramName(progname) { }
44 Linker::error(const std::string& message) {
46 if (!(Flags&QuietErrors))
47 cerr << ProgramName << ": error: " << message << "\n";
52 Linker::warning(const std::string& message) {
54 if (!(Flags&QuietWarnings))
55 cerr << ProgramName << ": warning: " << message << "\n";
60 Linker::verbose(const std::string& message) {
62 cerr << " " << message << "\n";
66 Linker::addPath(const sys::Path& path) {
67 LibPaths.push_back(path);
71 Linker::addPaths(const std::vector<std::string>& paths) {
72 for (unsigned i = 0; i != paths.size(); ++i) {
75 LibPaths.push_back(aPath);
80 Linker::addSystemPaths() {
81 sys::Path::GetBitcodeLibraryPaths(LibPaths);
82 LibPaths.insert(LibPaths.begin(),sys::Path("./"));
86 Linker::releaseModule() {
87 Module* result = Composite;
95 // LoadObject - Read in and parse the bitcode file named by FN and return the
96 // module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set
97 // Error if an error occurs.
99 Linker::LoadObject(const sys::Path &FN) {
100 std::string ParseErrorMessage;
103 const std::string &FNS = FN.toString();
104 std::auto_ptr<MemoryBuffer> Buffer(MemoryBuffer::getFileOrSTDIN(FNS.c_str()));
106 Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage);
108 ParseErrorMessage = "Error reading file '" + FNS + "'";
111 return std::auto_ptr<Module>(Result);
112 Error = "Bitcode file '" + FN.toString() + "' could not be loaded";
113 if (ParseErrorMessage.size())
114 Error += ": " + ParseErrorMessage;
115 return std::auto_ptr<Module>();
118 // IsLibrary - Determine if "Name" is a library in "Directory". Return
119 // a non-empty sys::Path if its found, an empty one otherwise.
120 static inline sys::Path IsLibrary(const std::string& Name,
121 const sys::Path& Directory) {
123 sys::Path FullPath(Directory);
125 // Try the libX.a form
126 FullPath.appendComponent("lib" + Name);
127 FullPath.appendSuffix("a");
128 if (FullPath.isArchive())
131 // Try the libX.bca form
132 FullPath.eraseSuffix();
133 FullPath.appendSuffix("bca");
134 if (FullPath.isArchive())
137 // Try the libX.so (or .dylib) form
138 FullPath.eraseSuffix();
139 FullPath.appendSuffix(&(LTDL_SHLIB_EXT[1]));
140 if (FullPath.isDynamicLibrary()) // Native shared library?
142 if (FullPath.isBitcodeFile()) // .so file containing bitcode?
145 // Not found .. fall through
147 // Indicate that the library was not found in the directory.
152 /// FindLib - Try to convert Filename into the name of a file that we can open,
153 /// if it does not already name a file we can open, by first trying to open
154 /// Filename, then libFilename.[suffix] for each of a set of several common
155 /// library suffixes, in each of the directories in LibPaths. Returns an empty
156 /// Path if no matching file can be found.
159 Linker::FindLib(const std::string &Filename) {
160 // Determine if the pathname can be found as it stands.
161 sys::Path FilePath(Filename);
162 if (FilePath.canRead() &&
163 (FilePath.isArchive() || FilePath.isDynamicLibrary()))
166 // Iterate over the directories in Paths to see if we can find the library
168 for (unsigned Index = 0; Index != LibPaths.size(); ++Index) {
169 sys::Path Directory(LibPaths[Index]);
170 sys::Path FullPath = IsLibrary(Filename,Directory);
171 if (!FullPath.isEmpty())