Add support to enable -lfoo to be processed correctly
[oota-llvm.git] / tools / llvm-link / llvm-link.cpp
1 //===----------------------------------------------------------------------===//
2 // LLVM 'LINK' UTILITY 
3 //
4 // This utility may be invoked in the following manner:
5 //  link a.bc b.bc c.bc -o x.bc
6 //
7 // Alternatively, this can be used as an 'ar' tool as well.  If invoked as
8 // either 'ar' or 'llvm-ar', it accepts a 'rc' parameter as well.
9 //
10 //===----------------------------------------------------------------------===//
11
12 #include "llvm/Transforms/Linker.h"
13 #include "llvm/Bytecode/Reader.h"
14 #include "llvm/Bytecode/Writer.h"
15 #include "llvm/Assembly/Writer.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Module.h"
18 #include "llvm/Method.h"
19 #include <fstream.h>
20 #include <memory>
21
22
23 cl::StringList InputFilenames("", "Load <arg> files, linking them together", 
24                               cl::OneOrMore);
25 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "-");
26 cl::Flag   Force         ("f", "Overwrite output files", cl::NoFlags, false);
27 cl::Flag   Verbose       ("v", "Print information about actions taken");
28 cl::Flag   DumpAsm       ("d", "Print assembly as linked", cl::Hidden, false);
29 cl::StringList LibPaths  ("L", "Specify a library search path", cl::ZeroOrMore);
30
31 static inline std::auto_ptr<Module> LoadFile(const string &FN) {
32   string Filename = FN;
33   string ErrorMessage;
34
35   unsigned NextLibPathIdx = 0;
36
37   while (1) {
38     if (Verbose) cerr << "Loading '" << Filename << "'\n";
39     Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
40     if (Result) return std::auto_ptr<Module>(Result);   // Load successful!
41
42     if (Verbose) {
43       cerr << "Error opening bytecode file: '" << Filename << "'";
44       if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
45       cerr << endl;
46     }
47     
48     if (NextLibPathIdx == LibPaths.size()) break;
49     Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
50   }
51
52   cerr << "Could not locate bytecode file: '" << FN << "'\n";
53   return std::auto_ptr<Module>();
54 }
55
56 int main(int argc, char **argv) {
57   cl::ParseCommandLineOptions(argc, argv, " llvm linker\n",
58                               cl::EnableSingleLetterArgValue |
59                               cl::DisableSingleLetterArgGrouping);
60   assert(InputFilenames.size() > 0 && "OneOrMore is not working");
61
62   unsigned BaseArg = 0;
63   string ErrorMessage;
64
65   // TODO: TEST argv[0] for llvm-ar forms... for now, this is a huge hack.
66   if (InputFilenames.size() >= 3 && InputFilenames[0] == "rc" &&
67       OutputFilename == "-") {
68     BaseArg = 2;
69     OutputFilename = InputFilenames[1];
70   }
71
72   std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
73   if (Composite.get() == 0) return 1;
74
75   for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
76     auto_ptr<Module> M(LoadFile(InputFilenames[i]));
77     if (M.get() == 0) return 1;
78
79     if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
80
81     if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
82       cerr << "Error linking in '" << InputFilenames[i] << "': "
83            << ErrorMessage << endl;
84       return 1;
85     }
86   }
87
88   if (DumpAsm)
89     cerr << "Here's the assembly:\n" << Composite.get();
90
91   ostream *Out = &cout;  // Default to printing to stdout...
92   if (OutputFilename != "-") {
93     Out = new ofstream(OutputFilename.c_str(), 
94                        (Force ? 0 : ios::noreplace)|ios::out);
95     if (!Out->good()) {
96       cerr << "Error opening '" << OutputFilename << "'!\n";
97       return 1;
98     }
99   }
100
101   if (Verbose) cerr << "Writing bytecode...\n";
102   WriteBytecodeToFile(Composite.get(), *Out);
103
104   if (Out != &cout) delete Out;
105   return 0;
106 }