1 //===-- llvm-readtapi.cpp - tapi file reader and manipulator -----*- C++-*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the command-line driver for llvm-readtapi.
11 //===----------------------------------------------------------------------===//
12 #include "DiffEngine.h"
13 #include "llvm/Object/TapiUniversal.h"
14 #include "llvm/Support/CommandLine.h"
15 #include "llvm/Support/Error.h"
16 #include "llvm/Support/InitLLVM.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Support/WithColor.h"
19 #include "llvm/Support/raw_ostream.h"
23 using namespace MachO
;
24 using namespace object
;
27 cl::OptionCategory
TapiCat("llvm-readtapi options");
28 cl::OptionCategory
CompareCat("llvm-readtapi --compare options");
30 cl::opt
<std::string
> InputFileName(cl::Positional
, cl::desc("<tapi file>"),
31 cl::Required
, cl::cat(TapiCat
));
32 cl::opt
<std::string
> CompareInputFileName(cl::Positional
,
33 cl::desc("<comparison file>"),
34 cl::Required
, cl::cat(CompareCat
));
40 Output(cl::desc("choose command action:"),
41 cl::values(clEnumValN(Compare
, "compare",
42 "compare tapi file for library differences")),
43 cl::init(OutputKind::Compare
), cl::cat(TapiCat
));
44 } // anonymous namespace
46 Expected
<std::unique_ptr
<Binary
>> convertFileToBinary(std::string
&Filename
) {
47 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> BufferOrErr
=
48 MemoryBuffer::getFileOrSTDIN(Filename
);
49 if (BufferOrErr
.getError())
50 return errorCodeToError(BufferOrErr
.getError());
51 return createBinary(BufferOrErr
.get()->getMemBufferRef());
54 int main(int Argc
, char **Argv
) {
55 InitLLVM
X(Argc
, Argv
);
56 cl::HideUnrelatedOptions(TapiCat
);
57 cl::ParseCommandLineOptions(Argc
, Argv
,
58 "TAPI File Reader and Manipulator Tool");
60 if (Output
== OutputKind::Compare
) {
61 if (InputFileName
.empty() || CompareInputFileName
.empty()) {
62 cl::PrintHelpMessage();
66 ExitOnError
ExitOnErr("error: '" + InputFileName
+ "' ",
67 /*DefaultErrorExitCode=*/2);
68 auto BinLHS
= ExitOnErr(convertFileToBinary(InputFileName
));
70 TapiUniversal
*FileLHS
= dyn_cast
<TapiUniversal
>(BinLHS
.get());
72 ExitOnErr(createStringError(std::errc::executable_format_error
,
73 "unsupported file format"));
76 ExitOnErr
.setBanner("error: '" + CompareInputFileName
+ "' ");
77 auto BinRHS
= ExitOnErr(convertFileToBinary(CompareInputFileName
));
79 TapiUniversal
*FileRHS
= dyn_cast
<TapiUniversal
>(BinRHS
.get());
81 ExitOnErr(createStringError(std::errc::executable_format_error
,
82 "unsupported file format"));
85 raw_ostream
&OS
= outs();
86 return DiffEngine(FileLHS
, FileRHS
).compareFiles(OS
);