1982604ccSCyndy Ishida //===-- llvm-tapi-diff.cpp - tbd comparator command-line driver --*- C++-*-===//
25b5ab80eSSam Powell //
35b5ab80eSSam Powell // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45b5ab80eSSam Powell // See https://llvm.org/LICENSE.txt for license information.
55b5ab80eSSam Powell // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65b5ab80eSSam Powell //
75b5ab80eSSam Powell //===----------------------------------------------------------------------===//
85b5ab80eSSam Powell //
95b5ab80eSSam Powell // This file defines the command-line driver for the llvm-tapi difference
105b5ab80eSSam Powell // engine.
115b5ab80eSSam Powell //
125b5ab80eSSam Powell //===----------------------------------------------------------------------===//
135b5ab80eSSam Powell #include "DiffEngine.h"
145b5ab80eSSam Powell #include "llvm/Object/TapiUniversal.h"
155b5ab80eSSam Powell #include "llvm/Support/CommandLine.h"
165b5ab80eSSam Powell #include "llvm/Support/Error.h"
175b5ab80eSSam Powell #include "llvm/Support/InitLLVM.h"
18*e72c195fSserge-sans-paille #include "llvm/Support/MemoryBuffer.h"
195b5ab80eSSam Powell #include "llvm/Support/WithColor.h"
205b5ab80eSSam Powell #include "llvm/Support/raw_ostream.h"
215b5ab80eSSam Powell #include <cstdlib>
225b5ab80eSSam Powell
235b5ab80eSSam Powell using namespace llvm;
245b5ab80eSSam Powell using namespace MachO;
255b5ab80eSSam Powell using namespace object;
265b5ab80eSSam Powell
275b5ab80eSSam Powell namespace {
285b5ab80eSSam Powell cl::OptionCategory NMCat("llvm-tapi-diff Options");
295b5ab80eSSam Powell cl::opt<std::string> InputFileNameLHS(cl::Positional, cl::desc("<first file>"),
305b5ab80eSSam Powell cl::cat(NMCat));
315b5ab80eSSam Powell cl::opt<std::string> InputFileNameRHS(cl::Positional, cl::desc("<second file>"),
325b5ab80eSSam Powell cl::cat(NMCat));
335b5ab80eSSam Powell } // anonymous namespace
345b5ab80eSSam Powell
convertFileToBinary(std::string & Filename)355b5ab80eSSam Powell Expected<std::unique_ptr<Binary>> convertFileToBinary(std::string &Filename) {
365b5ab80eSSam Powell ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
375b5ab80eSSam Powell MemoryBuffer::getFileOrSTDIN(Filename);
385b5ab80eSSam Powell if (BufferOrErr.getError())
395b5ab80eSSam Powell return errorCodeToError(BufferOrErr.getError());
405b5ab80eSSam Powell return createBinary(BufferOrErr.get()->getMemBufferRef());
415b5ab80eSSam Powell }
425b5ab80eSSam Powell
main(int Argc,char ** Argv)435b5ab80eSSam Powell int main(int Argc, char **Argv) {
445b5ab80eSSam Powell InitLLVM X(Argc, Argv);
455b5ab80eSSam Powell cl::HideUnrelatedOptions(NMCat);
46982604ccSCyndy Ishida cl::ParseCommandLineOptions(Argc, Argv, "Text-based Stubs Comparison Tool");
475b5ab80eSSam Powell if (InputFileNameLHS.empty() || InputFileNameRHS.empty()) {
485b5ab80eSSam Powell cl::PrintHelpMessage();
495b5ab80eSSam Powell return EXIT_FAILURE;
505b5ab80eSSam Powell }
515b5ab80eSSam Powell
52982604ccSCyndy Ishida ExitOnError ExitOnErr("error: '" + InputFileNameLHS + "' ",
53982604ccSCyndy Ishida /*DefaultErrorExitCode=*/2);
545b5ab80eSSam Powell auto BinLHS = ExitOnErr(convertFileToBinary(InputFileNameLHS));
555b5ab80eSSam Powell
565b5ab80eSSam Powell TapiUniversal *FileLHS = dyn_cast<TapiUniversal>(BinLHS.get());
575b5ab80eSSam Powell if (!FileLHS) {
58982604ccSCyndy Ishida ExitOnErr(createStringError(std::errc::executable_format_error,
59982604ccSCyndy Ishida "unsupported file format"));
605b5ab80eSSam Powell }
615b5ab80eSSam Powell
62982604ccSCyndy Ishida ExitOnErr.setBanner("error: '" + InputFileNameRHS + "' ");
635b5ab80eSSam Powell auto BinRHS = ExitOnErr(convertFileToBinary(InputFileNameRHS));
645b5ab80eSSam Powell
655b5ab80eSSam Powell TapiUniversal *FileRHS = dyn_cast<TapiUniversal>(BinRHS.get());
665b5ab80eSSam Powell if (!FileRHS) {
67982604ccSCyndy Ishida ExitOnErr(createStringError(std::errc::executable_format_error,
68982604ccSCyndy Ishida "unsupported file format"));
695b5ab80eSSam Powell }
705b5ab80eSSam Powell
715b5ab80eSSam Powell raw_ostream &OS = outs();
725b5ab80eSSam Powell
735b5ab80eSSam Powell return DiffEngine(FileLHS, FileRHS).compareFiles(OS);
745b5ab80eSSam Powell }
75