blob: 439709311d4ad6c948bc013058cbe2b328f159d7 [file] [log] [blame]
Peter Collingbourne5a0a2e62016-11-29 20:43:47 +00001//===-- llvm-cat.cpp - LLVM module concatenation utility ------------------===//
2//
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
16#include "llvm/Bitcode/BitcodeReader.h"
17#include "llvm/Bitcode/BitcodeWriter.h"
18#include "llvm/IRReader/IRReader.h"
19#include "llvm/Support/CommandLine.h"
20#include "llvm/Support/FileSystem.h"
21
22using namespace llvm;
23
24static cl::opt<bool>
25 BinaryCat("b", cl::desc("Whether to perform binary concatenation"));
26
27static cl::opt<std::string> OutputFilename("o", cl::Required,
28 cl::desc("Output filename"),
29 cl::value_desc("filename"));
30
31static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore,
32 cl::desc("<input files>"));
33
34int main(int argc, char **argv) {
35 cl::ParseCommandLineOptions(argc, argv, "Module concatenation");
36
37 ExitOnError ExitOnErr("llvm-cat: ");
38 LLVMContext Context;
39
40 SmallVector<char, 0> Buffer;
41 BitcodeWriter Writer(Buffer);
42 if (BinaryCat) {
43 for (std::string InputFilename : InputFilenames) {
44 std::unique_ptr<MemoryBuffer> MB = ExitOnErr(
45 errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
46 std::vector<BitcodeModule> Mods = ExitOnErr(getBitcodeModuleList(*MB));
47 for (auto &BitcodeMod : Mods)
48 Buffer.insert(Buffer.end(), BitcodeMod.getBuffer().begin(),
49 BitcodeMod.getBuffer().end());
50 }
51 } else {
52 for (std::string InputFilename : InputFilenames) {
53 SMDiagnostic Err;
54 std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
55 if (!M) {
56 Err.print(argv[0], errs());
57 return 1;
58 }
59 Writer.writeModule(M.get());
60 }
61 }
62
63 std::error_code EC;
64 raw_fd_ostream OS(OutputFilename, EC, sys::fs::OpenFlags::F_None);
65 if (EC) {
66 llvm::errs() << argv[0] << ": cannot open " << OutputFilename
67 << " for writing: " << EC.message();
68 return 1;
69 }
70
71 OS.write(Buffer.data(), Buffer.size());
72 return 0;
73}