Reorder #includes
[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 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Module.h"
10 #include "llvm/Analysis/Verifier.h"
11 #include "llvm/Bytecode/Reader.h"
12 #include "llvm/Bytecode/Writer.h"
13 #include "llvm/Transforms/Utils/Linker.h"
14 #include "Support/CommandLine.h"
15 #include "Support/Signals.h"
16 #include <fstream>
17 #include <memory>
18 #include <sys/types.h>     // For FileExists
19 #include <sys/stat.h>
20
21 static cl::list<std::string>
22 InputFilenames(cl::Positional, cl::OneOrMore,
23                cl::desc("<input bytecode files>"));
24
25 static cl::opt<std::string>
26 OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
27                cl::value_desc("filename"));
28
29 static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
30
31 static cl::opt<bool>
32 Verbose("v", cl::desc("Print information about actions taken"));
33
34 static cl::opt<bool>
35 DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
36
37 static cl::list<std::string>
38 LibPaths("L", cl::desc("Specify a library search path"), cl::ZeroOrMore,
39          cl::value_desc("directory"), cl::Prefix);
40
41 // FileExists - Return true if the specified string is an openable file...
42 static inline bool FileExists(const std::string &FN) {
43   struct stat StatBuf;
44   return stat(FN.c_str(), &StatBuf) != -1;
45 }
46
47 // LoadFile - Read the specified bytecode file in and return it.  This routine
48 // searches the link path for the specified file to try to find it...
49 //
50 static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
51   std::string Filename = FN;
52   std::string ErrorMessage;
53
54   unsigned NextLibPathIdx = 0;
55   bool FoundAFile = false;
56
57   while (1) {
58     if (Verbose) std::cerr << "Loading '" << Filename << "'\n";
59     if (FileExists(Filename)) FoundAFile = true;
60     Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
61     if (Result) return std::auto_ptr<Module>(Result);   // Load successful!
62
63     if (Verbose) {
64       std::cerr << "Error opening bytecode file: '" << Filename << "'";
65       if (ErrorMessage.size()) std::cerr << ": " << ErrorMessage;
66       std::cerr << "\n";
67     }
68     
69     if (NextLibPathIdx == LibPaths.size()) break;
70     Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
71   }
72
73   if (FoundAFile)
74     std::cerr << "Bytecode file '" << FN << "' corrupt!  "
75               << "Use 'link -v ...' for more info.\n";
76   else
77     std::cerr << "Could not locate bytecode file: '" << FN << "'\n";
78   return std::auto_ptr<Module>();
79 }
80
81
82
83
84 int main(int argc, char **argv) {
85   cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
86   assert(InputFilenames.size() > 0 && "OneOrMore is not working");
87
88   unsigned BaseArg = 0;
89   std::string ErrorMessage;
90
91   std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
92   if (Composite.get() == 0) return 1;
93
94   for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
95     std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
96     if (M.get() == 0) return 1;
97
98     if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n";
99
100     if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
101       std::cerr << argv[0] << ": error linking in '" << InputFilenames[i]
102                 << "': " << ErrorMessage << "\n";
103       return 1;
104     }
105   }
106
107   if (DumpAsm) std::cerr << "Here's the assembly:\n" << Composite.get();
108
109   std::ostream *Out = &std::cout;  // Default to printing to stdout...
110   if (OutputFilename != "-") {
111     if (!Force && std::ifstream(OutputFilename.c_str())) {
112       // If force is not specified, make sure not to overwrite a file!
113       std::cerr << argv[0] << ": error opening '" << OutputFilename
114                 << "': 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       std::cerr << argv[0] << ": 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 (verifyModule(*Composite.get())) {
130     std::cerr << argv[0] << ": linked module is broken!\n";
131     return 1;
132   }
133
134   if (Verbose) std::cerr << "Writing bytecode...\n";
135   WriteBytecodeToFile(Composite.get(), *Out);
136
137   if (Out != &std::cout) delete Out;
138   return 0;
139 }