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
23 static cl::list<std::string>
24 InputFilenames(cl::Positional, cl::OneOrMore,
25 cl::desc("<input bytecode files>"));
27 static cl::opt<std::string>
28 OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
29 cl::value_desc("filename"));
31 static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
34 Verbose("v", cl::desc("Print information about actions taken"));
37 DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
39 static cl::list<std::string>
40 LibPaths("L", cl::desc("Specify a library search path"), cl::ZeroOrMore,
41 cl::value_desc("directory"), cl::Prefix);
43 // FileExists - Return true if the specified string is an openable file...
44 static inline bool FileExists(const std::string &FN) {
46 return stat(FN.c_str(), &StatBuf) != -1;
49 // LoadFile - Read the specified bytecode file in and return it. This routine
50 // searches the link path for the specified file to try to find it...
52 static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
53 std::string Filename = FN;
54 std::string ErrorMessage;
56 unsigned NextLibPathIdx = 0;
57 bool FoundAFile = false;
60 if (Verbose) std::cerr << "Loading '" << Filename << "'\n";
61 if (FileExists(Filename)) FoundAFile = true;
62 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
63 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
66 std::cerr << "Error opening bytecode file: '" << Filename << "'";
67 if (ErrorMessage.size()) std::cerr << ": " << ErrorMessage;
71 if (NextLibPathIdx == LibPaths.size()) break;
72 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
76 std::cerr << "Bytecode file '" << FN << "' corrupt! "
77 << "Use 'link -v ...' for more info.\n";
79 std::cerr << "Could not locate bytecode file: '" << FN << "'\n";
80 return std::auto_ptr<Module>();
86 int main(int argc, char **argv) {
87 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
88 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
91 std::string ErrorMessage;
93 // TODO: TEST argv[0] for llvm-ar forms... for now, this is a huge hack.
94 if (InputFilenames.size() >= 3 && InputFilenames[0] == "rc" &&
95 OutputFilename == "-") {
97 OutputFilename = InputFilenames[1];
100 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
101 if (Composite.get() == 0) return 1;
103 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
104 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
105 if (M.get() == 0) return 1;
107 if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n";
109 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
110 std::cerr << argv[0] << ": error linking in '" << InputFilenames[i]
111 << "': " << ErrorMessage << "\n";
116 if (DumpAsm) std::cerr << "Here's the assembly:\n" << Composite.get();
118 std::ostream *Out = &std::cout; // Default to printing to stdout...
119 if (OutputFilename != "-") {
120 if (!Force && std::ifstream(OutputFilename.c_str())) {
121 // If force is not specified, make sure not to overwrite a file!
122 std::cerr << argv[0] << ": error opening '" << OutputFilename
123 << "': file exists!\n"
124 << "Use -f command line argument to force output\n";
127 Out = new std::ofstream(OutputFilename.c_str());
129 std::cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
133 // Make sure that the Out file gets unlink'd from the disk if we get a
135 RemoveFileOnSignal(OutputFilename);
138 if (Verbose) std::cerr << "Writing bytecode...\n";
139 WriteBytecodeToFile(Composite.get(), *Out);
141 if (Out != &std::cout) delete Out;