|
| 1 | +//===----------------------------------------------------------------------===// |
| 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 | +/// \file |
| 10 | +/// This file contains the declarations of the concrete VirtualOutputBackend |
| 11 | +/// classes, which are the implementation for different output style and |
| 12 | +/// functions. This file contains: |
| 13 | +/// * NullOutputBackend: discard all outputs written. |
| 14 | +/// * OnDiskOutputBackend: write output to disk, with support for common output |
| 15 | +/// types, like append, or atomic update. |
| 16 | +/// * FilteringOutputBackend: filer some output paths to underlying output |
| 17 | +/// backend. |
| 18 | +/// * MirrorOutputBackend: mirror output to two output backends. |
| 19 | +/// |
| 20 | +//===----------------------------------------------------------------------===// |
| 21 | + |
| 22 | +#ifndef LLVM_SUPPORT_VIRTUALOUTPUTBACKENDS_H |
| 23 | +#define LLVM_SUPPORT_VIRTUALOUTPUTBACKENDS_H |
| 24 | + |
| 25 | +#include "llvm/ADT/IntrusiveRefCntPtr.h" |
| 26 | +#include "llvm/Support/VirtualOutputBackend.h" |
| 27 | +#include "llvm/Support/VirtualOutputConfig.h" |
| 28 | + |
| 29 | +namespace llvm::vfs { |
| 30 | + |
| 31 | +/// Create a backend that ignores all output. |
| 32 | +IntrusiveRefCntPtr<OutputBackend> makeNullOutputBackend(); |
| 33 | + |
| 34 | +/// Make a backend where \a OutputBackend::createFile() forwards to |
| 35 | +/// \p UnderlyingBackend when \p Filter is true, and otherwise returns a |
| 36 | +/// \a NullOutput. |
| 37 | +IntrusiveRefCntPtr<OutputBackend> makeFilteringOutputBackend( |
| 38 | + IntrusiveRefCntPtr<OutputBackend> UnderlyingBackend, |
| 39 | + std::function<bool(StringRef, std::optional<OutputConfig>)> Filter); |
| 40 | + |
| 41 | +/// Create a backend that forwards \a OutputBackend::createFile() to both \p |
| 42 | +/// Backend1 and \p Backend2. Writing to such backend will create identical |
| 43 | +/// outputs using two different backends. |
| 44 | +IntrusiveRefCntPtr<OutputBackend> |
| 45 | +makeMirroringOutputBackend(IntrusiveRefCntPtr<OutputBackend> Backend1, |
| 46 | + IntrusiveRefCntPtr<OutputBackend> Backend2); |
| 47 | + |
| 48 | +/// A helper class for proxying another backend, with the default |
| 49 | +/// implementation to forward to the underlying backend. |
| 50 | +class ProxyOutputBackend : public OutputBackend { |
| 51 | + void anchor() override; |
| 52 | + |
| 53 | +protected: |
| 54 | + // Require subclass to implement cloneImpl(). |
| 55 | + // |
| 56 | + // IntrusiveRefCntPtr<OutputBackend> cloneImpl() const override; |
| 57 | + |
| 58 | + Expected<std::unique_ptr<OutputFileImpl>> |
| 59 | + createFileImpl(StringRef Path, std::optional<OutputConfig> Config) override { |
| 60 | + OutputFile File; |
| 61 | + if (Error E = UnderlyingBackend->createFile(Path, Config).moveInto(File)) |
| 62 | + return std::move(E); |
| 63 | + return File.takeImpl(); |
| 64 | + } |
| 65 | + |
| 66 | + OutputBackend &getUnderlyingBackend() const { return *UnderlyingBackend; } |
| 67 | + |
| 68 | +public: |
| 69 | + ProxyOutputBackend(IntrusiveRefCntPtr<OutputBackend> UnderlyingBackend) |
| 70 | + : UnderlyingBackend(std::move(UnderlyingBackend)) { |
| 71 | + assert(this->UnderlyingBackend && "Expected non-null backend"); |
| 72 | + } |
| 73 | + |
| 74 | +private: |
| 75 | + IntrusiveRefCntPtr<OutputBackend> UnderlyingBackend; |
| 76 | +}; |
| 77 | + |
| 78 | +/// An output backend that creates files on disk, wrapping APIs in sys::fs. |
| 79 | +class OnDiskOutputBackend : public OutputBackend { |
| 80 | + void anchor() override; |
| 81 | + |
| 82 | +protected: |
| 83 | + IntrusiveRefCntPtr<OutputBackend> cloneImpl() const override { |
| 84 | + return clone(); |
| 85 | + } |
| 86 | + |
| 87 | + Expected<std::unique_ptr<OutputFileImpl>> |
| 88 | + createFileImpl(StringRef Path, std::optional<OutputConfig> Config) override; |
| 89 | + |
| 90 | +public: |
| 91 | + /// Resolve an absolute path. |
| 92 | + Error makeAbsolute(SmallVectorImpl<char> &Path) const; |
| 93 | + |
| 94 | + /// On disk output settings. |
| 95 | + struct OutputSettings { |
| 96 | + /// Register output files to be deleted if a signal is received. Also |
| 97 | + /// enabled for outputs with \a OutputConfig::getDiscardOnSignal(). |
| 98 | + bool RemoveOnSignal = true; |
| 99 | + |
| 100 | + /// Use temporary files. Also enabled for outputs with \a |
| 101 | + /// OutputConfig::getAtomicWrite(). |
| 102 | + bool UseTemporaries = true; |
| 103 | + |
| 104 | + // Default configuration for this backend. |
| 105 | + OutputConfig DefaultConfig; |
| 106 | + }; |
| 107 | + |
| 108 | + IntrusiveRefCntPtr<OnDiskOutputBackend> clone() const { |
| 109 | + auto Clone = makeIntrusiveRefCnt<OnDiskOutputBackend>(); |
| 110 | + Clone->Settings = Settings; |
| 111 | + return Clone; |
| 112 | + } |
| 113 | + |
| 114 | + OnDiskOutputBackend() = default; |
| 115 | + |
| 116 | + /// Settings for this backend. |
| 117 | + /// |
| 118 | + /// Access is not thread-safe. |
| 119 | + OutputSettings Settings; |
| 120 | +}; |
| 121 | + |
| 122 | +} // namespace llvm::vfs |
| 123 | + |
| 124 | +#endif // LLVM_SUPPORT_VIRTUALOUTPUTBACKENDS_H |
0 commit comments