Change errs() to dbgs().
[oota-llvm.git] / lib / Linker / Linker.cpp
1 //===- lib/Linker/Linker.cpp - Basic Linker functionality  ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains basic Linker functionality that all usages will need.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Linker.h"
15 #include "llvm/Module.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/System/Path.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Config/config.h"
22 using namespace llvm;
23
24 Linker::Linker(StringRef progname, StringRef modname,
25                LLVMContext& C, unsigned flags):
26   Context(C),
27   Composite(new Module(modname, C)),
28   LibPaths(),
29   Flags(flags),
30   Error(),
31   ProgramName(progname) { }
32
33 Linker::Linker(StringRef progname, Module* aModule, unsigned flags) :
34   Context(aModule->getContext()),
35   Composite(aModule),
36   LibPaths(),
37   Flags(flags),
38   Error(),
39   ProgramName(progname) { }
40
41 Linker::~Linker() {
42   delete Composite;
43 }
44
45 bool
46 Linker::error(StringRef message) {
47   Error = message;
48   if (!(Flags&QuietErrors))
49     errs() << ProgramName << ": error: " << message << "\n";
50   return true;
51 }
52
53 bool
54 Linker::warning(StringRef message) {
55   Error = message;
56   if (!(Flags&QuietWarnings))
57     errs() << ProgramName << ": warning: " << message << "\n";
58   return false;
59 }
60
61 void
62 Linker::verbose(StringRef message) {
63   if (Flags&Verbose)
64     dbgs() << "  " << message << "\n";
65 }
66
67 void
68 Linker::addPath(const sys::Path& path) {
69   LibPaths.push_back(path);
70 }
71
72 void
73 Linker::addPaths(const std::vector<std::string>& paths) {
74   for (unsigned i = 0, e = paths.size(); i != e; ++i)
75     LibPaths.push_back(sys::Path(paths[i]));
76 }
77
78 void
79 Linker::addSystemPaths() {
80   sys::Path::GetBitcodeLibraryPaths(LibPaths);
81   LibPaths.insert(LibPaths.begin(),sys::Path("./"));
82 }
83
84 Module*
85 Linker::releaseModule() {
86   Module* result = Composite;
87   LibPaths.clear();
88   Error.clear();
89   Composite = 0;
90   Flags = 0;
91   return result;
92 }
93
94 // LoadObject - Read in and parse the bitcode file named by FN and return the
95 // module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set
96 // Error if an error occurs.
97 std::auto_ptr<Module>
98 Linker::LoadObject(const sys::Path &FN) {
99   std::string ParseErrorMessage;
100   Module *Result = 0;
101   
102   std::auto_ptr<MemoryBuffer> Buffer(MemoryBuffer::getFileOrSTDIN(FN.c_str()));
103   if (Buffer.get())
104     Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage);
105   else
106     ParseErrorMessage = "Error reading file '" + FN.str() + "'";
107     
108   if (Result)
109     return std::auto_ptr<Module>(Result);
110   Error = "Bitcode file '" + FN.str() + "' could not be loaded";
111   if (ParseErrorMessage.size())
112     Error += ": " + ParseErrorMessage;
113   return std::auto_ptr<Module>();
114 }
115
116 // IsLibrary - Determine if "Name" is a library in "Directory". Return
117 // a non-empty sys::Path if its found, an empty one otherwise.
118 static inline sys::Path IsLibrary(StringRef Name,
119                                   const sys::Path &Directory) {
120
121   sys::Path FullPath(Directory);
122
123   // Try the libX.a form
124   FullPath.appendComponent(("lib" + Name).str());
125   FullPath.appendSuffix("a");
126   if (FullPath.isArchive())
127     return FullPath;
128
129   // Try the libX.bca form
130   FullPath.eraseSuffix();
131   FullPath.appendSuffix("bca");
132   if (FullPath.isArchive())
133     return FullPath;
134
135   // Try the libX.so (or .dylib) form
136   FullPath.eraseSuffix();
137   FullPath.appendSuffix(&(LTDL_SHLIB_EXT[1]));
138   if (FullPath.isDynamicLibrary())  // Native shared library?
139     return FullPath;
140   if (FullPath.isBitcodeFile())    // .so file containing bitcode?
141     return FullPath;
142
143   // Not found .. fall through
144
145   // Indicate that the library was not found in the directory.
146   FullPath.clear();
147   return FullPath;
148 }
149
150 /// FindLib - Try to convert Filename into the name of a file that we can open,
151 /// if it does not already name a file we can open, by first trying to open
152 /// Filename, then libFilename.[suffix] for each of a set of several common
153 /// library suffixes, in each of the directories in LibPaths. Returns an empty
154 /// Path if no matching file can be found.
155 ///
156 sys::Path
157 Linker::FindLib(StringRef Filename) {
158   // Determine if the pathname can be found as it stands.
159   sys::Path FilePath(Filename);
160   if (FilePath.canRead() &&
161       (FilePath.isArchive() || FilePath.isDynamicLibrary()))
162     return FilePath;
163
164   // Iterate over the directories in Paths to see if we can find the library
165   // there.
166   for (unsigned Index = 0; Index != LibPaths.size(); ++Index) {
167     sys::Path Directory(LibPaths[Index]);
168     sys::Path FullPath = IsLibrary(Filename, Directory);
169     if (!FullPath.isEmpty())
170       return FullPath;
171   }
172   return sys::Path();
173 }