blob: 1196e42642afc7f9f7eba3c3a6993b46682d089d [file] [log] [blame]
Chris Lattner9d810e02001-10-13 07:06:23 +00001//===----------------------------------------------------------------------===//
2// LLVM 'LINK' UTILITY
3//
4// This utility may be invoked in the following manner:
5// link a.bc b.bc c.bc -o x.bc
6//
Chris Lattnerebaa7882001-10-23 20:44:55 +00007// Alternatively, this can be used as an 'ar' tool as well. If invoked as
Chris Lattnerae31f5b2001-10-24 06:23:00 +00008// either 'ar' or 'llvm-ar', it accepts a 'rc' parameter as well.
Chris Lattnerebaa7882001-10-23 20:44:55 +00009//
Chris Lattner9d810e02001-10-13 07:06:23 +000010//===----------------------------------------------------------------------===//
11
Chris Lattner7608a462002-05-07 18:36:35 +000012#include "llvm/Transforms/Utils/Linker.h"
Chris Lattner9d810e02001-10-13 07:06:23 +000013#include "llvm/Bytecode/Reader.h"
14#include "llvm/Bytecode/Writer.h"
Chris Lattner9d810e02001-10-13 07:06:23 +000015#include "llvm/Module.h"
Chris Lattner5de22042001-11-27 00:03:19 +000016#include "Support/CommandLine.h"
Chris Lattnerc065ad82002-04-18 19:55:25 +000017#include "Support/Signals.h"
Chris Lattner5de22042001-11-27 00:03:19 +000018#include <fstream>
Chris Lattner9d810e02001-10-13 07:06:23 +000019#include <memory>
Chris Lattner5053ba92001-12-08 20:31:32 +000020#include <sys/types.h> // For FileExists
21#include <sys/stat.h>
Chris Lattner9d810e02001-10-13 07:06:23 +000022
Chris Lattnerf5cad152002-07-22 02:10:13 +000023static cl::list<std::string>
24InputFilenames(cl::Positional, cl::OneOrMore,
Chris Lattner17570e12002-07-22 02:18:09 +000025 cl::desc("<input bytecode files>"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000026
27static cl::opt<std::string>
28OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
29 cl::value_desc("filename"));
30
31static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
32
33static cl::opt<bool>
34Verbose("v", cl::desc("Print information about actions taken"));
35
36static cl::opt<bool>
37DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
38
39static cl::list<std::string>
40LibPaths("L", cl::desc("Specify a library search path"), cl::ZeroOrMore,
41 cl::value_desc("directory"), cl::Prefix);
Chris Lattner5053ba92001-12-08 20:31:32 +000042
43// FileExists - Return true if the specified string is an openable file...
Chris Lattner7f74a562002-01-20 22:54:45 +000044static inline bool FileExists(const std::string &FN) {
Chris Lattner5053ba92001-12-08 20:31:32 +000045 struct stat StatBuf;
46 return stat(FN.c_str(), &StatBuf) != -1;
47}
48
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...
51//
Chris Lattner7f74a562002-01-20 22:54:45 +000052static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
53 std::string Filename = FN;
54 std::string ErrorMessage;
Chris Lattnerae31f5b2001-10-24 06:23:00 +000055
56 unsigned NextLibPathIdx = 0;
Chris Lattner5053ba92001-12-08 20:31:32 +000057 bool FoundAFile = false;
Chris Lattnerae31f5b2001-10-24 06:23:00 +000058
59 while (1) {
Chris Lattner02a16832003-05-22 20:13:16 +000060 if (Verbose) std::cerr << "Loading '" << Filename << "'\n";
Chris Lattner5053ba92001-12-08 20:31:32 +000061 if (FileExists(Filename)) FoundAFile = true;
Chris Lattnerae31f5b2001-10-24 06:23:00 +000062 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
63 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
64
65 if (Verbose) {
Chris Lattner02a16832003-05-22 20:13:16 +000066 std::cerr << "Error opening bytecode file: '" << Filename << "'";
67 if (ErrorMessage.size()) std::cerr << ": " << ErrorMessage;
68 std::cerr << "\n";
Chris Lattnerae31f5b2001-10-24 06:23:00 +000069 }
70
71 if (NextLibPathIdx == LibPaths.size()) break;
72 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
73 }
74
Chris Lattner5053ba92001-12-08 20:31:32 +000075 if (FoundAFile)
Chris Lattner02a16832003-05-22 20:13:16 +000076 std::cerr << "Bytecode file '" << FN << "' corrupt! "
77 << "Use 'link -v ...' for more info.\n";
Chris Lattner5053ba92001-12-08 20:31:32 +000078 else
Chris Lattner02a16832003-05-22 20:13:16 +000079 std::cerr << "Could not locate bytecode file: '" << FN << "'\n";
Chris Lattnerae31f5b2001-10-24 06:23:00 +000080 return std::auto_ptr<Module>();
81}
Chris Lattner9d810e02001-10-13 07:06:23 +000082
Chris Lattner5053ba92001-12-08 20:31:32 +000083
84
85
Chris Lattner9d810e02001-10-13 07:06:23 +000086int main(int argc, char **argv) {
Chris Lattnerf5cad152002-07-22 02:10:13 +000087 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
Chris Lattner9d810e02001-10-13 07:06:23 +000088 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
89
Chris Lattnerae31f5b2001-10-24 06:23:00 +000090 unsigned BaseArg = 0;
Chris Lattner7f74a562002-01-20 22:54:45 +000091 std::string ErrorMessage;
Chris Lattnerae31f5b2001-10-24 06:23:00 +000092
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 == "-") {
96 BaseArg = 2;
97 OutputFilename = InputFilenames[1];
Chris Lattner9d810e02001-10-13 07:06:23 +000098 }
99
Chris Lattnerae31f5b2001-10-24 06:23:00 +0000100 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
101 if (Composite.get() == 0) return 1;
102
103 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000104 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
Chris Lattnerae31f5b2001-10-24 06:23:00 +0000105 if (M.get() == 0) return 1;
Chris Lattnerebaa7882001-10-23 20:44:55 +0000106
Chris Lattner02a16832003-05-22 20:13:16 +0000107 if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n";
Chris Lattnerebaa7882001-10-23 20:44:55 +0000108
Chris Lattner9d810e02001-10-13 07:06:23 +0000109 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
Chris Lattner02a16832003-05-22 20:13:16 +0000110 std::cerr << argv[0] << ": error linking in '" << InputFilenames[i]
111 << "': " << ErrorMessage << "\n";
Chris Lattner9d810e02001-10-13 07:06:23 +0000112 return 1;
113 }
114 }
115
Chris Lattner02a16832003-05-22 20:13:16 +0000116 if (DumpAsm) std::cerr << "Here's the assembly:\n" << Composite.get();
Chris Lattnerd6311652001-10-14 23:23:33 +0000117
Anand Shuklafef32412002-06-25 21:57:48 +0000118 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner9d810e02001-10-13 07:06:23 +0000119 if (OutputFilename != "-") {
Chris Lattner0e11e542002-01-22 21:07:24 +0000120 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000121 // If force is not specified, make sure not to overwrite a file!
Chris Lattner02a16832003-05-22 20:13:16 +0000122 std::cerr << argv[0] << ": error opening '" << OutputFilename
123 << "': file exists!\n"
124 << "Use -f command line argument to force output\n";
Chris Lattner7f74a562002-01-20 22:54:45 +0000125 return 1;
126 }
127 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner9d810e02001-10-13 07:06:23 +0000128 if (!Out->good()) {
Chris Lattner02a16832003-05-22 20:13:16 +0000129 std::cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
Chris Lattner9d810e02001-10-13 07:06:23 +0000130 return 1;
131 }
Chris Lattnerc065ad82002-04-18 19:55:25 +0000132
133 // Make sure that the Out file gets unlink'd from the disk if we get a
134 // SIGINT
135 RemoveFileOnSignal(OutputFilename);
Chris Lattner9d810e02001-10-13 07:06:23 +0000136 }
137
Chris Lattner02a16832003-05-22 20:13:16 +0000138 if (Verbose) std::cerr << "Writing bytecode...\n";
Chris Lattner9d810e02001-10-13 07:06:23 +0000139 WriteBytecodeToFile(Composite.get(), *Out);
140
Chris Lattner7f74a562002-01-20 22:54:45 +0000141 if (Out != &std::cout) delete Out;
Chris Lattner9d810e02001-10-13 07:06:23 +0000142 return 0;
143}