llvm-project
96 строк · 3.3 Кб
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>
38BinaryCat("b", cl::desc("Whether to perform binary concatenation"),
39cl::cat(CatCategory));
40
41static cl::opt<std::string> OutputFilename("o", cl::Required,
42cl::desc("Output filename"),
43cl::value_desc("filename"),
44cl::cat(CatCategory));
45
46static cl::list<std::string> InputFilenames(cl::Positional,
47cl::desc("<input files>"),
48cl::cat(CatCategory));
49
50int main(int argc, char **argv) {
51cl::HideUnrelatedOptions(CatCategory);
52cl::ParseCommandLineOptions(argc, argv, "Module concatenation");
53
54ExitOnError ExitOnErr("llvm-cat: ");
55LLVMContext Context;
56
57SmallVector<char, 0> Buffer;
58BitcodeWriter Writer(Buffer);
59if (BinaryCat) {
60for (const auto &InputFilename : InputFilenames) {
61std::unique_ptr<MemoryBuffer> MB = ExitOnErr(
62errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
63std::vector<BitcodeModule> Mods = ExitOnErr(getBitcodeModuleList(*MB));
64for (auto &BitcodeMod : Mods) {
65llvm::append_range(Buffer, BitcodeMod.getBuffer());
66Writer.copyStrtab(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.
72std::vector<std::unique_ptr<Module>> OwnedMods;
73for (const auto &InputFilename : InputFilenames) {
74SMDiagnostic Err;
75std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
76if (!M) {
77Err.print(argv[0], errs());
78return 1;
79}
80Writer.writeModule(*M);
81OwnedMods.push_back(std::move(M));
82}
83Writer.writeStrtab();
84}
85
86std::error_code EC;
87raw_fd_ostream OS(OutputFilename, EC, sys::fs::OpenFlags::OF_None);
88if (EC) {
89errs() << argv[0] << ": cannot open " << OutputFilename << " for writing: "
90<< EC.message();
91return 1;
92}
93
94OS.write(Buffer.data(), Buffer.size());
95return 0;
96}
97