Hold the LLVMContext by reference rather than by pointer.
[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/Config/config.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/Streams.h"
20 using namespace llvm;
21
22 Linker::Linker(const std::string& progname, const std::string& modname,
23                const LLVMContext& C, unsigned flags): 
24   Context(C),
25   Composite(new Module(modname, C)),
26   LibPaths(),
27   Flags(flags),
28   Error(),
29   ProgramName(progname) { }
30
31 Linker::Linker(const std::string& progname, Module* aModule, unsigned flags) : 
32   Context(aModule->getContext()),
33   Composite(aModule),
34   LibPaths(),
35   Flags(flags),
36   Error(),
37   ProgramName(progname) { }
38
39 Linker::~Linker() {
40   delete Composite;
41 }
42
43 bool
44 Linker::error(const std::string& message) {
45   Error = message;
46   if (!(Flags&QuietErrors))
47     cerr << ProgramName << ": error: " << message << "\n";
48   return true;
49 }
50
51 bool
52 Linker::warning(const std::string& message) {
53   Error = message;
54   if (!(Flags&QuietWarnings))
55     cerr << ProgramName << ": warning: " << message << "\n";
56   return false;
57 }
58
59 void
60 Linker::verbose(const std::string& message) {
61   if (Flags&Verbose)
62     cerr << "  " << message << "\n";
63 }
64
65 void
66 Linker::addPath(const sys::Path& path) {
67   LibPaths.push_back(path);
68 }
69
70 void
71 Linker::addPaths(const std::vector<std::string>& paths) {
72   for (unsigned i = 0; i != paths.size(); ++i) {
73     sys::Path aPath;
74     aPath.set(paths[i]);
75     LibPaths.push_back(aPath);
76   }
77 }
78
79 void
80 Linker::addSystemPaths() {
81   sys::Path::GetBitcodeLibraryPaths(LibPaths);
82   LibPaths.insert(LibPaths.begin(),sys::Path("./"));
83 }
84
85 Module*
86 Linker::releaseModule() {
87   Module* result = Composite;
88   LibPaths.clear();
89   Error.clear();
90   Composite = 0;
91   Flags = 0;
92   return result;
93 }
94
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.
98 std::auto_ptr<Module>
99 Linker::LoadObject(const sys::Path &FN) {
100   std::string ParseErrorMessage;
101   Module *Result = 0;
102   
103   const std::string &FNS = FN.toString();
104   std::auto_ptr<MemoryBuffer> Buffer(MemoryBuffer::getFileOrSTDIN(FNS.c_str()));
105   if (Buffer.get())
106     Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage);
107   else
108     ParseErrorMessage = "Error reading file '" + FNS + "'";
109     
110   if (Result)
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>();
116 }
117
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) {
122
123   sys::Path FullPath(Directory);
124
125   // Try the libX.a form
126   FullPath.appendComponent("lib" + Name);
127   FullPath.appendSuffix("a");
128   if (FullPath.isArchive())
129     return FullPath;
130
131   // Try the libX.bca form
132   FullPath.eraseSuffix();
133   FullPath.appendSuffix("bca");
134   if (FullPath.isArchive())
135     return FullPath;
136
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?
141     return FullPath;
142   if (FullPath.isBitcodeFile())    // .so file containing bitcode?
143     return FullPath;
144
145   // Not found .. fall through
146
147   // Indicate that the library was not found in the directory.
148   FullPath.clear();
149   return FullPath;
150 }
151
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.
157 ///
158 sys::Path
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()))
164     return FilePath;
165
166   // Iterate over the directories in Paths to see if we can find the library
167   // there.
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())
172       return FullPath;
173   }
174   return sys::Path();
175 }