1//===- llvm-cat.cpp - LLVM module concatenation utility -------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This program is for testing features that rely on multi-module bitcode files.
10// It takes a list of input modules and uses them to create a multi-module
11// bitcode file.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/ADT/SmallVector.h"
16#include "llvm/Bitcode/BitcodeReader.h"
17#include "llvm/Bitcode/BitcodeWriter.h"
18#include "llvm/IR/LLVMContext.h"
19#include "llvm/IR/Module.h"
20#include "llvm/IRReader/IRReader.h"
21#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/Error.h"
23#include "llvm/Support/FileSystem.h"
24#include "llvm/Support/MemoryBuffer.h"
25#include "llvm/Support/SourceMgr.h"
26#include "llvm/Support/raw_ostream.h"
27#include <algorithm>
28#include <memory>
29#include <string>
30#include <system_error>
31#include <vector>
32
33using namespace llvm;
34
35cl::OptionCategory CatCategory("llvm-cat Options");
36
37static cl::opt<bool>
38 BinaryCat("b", cl::desc("Whether to perform binary concatenation"),
39 cl::cat(CatCategory));
40
41static cl::opt<std::string> OutputFilename("o", cl::Required,
42 cl::desc("Output filename"),
43 cl::value_desc("filename"),
44 cl::cat(CatCategory));
45
46static cl::list<std::string> InputFilenames(cl::Positional,
47 cl::desc("<input files>"),
48 cl::cat(CatCategory));
49
50int main(int argc, char **argv) {
51 cl::HideUnrelatedOptions(Category&: CatCategory);
52 cl::ParseCommandLineOptions(argc, argv, Overview: "Module concatenation");
53
54 ExitOnError ExitOnErr("llvm-cat: ");
55 LLVMContext Context;
56
57 SmallVector<char, 0> Buffer;
58 BitcodeWriter Writer(Buffer);
59 if (BinaryCat) {
60 for (const auto &InputFilename : InputFilenames) {
61 std::unique_ptr<MemoryBuffer> MB = ExitOnErr(
62 errorOrToExpected(EO: MemoryBuffer::getFileOrSTDIN(Filename: InputFilename)));
63 std::vector<BitcodeModule> Mods = ExitOnErr(getBitcodeModuleList(Buffer: *MB));
64 for (auto &BitcodeMod : Mods) {
65 llvm::append_range(C&: Buffer, R: BitcodeMod.getBuffer());
66 Writer.copyStrtab(Strtab: BitcodeMod.getStrtab());
67 }
68 }
69 } else {
70 // The string table does not own strings added to it, some of which are
71 // owned by the modules; keep them alive until we write the string table.
72 std::vector<std::unique_ptr<Module>> OwnedMods;
73 for (const auto &InputFilename : InputFilenames) {
74 SMDiagnostic Err;
75 std::unique_ptr<Module> M = parseIRFile(Filename: InputFilename, Err, Context);
76 if (!M) {
77 Err.print(ProgName: argv[0], S&: errs());
78 return 1;
79 }
80 Writer.writeModule(M: *M);
81 OwnedMods.push_back(x: std::move(M));
82 }
83 Writer.writeStrtab();
84 }
85
86 std::error_code EC;
87 raw_fd_ostream OS(OutputFilename, EC, sys::fs::OpenFlags::OF_None);
88 if (EC) {
89 errs() << argv[0] << ": cannot open " << OutputFilename << " for writing: "
90 << EC.message();
91 return 1;
92 }
93
94 OS.write(Ptr: Buffer.data(), Size: Buffer.size());
95 return 0;
96}
97

source code of llvm/tools/llvm-cat/llvm-cat.cpp