1 //===- lib/Linker/Linker.cpp - Basic Linker functionality ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/Support/Path.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/Support/system_error.h"
23 Linker::Linker(StringRef progname, StringRef modname,
24 LLVMContext& C, unsigned flags):
26 Composite(new Module(modname, C)),
30 ProgramName(progname) { }
32 Linker::Linker(StringRef progname, Module* aModule, unsigned flags) :
33 Context(aModule->getContext()),
38 ProgramName(progname) { }
45 Linker::error(StringRef message) {
47 if (!(Flags&QuietErrors))
48 errs() << ProgramName << ": error: " << message << "\n";
53 Linker::warning(StringRef message) {
55 if (!(Flags&QuietWarnings))
56 errs() << ProgramName << ": warning: " << message << "\n";
61 Linker::verbose(StringRef message) {
63 errs() << " " << message << "\n";
67 Linker::addPath(const sys::Path& path) {
68 LibPaths.push_back(path);
72 Linker::addPaths(const std::vector<std::string>& paths) {
73 for (unsigned i = 0, e = paths.size(); i != e; ++i)
74 LibPaths.push_back(sys::Path(paths[i]));
78 Linker::addSystemPaths() {
79 sys::Path::GetBitcodeLibraryPaths(LibPaths);
80 LibPaths.insert(LibPaths.begin(),sys::Path("./"));
84 Linker::releaseModule() {
85 Module* result = Composite;
93 // LoadObject - Read in and parse the bitcode file named by FN and return the
94 // module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set
95 // Error if an error occurs.
97 Linker::LoadObject(const sys::Path &FN) {
98 std::string ParseErrorMessage;
101 OwningPtr<MemoryBuffer> Buffer;
102 if (error_code ec = MemoryBuffer::getFileOrSTDIN(FN.c_str(), Buffer))
103 ParseErrorMessage = "Error reading file '" + FN.str() + "'" + ": "
106 Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage);
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>();
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) {
121 sys::Path FullPath(Directory);
123 // Try the libX.a form
124 FullPath.appendComponent(("lib" + Name).str());
125 FullPath.appendSuffix("a");
126 if (FullPath.isArchive())
129 // Try the libX.bca form
130 FullPath.eraseSuffix();
131 FullPath.appendSuffix("bca");
132 if (FullPath.isArchive())
135 // Try the libX.so (or .dylib) form
136 FullPath.eraseSuffix();
137 FullPath.appendSuffix(sys::Path::GetDLLSuffix());
138 if (FullPath.isDynamicLibrary()) // Native shared library?
140 if (FullPath.isBitcodeFile()) // .so file containing bitcode?
143 // Try libX form, to make it possible to add dependency on the
144 // specific version of .so, like liblzma.so.1.0.0
145 FullPath.eraseSuffix();
146 if (FullPath.isDynamicLibrary()) // Native shared library?
148 if (FullPath.isBitcodeFile()) // .so file containing bitcode?
151 // Not found .. fall through
153 // Indicate that the library was not found in the directory.
158 /// FindLib - Try to convert Filename into the name of a file that we can open,
159 /// if it does not already name a file we can open, by first trying to open
160 /// Filename, then libFilename.[suffix] for each of a set of several common
161 /// library suffixes, in each of the directories in LibPaths. Returns an empty
162 /// Path if no matching file can be found.
165 Linker::FindLib(StringRef Filename) {
166 // Determine if the pathname can be found as it stands.
167 sys::Path FilePath(Filename);
168 if (FilePath.canRead() &&
169 (FilePath.isArchive() || FilePath.isDynamicLibrary()))
172 // Iterate over the directories in Paths to see if we can find the library
174 for (unsigned Index = 0; Index != LibPaths.size(); ++Index) {
175 sys::Path Directory(LibPaths[Index]);
176 sys::Path FullPath = IsLibrary(Filename, Directory);
177 if (!FullPath.isEmpty())