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