xref: /openbsd-src/gnu/llvm/llvm/tools/llvm-tapi-diff/llvm-tapi-diff.cpp (revision e068048151d29f2562a32185e21a8ba885482260)
1 //===-- llvm-tapi-diff.cpp - tbd comparator command-line driver --*- C++-*-===//
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 file defines the command-line driver for the llvm-tapi difference
10 // engine.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "DiffEngine.h"
14 #include "llvm/Object/TapiUniversal.h"
15 #include "llvm/Support/CommandLine.h"
16 #include "llvm/Support/Error.h"
17 #include "llvm/Support/InitLLVM.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/WithColor.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include <cstdlib>
22 
23 using namespace llvm;
24 using namespace MachO;
25 using namespace object;
26 
27 namespace {
28 cl::OptionCategory NMCat("llvm-tapi-diff Options");
29 cl::opt<std::string> InputFileNameLHS(cl::Positional, cl::desc("<first file>"),
30                                       cl::cat(NMCat));
31 cl::opt<std::string> InputFileNameRHS(cl::Positional, cl::desc("<second file>"),
32                                       cl::cat(NMCat));
33 } // anonymous namespace
34 
35 Expected<std::unique_ptr<Binary>> convertFileToBinary(std::string &Filename) {
36   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
37       MemoryBuffer::getFileOrSTDIN(Filename);
38   if (BufferOrErr.getError())
39     return errorCodeToError(BufferOrErr.getError());
40   return createBinary(BufferOrErr.get()->getMemBufferRef());
41 }
42 
43 int main(int Argc, char **Argv) {
44   InitLLVM X(Argc, Argv);
45   cl::HideUnrelatedOptions(NMCat);
46   cl::ParseCommandLineOptions(Argc, Argv, "Text-based Stubs Comparison Tool");
47   if (InputFileNameLHS.empty() || InputFileNameRHS.empty()) {
48     cl::PrintHelpMessage();
49     return EXIT_FAILURE;
50   }
51 
52   ExitOnError ExitOnErr("error: '" + InputFileNameLHS + "' ",
53                         /*DefaultErrorExitCode=*/2);
54   auto BinLHS = ExitOnErr(convertFileToBinary(InputFileNameLHS));
55 
56   TapiUniversal *FileLHS = dyn_cast<TapiUniversal>(BinLHS.get());
57   if (!FileLHS) {
58     ExitOnErr(createStringError(std::errc::executable_format_error,
59                                 "unsupported file format"));
60   }
61 
62   ExitOnErr.setBanner("error: '" + InputFileNameRHS + "' ");
63   auto BinRHS = ExitOnErr(convertFileToBinary(InputFileNameRHS));
64 
65   TapiUniversal *FileRHS = dyn_cast<TapiUniversal>(BinRHS.get());
66   if (!FileRHS) {
67     ExitOnErr(createStringError(std::errc::executable_format_error,
68                                 "unsupported file format"));
69   }
70 
71   raw_ostream &OS = outs();
72 
73   return DiffEngine(FileLHS, FileRHS).compareFiles(OS);
74 }
75