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