|
| 1 | +//===- CASConfiguration.cpp -------------------------------------*- 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 | +#include "llvm/CAS/CASConfiguration.h" |
| 10 | +#include "llvm/CAS/ActionCache.h" |
| 11 | +#include "llvm/CAS/BuiltinUnifiedCASDatabases.h" |
| 12 | +#include "llvm/CAS/ObjectStore.h" |
| 13 | +#include "llvm/Support/JSON.h" |
| 14 | + |
| 15 | +using namespace llvm; |
| 16 | +using namespace llvm::cas; |
| 17 | + |
| 18 | +void CASConfiguration::getResolvedCASPath( |
| 19 | + llvm::SmallVectorImpl<char> &Result) const { |
| 20 | + if (CASPath == "auto") { |
| 21 | + getDefaultOnDiskCASPath(Result); |
| 22 | + } else { |
| 23 | + Result.assign(CASPath.begin(), CASPath.end()); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +Expected<std::pair<std::shared_ptr<ObjectStore>, std::shared_ptr<ActionCache>>> |
| 28 | +CASConfiguration::createDatabases() const { |
| 29 | + if (!PluginPath.empty()) |
| 30 | + return createPluginCASDatabases(PluginPath, CASPath, PluginOptions); |
| 31 | + |
| 32 | + if (CASPath.empty()) { |
| 33 | + return std::pair(createInMemoryCAS(), createInMemoryActionCache()); |
| 34 | + } |
| 35 | + |
| 36 | + SmallString<128> PathBuf; |
| 37 | + getResolvedCASPath(PathBuf); |
| 38 | + |
| 39 | + std::pair<std::unique_ptr<ObjectStore>, std::unique_ptr<ActionCache>> DBs; |
| 40 | + return createOnDiskUnifiedCASDatabases(PathBuf); |
| 41 | +} |
| 42 | + |
| 43 | +void CASConfiguration::writeConfigurationFile(raw_ostream &OS) const { |
| 44 | + using namespace llvm::json; |
| 45 | + Object Root; |
| 46 | + Root["CASPath"] = CASPath; |
| 47 | + Root["PluginPath"] = PluginPath; |
| 48 | + |
| 49 | + Array PlugOpts; |
| 50 | + for (const auto &Opt : PluginOptions) { |
| 51 | + Object Entry; |
| 52 | + Entry[Opt.first] = Opt.second; |
| 53 | + PlugOpts.emplace_back(std::move(Entry)); |
| 54 | + } |
| 55 | + Root["PluginOptions"] = std::move(PlugOpts); |
| 56 | + |
| 57 | + OS << formatv("{0:2}", Value(std::move(Root))); |
| 58 | +} |
| 59 | + |
| 60 | +Expected<CASConfiguration> |
| 61 | +CASConfiguration::createFromConfig(StringRef Content) { |
| 62 | + auto Parsed = json::parse(Content); |
| 63 | + if (!Parsed) |
| 64 | + return Parsed.takeError(); |
| 65 | + |
| 66 | + CASConfiguration Config; |
| 67 | + auto *Root = Parsed->getAsObject(); |
| 68 | + if (!Root) |
| 69 | + return createStringError( |
| 70 | + "CASConfiguration file error: top level object missing"); |
| 71 | + |
| 72 | + if (auto CASPath = Root->getString("CASPath")) |
| 73 | + Config.CASPath = *CASPath; |
| 74 | + |
| 75 | + if (auto PluginPath = Root->getString("PluginPath")) |
| 76 | + Config.PluginPath = *PluginPath; |
| 77 | + |
| 78 | + if (auto *Opts = Root->getArray("PluginOptions")) { |
| 79 | + for (auto &Opt : *Opts) { |
| 80 | + if (auto *Arg = Opt.getAsObject()) { |
| 81 | + for (auto &Entry : *Arg) { |
| 82 | + if (auto V = Entry.second.getAsString()) |
| 83 | + Config.PluginOptions.emplace_back(Entry.first.str(), *V); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return Config; |
| 90 | +} |
| 91 | + |
| 92 | +std::optional<std::pair<std::string, CASConfiguration>> |
| 93 | +CASConfiguration::createFromSearchConfigFile( |
| 94 | + StringRef Path, IntrusiveRefCntPtr<vfs::FileSystem> VFS) { |
| 95 | + if (!VFS) |
| 96 | + VFS = vfs::getRealFileSystem(); |
| 97 | + |
| 98 | + while (!Path.empty()) { |
| 99 | + SmallString<256> ConfigPath(Path); |
| 100 | + sys::path::append(ConfigPath, ".cas-config"); |
| 101 | + auto File = VFS->openFileForRead(ConfigPath); |
| 102 | + if (!File || !*File) { |
| 103 | + Path = sys::path::parent_path(Path); |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + auto Buffer = (*File)->getBuffer(ConfigPath); |
| 108 | + if (!Buffer || !*Buffer) { |
| 109 | + Path = sys::path::parent_path(Path); |
| 110 | + continue; |
| 111 | + } |
| 112 | + |
| 113 | + auto Config = createFromConfig((*Buffer)->getBuffer()); |
| 114 | + if (!Config) { |
| 115 | + consumeError(Config.takeError()); |
| 116 | + Path = sys::path::parent_path(Path); |
| 117 | + continue; |
| 118 | + } |
| 119 | + return std::pair{ConfigPath.str().str(), *Config}; |
| 120 | + } |
| 121 | + return std::nullopt; |
| 122 | +} |
0 commit comments