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);
32 cl::StringList Libraries ("l", "Specify libraries to link to", cl::ZeroOrMore);
35 // FileExists - Return true if the specified string is an openable file...
36 static inline bool FileExists(const std::string &FN) {
38 return stat(FN.c_str(), &StatBuf) != -1;
41 // LoadFile - Read the specified bytecode file in and return it. This routine
42 // searches the link path for the specified file to try to find it...
44 static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
45 std::string Filename = FN;
46 std::string ErrorMessage;
48 unsigned NextLibPathIdx = 0;
49 bool FoundAFile = false;
52 if (Verbose) cerr << "Loading '" << Filename << "'\n";
53 if (FileExists(Filename)) FoundAFile = true;
54 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
55 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
58 cerr << "Error opening bytecode file: '" << Filename << "'";
59 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
63 if (NextLibPathIdx == LibPaths.size()) break;
64 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
68 cerr << "Bytecode file '" << FN << "' corrupt! "
69 << "Use 'link -v ...' for more info.\n";
71 cerr << "Could not locate bytecode file: '" << FN << "'\n";
72 return std::auto_ptr<Module>();
78 int main(int argc, char **argv) {
79 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n",
80 cl::EnableSingleLetterArgValue |
81 cl::DisableSingleLetterArgGrouping);
82 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
85 std::string ErrorMessage;
87 // TODO: TEST argv[0] for llvm-ar forms... for now, this is a huge hack.
88 if (InputFilenames.size() >= 3 && InputFilenames[0] == "rc" &&
89 OutputFilename == "-") {
91 OutputFilename = InputFilenames[1];
94 if (!Libraries.empty())
95 cerr << "LLVM Linker Warning: Linking to libraries is unimplemented!\n";
97 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
98 if (Composite.get() == 0) return 1;
100 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
101 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
102 if (M.get() == 0) return 1;
104 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
106 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
107 cerr << "Error linking in '" << InputFilenames[i] << "': "
108 << ErrorMessage << endl;
114 cerr << "Here's the assembly:\n" << Composite.get();
116 ostream *Out = &cout; // Default to printing to stdout...
117 if (OutputFilename != "-") {
118 if (!Force && std::ifstream(OutputFilename.c_str())) {
119 // If force is not specified, make sure not to overwrite a file!
120 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
121 << "Use -f command line argument to force output\n";
124 Out = new std::ofstream(OutputFilename.c_str());
126 cerr << "Error opening '" << OutputFilename << "'!\n";
131 if (Verbose) cerr << "Writing bytecode...\n";
132 WriteBytecodeToFile(Composite.get(), *Out);
134 if (Out != &std::cout) delete Out;