Reid Spencer | 903f21d | 2004-12-13 03:50:50 +0000 | [diff] [blame] | 1 | //===- lib/Linker/Linker.cpp - Basic Linker functionality ----------------===// |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 2 | // |
| 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 Spencer | 903f21d | 2004-12-13 03:50:50 +0000 | [diff] [blame] | 10 | // This file contains basic Linker functionality that all usages will need. |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Linker.h" |
| 15 | #include "llvm/Module.h" |
| 16 | #include "llvm/Bytecode/Reader.h" |
Reid Spencer | a834f5d | 2004-12-16 19:19:24 +0000 | [diff] [blame^] | 17 | #include "llvm/Config/config.h" |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 18 | #include <iostream> |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | Linker::Linker(const std::string& progname, unsigned flags) |
| 23 | : Composite(0) |
| 24 | , LibPaths() |
| 25 | , Flags(flags) |
| 26 | , Error() |
| 27 | , ProgramName(progname) |
| 28 | { |
| 29 | Composite = new Module(progname); |
| 30 | } |
| 31 | |
| 32 | Linker::Linker(const std::string& progname, Module* aModule, unsigned flags) |
| 33 | : Composite(aModule) |
| 34 | , LibPaths() |
| 35 | , Flags(flags) |
| 36 | , Error() |
| 37 | , ProgramName(progname) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | Linker::~Linker() { |
| 42 | delete Composite; |
| 43 | } |
| 44 | |
| 45 | bool |
| 46 | Linker::error(const std::string& message) { |
| 47 | Error = message; |
| 48 | if (!(Flags&QuietErrors)) { |
| 49 | std::cerr << ProgramName << ": error: " << message << "\n"; |
| 50 | } |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | bool |
| 55 | Linker::warning(const std::string& message) { |
| 56 | Error = message; |
| 57 | if (!(Flags&QuietErrors)) { |
| 58 | std::cerr << ProgramName << ": warning: " << message << "\n"; |
| 59 | } |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | Linker::verbose(const std::string& message) { |
| 65 | if (Flags&Verbose) { |
| 66 | std::cerr << " " << message << "\n"; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | Linker::addPath(const sys::Path& path) { |
| 72 | assert(path.isDirectory() && "Can only insert directories into the path"); |
| 73 | LibPaths.push_back(path); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | Linker::addPaths(const std::vector<std::string>& paths) { |
Reid Spencer | 903f21d | 2004-12-13 03:50:50 +0000 | [diff] [blame] | 78 | for (unsigned i = 0; i != paths.size(); ++i) { |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 79 | sys::Path aPath; |
| 80 | aPath.setDirectory(paths[i]); |
| 81 | LibPaths.push_back(aPath); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | Linker::addSystemPaths() { |
| 87 | sys::Path::GetBytecodeLibraryPaths(LibPaths); |
| 88 | LibPaths.insert(LibPaths.begin(),sys::Path("./")); |
| 89 | } |
| 90 | |
| 91 | Module* |
| 92 | Linker::releaseModule() { |
| 93 | Module* result = Composite; |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 94 | LibPaths.clear(); |
| 95 | Error.clear(); |
Reid Spencer | 903f21d | 2004-12-13 03:50:50 +0000 | [diff] [blame] | 96 | Composite = 0; |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 97 | Flags = 0; |
| 98 | return result; |
| 99 | } |
| 100 | |
| 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); |
| 108 | if (Result) |
| 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>(); |
| 114 | } |
| 115 | |
Reid Spencer | 903f21d | 2004-12-13 03:50:50 +0000 | [diff] [blame] | 116 | // IsLibrary - Determine if "Name" is a library in "Directory". Return |
| 117 | // a non-empty sys::Path if its found, an empty one otherwise. |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 118 | static inline sys::Path IsLibrary(const std::string& Name, |
| 119 | const sys::Path& Directory) { |
| 120 | |
| 121 | assert(Directory.isDirectory() && "Need to specify a directory"); |
| 122 | sys::Path FullPath(Directory); |
| 123 | FullPath.appendFile("lib" + Name); |
| 124 | |
| 125 | FullPath.appendSuffix("a"); |
| 126 | if (FullPath.isArchive()) |
| 127 | return FullPath; |
| 128 | |
| 129 | FullPath.elideSuffix(); |
| 130 | FullPath.appendSuffix("bca"); |
| 131 | if (FullPath.isArchive()) |
| 132 | return FullPath; |
| 133 | |
| 134 | FullPath.elideSuffix(); |
| 135 | FullPath.appendSuffix(&(LTDL_SHLIB_EXT[1])); |
| 136 | if (FullPath.isDynamicLibrary()) |
| 137 | return FullPath; |
| 138 | |
| 139 | FullPath.clear(); |
| 140 | return FullPath; |
| 141 | } |
| 142 | |
| 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 |
Reid Spencer | 903f21d | 2004-12-13 03:50:50 +0000 | [diff] [blame] | 146 | /// library suffixes, in each of the directories in LibPaths. Returns an empty |
| 147 | /// Path if no matching file can be found. |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 148 | /// |
| 149 | sys::Path |
| 150 | Linker::FindLib(const std::string &Filename) |
| 151 | { |
| 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())) |
| 156 | return FilePath; |
| 157 | |
| 158 | // Iterate over the directories in Paths to see if we can find the library |
| 159 | // there. |
| 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()) |
| 164 | return FullPath; |
| 165 | } |
| 166 | return sys::Path(); |
| 167 | } |