1 //===- lib/Linker/LinkItems.cpp - Link LLVM objects and libraries ---------===//
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 routines to handle linking together LLVM bytecode files,
11 // and to handle annoying things like static libraries.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Linker.h"
16 #include "llvm/Module.h"
17 #include "llvm/Bytecode/Reader.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;
94 Composite = new Module(ProgramName);
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 static inline sys::Path IsLibrary(const std::string& Name,
117 const sys::Path& Directory) {
119 assert(Directory.isDirectory() && "Need to specify a directory");
120 sys::Path FullPath(Directory);
121 FullPath.appendFile("lib" + Name);
123 FullPath.appendSuffix("a");
124 if (FullPath.isArchive())
127 FullPath.elideSuffix();
128 FullPath.appendSuffix("bca");
129 if (FullPath.isArchive())
132 FullPath.elideSuffix();
133 FullPath.appendSuffix(&(LTDL_SHLIB_EXT[1]));
134 if (FullPath.isDynamicLibrary())
141 /// FindLib - Try to convert Filename into the name of a file that we can open,
142 /// if it does not already name a file we can open, by first trying to open
143 /// Filename, then libFilename.[suffix] for each of a set of several common
144 /// library suffixes, in each of the directories in Paths and the directory
145 /// named by the value of the environment variable LLVM_LIB_SEARCH_PATH. Returns
146 /// an empty string if no matching file can be found.
149 Linker::FindLib(const std::string &Filename)
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()))
157 // Iterate over the directories in Paths to see if we can find the library
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())