blob: a07ecc6d71daf2b60d5bb87bee71fa72acced8d4 [file] [log] [blame]
Reid Spencer903f21d2004-12-13 03:50:50 +00001//===- lib/Linker/Linker.cpp - Basic Linker functionality ----------------===//
Reid Spencerde4cedc2004-12-13 03:00:28 +00002//
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//
Reid Spencer903f21d2004-12-13 03:50:50 +000010// This file contains basic Linker functionality that all usages will need.
Reid Spencerde4cedc2004-12-13 03:00:28 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Linker.h"
15#include "llvm/Module.h"
16#include "llvm/Bytecode/Reader.h"
17#include <iostream>
18
19using namespace llvm;
20
21Linker::Linker(const std::string& progname, unsigned flags)
22 : Composite(0)
23 , LibPaths()
24 , Flags(flags)
25 , Error()
26 , ProgramName(progname)
27{
28 Composite = new Module(progname);
29}
30
31Linker::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
40Linker::~Linker() {
41 delete Composite;
42}
43
44bool
45Linker::error(const std::string& message) {
46 Error = message;
47 if (!(Flags&QuietErrors)) {
48 std::cerr << ProgramName << ": error: " << message << "\n";
49 }
50 return true;
51}
52
53bool
54Linker::warning(const std::string& message) {
55 Error = message;
56 if (!(Flags&QuietErrors)) {
57 std::cerr << ProgramName << ": warning: " << message << "\n";
58 }
59 return false;
60}
61
62void
63Linker::verbose(const std::string& message) {
64 if (Flags&Verbose) {
65 std::cerr << " " << message << "\n";
66 }
67}
68
69void
70Linker::addPath(const sys::Path& path) {
71 assert(path.isDirectory() && "Can only insert directories into the path");
72 LibPaths.push_back(path);
73}
74
75void
76Linker::addPaths(const std::vector<std::string>& paths) {
Reid Spencer903f21d2004-12-13 03:50:50 +000077 for (unsigned i = 0; i != paths.size(); ++i) {
Reid Spencerde4cedc2004-12-13 03:00:28 +000078 sys::Path aPath;
79 aPath.setDirectory(paths[i]);
80 LibPaths.push_back(aPath);
81 }
82}
83
84void
85Linker::addSystemPaths() {
86 sys::Path::GetBytecodeLibraryPaths(LibPaths);
87 LibPaths.insert(LibPaths.begin(),sys::Path("./"));
88}
89
90Module*
91Linker::releaseModule() {
92 Module* result = Composite;
Reid Spencerde4cedc2004-12-13 03:00:28 +000093 LibPaths.clear();
94 Error.clear();
Reid Spencer903f21d2004-12-13 03:50:50 +000095 Composite = 0;
Reid Spencerde4cedc2004-12-13 03:00:28 +000096 Flags = 0;
97 return result;
98}
99
100// LoadObject - Read in and parse the bytecode file named by FN and return the
101// module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set
102// Error if an error occurs.
103std::auto_ptr<Module>
104Linker::LoadObject(const sys::Path &FN) {
105 std::string ParseErrorMessage;
106 Module *Result = ParseBytecodeFile(FN.toString(), &ParseErrorMessage);
107 if (Result)
108 return std::auto_ptr<Module>(Result);
109 Error = "Bytecode file '" + FN.toString() + "' could not be loaded";
110 if (ParseErrorMessage.size())
111 Error += ": " + ParseErrorMessage;
112 return std::auto_ptr<Module>();
113}
114
Reid Spencer903f21d2004-12-13 03:50:50 +0000115// IsLibrary - Determine if "Name" is a library in "Directory". Return
116// a non-empty sys::Path if its found, an empty one otherwise.
Reid Spencerde4cedc2004-12-13 03:00:28 +0000117static inline sys::Path IsLibrary(const std::string& Name,
118 const sys::Path& Directory) {
119
120 assert(Directory.isDirectory() && "Need to specify a directory");
121 sys::Path FullPath(Directory);
122 FullPath.appendFile("lib" + Name);
123
124 FullPath.appendSuffix("a");
125 if (FullPath.isArchive())
126 return FullPath;
127
128 FullPath.elideSuffix();
129 FullPath.appendSuffix("bca");
130 if (FullPath.isArchive())
131 return FullPath;
132
133 FullPath.elideSuffix();
134 FullPath.appendSuffix(&(LTDL_SHLIB_EXT[1]));
135 if (FullPath.isDynamicLibrary())
136 return FullPath;
137
138 FullPath.clear();
139 return FullPath;
140}
141
142/// FindLib - Try to convert Filename into the name of a file that we can open,
143/// if it does not already name a file we can open, by first trying to open
144/// Filename, then libFilename.[suffix] for each of a set of several common
Reid Spencer903f21d2004-12-13 03:50:50 +0000145/// library suffixes, in each of the directories in LibPaths. Returns an empty
146/// Path if no matching file can be found.
Reid Spencerde4cedc2004-12-13 03:00:28 +0000147///
148sys::Path
149Linker::FindLib(const std::string &Filename)
150{
151 // Determine if the pathname can be found as it stands.
152 sys::Path FilePath(Filename);
153 if (FilePath.readable() &&
154 (FilePath.isArchive() || FilePath.isDynamicLibrary()))
155 return FilePath;
156
157 // Iterate over the directories in Paths to see if we can find the library
158 // there.
159 for (unsigned Index = 0; Index != LibPaths.size(); ++Index) {
160 sys::Path Directory(LibPaths[Index]);
161 sys::Path FullPath = IsLibrary(Filename,Directory);
162 if (!FullPath.isEmpty())
163 return FullPath;
164 }
165 return sys::Path();
166}