llvm-modextract.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //===-- llvm-modextract.cpp - LLVM module extractor 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 multi-module bitcode file, extracts one of the modules and writes
  11. // it to the output file.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Bitcode/BitcodeReader.h"
  15. #include "llvm/Bitcode/BitcodeWriter.h"
  16. #include "llvm/Support/CommandLine.h"
  17. #include "llvm/Support/Error.h"
  18. #include "llvm/Support/FileSystem.h"
  19. #include "llvm/Support/MemoryBuffer.h"
  20. #include "llvm/Support/ToolOutputFile.h"
  21. #include "llvm/Support/WithColor.h"
  22. using namespace llvm;
  23. static cl::OptionCategory ModextractCategory("Modextract Options");
  24. static cl::opt<bool>
  25. BinaryExtract("b", cl::desc("Whether to perform binary extraction"),
  26. cl::cat(ModextractCategory));
  27. static cl::opt<std::string> OutputFilename("o", cl::Required,
  28. cl::desc("Output filename"),
  29. cl::value_desc("filename"),
  30. cl::cat(ModextractCategory));
  31. static cl::opt<std::string> InputFilename(cl::Positional,
  32. cl::desc("<input bitcode>"),
  33. cl::init("-"),
  34. cl::cat(ModextractCategory));
  35. static cl::opt<unsigned> ModuleIndex("n", cl::Required,
  36. cl::desc("Index of module to extract"),
  37. cl::value_desc("index"),
  38. cl::cat(ModextractCategory));
  39. int main(int argc, char **argv) {
  40. cl::HideUnrelatedOptions({&ModextractCategory, &getColorCategory()});
  41. cl::ParseCommandLineOptions(argc, argv, "Module extractor");
  42. ExitOnError ExitOnErr("llvm-modextract: error: ");
  43. std::unique_ptr<MemoryBuffer> MB =
  44. ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
  45. std::vector<BitcodeModule> Ms = ExitOnErr(getBitcodeModuleList(*MB));
  46. LLVMContext Context;
  47. if (ModuleIndex >= Ms.size()) {
  48. errs() << "llvm-modextract: error: module index out of range; bitcode file "
  49. "contains "
  50. << Ms.size() << " module(s)\n";
  51. return 1;
  52. }
  53. std::error_code EC;
  54. std::unique_ptr<ToolOutputFile> Out(
  55. new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None));
  56. ExitOnErr(errorCodeToError(EC));
  57. if (BinaryExtract) {
  58. SmallVector<char, 0> Result;
  59. BitcodeWriter Writer(Result);
  60. Result.append(Ms[ModuleIndex].getBuffer().begin(),
  61. Ms[ModuleIndex].getBuffer().end());
  62. Writer.copyStrtab(Ms[ModuleIndex].getStrtab());
  63. Out->os() << Result;
  64. Out->keep();
  65. return 0;
  66. }
  67. std::unique_ptr<Module> M = ExitOnErr(Ms[ModuleIndex].parseModule(Context));
  68. WriteBitcodeToFile(*M, Out->os());
  69. Out->keep();
  70. return 0;
  71. }