Perform debug outputs with ->dump() instead of <<
[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/Module.h"
16 #include "Support/CommandLine.h"
17 #include <fstream>
18 #include <memory>
19 #include <sys/types.h>     // For FileExists
20 #include <sys/stat.h>
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 cl::StringList Libraries ("l", "Specify libraries to link to", cl::ZeroOrMore);
31
32
33 // FileExists - Return true if the specified string is an openable file...
34 static inline bool FileExists(const std::string &FN) {
35   struct stat StatBuf;
36   return stat(FN.c_str(), &StatBuf) != -1;
37 }
38
39 // LoadFile - Read the specified bytecode file in and return it.  This routine
40 // searches the link path for the specified file to try to find it...
41 //
42 static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
43   std::string Filename = FN;
44   std::string ErrorMessage;
45
46   unsigned NextLibPathIdx = 0;
47   bool FoundAFile = false;
48
49   while (1) {
50     if (Verbose) cerr << "Loading '" << Filename << "'\n";
51     if (FileExists(Filename)) FoundAFile = true;
52     Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
53     if (Result) return std::auto_ptr<Module>(Result);   // Load successful!
54
55     if (Verbose) {
56       cerr << "Error opening bytecode file: '" << Filename << "'";
57       if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
58       cerr << "\n";
59     }
60     
61     if (NextLibPathIdx == LibPaths.size()) break;
62     Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
63   }
64
65   if (FoundAFile)
66     cerr << "Bytecode file '" << FN << "' corrupt!  "
67          << "Use 'link -v ...' for more info.\n";
68   else
69     cerr << "Could not locate bytecode file: '" << FN << "'\n";
70   return std::auto_ptr<Module>();
71 }
72
73
74
75
76 int main(int argc, char **argv) {
77   cl::ParseCommandLineOptions(argc, argv, " llvm linker\n",
78                               cl::EnableSingleLetterArgValue |
79                               cl::DisableSingleLetterArgGrouping);
80   assert(InputFilenames.size() > 0 && "OneOrMore is not working");
81
82   unsigned BaseArg = 0;
83   std::string ErrorMessage;
84
85   // TODO: TEST argv[0] for llvm-ar forms... for now, this is a huge hack.
86   if (InputFilenames.size() >= 3 && InputFilenames[0] == "rc" &&
87       OutputFilename == "-") {
88     BaseArg = 2;
89     OutputFilename = InputFilenames[1];
90   }
91
92   if (!Libraries.empty())
93     cerr << "LLVM Linker Warning:  Linking to libraries is unimplemented!\n";
94
95   std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
96   if (Composite.get() == 0) return 1;
97
98   for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
99     std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
100     if (M.get() == 0) return 1;
101
102     if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
103
104     if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
105       cerr << "Error linking in '" << InputFilenames[i] << "': "
106            << ErrorMessage << "\n";
107       return 1;
108     }
109   }
110
111   if (DumpAsm) {
112     cerr << "Here's the assembly:\n";
113     Composite.get()->dump();
114   }
115
116   ostream *Out = &cout;  // Default to printing to stdout...
117   if (OutputFilename != "-") {
118     if (!Force && std::ifstream(OutputFilename.c_str())) {
119       // If force is not specified, make sure not to overwrite a file!
120       cerr << "Error opening '" << OutputFilename << "': File exists!\n"
121            << "Use -f command line argument to force output\n";
122       return 1;
123     }
124     Out = new std::ofstream(OutputFilename.c_str());
125     if (!Out->good()) {
126       cerr << "Error opening '" << OutputFilename << "'!\n";
127       return 1;
128     }
129   }
130
131   if (Verbose) cerr << "Writing bytecode...\n";
132   WriteBytecodeToFile(Composite.get(), *Out);
133
134   if (Out != &std::cout) delete Out;
135   return 0;
136 }