bdd04685ce3a7bdc24737f98a1c94e6ae9e2910f
[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 #include <sys/stat.h>
23
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 cl::StringList Libraries ("l", "Specify libraries to link to", 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   if (!Libraries.empty())
95     cerr << "LLVM Linker Warning:  Linking to libraries is unimplemented!\n";
96
97   std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
98   if (Composite.get() == 0) return 1;
99
100   for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
101     std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
102     if (M.get() == 0) return 1;
103
104     if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
105
106     if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
107       cerr << "Error linking in '" << InputFilenames[i] << "': "
108            << ErrorMessage << endl;
109       return 1;
110     }
111   }
112
113   if (DumpAsm)
114     cerr << "Here's the assembly:\n" << Composite.get();
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 }