Eugene Zelenko | f95cab8 | 2017-09-07 23:28:24 +0000 | [diff] [blame] | 1 | //===- llvm-cat.cpp - LLVM module concatenation utility -------------------===// |
Peter Collingbourne | e6480e2 | 2016-11-29 20:43:47 +0000 | [diff] [blame] | 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 | |
Eugene Zelenko | f95cab8 | 2017-09-07 23:28:24 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallVector.h" |
Peter Collingbourne | e6480e2 | 2016-11-29 20:43:47 +0000 | [diff] [blame] | 17 | #include "llvm/Bitcode/BitcodeReader.h" |
| 18 | #include "llvm/Bitcode/BitcodeWriter.h" |
Eugene Zelenko | f95cab8 | 2017-09-07 23:28:24 +0000 | [diff] [blame] | 19 | #include "llvm/IR/LLVMContext.h" |
| 20 | #include "llvm/IR/Module.h" |
Peter Collingbourne | e6480e2 | 2016-11-29 20:43:47 +0000 | [diff] [blame] | 21 | #include "llvm/IRReader/IRReader.h" |
| 22 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | f95cab8 | 2017-09-07 23:28:24 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Error.h" |
Peter Collingbourne | e6480e2 | 2016-11-29 20:43:47 +0000 | [diff] [blame] | 24 | #include "llvm/Support/FileSystem.h" |
Eugene Zelenko | f95cab8 | 2017-09-07 23:28:24 +0000 | [diff] [blame] | 25 | #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 Collingbourne | e6480e2 | 2016-11-29 20:43:47 +0000 | [diff] [blame] | 33 | |
| 34 | using namespace llvm; |
| 35 | |
| 36 | static cl::opt<bool> |
| 37 | BinaryCat("b", cl::desc("Whether to perform binary concatenation")); |
| 38 | |
| 39 | static cl::opt<std::string> OutputFilename("o", cl::Required, |
| 40 | cl::desc("Output filename"), |
| 41 | cl::value_desc("filename")); |
| 42 | |
Peter Collingbourne | 06f5b6a | 2016-12-13 23:14:55 +0000 | [diff] [blame] | 43 | static cl::list<std::string> InputFilenames(cl::Positional, cl::ZeroOrMore, |
Peter Collingbourne | e6480e2 | 2016-11-29 20:43:47 +0000 | [diff] [blame] | 44 | cl::desc("<input files>")); |
| 45 | |
| 46 | int 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 Kumar | 15d522b | 2017-04-11 22:11:46 +0000 | [diff] [blame] | 55 | for (const auto &InputFilename : InputFilenames) { |
Peter Collingbourne | e6480e2 | 2016-11-29 20:43:47 +0000 | [diff] [blame] | 56 | std::unique_ptr<MemoryBuffer> MB = ExitOnErr( |
| 57 | errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename))); |
| 58 | std::vector<BitcodeModule> Mods = ExitOnErr(getBitcodeModuleList(*MB)); |
Peter Collingbourne | 6163b4a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 59 | for (auto &BitcodeMod : Mods) { |
Peter Collingbourne | e6480e2 | 2016-11-29 20:43:47 +0000 | [diff] [blame] | 60 | Buffer.insert(Buffer.end(), BitcodeMod.getBuffer().begin(), |
| 61 | BitcodeMod.getBuffer().end()); |
Peter Collingbourne | 6163b4a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 62 | Writer.copyStrtab(BitcodeMod.getStrtab()); |
| 63 | } |
Peter Collingbourne | e6480e2 | 2016-11-29 20:43:47 +0000 | [diff] [blame] | 64 | } |
| 65 | } else { |
Peter Collingbourne | 6163b4a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 66 | // 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 Kumar | 15d522b | 2017-04-11 22:11:46 +0000 | [diff] [blame] | 69 | for (const auto &InputFilename : InputFilenames) { |
Peter Collingbourne | e6480e2 | 2016-11-29 20:43:47 +0000 | [diff] [blame] | 70 | 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 Espindola | 06d6207 | 2018-02-14 19:11:32 +0000 | [diff] [blame] | 76 | Writer.writeModule(*M); |
Peter Collingbourne | 6163b4a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 77 | OwnedMods.push_back(std::move(M)); |
Peter Collingbourne | e6480e2 | 2016-11-29 20:43:47 +0000 | [diff] [blame] | 78 | } |
Peter Collingbourne | 6163b4a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 79 | Writer.writeStrtab(); |
Peter Collingbourne | e6480e2 | 2016-11-29 20:43:47 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | std::error_code EC; |
| 83 | raw_fd_ostream OS(OutputFilename, EC, sys::fs::OpenFlags::F_None); |
| 84 | if (EC) { |
Eugene Zelenko | f95cab8 | 2017-09-07 23:28:24 +0000 | [diff] [blame] | 85 | errs() << argv[0] << ": cannot open " << OutputFilename << " for writing: " |
| 86 | << EC.message(); |
Peter Collingbourne | e6480e2 | 2016-11-29 20:43:47 +0000 | [diff] [blame] | 87 | return 1; |
| 88 | } |
| 89 | |
| 90 | OS.write(Buffer.data(), Buffer.size()); |
| 91 | return 0; |
| 92 | } |