1 //===----------------------------------------------------------------------===//
4 // This utility may be invoked in the following manner:
5 // llvm-link a.bc b.bc c.bc -o x.bc
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Module.h"
10 #include "llvm/Analysis/Verifier.h"
11 #include "llvm/Bytecode/Reader.h"
12 #include "llvm/Bytecode/Writer.h"
13 #include "llvm/Transforms/Utils/Linker.h"
14 #include "Support/CommandLine.h"
15 #include "Support/Signals.h"
18 #include <sys/types.h> // For FileExists
21 static cl::list<std::string>
22 InputFilenames(cl::Positional, cl::OneOrMore,
23 cl::desc("<input bytecode files>"));
25 static cl::opt<std::string>
26 OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
27 cl::value_desc("filename"));
29 static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
32 Verbose("v", cl::desc("Print information about actions taken"));
35 DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
37 static cl::list<std::string>
38 LibPaths("L", cl::desc("Specify a library search path"), cl::ZeroOrMore,
39 cl::value_desc("directory"), cl::Prefix);
41 // FileExists - Return true if the specified string is an openable file...
42 static inline bool FileExists(const std::string &FN) {
44 return stat(FN.c_str(), &StatBuf) != -1;
47 // LoadFile - Read the specified bytecode file in and return it. This routine
48 // searches the link path for the specified file to try to find it...
50 static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
51 std::string Filename = FN;
52 std::string ErrorMessage;
54 unsigned NextLibPathIdx = 0;
55 bool FoundAFile = false;
58 if (Verbose) std::cerr << "Loading '" << Filename << "'\n";
59 if (FileExists(Filename)) FoundAFile = true;
60 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
61 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
64 std::cerr << "Error opening bytecode file: '" << Filename << "'";
65 if (ErrorMessage.size()) std::cerr << ": " << ErrorMessage;
69 if (NextLibPathIdx == LibPaths.size()) break;
70 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
74 std::cerr << "Bytecode file '" << FN << "' corrupt! "
75 << "Use 'llvm-link -v ...' for more info.\n";
77 std::cerr << "Could not locate bytecode file: '" << FN << "'\n";
78 return std::auto_ptr<Module>();
84 int main(int argc, char **argv) {
85 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
86 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
89 std::string ErrorMessage;
91 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
92 if (Composite.get() == 0) return 1;
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;
98 if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n";
100 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
101 std::cerr << argv[0] << ": error linking in '" << InputFilenames[i]
102 << "': " << ErrorMessage << "\n";
107 if (DumpAsm) std::cerr << "Here's the assembly:\n" << Composite.get();
109 std::ostream *Out = &std::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 std::cerr << argv[0] << ": error opening '" << OutputFilename
114 << "': file exists!\n"
115 << "Use -f command line argument to force output\n";
118 Out = new std::ofstream(OutputFilename.c_str());
120 std::cerr << argv[0] << ": 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 (verifyModule(*Composite.get())) {
130 std::cerr << argv[0] << ": linked module is broken!\n";
134 if (Verbose) std::cerr << "Writing bytecode...\n";
135 WriteBytecodeToFile(Composite.get(), *Out);
137 if (Out != &std::cout) delete Out;