1 //===----------------------------------------------------------------------===//
4 // This utility may be invoked in the following manner:
5 // link a.bc b.bc c.bc -o x.bc
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.
10 //===----------------------------------------------------------------------===//
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/Support/CommandLine.h"
17 #include "llvm/Module.h"
18 #include "llvm/Method.h"
23 cl::StringList InputFilenames("", "Load <arg> files, linking them together",
25 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "-");
26 cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
27 cl::Flag Verbose ("v", "Print information about actions taken");
28 cl::Flag DumpAsm ("d", "Print assembly as linked", cl::Hidden, false);
29 cl::StringList LibPaths ("L", "Specify a library search path", cl::ZeroOrMore);
31 static inline std::auto_ptr<Module> LoadFile(const string &FN) {
35 unsigned NextLibPathIdx = 0;
38 if (Verbose) cerr << "Loading '" << Filename << "'\n";
39 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
40 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
43 cerr << "Error opening bytecode file: '" << Filename << "'";
44 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
48 if (NextLibPathIdx == LibPaths.size()) break;
49 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
52 cerr << "Could not locate bytecode file: '" << FN << "'\n";
53 return std::auto_ptr<Module>();
56 int main(int argc, char **argv) {
57 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n",
58 cl::EnableSingleLetterArgValue |
59 cl::DisableSingleLetterArgGrouping);
60 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
65 // TODO: TEST argv[0] for llvm-ar forms... for now, this is a huge hack.
66 if (InputFilenames.size() >= 3 && InputFilenames[0] == "rc" &&
67 OutputFilename == "-") {
69 OutputFilename = InputFilenames[1];
72 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
73 if (Composite.get() == 0) return 1;
75 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
76 auto_ptr<Module> M(LoadFile(InputFilenames[i]));
77 if (M.get() == 0) return 1;
79 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
81 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
82 cerr << "Error linking in '" << InputFilenames[i] << "': "
83 << ErrorMessage << endl;
89 cerr << "Here's the assembly:\n" << Composite.get();
91 ostream *Out = &cout; // Default to printing to stdout...
92 if (OutputFilename != "-") {
93 Out = new ofstream(OutputFilename.c_str(),
94 (Force ? 0 : ios::noreplace)|ios::out);
96 cerr << "Error opening '" << OutputFilename << "'!\n";
101 if (Verbose) cerr << "Writing bytecode...\n";
102 WriteBytecodeToFile(Composite.get(), *Out);
104 if (Out != &cout) delete Out;