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 <memory>
28#include <string>
29#include <system_error>
30#include <vector>
31
32using namespace llvm;
33
34static cl::OptionCategory CatCategory("llvm-cat Options");
35
36static cl::opt<bool>
37 BinaryCat("b", cl::desc("Whether to perform binary concatenation"),
38 cl::cat(CatCategory));
39
40static cl::opt<std::string> OutputFilename("o", cl::Required,
41 cl::desc("Output filename"),
42 cl::value_desc("filename"),
43 cl::cat(CatCategory));
44
45static cl::list<std::string> InputFilenames(cl::Positional,
46 cl::desc("<input files>"),
47 cl::cat(CatCategory));
48
49int main(int argc, char **argv) {
50 cl::HideUnrelatedOptions(Category&: CatCategory);
51 cl::ParseCommandLineOptions(argc, argv, Overview: "Module concatenation");
52
53 ExitOnError ExitOnErr("llvm-cat: ");
54 LLVMContext Context;
55
56 SmallVector<char, 0> Buffer;
57 BitcodeWriter Writer(Buffer);
58 if (BinaryCat) {
59 for (const auto &InputFilename : InputFilenames) {
60 std::unique_ptr<MemoryBuffer> MB = ExitOnErr(
61 errorOrToExpected(EO: MemoryBuffer::getFileOrSTDIN(Filename: InputFilename)));
62 std::vector<BitcodeModule> Mods = ExitOnErr(getBitcodeModuleList(Buffer: *MB));
63 for (auto &BitcodeMod : Mods) {
64 llvm::append_range(C&: Buffer, R: BitcodeMod.getBuffer());
65 Writer.copyStrtab(Strtab: BitcodeMod.getStrtab());
66 }
67 }
68 } else {
69 // The string table does not own strings added to it, some of which are
70 // owned by the modules; keep them alive until we write the string table.
71 std::vector<std::unique_ptr<Module>> OwnedMods;
72 for (const auto &InputFilename : InputFilenames) {
73 SMDiagnostic Err;
74 std::unique_ptr<Module> M = parseIRFile(Filename: InputFilename, Err, Context);
75 if (!M) {
76 Err.print(ProgName: argv[0], S&: errs());
77 return 1;
78 }
79 Writer.writeModule(M: *M);
80 OwnedMods.push_back(x: std::move(M));
81 }
82 Writer.writeStrtab();
83 }
84
85 std::error_code EC;
86 raw_fd_ostream OS(OutputFilename, EC, sys::fs::OpenFlags::OF_None);
87 if (EC) {
88 errs() << argv[0] << ": cannot open " << OutputFilename << " for writing: "
89 << EC.message();
90 return 1;
91 }
92
93 OS.write(Ptr: Buffer.data(), Size: Buffer.size());
94 return 0;
95}
96