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/Module.h"
17 #include "llvm/Method.h"
18 #include "Support/CommandLine.h"
21 #include <sys/types.h> // For FileExists
25 cl::StringList InputFilenames("", "Load <arg> files, linking them together",
27 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "-");
28 cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
29 cl::Flag Verbose ("v", "Print information about actions taken");
30 cl::Flag DumpAsm ("d", "Print assembly as linked", cl::Hidden, false);
31 cl::StringList LibPaths ("L", "Specify a library search path", cl::ZeroOrMore);
34 // FileExists - Return true if the specified string is an openable file...
35 static inline bool FileExists(const string &FN) {
37 return stat(FN.c_str(), &StatBuf) != -1;
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...
43 static inline std::auto_ptr<Module> LoadFile(const string &FN) {
47 unsigned NextLibPathIdx = 0;
48 bool FoundAFile = false;
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!
57 cerr << "Error opening bytecode file: '" << Filename << "'";
58 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
62 if (NextLibPathIdx == LibPaths.size()) break;
63 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
67 cerr << "Bytecode file '" << FN << "' corrupt! "
68 << "Use 'link -v ...' for more info.\n";
70 cerr << "Could not locate bytecode file: '" << FN << "'\n";
71 return std::auto_ptr<Module>();
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");
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 == "-") {
90 OutputFilename = InputFilenames[1];
93 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
94 if (Composite.get() == 0) return 1;
96 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
97 auto_ptr<Module> M(LoadFile(InputFilenames[i]));
98 if (M.get() == 0) return 1;
100 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
102 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
103 cerr << "Error linking in '" << InputFilenames[i] << "': "
104 << ErrorMessage << endl;
110 cerr << "Here's the assembly:\n" << Composite.get();
112 ostream *Out = &cout; // Default to printing to stdout...
113 if (OutputFilename != "-") {
114 Out = new ofstream(OutputFilename.c_str(),
115 (Force ? 0 : ios::noreplace)|ios::out);
117 cerr << "Error opening '" << OutputFilename << "'!\n";
122 if (Verbose) cerr << "Writing bytecode...\n";
123 WriteBytecodeToFile(Composite.get(), *Out);
125 if (Out != &cout) delete Out;