blob: 99973a22eb9666f08f758fbc40a2f07beac8aefa [file] [log] [blame]
Eugene Zelenkof95cab82017-09-07 23:28:24 +00001//===- llvm-cat.cpp - LLVM module concatenation utility -------------------===//
Peter Collingbournee6480e22016-11-29 20:43:47 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This program is for testing features that rely on multi-module bitcode files.
11// It takes a list of input modules and uses them to create a multi-module
12// bitcode file.
13//
14//===----------------------------------------------------------------------===//
15
Eugene Zelenkof95cab82017-09-07 23:28:24 +000016#include "llvm/ADT/SmallVector.h"
Peter Collingbournee6480e22016-11-29 20:43:47 +000017#include "llvm/Bitcode/BitcodeReader.h"
18#include "llvm/Bitcode/BitcodeWriter.h"
Eugene Zelenkof95cab82017-09-07 23:28:24 +000019#include "llvm/IR/LLVMContext.h"
20#include "llvm/IR/Module.h"
Peter Collingbournee6480e22016-11-29 20:43:47 +000021#include "llvm/IRReader/IRReader.h"
22#include "llvm/Support/CommandLine.h"
Eugene Zelenkof95cab82017-09-07 23:28:24 +000023#include "llvm/Support/Error.h"
Peter Collingbournee6480e22016-11-29 20:43:47 +000024#include "llvm/Support/FileSystem.h"
Eugene Zelenkof95cab82017-09-07 23:28:24 +000025#include "llvm/Support/MemoryBuffer.h"
26#include "llvm/Support/SourceMgr.h"
27#include "llvm/Support/raw_ostream.h"
28#include <algorithm>
29#include <memory>
30#include <string>
31#include <system_error>
32#include <vector>
Peter Collingbournee6480e22016-11-29 20:43:47 +000033
34using namespace llvm;
35
36static cl::opt<bool>
37 BinaryCat("b", cl::desc("Whether to perform binary concatenation"));
38
39static cl::opt<std::string> OutputFilename("o", cl::Required,
40 cl::desc("Output filename"),
41 cl::value_desc("filename"));
42
Peter Collingbourne06f5b6a2016-12-13 23:14:55 +000043static cl::list<std::string> InputFilenames(cl::Positional, cl::ZeroOrMore,
Peter Collingbournee6480e22016-11-29 20:43:47 +000044 cl::desc("<input files>"));
45
46int main(int argc, char **argv) {
47 cl::ParseCommandLineOptions(argc, argv, "Module concatenation");
48
49 ExitOnError ExitOnErr("llvm-cat: ");
50 LLVMContext Context;
51
52 SmallVector<char, 0> Buffer;
53 BitcodeWriter Writer(Buffer);
54 if (BinaryCat) {
Vedant Kumar15d522b2017-04-11 22:11:46 +000055 for (const auto &InputFilename : InputFilenames) {
Peter Collingbournee6480e22016-11-29 20:43:47 +000056 std::unique_ptr<MemoryBuffer> MB = ExitOnErr(
57 errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
58 std::vector<BitcodeModule> Mods = ExitOnErr(getBitcodeModuleList(*MB));
Peter Collingbourne6163b4a2017-04-17 17:51:36 +000059 for (auto &BitcodeMod : Mods) {
Peter Collingbournee6480e22016-11-29 20:43:47 +000060 Buffer.insert(Buffer.end(), BitcodeMod.getBuffer().begin(),
61 BitcodeMod.getBuffer().end());
Peter Collingbourne6163b4a2017-04-17 17:51:36 +000062 Writer.copyStrtab(BitcodeMod.getStrtab());
63 }
Peter Collingbournee6480e22016-11-29 20:43:47 +000064 }
65 } else {
Peter Collingbourne6163b4a2017-04-17 17:51:36 +000066 // The string table does not own strings added to it, some of which are
67 // owned by the modules; keep them alive until we write the string table.
68 std::vector<std::unique_ptr<Module>> OwnedMods;
Vedant Kumar15d522b2017-04-11 22:11:46 +000069 for (const auto &InputFilename : InputFilenames) {
Peter Collingbournee6480e22016-11-29 20:43:47 +000070 SMDiagnostic Err;
71 std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
72 if (!M) {
73 Err.print(argv[0], errs());
74 return 1;
75 }
Rafael Espindola06d62072018-02-14 19:11:32 +000076 Writer.writeModule(*M);
Peter Collingbourne6163b4a2017-04-17 17:51:36 +000077 OwnedMods.push_back(std::move(M));
Peter Collingbournee6480e22016-11-29 20:43:47 +000078 }
Peter Collingbourne6163b4a2017-04-17 17:51:36 +000079 Writer.writeStrtab();
Peter Collingbournee6480e22016-11-29 20:43:47 +000080 }
81
82 std::error_code EC;
83 raw_fd_ostream OS(OutputFilename, EC, sys::fs::OpenFlags::F_None);
84 if (EC) {
Eugene Zelenkof95cab82017-09-07 23:28:24 +000085 errs() << argv[0] << ": cannot open " << OutputFilename << " for writing: "
86 << EC.message();
Peter Collingbournee6480e22016-11-29 20:43:47 +000087 return 1;
88 }
89
90 OS.write(Buffer.data(), Buffer.size());
91 return 0;
92}