blob: 1b3dda790d32b9be90718f3e2460f0f3da271780 [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 Lattnerb74f4d02003-12-30 07:48:17 +000021#include "Support/FileUtilities.h"
Chris Lattner76d12292002-04-18 19:55:25 +000022#include "Support/Signals.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000023#include <fstream>
Chris Lattner075a0b72001-10-13 07:06:23 +000024#include <memory>
25
Brian Gaeked0fde302003-11-11 22:41:34 +000026using namespace llvm;
27
Chris Lattner5ff62e92002-07-22 02:10:13 +000028static cl::list<std::string>
29InputFilenames(cl::Positional, cl::OneOrMore,
Chris Lattner54e05af2002-07-22 02:18:09 +000030 cl::desc("<input bytecode files>"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000031
32static cl::opt<std::string>
33OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
34 cl::value_desc("filename"));
35
36static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
37
38static cl::opt<bool>
39Verbose("v", cl::desc("Print information about actions taken"));
40
41static cl::opt<bool>
42DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
43
44static cl::list<std::string>
45LibPaths("L", cl::desc("Specify a library search path"), cl::ZeroOrMore,
46 cl::value_desc("directory"), cl::Prefix);
Chris Lattner952d3652001-12-08 20:31:32 +000047
Chris Lattner952d3652001-12-08 20:31:32 +000048// LoadFile - Read the specified bytecode file in and return it. This routine
49// searches the link path for the specified file to try to find it...
50//
Chris Lattner697954c2002-01-20 22:54:45 +000051static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
52 std::string Filename = FN;
53 std::string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000054
55 unsigned NextLibPathIdx = 0;
Chris Lattner952d3652001-12-08 20:31:32 +000056 bool FoundAFile = false;
Chris Lattner65be3212001-10-24 06:23:00 +000057
58 while (1) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000059 if (Verbose) std::cerr << "Loading '" << Filename << "'\n";
Chris Lattnerb74f4d02003-12-30 07:48:17 +000060 if (getFileSize(Filename) != -1) FoundAFile = true;
Chris Lattner65be3212001-10-24 06:23:00 +000061 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
62 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
63
64 if (Verbose) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000065 std::cerr << "Error opening bytecode file: '" << Filename << "'";
66 if (ErrorMessage.size()) std::cerr << ": " << ErrorMessage;
67 std::cerr << "\n";
Chris Lattner65be3212001-10-24 06:23:00 +000068 }
69
70 if (NextLibPathIdx == LibPaths.size()) break;
71 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
72 }
73
Chris Lattner952d3652001-12-08 20:31:32 +000074 if (FoundAFile)
Chris Lattner6c8103f2003-05-22 20:13:16 +000075 std::cerr << "Bytecode file '" << FN << "' corrupt! "
Misha Brukman3ef3beb2003-09-15 18:34:34 +000076 << "Use 'llvm-link -v ...' for more info.\n";
Chris Lattner952d3652001-12-08 20:31:32 +000077 else
Chris Lattner6c8103f2003-05-22 20:13:16 +000078 std::cerr << "Could not locate bytecode file: '" << FN << "'\n";
Chris Lattner65be3212001-10-24 06:23:00 +000079 return std::auto_ptr<Module>();
80}
Chris Lattner075a0b72001-10-13 07:06:23 +000081
Chris Lattner952d3652001-12-08 20:31:32 +000082
83
84
Chris Lattner075a0b72001-10-13 07:06:23 +000085int main(int argc, char **argv) {
Chris Lattner5ff62e92002-07-22 02:10:13 +000086 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
Chris Lattner075a0b72001-10-13 07:06:23 +000087 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
88
Chris Lattner65be3212001-10-24 06:23:00 +000089 unsigned BaseArg = 0;
Chris Lattner697954c2002-01-20 22:54:45 +000090 std::string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000091
Chris Lattner65be3212001-10-24 06:23:00 +000092 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
93 if (Composite.get() == 0) return 1;
94
95 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
Chris Lattner697954c2002-01-20 22:54:45 +000096 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
Chris Lattner65be3212001-10-24 06:23:00 +000097 if (M.get() == 0) return 1;
Chris Lattnerb81adf12001-10-23 20:44:55 +000098
Chris Lattner6c8103f2003-05-22 20:13:16 +000099 if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n";
Chris Lattnerb81adf12001-10-23 20:44:55 +0000100
Chris Lattner075a0b72001-10-13 07:06:23 +0000101 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000102 std::cerr << argv[0] << ": error linking in '" << InputFilenames[i]
103 << "': " << ErrorMessage << "\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000104 return 1;
105 }
106 }
107
Chris Lattner6c8103f2003-05-22 20:13:16 +0000108 if (DumpAsm) std::cerr << "Here's the assembly:\n" << Composite.get();
Chris Lattner164cb692001-10-14 23:23:33 +0000109
Anand Shuklacf17bcc2002-06-25 21:57:48 +0000110 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner4a4daba2003-06-13 16:10:26 +0000111 if (OutputFilename != "-") {
Chris Lattner888912d2002-01-22 21:07:24 +0000112 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000113 // If force is not specified, make sure not to overwrite a file!
Chris Lattner6c8103f2003-05-22 20:13:16 +0000114 std::cerr << argv[0] << ": error opening '" << OutputFilename
115 << "': file exists!\n"
116 << "Use -f command line argument to force output\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000117 return 1;
118 }
119 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner075a0b72001-10-13 07:06:23 +0000120 if (!Out->good()) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000121 std::cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000122 return 1;
123 }
Chris Lattner76d12292002-04-18 19:55:25 +0000124
Misha Brukman452fea92003-10-10 17:56:49 +0000125 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattner76d12292002-04-18 19:55:25 +0000126 // SIGINT
127 RemoveFileOnSignal(OutputFilename);
Chris Lattner075a0b72001-10-13 07:06:23 +0000128 }
129
Chris Lattnera55c4b12003-08-28 16:25:34 +0000130 if (verifyModule(*Composite.get())) {
131 std::cerr << argv[0] << ": linked module is broken!\n";
132 return 1;
133 }
134
Chris Lattner6c8103f2003-05-22 20:13:16 +0000135 if (Verbose) std::cerr << "Writing bytecode...\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000136 WriteBytecodeToFile(Composite.get(), *Out);
137
Chris Lattner697954c2002-01-20 22:54:45 +0000138 if (Out != &std::cout) delete Out;
Chris Lattner075a0b72001-10-13 07:06:23 +0000139 return 0;
140}