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