Allow globals to be of different const'nesses when we link.
[oota-llvm.git] / lib / Linker / LinkLibraries.cpp
1 //===- lib/Linker/LinkLibraries.cpp - Link LLVM libraries -----------------===//
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 //
10 // This file contains routines to handle finding libraries and linking them in. 
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Linker.h"
15 #include "llvm/Module.h"
16
17 using namespace llvm;
18
19 /// LinkInLibrary - links one library into the HeadModule
20 bool 
21 Linker::LinkInLibrary(const std::string& Lib) 
22 {
23   // Determine where this library lives.
24   sys::Path Pathname = FindLib(Lib);
25   if (Pathname.isEmpty())
26     return warning("Cannot find library '" + Lib + "'");
27
28   // If its an archive, try to link it in
29   if (Pathname.isArchive()) {
30     if (LinkInArchive(Pathname)) {
31       return error("Cannot link archive '" + Pathname.toString() + "'");
32     }
33   } else {
34     return warning("Supposed library '" + Lib + "' isn't a library.");
35   }
36   return false;
37 }
38
39 /// LinkLibraries - takes the specified library files and links them into the
40 /// main bytecode object file.
41 ///
42 /// Inputs:
43 ///  Libraries  - The list of libraries to link into the module.
44 ///
45 /// Return value:
46 ///  FALSE - No error.
47 ///  TRUE  - Error.
48 ///
49 bool 
50 Linker::LinkInLibraries(const std::vector<std::string> &Libraries) {
51
52   // Process the set of libraries we've been provided
53   for (unsigned i = 0; i < Libraries.size(); ++i) {
54     if (LinkInLibrary(Libraries[i]))
55       return true;
56   }
57
58   // At this point we have processed all the libraries provided to us. Since
59   // we have an aggregated module at this point, the dependent libraries in
60   // that module should also be aggregated with duplicates eliminated. This is
61   // now the time to process the dependent libraries to resolve any remaining
62   // symbols.
63   const Module::LibraryListType& DepLibs = Composite->getLibraries();
64   for (Module::LibraryListType::const_iterator I = DepLibs.begin(), 
65       E = DepLibs.end(); I != E; ++I) {
66     if (LinkInLibrary(*I)) 
67       return true;
68   }
69   return false;
70 }