1 //===- lib/Linker/Linker.cpp - Basic Linker functionality ----------------===//
3 // The LLVM Compiler Infrastructure
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.
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, unsigned flags)
27 , ProgramName(progname)
29 Composite = new Module(modname);
32 Linker::Linker(const std::string& progname, Module* aModule, unsigned flags)
37 , ProgramName(progname)
46 Linker::error(const std::string& message) {
48 if (!(Flags&QuietErrors))
49 cerr << ProgramName << ": error: " << message << "\n";
54 Linker::warning(const std::string& message) {
56 if (!(Flags&QuietErrors))
57 cerr << ProgramName << ": warning: " << message << "\n";
62 Linker::verbose(const std::string& message) {
64 cerr << " " << message << "\n";
68 Linker::addPath(const sys::Path& path) {
69 LibPaths.push_back(path);
73 Linker::addPaths(const std::vector<std::string>& paths) {
74 for (unsigned i = 0; i != paths.size(); ++i) {
77 LibPaths.push_back(aPath);
82 Linker::addSystemPaths() {
83 sys::Path::GetBitcodeLibraryPaths(LibPaths);
84 LibPaths.insert(LibPaths.begin(),sys::Path("./"));
88 Linker::releaseModule() {
89 Module* result = Composite;
97 // LoadObject - Read in and parse the bitcode 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;
105 const std::string &FNS = FN.toString();
106 std::auto_ptr<MemoryBuffer> Buffer(
107 MemoryBuffer::getFileOrSTDIN(&FNS[0], FNS.size()));
109 Result = ParseBitcodeFile(Buffer.get(), &ParseErrorMessage);
111 ParseErrorMessage = "Error reading file '" + FNS + "'";
114 return std::auto_ptr<Module>(Result);
115 Error = "Bitcode file '" + FN.toString() + "' could not be loaded";
116 if (ParseErrorMessage.size())
117 Error += ": " + ParseErrorMessage;
118 return std::auto_ptr<Module>();
121 // IsLibrary - Determine if "Name" is a library in "Directory". Return
122 // a non-empty sys::Path if its found, an empty one otherwise.
123 static inline sys::Path IsLibrary(const std::string& Name,
124 const sys::Path& Directory) {
126 sys::Path FullPath(Directory);
128 // Try the libX.a form
129 FullPath.appendComponent("lib" + Name);
130 FullPath.appendSuffix("a");
131 if (FullPath.isArchive())
134 // Try the libX.bca form
135 FullPath.eraseSuffix();
136 FullPath.appendSuffix("bca");
137 if (FullPath.isArchive())
140 // Try the libX.so (or .dylib) form
141 FullPath.eraseSuffix();
142 FullPath.appendSuffix(&(LTDL_SHLIB_EXT[1]));
143 if (FullPath.isDynamicLibrary()) // Native shared library?
145 if (FullPath.isBitcodeFile()) // .so file containing bitcode?
148 // Not found .. fall through
150 // Indicate that the library was not found in the directory.
155 /// FindLib - Try to convert Filename into the name of a file that we can open,
156 /// if it does not already name a file we can open, by first trying to open
157 /// Filename, then libFilename.[suffix] for each of a set of several common
158 /// library suffixes, in each of the directories in LibPaths. Returns an empty
159 /// Path if no matching file can be found.
162 Linker::FindLib(const std::string &Filename) {
163 // Determine if the pathname can be found as it stands.
164 sys::Path FilePath(Filename);
165 if (FilePath.canRead() &&
166 (FilePath.isArchive() || FilePath.isDynamicLibrary()))
169 // Iterate over the directories in Paths to see if we can find the library
171 for (unsigned Index = 0; Index != LibPaths.size(); ++Index) {
172 sys::Path Directory(LibPaths[Index]);
173 sys::Path FullPath = IsLibrary(Filename,Directory);
174 if (!FullPath.isEmpty())