|
| 1 | +//===-- ClangInstallAPI.cpp ----------------------------------------------===// |
| 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 is the entry point to clang-installapi; it is a wrapper |
| 10 | +// for functionality in the InstallAPI clang library. |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#include "Options.h" |
| 15 | +#include "clang/Basic/DiagnosticIDs.h" |
| 16 | +#include "clang/Driver/Driver.h" |
| 17 | +#include "clang/Driver/DriverDiagnostic.h" |
| 18 | +#include "clang/Frontend/CompilerInstance.h" |
| 19 | +#include "clang/Frontend/TextDiagnosticPrinter.h" |
| 20 | +#include "clang/InstallAPI/Context.h" |
| 21 | +#include "llvm/ADT/ArrayRef.h" |
| 22 | +#include "llvm/Option/Option.h" |
| 23 | +#include "llvm/Support/CommandLine.h" |
| 24 | +#include "llvm/Support/LLVMDriver.h" |
| 25 | +#include "llvm/Support/ManagedStatic.h" |
| 26 | +#include "llvm/Support/PrettyStackTrace.h" |
| 27 | +#include "llvm/Support/Process.h" |
| 28 | +#include "llvm/Support/Signals.h" |
| 29 | +#include "llvm/TargetParser/Host.h" |
| 30 | +#include "llvm/TextAPI/TextAPIWriter.h" |
| 31 | + |
| 32 | +using namespace clang; |
| 33 | +using namespace clang::installapi; |
| 34 | +using namespace clang::driver::options; |
| 35 | +using namespace llvm::opt; |
| 36 | +using namespace llvm::MachO; |
| 37 | + |
| 38 | +static InstallAPIContext createContextFromOptions(const Options &Opts) { |
| 39 | + InstallAPIContext Ctx; |
| 40 | + // InstallAPI requires two level namespacing. |
| 41 | + Ctx.BA.TwoLevelNamespace = true; |
| 42 | + |
| 43 | + Ctx.BA.InstallName = Opts.LinkerOpts.InstallName; |
| 44 | + Ctx.BA.CurrentVersion = Opts.LinkerOpts.CurrentVersion; |
| 45 | + Ctx.BA.AppExtensionSafe = Opts.LinkerOpts.AppExtensionSafe; |
| 46 | + Ctx.FT = Opts.DriverOpts.OutFT; |
| 47 | + Ctx.OutputLoc = Opts.DriverOpts.OutputPath; |
| 48 | + return Ctx; |
| 49 | +} |
| 50 | + |
| 51 | +static bool run(ArrayRef<const char *> CommandArgs, const char *ProgName) { |
| 52 | + // InstallAPI only needs to parse AST, so always force on certain options. |
| 53 | + std::vector<const char *> Args; |
| 54 | + Args.reserve(CommandArgs.size() + 1); |
| 55 | + llvm::copy(CommandArgs, std::back_inserter(Args)); |
| 56 | + Args.push_back("-fsyntax-only"); |
| 57 | + |
| 58 | + // Setup Diagnostics engine. |
| 59 | + IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
| 60 | + const llvm::opt::OptTable &ClangOpts = clang::driver::getDriverOptTable(); |
| 61 | + unsigned MissingArgIndex, MissingArgCount; |
| 62 | + llvm::opt::InputArgList ParsedArgs = ClangOpts.ParseArgs( |
| 63 | + ArrayRef(Args).slice(1), MissingArgIndex, MissingArgCount); |
| 64 | + ParseDiagnosticArgs(*DiagOpts, ParsedArgs); |
| 65 | + |
| 66 | + IntrusiveRefCntPtr<DiagnosticsEngine> Diag = new clang::DiagnosticsEngine( |
| 67 | + new clang::DiagnosticIDs(), DiagOpts.get(), |
| 68 | + new clang::TextDiagnosticPrinter(llvm::errs(), DiagOpts.get())); |
| 69 | + |
| 70 | + // Create file manager for all file operations. |
| 71 | + IntrusiveRefCntPtr<clang::FileManager> FM( |
| 72 | + new FileManager(clang::FileSystemOptions())); |
| 73 | + |
| 74 | + // Set up driver to parse input arguments. |
| 75 | + auto DriverArgs = llvm::ArrayRef(Args).slice(1); |
| 76 | + clang::driver::Driver Driver(ProgName, llvm::sys::getDefaultTargetTriple(), |
| 77 | + *Diag, "clang installapi tool"); |
| 78 | + Driver.setInstalledDir(llvm::sys::path::parent_path(ProgName)); |
| 79 | + auto TargetAndMode = |
| 80 | + clang::driver::ToolChain::getTargetAndModeFromProgramName(ProgName); |
| 81 | + Driver.setTargetAndMode(TargetAndMode); |
| 82 | + bool HasError = false; |
| 83 | + llvm::opt::InputArgList ArgList = |
| 84 | + Driver.ParseArgStrings(DriverArgs, /*UseDriverMode=*/true, HasError); |
| 85 | + if (HasError) |
| 86 | + return EXIT_FAILURE; |
| 87 | + Driver.setCheckInputsExist(false); |
| 88 | + |
| 89 | + // Capture InstallAPI specific options and diagnose any option errors. |
| 90 | + Options Opts(*Diag, FM.get(), ArgList); |
| 91 | + if (Diag->hasErrorOccurred()) |
| 92 | + return EXIT_FAILURE; |
| 93 | + InstallAPIContext Ctx = createContextFromOptions(Opts); |
| 94 | + |
| 95 | + // Set up compilation. |
| 96 | + std::unique_ptr<CompilerInstance> CI(new CompilerInstance()); |
| 97 | + CI->setFileManager(FM.get()); |
| 98 | + CI->createDiagnostics(); |
| 99 | + if (!CI->hasDiagnostics()) |
| 100 | + return EXIT_FAILURE; |
| 101 | + |
| 102 | + auto Out = CI->createOutputFile(Ctx.OutputLoc, /*Binary=*/false, |
| 103 | + /*RemoveFileOnSignal=*/false, |
| 104 | + /*UseTemporary=*/false, |
| 105 | + /*CreateMissingDirectories=*/false); |
| 106 | + if (!Out) |
| 107 | + return EXIT_FAILURE; |
| 108 | + |
| 109 | + // Assign attributes for serialization. |
| 110 | + InterfaceFile IF; |
| 111 | + for (const auto &TargetInfo : Opts.DriverOpts.Targets) { |
| 112 | + IF.addTarget(TargetInfo.first); |
| 113 | + IF.setFromBinaryAttrs(Ctx.BA, TargetInfo.first); |
| 114 | + } |
| 115 | + |
| 116 | + // Write output file and perform CI cleanup. |
| 117 | + if (auto Err = TextAPIWriter::writeToStream(*Out, IF, Ctx.FT)) { |
| 118 | + Diag->Report(diag::err_cannot_open_file) << Ctx.OutputLoc; |
| 119 | + CI->clearOutputFiles(/*EraseFiles=*/true); |
| 120 | + return EXIT_FAILURE; |
| 121 | + } |
| 122 | + |
| 123 | + CI->clearOutputFiles(/*EraseFiles=*/false); |
| 124 | + return EXIT_SUCCESS; |
| 125 | +} |
| 126 | + |
| 127 | +int clang_installapi_main(int argc, char **argv, |
| 128 | + const llvm::ToolContext &ToolContext) { |
| 129 | + // Standard set up, so program fails gracefully. |
| 130 | + llvm::sys::PrintStackTraceOnErrorSignal(argv[0]); |
| 131 | + llvm::PrettyStackTraceProgram StackPrinter(argc, argv); |
| 132 | + llvm::llvm_shutdown_obj Shutdown; |
| 133 | + |
| 134 | + if (llvm::sys::Process::FixupStandardFileDescriptors()) |
| 135 | + return EXIT_FAILURE; |
| 136 | + |
| 137 | + const char *ProgName = |
| 138 | + ToolContext.NeedsPrependArg ? ToolContext.PrependArg : ToolContext.Path; |
| 139 | + return run(llvm::ArrayRef(argv, argc), ProgName); |
| 140 | +} |
0 commit comments