Drop something that link will never support, use gccld instead
[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
32 // FileExists - Return true if the specified string is an openable file...
33 static inline bool FileExists(const std::string &FN) {
34   struct stat StatBuf;
35   return stat(FN.c_str(), &StatBuf) != -1;
36 }
37
38 // LoadFile - Read the specified bytecode file in and return it.  This routine
39 // searches the link path for the specified file to try to find it...
40 //
41 static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
42   std::string Filename = FN;
43   std::string ErrorMessage;
44
45   unsigned NextLibPathIdx = 0;
46   bool FoundAFile = false;
47
48   while (1) {
49     if (Verbose) cerr << "Loading '" << Filename << "'\n";
50     if (FileExists(Filename)) FoundAFile = true;
51     Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
52     if (Result) return std::auto_ptr<Module>(Result);   // Load successful!
53
54     if (Verbose) {
55       cerr << "Error opening bytecode file: '" << Filename << "'";
56       if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
57       cerr << "\n";
58     }
59     
60     if (NextLibPathIdx == LibPaths.size()) break;
61     Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
62   }
63
64   if (FoundAFile)
65     cerr << "Bytecode file '" << FN << "' corrupt!  "
66          << "Use 'link -v ...' for more info.\n";
67   else
68     cerr << "Could not locate bytecode file: '" << FN << "'\n";
69   return std::auto_ptr<Module>();
70 }
71
72
73
74
75 int main(int argc, char **argv) {
76   cl::ParseCommandLineOptions(argc, argv, " llvm linker\n",
77                               cl::EnableSingleLetterArgValue |
78                               cl::DisableSingleLetterArgGrouping);
79   assert(InputFilenames.size() > 0 && "OneOrMore is not working");
80
81   unsigned BaseArg = 0;
82   std::string ErrorMessage;
83
84   // TODO: TEST argv[0] for llvm-ar forms... for now, this is a huge hack.
85   if (InputFilenames.size() >= 3 && InputFilenames[0] == "rc" &&
86       OutputFilename == "-") {
87     BaseArg = 2;
88     OutputFilename = InputFilenames[1];
89   }
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) cerr << "Linking in '" << InputFilenames[i] << "'\n";
99
100     if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
101       cerr << "Error linking in '" << InputFilenames[i] << "': "
102            << ErrorMessage << "\n";
103       return 1;
104     }
105   }
106
107   if (DumpAsm) cerr << "Here's the assembly:\n" << Composite.get();
108
109   ostream *Out = &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       cerr << "Error opening '" << OutputFilename << "': File exists!\n"
114            << "Use -f command line argument to force output\n";
115       return 1;
116     }
117     Out = new std::ofstream(OutputFilename.c_str());
118     if (!Out->good()) {
119       cerr << "Error opening '" << OutputFilename << "'!\n";
120       return 1;
121     }
122
123     // Make sure that the Out file gets unlink'd from the disk if we get a
124     // SIGINT
125     RemoveFileOnSignal(OutputFilename);
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 }