blob: 7eba82d13f11826bca8570321d7b56f962fda5c0 [file] [log] [blame]
Reid Spencer3ece6392004-12-13 02:59:41 +00001//===- lib/Linker/LinkArchives.cpp - Link LLVM objects and libraries ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains routines to handle linking together LLVM bytecode files,
11// and to handle annoying things like static libraries.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Linker.h"
16#include "llvm/Module.h"
17//#include "llvm/Bytecode/Archive.h"
18
19using namespace llvm;
20
21/// LinkInFile - opens a bytecode file and links in all objects which
22/// provide symbols that are currently undefined.
23///
24/// Inputs:
25/// HeadModule - The module in which to link the bytecode file.
26/// Filename - The pathname of the bytecode file.
27/// Verbose - Flags whether verbose messages should be printed.
28///
29/// Outputs:
30/// ErrorMessage - A C++ string detailing what error occurred, if any.
31///
32/// Return Value:
33/// TRUE - An error occurred.
34/// FALSE - No errors.
35///
36bool
37Linker::LinkInFile(const sys::Path &File)
38{
39 // Make sure we can at least read the file
40 if (!File.readable())
41 return error("Cannot find linker input '" + File.toString() + "'");
42
43 // A user may specify an ar archive without -l, perhaps because it
44 // is not installed as a library. Detect that and link the library.
45 if (File.isArchive()) {
46 if (LinkInArchive(File))
47 return error("Cannot link archive '" + File.toString() + "'");
48 } else if (File.isBytecodeFile()) {
49 verbose("Linking bytecode file '" + File.toString() + "'");
50
51 std::auto_ptr<Module> M(LoadObject(File));
52 if (M.get() == 0)
53 return error("Cannot load file '" + File.toString() + "'" + Error);
54 if (LinkInModule(M.get()))
55 return error("Cannot link file '" + File.toString() + "'" + Error);
56
57 verbose("Linked in file '" + File.toString() + "'");
58 } else {
59 return warning("File of unknown type '" + File.toString() + "' ignored.");
60 }
61 return false;
62}
63
64/// LinkFiles - takes a module and a list of files and links them all together.
65/// It locates the file either in the current directory, as its absolute
66/// or relative pathname, or as a file somewhere in LLVM_LIB_SEARCH_PATH.
67///
68/// Inputs:
69/// Files - A vector of C++ strings indicating the LLVM bytecode filenames
70/// to be linked. The names can refer to a mixture of pure LLVM
71/// bytecode files and archive (ar) formatted files.
72///
73/// Outputs:
74/// HeadModule - The module will have the specified LLVM bytecode files linked
75/// in.
76///
77/// Return value:
78/// FALSE - No errors.
79/// TRUE - Some error occurred.
80///
81bool
82Linker::LinkInFiles(const std::vector<sys::Path> &Files)
83{
84 for (unsigned i = 0; i < Files.size(); ++i) {
85 if (LinkInFile(Files[i]))
86 return true;
87 }
88 return false;
89}