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/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"
20 #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);
33 // FileExists - Return true if the specified string is an openable file...
34 static inline bool FileExists(const std::string &FN) {
36 return stat(FN.c_str(), &StatBuf) != -1;
39 // LoadFile - Read the specified bytecode file in and return it. This routine
40 // searches the link path for the specified file to try to find it...
42 static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
43 std::string Filename = FN;
44 std::string ErrorMessage;
46 unsigned NextLibPathIdx = 0;
47 bool FoundAFile = false;
50 if (Verbose) cerr << "Loading '" << Filename << "'\n";
51 if (FileExists(Filename)) FoundAFile = true;
52 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
53 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
56 cerr << "Error opening bytecode file: '" << Filename << "'";
57 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
61 if (NextLibPathIdx == LibPaths.size()) break;
62 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
66 cerr << "Bytecode file '" << FN << "' corrupt! "
67 << "Use 'link -v ...' for more info.\n";
69 cerr << "Could not locate bytecode file: '" << FN << "'\n";
70 return std::auto_ptr<Module>();
76 int main(int argc, char **argv) {
77 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n",
78 cl::EnableSingleLetterArgValue |
79 cl::DisableSingleLetterArgGrouping);
80 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
83 std::string ErrorMessage;
85 // TODO: TEST argv[0] for llvm-ar forms... for now, this is a huge hack.
86 if (InputFilenames.size() >= 3 && InputFilenames[0] == "rc" &&
87 OutputFilename == "-") {
89 OutputFilename = InputFilenames[1];
92 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
93 if (Composite.get() == 0) return 1;
95 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
96 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
97 if (M.get() == 0) return 1;
99 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
101 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
102 cerr << "Error linking in '" << InputFilenames[i] << "': "
103 << ErrorMessage << "\n";
108 if (DumpAsm) cerr << "Here's the assembly:\n" << Composite.get();
110 std::ostream *Out = &std::cout; // Default to printing to stdout...
111 if (OutputFilename != "-") {
112 if (!Force && std::ifstream(OutputFilename.c_str())) {
113 // If force is not specified, make sure not to overwrite a file!
114 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
115 << "Use -f command line argument to force output\n";
118 Out = new std::ofstream(OutputFilename.c_str());
120 cerr << "Error opening '" << OutputFilename << "'!\n";
124 // Make sure that the Out file gets unlink'd from the disk if we get a
126 RemoveFileOnSignal(OutputFilename);
129 if (Verbose) cerr << "Writing bytecode...\n";
130 WriteBytecodeToFile(Composite.get(), *Out);
132 if (Out != &std::cout) delete Out;