1 //===- llvm-link.cpp - Low-level LLVM linker ------------------------------===//
3 // This utility may be invoked in the following manner:
4 // llvm-link a.bc b.bc c.bc -o x.bc
6 //===----------------------------------------------------------------------===//
8 #include "llvm/Module.h"
9 #include "llvm/Analysis/Verifier.h"
10 #include "llvm/Bytecode/Reader.h"
11 #include "llvm/Bytecode/Writer.h"
12 #include "llvm/Transforms/Utils/Linker.h"
13 #include "Support/CommandLine.h"
14 #include "Support/Signals.h"
17 #include <sys/types.h> // For FileExists
20 static cl::list<std::string>
21 InputFilenames(cl::Positional, cl::OneOrMore,
22 cl::desc("<input bytecode files>"));
24 static cl::opt<std::string>
25 OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
26 cl::value_desc("filename"));
28 static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
31 Verbose("v", cl::desc("Print information about actions taken"));
34 DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
36 static cl::list<std::string>
37 LibPaths("L", cl::desc("Specify a library search path"), cl::ZeroOrMore,
38 cl::value_desc("directory"), cl::Prefix);
40 // FileExists - Return true if the specified string is an openable file...
41 static inline bool FileExists(const std::string &FN) {
43 return stat(FN.c_str(), &StatBuf) != -1;
46 // LoadFile - Read the specified bytecode file in and return it. This routine
47 // searches the link path for the specified file to try to find it...
49 static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
50 std::string Filename = FN;
51 std::string ErrorMessage;
53 unsigned NextLibPathIdx = 0;
54 bool FoundAFile = false;
57 if (Verbose) std::cerr << "Loading '" << Filename << "'\n";
58 if (FileExists(Filename)) FoundAFile = true;
59 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
60 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
63 std::cerr << "Error opening bytecode file: '" << Filename << "'";
64 if (ErrorMessage.size()) std::cerr << ": " << ErrorMessage;
68 if (NextLibPathIdx == LibPaths.size()) break;
69 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
73 std::cerr << "Bytecode file '" << FN << "' corrupt! "
74 << "Use 'llvm-link -v ...' for more info.\n";
76 std::cerr << "Could not locate bytecode file: '" << FN << "'\n";
77 return std::auto_ptr<Module>();
83 int main(int argc, char **argv) {
84 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
85 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
88 std::string ErrorMessage;
90 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
91 if (Composite.get() == 0) return 1;
93 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
94 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
95 if (M.get() == 0) return 1;
97 if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n";
99 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
100 std::cerr << argv[0] << ": error linking in '" << InputFilenames[i]
101 << "': " << ErrorMessage << "\n";
106 if (DumpAsm) std::cerr << "Here's the assembly:\n" << Composite.get();
108 std::ostream *Out = &std::cout; // Default to printing to stdout...
109 if (OutputFilename != "-") {
110 if (!Force && std::ifstream(OutputFilename.c_str())) {
111 // If force is not specified, make sure not to overwrite a file!
112 std::cerr << argv[0] << ": error opening '" << OutputFilename
113 << "': file exists!\n"
114 << "Use -f command line argument to force output\n";
117 Out = new std::ofstream(OutputFilename.c_str());
119 std::cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
123 // Make sure that the Out file gets unlink'd from the disk if we get a
125 RemoveFileOnSignal(OutputFilename);
128 if (verifyModule(*Composite.get())) {
129 std::cerr << argv[0] << ": linked module is broken!\n";
133 if (Verbose) std::cerr << "Writing bytecode...\n";
134 WriteBytecodeToFile(Composite.get(), *Out);
136 if (Out != &std::cout) delete Out;