summaryrefslogtreecommitdiff
path: root/llvm/tools/llvm-tapi-diff/llvm-tapi-diff.cpp
blob: 40f1eec162be5b47af1bfdb82563c382635c9d9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//===-- llvm-tapi-diff.cpp - tbd comparator command-line driver ---*-
// C++
//-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines the command-line driver for the llvm-tapi difference
// engine.
//
//===----------------------------------------------------------------------===//
#include "DiffEngine.h"
#include "llvm/Object/TapiUniversal.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/WithColor.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdlib>

using namespace llvm;
using namespace MachO;
using namespace object;

namespace {
cl::OptionCategory NMCat("llvm-tapi-diff Options");
cl::opt<std::string> InputFileNameLHS(cl::Positional, cl::desc("<first file>"),
                                      cl::cat(NMCat));
cl::opt<std::string> InputFileNameRHS(cl::Positional, cl::desc("<second file>"),
                                      cl::cat(NMCat));

std::string ToolName;
} // anonymous namespace

ExitOnError ExitOnErr;

void setErrorBanner(ExitOnError &ExitOnErr, std::string InputFile) {
  ExitOnErr.setBanner(ToolName + ": error: " + InputFile + ": ");
}

Expected<std::unique_ptr<Binary>> convertFileToBinary(std::string &Filename) {
  ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
      MemoryBuffer::getFileOrSTDIN(Filename);
  if (BufferOrErr.getError())
    return errorCodeToError(BufferOrErr.getError());
  return createBinary(BufferOrErr.get()->getMemBufferRef());
}

int main(int Argc, char **Argv) {
  InitLLVM X(Argc, Argv);
  cl::HideUnrelatedOptions(NMCat);
  cl::ParseCommandLineOptions(
      Argc, Argv,
      "This tool will compare two tbd files and return the "
      "differences in those files.");
  if (InputFileNameLHS.empty() || InputFileNameRHS.empty()) {
    cl::PrintHelpMessage();
    return EXIT_FAILURE;
  }

  ToolName = Argv[0];

  setErrorBanner(ExitOnErr, InputFileNameLHS);
  auto BinLHS = ExitOnErr(convertFileToBinary(InputFileNameLHS));

  TapiUniversal *FileLHS = dyn_cast<TapiUniversal>(BinLHS.get());
  if (!FileLHS) {
    ExitOnErr(
        createStringError(std::errc::executable_format_error,
                          "Error when parsing file, unsupported file format"));
  }

  setErrorBanner(ExitOnErr, InputFileNameRHS);
  auto BinRHS = ExitOnErr(convertFileToBinary(InputFileNameRHS));

  TapiUniversal *FileRHS = dyn_cast<TapiUniversal>(BinRHS.get());
  if (!FileRHS) {
    ExitOnErr(
        createStringError(std::errc::executable_format_error,
                          "Error when parsing file, unsupported file format"));
  }

  raw_ostream &OS = outs();

  return DiffEngine(FileLHS, FileRHS).compareFiles(OS);
}