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