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/ModuleProvider.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/ADT/SetOperations.h"
20 #include "llvm/Bytecode/Reader.h"
21 #include "llvm/Bytecode/Archive.h"
22 #include "llvm/Bytecode/WriteBytecodePass.h"
23 #include "llvm/Target/TargetData.h"
24 #include "llvm/Transforms/IPO.h"
25 #include "llvm/Transforms/Scalar.h"
26 #include "llvm/Config/config.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/FileUtilities.h"
29 #include "llvm/Support/Timer.h"
30 #include "llvm/System/Signals.h"
31 #include "llvm/Support/SystemUtils.h"
39 LinkOneLibrary(const char*progname, Module* HeadModule,
40 const std::string& Lib,
41 const std::vector<std::string>& LibPaths,
42 bool Verbose, bool Native) {
44 // String in which to receive error messages.
45 std::string ErrorMessage;
47 // Determine where this library lives.
48 std::string Pathname = FindLib(Lib, LibPaths);
49 if (Pathname.empty()) {
50 // If the pathname does not exist, then simply return if we're doing a
51 // native link and give a warning if we're doing a bytecode link.
53 std::cerr << progname << ": warning: Cannot find library '"
59 // A user may specify an ar archive without -l, perhaps because it
60 // is not installed as a library. Detect that and link the library.
61 if (IsArchive(Pathname)) {
63 std::cerr << "Trying to link archive '" << Pathname << "' (-l"
66 if (LinkInArchive(HeadModule, Pathname, &ErrorMessage, Verbose)) {
67 std::cerr << progname << ": " << ErrorMessage
68 << ": Error linking in archive '" << Pathname << "' (-l"
73 std::cerr << progname << ": WARNING: Supposed library -l"
74 << Lib << " isn't a library.\n";
79 // LinkItems - preserve link order for an arbitrary set of linkage items.
81 llvm::LinkItems(const char *progname, const LinkItemList& Items,
82 const std::vector<std::string>& LibPaths,
83 bool Verbose, bool Native) {
85 // Construct the HeadModule to contain the result of the linkage
86 std::auto_ptr<Module> HeadModule(new Module(progname));
88 // Construct a mutable path list we can add paths to. This list will always
89 // have LLVM_LIB_SEARCH_PATH at the end so we place it there now.
90 std::vector<std::string> MyLibPaths(LibPaths);
91 MyLibPaths.insert(MyLibPaths.begin(),".");
92 char* SearchPath = getenv("LLVM_LIB_SEARCH_PATH");
94 MyLibPaths.push_back(SearchPath);
96 // For each linkage item ...
97 for (LinkItemList::const_iterator I = Items.begin(), E = Items.end();
100 // Link in the library suggested.
101 if (LinkOneLibrary(progname,HeadModule.get(),I->first,MyLibPaths,
105 std::vector<std::string> Files;
106 Files.push_back(I->first);
107 if (LinkFiles(progname,HeadModule.get(),Files,Verbose))
112 // At this point we have processed all the link items provided to us. Since
113 // we have an aggregated module at this point, the dependent libraries in
114 // that module should also be aggregated with duplicates eliminated. This is
115 // now the time to process the dependent libraries to resolve any remaining
117 const Module::LibraryListType& DepLibs = HeadModule->getLibraries();
118 for (Module::LibraryListType::const_iterator I = DepLibs.begin(),
119 E = DepLibs.end(); I != E; ++I) {
120 if(LinkOneLibrary(progname,HeadModule.get(),*I,MyLibPaths,Verbose,Native))
124 return HeadModule.release();
127 // BuildLinkItems -- This function
128 void llvm::BuildLinkItems(
130 const cl::list<std::string>& Files,
131 const cl::list<std::string>& Libraries) {
133 // Build the list of linkage items for LinkItems.
135 cl::list<std::string>::const_iterator fileIt = Files.begin();
136 cl::list<std::string>::const_iterator libIt = Libraries.begin();
138 int libPos = -1, filePos = -1;
140 if (libIt != Libraries.end())
141 libPos = Libraries.getPosition(libIt - Libraries.begin());
144 if (fileIt != Files.end())
145 filePos = Files.getPosition(fileIt - Files.begin());
149 if (filePos != -1 && (libPos == -1 || filePos < libPos)) {
151 Items.push_back(std::make_pair(*fileIt++, false));
152 } else if (libPos != -1 && (filePos == -1 || libPos < filePos)) {
154 Items.push_back(std::make_pair(*libIt++, true));
156 break; // we're done with the list