blob: 764b59b43f85174fbb411de7fcf27d115ca1d0d9 [file] [log] [blame]
Chris Lattnerbb37a692003-09-20 02:42:54 +00001//===- llvm-link.cpp - Low-level LLVM linker ------------------------------===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner075a0b72001-10-13 07:06:23 +00009//
10// This utility may be invoked in the following manner:
Misha Brukman3ef3beb2003-09-15 18:34:34 +000011// llvm-link a.bc b.bc c.bc -o x.bc
Chris Lattner075a0b72001-10-13 07:06:23 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattnera55c4b12003-08-28 16:25:34 +000015#include "llvm/Module.h"
16#include "llvm/Analysis/Verifier.h"
Chris Lattner075a0b72001-10-13 07:06:23 +000017#include "llvm/Bytecode/Reader.h"
18#include "llvm/Bytecode/Writer.h"
Chris Lattnera55c4b12003-08-28 16:25:34 +000019#include "llvm/Transforms/Utils/Linker.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000020#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000021#include "Support/Signals.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000022#include <fstream>
Chris Lattner075a0b72001-10-13 07:06:23 +000023#include <memory>
Chris Lattner952d3652001-12-08 20:31:32 +000024#include <sys/types.h> // For FileExists
25#include <sys/stat.h>
Chris Lattner075a0b72001-10-13 07:06:23 +000026
Chris Lattner5ff62e92002-07-22 02:10:13 +000027static cl::list<std::string>
28InputFilenames(cl::Positional, cl::OneOrMore,
Chris Lattner54e05af2002-07-22 02:18:09 +000029 cl::desc("<input bytecode files>"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000030
31static cl::opt<std::string>
32OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
33 cl::value_desc("filename"));
34
35static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
36
37static cl::opt<bool>
38Verbose("v", cl::desc("Print information about actions taken"));
39
40static cl::opt<bool>
41DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
42
43static cl::list<std::string>
44LibPaths("L", cl::desc("Specify a library search path"), cl::ZeroOrMore,
45 cl::value_desc("directory"), cl::Prefix);
Chris Lattner952d3652001-12-08 20:31:32 +000046
47// FileExists - Return true if the specified string is an openable file...
Chris Lattner697954c2002-01-20 22:54:45 +000048static inline bool FileExists(const std::string &FN) {
Chris Lattner952d3652001-12-08 20:31:32 +000049 struct stat StatBuf;
50 return stat(FN.c_str(), &StatBuf) != -1;
51}
52
53// LoadFile - Read the specified bytecode file in and return it. This routine
54// searches the link path for the specified file to try to find it...
55//
Chris Lattner697954c2002-01-20 22:54:45 +000056static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
57 std::string Filename = FN;
58 std::string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000059
60 unsigned NextLibPathIdx = 0;
Chris Lattner952d3652001-12-08 20:31:32 +000061 bool FoundAFile = false;
Chris Lattner65be3212001-10-24 06:23:00 +000062
63 while (1) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000064 if (Verbose) std::cerr << "Loading '" << Filename << "'\n";
Chris Lattner952d3652001-12-08 20:31:32 +000065 if (FileExists(Filename)) FoundAFile = true;
Chris Lattner65be3212001-10-24 06:23:00 +000066 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
67 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
68
69 if (Verbose) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000070 std::cerr << "Error opening bytecode file: '" << Filename << "'";
71 if (ErrorMessage.size()) std::cerr << ": " << ErrorMessage;
72 std::cerr << "\n";
Chris Lattner65be3212001-10-24 06:23:00 +000073 }
74
75 if (NextLibPathIdx == LibPaths.size()) break;
76 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
77 }
78
Chris Lattner952d3652001-12-08 20:31:32 +000079 if (FoundAFile)
Chris Lattner6c8103f2003-05-22 20:13:16 +000080 std::cerr << "Bytecode file '" << FN << "' corrupt! "
Misha Brukman3ef3beb2003-09-15 18:34:34 +000081 << "Use 'llvm-link -v ...' for more info.\n";
Chris Lattner952d3652001-12-08 20:31:32 +000082 else
Chris Lattner6c8103f2003-05-22 20:13:16 +000083 std::cerr << "Could not locate bytecode file: '" << FN << "'\n";
Chris Lattner65be3212001-10-24 06:23:00 +000084 return std::auto_ptr<Module>();
85}
Chris Lattner075a0b72001-10-13 07:06:23 +000086
Chris Lattner952d3652001-12-08 20:31:32 +000087
88
89
Chris Lattner075a0b72001-10-13 07:06:23 +000090int main(int argc, char **argv) {
Chris Lattner5ff62e92002-07-22 02:10:13 +000091 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
Chris Lattner075a0b72001-10-13 07:06:23 +000092 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
93
Chris Lattner65be3212001-10-24 06:23:00 +000094 unsigned BaseArg = 0;
Chris Lattner697954c2002-01-20 22:54:45 +000095 std::string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000096
Chris Lattner65be3212001-10-24 06:23:00 +000097 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
98 if (Composite.get() == 0) return 1;
99
100 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
Chris Lattner697954c2002-01-20 22:54:45 +0000101 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
Chris Lattner65be3212001-10-24 06:23:00 +0000102 if (M.get() == 0) return 1;
Chris Lattnerb81adf12001-10-23 20:44:55 +0000103
Chris Lattner6c8103f2003-05-22 20:13:16 +0000104 if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n";
Chris Lattnerb81adf12001-10-23 20:44:55 +0000105
Chris Lattner075a0b72001-10-13 07:06:23 +0000106 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000107 std::cerr << argv[0] << ": error linking in '" << InputFilenames[i]
108 << "': " << ErrorMessage << "\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000109 return 1;
110 }
111 }
112
Chris Lattner6c8103f2003-05-22 20:13:16 +0000113 if (DumpAsm) std::cerr << "Here's the assembly:\n" << Composite.get();
Chris Lattner164cb692001-10-14 23:23:33 +0000114
Anand Shuklacf17bcc2002-06-25 21:57:48 +0000115 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner4a4daba2003-06-13 16:10:26 +0000116 if (OutputFilename != "-") {
Chris Lattner888912d2002-01-22 21:07:24 +0000117 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000118 // If force is not specified, make sure not to overwrite a file!
Chris Lattner6c8103f2003-05-22 20:13:16 +0000119 std::cerr << argv[0] << ": error opening '" << OutputFilename
120 << "': file exists!\n"
121 << "Use -f command line argument to force output\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000122 return 1;
123 }
124 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner075a0b72001-10-13 07:06:23 +0000125 if (!Out->good()) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000126 std::cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000127 return 1;
128 }
Chris Lattner76d12292002-04-18 19:55:25 +0000129
Misha Brukman452fea92003-10-10 17:56:49 +0000130 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattner76d12292002-04-18 19:55:25 +0000131 // SIGINT
132 RemoveFileOnSignal(OutputFilename);
Chris Lattner075a0b72001-10-13 07:06:23 +0000133 }
134
Chris Lattnera55c4b12003-08-28 16:25:34 +0000135 if (verifyModule(*Composite.get())) {
136 std::cerr << argv[0] << ": linked module is broken!\n";
137 return 1;
138 }
139
Chris Lattner6c8103f2003-05-22 20:13:16 +0000140 if (Verbose) std::cerr << "Writing bytecode...\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000141 WriteBytecodeToFile(Composite.get(), *Out);
142
Chris Lattner697954c2002-01-20 22:54:45 +0000143 if (Out != &std::cout) delete Out;
Chris Lattner075a0b72001-10-13 07:06:23 +0000144 return 0;
145}