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"
22 Linker::Linker(const std::string& progname, unsigned flags)
27 , ProgramName(progname)
29 Composite = new Module(progname);
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 std::cerr << ProgramName << ": error: " << message << "\n";
55 Linker::warning(const std::string& message) {
57 if (!(Flags&QuietErrors)) {
58 std::cerr << ProgramName << ": warning: " << message << "\n";
64 Linker::verbose(const std::string& message) {
66 std::cerr << " " << message << "\n";
71 Linker::addPath(const sys::Path& path) {
72 assert(path.isDirectory() && "Can only insert directories into the path");
73 LibPaths.push_back(path);
77 Linker::addPaths(const std::vector<std::string>& paths) {
78 for (unsigned i = 0; i != paths.size(); ++i) {
80 aPath.setDirectory(paths[i]);
81 LibPaths.push_back(aPath);
86 Linker::addSystemPaths() {
87 sys::Path::GetBytecodeLibraryPaths(LibPaths);
88 LibPaths.insert(LibPaths.begin(),sys::Path("./"));
92 Linker::releaseModule() {
93 Module* result = Composite;
101 // LoadObject - Read in and parse the bytecode file named by FN and return the
102 // module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set
103 // Error if an error occurs.
104 std::auto_ptr<Module>
105 Linker::LoadObject(const sys::Path &FN) {
106 std::string ParseErrorMessage;
107 Module *Result = ParseBytecodeFile(FN.toString(), &ParseErrorMessage);
109 return std::auto_ptr<Module>(Result);
110 Error = "Bytecode file '" + FN.toString() + "' 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(const std::string& Name,
119 const sys::Path& Directory) {
121 assert(Directory.isDirectory() && "Need to specify a directory");
122 sys::Path FullPath(Directory);
123 FullPath.appendFile("lib" + Name);
125 FullPath.appendSuffix("a");
126 if (FullPath.isArchive())
129 FullPath.elideSuffix();
130 FullPath.appendSuffix("bca");
131 if (FullPath.isArchive())
134 FullPath.elideSuffix();
135 FullPath.appendSuffix(&(LTDL_SHLIB_EXT[1]));
136 if (FullPath.isDynamicLibrary())
143 /// FindLib - Try to convert Filename into the name of a file that we can open,
144 /// if it does not already name a file we can open, by first trying to open
145 /// Filename, then libFilename.[suffix] for each of a set of several common
146 /// library suffixes, in each of the directories in LibPaths. Returns an empty
147 /// Path if no matching file can be found.
150 Linker::FindLib(const std::string &Filename)
152 // Determine if the pathname can be found as it stands.
153 sys::Path FilePath(Filename);
154 if (FilePath.readable() &&
155 (FilePath.isArchive() || FilePath.isDynamicLibrary()))
158 // Iterate over the directories in Paths to see if we can find the library
160 for (unsigned Index = 0; Index != LibPaths.size(); ++Index) {
161 sys::Path Directory(LibPaths[Index]);
162 sys::Path FullPath = IsLibrary(Filename,Directory);
163 if (!FullPath.isEmpty())