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 static cl::list<std::string>
26 InputFilenames(cl::Positional, cl::OneOrMore,
27 cl::desc("<input bytecode files>"));
29 static cl::opt<std::string>
30 OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
31 cl::value_desc("filename"));
33 static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
36 Verbose("v", cl::desc("Print information about actions taken"));
39 DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
41 static cl::list<std::string>
42 LibPaths("L", cl::desc("Specify a library search path"), cl::ZeroOrMore,
43 cl::value_desc("directory"), cl::Prefix);
45 // FileExists - Return true if the specified string is an openable file...
46 static inline bool FileExists(const std::string &FN) {
48 return stat(FN.c_str(), &StatBuf) != -1;
51 // LoadFile - Read the specified bytecode file in and return it. This routine
52 // searches the link path for the specified file to try to find it...
54 static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
55 std::string Filename = FN;
56 std::string ErrorMessage;
58 unsigned NextLibPathIdx = 0;
59 bool FoundAFile = false;
62 if (Verbose) cerr << "Loading '" << Filename << "'\n";
63 if (FileExists(Filename)) FoundAFile = true;
64 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
65 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
68 cerr << "Error opening bytecode file: '" << Filename << "'";
69 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
73 if (NextLibPathIdx == LibPaths.size()) break;
74 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
78 cerr << "Bytecode file '" << FN << "' corrupt! "
79 << "Use 'link -v ...' for more info.\n";
81 cerr << "Could not locate bytecode file: '" << FN << "'\n";
82 return std::auto_ptr<Module>();
88 int main(int argc, char **argv) {
89 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
90 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
93 std::string ErrorMessage;
95 // TODO: TEST argv[0] for llvm-ar forms... for now, this is a huge hack.
96 if (InputFilenames.size() >= 3 && InputFilenames[0] == "rc" &&
97 OutputFilename == "-") {
99 OutputFilename = InputFilenames[1];
102 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
103 if (Composite.get() == 0) return 1;
105 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
106 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
107 if (M.get() == 0) return 1;
109 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
111 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
112 cerr << argv[0] << ": error linking in '" << InputFilenames[i] << "': "
113 << ErrorMessage << "\n";
118 if (DumpAsm) cerr << "Here's the assembly:\n" << Composite.get();
120 std::ostream *Out = &std::cout; // Default to printing to stdout...
121 if (OutputFilename != "-") {
122 if (!Force && std::ifstream(OutputFilename.c_str())) {
123 // If force is not specified, make sure not to overwrite a file!
124 cerr << argv[0] << ": error opening '" << OutputFilename
125 << "': file exists!\n"
126 << "Use -f command line argument to force output\n";
129 Out = new std::ofstream(OutputFilename.c_str());
131 cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
135 // Make sure that the Out file gets unlink'd from the disk if we get a
137 RemoveFileOnSignal(OutputFilename);
140 if (Verbose) cerr << "Writing bytecode...\n";
141 WriteBytecodeToFile(Composite.get(), *Out);
143 if (Out != &std::cout) delete Out;