Clean up anands patch
[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/Utils/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 "Support/Signals.h"
18 #include <fstream>
19 #include <memory>
20 #include <sys/types.h>     // For FileExists
21 #include <sys/stat.h>
22
23 using std::cerr;
24
25 cl::StringList InputFilenames("", "Load <arg> files, linking them together", 
26                               cl::OneOrMore);
27 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "-");
28 cl::Flag   Force         ("f", "Overwrite output files", cl::NoFlags, false);
29 cl::Flag   Verbose       ("v", "Print information about actions taken");
30 cl::Flag   DumpAsm       ("d", "Print assembly as linked", cl::Hidden, false);
31 cl::StringList LibPaths  ("L", "Specify a library search path", cl::ZeroOrMore);
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   std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
93   if (Composite.get() == 0) return 1;
94
95   for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
96     std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
97     if (M.get() == 0) return 1;
98
99     if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
100
101     if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
102       cerr << "Error linking in '" << InputFilenames[i] << "': "
103            << ErrorMessage << "\n";
104       return 1;
105     }
106   }
107
108   if (DumpAsm) cerr << "Here's the assembly:\n" << Composite.get();
109
110   std::ostream *Out = &std::cout;  // Default to printing to stdout...
111   if (OutputFilename != "-") {
112     if (!Force && std::ifstream(OutputFilename.c_str())) {
113       // If force is not specified, make sure not to overwrite a file!
114       cerr << "Error opening '" << OutputFilename << "': File exists!\n"
115            << "Use -f command line argument to force output\n";
116       return 1;
117     }
118     Out = new std::ofstream(OutputFilename.c_str());
119     if (!Out->good()) {
120       cerr << "Error opening '" << OutputFilename << "'!\n";
121       return 1;
122     }
123
124     // Make sure that the Out file gets unlink'd from the disk if we get a
125     // SIGINT
126     RemoveFileOnSignal(OutputFilename);
127   }
128
129   if (Verbose) cerr << "Writing bytecode...\n";
130   WriteBytecodeToFile(Composite.get(), *Out);
131
132   if (Out != &std::cout) delete Out;
133   return 0;
134 }