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/Bytecode/Reader.h"
17 #include "llvm/Config/config.h"
18 #include "llvm/Support/Streams.h"
21 Linker::Linker(const std::string& progname, const std::string& modname, unsigned flags)
26 , ProgramName(progname)
28 Composite = new Module(modname);
31 Linker::Linker(const std::string& progname, Module* aModule, unsigned flags)
36 , ProgramName(progname)
45 Linker::error(const std::string& message) {
47 if (!(Flags&QuietErrors))
48 cerr << ProgramName << ": error: " << message << "\n";
53 Linker::warning(const std::string& message) {
55 if (!(Flags&QuietErrors))
56 cerr << ProgramName << ": warning: " << message << "\n";
61 Linker::verbose(const std::string& message) {
63 cerr << " " << 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; i != paths.size(); ++i) {
76 LibPaths.push_back(aPath);
81 Linker::addSystemPaths() {
82 sys::Path::GetBytecodeLibraryPaths(LibPaths);
83 LibPaths.insert(LibPaths.begin(),sys::Path("./"));
87 Linker::releaseModule() {
88 Module* result = Composite;
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.
100 Linker::LoadObject(const sys::Path &FN) {
101 std::string ParseErrorMessage;
102 Module *Result = ParseBytecodeFile(FN.toString(), &ParseErrorMessage);
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>();
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) {
116 sys::Path FullPath(Directory);
118 // Try the libX.a form
119 FullPath.appendComponent("lib" + Name);
120 FullPath.appendSuffix("a");
121 if (FullPath.isArchive())
124 // Try the libX.bca form
125 FullPath.eraseSuffix();
126 FullPath.appendSuffix("bca");
127 if (FullPath.isArchive())
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?
135 if (FullPath.isBytecodeFile()) // .so file containing bytecode?
138 // Not found .. fall through
140 // Indicate that the library was not found in the directory.
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.
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()))
159 // Iterate over the directories in Paths to see if we can find the library
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())