Changed llvm_ostream et all to OStream. llvm_cerr, llvm_cout, llvm_null, are
[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 was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source 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/Bytecode/Reader.h"
17 #include "llvm/Config/config.h"
18 #include "llvm/Support/Streams.h"
19 using namespace llvm;
20
21 Linker::Linker(const std::string& progname, const std::string& modname, unsigned flags)
22   : Composite(0)
23   , LibPaths()
24   , Flags(flags)
25   , Error()
26   , ProgramName(progname)
27 {
28   Composite = new Module(modname);
29 }
30
31 Linker::Linker(const std::string& progname, Module* aModule, unsigned flags)
32   : Composite(aModule)
33   , LibPaths()
34   , Flags(flags)
35   , Error()
36   , ProgramName(progname)
37 {
38 }
39
40 Linker::~Linker() {
41   delete Composite;
42 }
43
44 bool
45 Linker::error(const std::string& message) {
46   Error = message;
47   if (!(Flags&QuietErrors))
48     cerr << ProgramName << ": error: " << message << "\n";
49   return true;
50 }
51
52 bool
53 Linker::warning(const std::string& message) {
54   Error = message;
55   if (!(Flags&QuietErrors))
56     cerr << ProgramName << ": warning: " << message << "\n";
57   return false;
58 }
59
60 void
61 Linker::verbose(const std::string& message) {
62   if (Flags&Verbose)
63     cerr << "  " << message << "\n";
64 }
65
66 void
67 Linker::addPath(const sys::Path& path) {
68   LibPaths.push_back(path);
69 }
70
71 void
72 Linker::addPaths(const std::vector<std::string>& paths) {
73   for (unsigned i = 0; i != paths.size(); ++i) {
74     sys::Path aPath;
75     aPath.set(paths[i]);
76     LibPaths.push_back(aPath);
77   }
78 }
79
80 void
81 Linker::addSystemPaths() {
82   sys::Path::GetBytecodeLibraryPaths(LibPaths);
83   LibPaths.insert(LibPaths.begin(),sys::Path("./"));
84 }
85
86 Module*
87 Linker::releaseModule() {
88   Module* result = Composite;
89   LibPaths.clear();
90   Error.clear();
91   Composite = 0;
92   Flags = 0;
93   return result;
94 }
95
96 // LoadObject - Read in and parse the bytecode file named by FN and return the
97 // module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set
98 // Error if an error occurs.
99 std::auto_ptr<Module>
100 Linker::LoadObject(const sys::Path &FN) {
101   std::string ParseErrorMessage;
102   Module *Result = ParseBytecodeFile(FN.toString(), &ParseErrorMessage);
103   if (Result)
104     return std::auto_ptr<Module>(Result);
105   Error = "Bytecode file '" + FN.toString() + "' could not be loaded";
106   if (ParseErrorMessage.size())
107     Error += ": " + ParseErrorMessage;
108   return std::auto_ptr<Module>();
109 }
110
111 // IsLibrary - Determine if "Name" is a library in "Directory". Return
112 // a non-empty sys::Path if its found, an empty one otherwise.
113 static inline sys::Path IsLibrary(const std::string& Name,
114                                   const sys::Path& Directory) {
115
116   sys::Path FullPath(Directory);
117
118   // Try the libX.a form
119   FullPath.appendComponent("lib" + Name);
120   FullPath.appendSuffix("a");
121   if (FullPath.isArchive())
122     return FullPath;
123
124   // Try the libX.bca form
125   FullPath.eraseSuffix();
126   FullPath.appendSuffix("bca");
127   if (FullPath.isArchive())
128     return FullPath;
129
130   // Try the libX.so (or .dylib) form
131   FullPath.eraseSuffix();
132   FullPath.appendSuffix(&(LTDL_SHLIB_EXT[1]));
133   if (FullPath.isDynamicLibrary())  // Native shared library?
134     return FullPath;
135   if (FullPath.isBytecodeFile())    // .so file containing bytecode?
136     return FullPath;
137
138   // Not found .. fall through
139
140   // Indicate that the library was not found in the directory.
141   FullPath.clear();
142   return FullPath;
143 }
144
145 /// FindLib - Try to convert Filename into the name of a file that we can open,
146 /// if it does not already name a file we can open, by first trying to open
147 /// Filename, then libFilename.[suffix] for each of a set of several common
148 /// library suffixes, in each of the directories in LibPaths. Returns an empty
149 /// Path if no matching file can be found.
150 ///
151 sys::Path
152 Linker::FindLib(const std::string &Filename) {
153   // Determine if the pathname can be found as it stands.
154   sys::Path FilePath(Filename);
155   if (FilePath.canRead() &&
156       (FilePath.isArchive() || FilePath.isDynamicLibrary()))
157     return FilePath;
158
159   // Iterate over the directories in Paths to see if we can find the library
160   // there.
161   for (unsigned Index = 0; Index != LibPaths.size(); ++Index) {
162     sys::Path Directory(LibPaths[Index]);
163     sys::Path FullPath = IsLibrary(Filename,Directory);
164     if (!FullPath.isEmpty())
165       return FullPath;
166   }
167   return sys::Path();
168 }