Android-cuttlefish cvd tool
feature.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#pragma once
17
18#include <ostream>
19#include <string>
20#include <type_traits>
21#include <unordered_map>
22#include <unordered_set>
23#include <vector>
24
25#include "absl/log/check.h"
26#include "fruit/fruit.h"
27
30
31namespace cuttlefish {
32
33template <typename Subclass>
34class Feature {
35 public:
36 virtual ~Feature() = default;
37
38 virtual std::string Name() const = 0;
39
41 const std::unordered_set<Subclass*>& features,
42 const std::function<Result<void>(Subclass*)>& callback);
43
44 private:
45 virtual std::unordered_set<Subclass*> Dependencies() const = 0;
46};
47
48class SetupFeature : public virtual Feature<SetupFeature> {
49 public:
50 virtual ~SetupFeature();
51
52 static Result<void> RunSetup(const std::vector<SetupFeature*>& features);
53
54 virtual bool Enabled() const { return true; }
55
56 private:
58};
59
60template <typename T>
62 public:
64 if constexpr (std::is_void_v<T>) {
65 calculated_ = false;
66 } else {
67 calculated_ = {};
68 }
69 }
70 template <typename S = T>
71 std::enable_if_t<!std::is_void_v<S>, S&> operator*() {
72 CHECK(calculated_.has_value()) << "precondition violation";
73 return *calculated_;
74 }
75 template <typename S = T>
76 std::enable_if_t<!std::is_void_v<S>, const S&> operator*() const {
77 CHECK(calculated_.has_value()) << "precondition violation";
78 return *calculated_;
79 }
80 template <typename S = T>
81 std::enable_if_t<!std::is_void_v<S>, S*> operator->() {
82 CHECK(calculated_.has_value()) << "precondition violation";
83 return &*calculated_;
84 }
85 template <typename S = T>
86 std::enable_if_t<!std::is_void_v<S>, const S*> operator->() const {
87 CHECK(calculated_.has_value()) << "precondition violation";
88 return &*calculated_;
89 }
90 template <typename S = T>
91 std::enable_if_t<!std::is_void_v<S>, S> Move() {
92 CHECK(calculated_.has_value()) << "precondition violation";
93 return std::move(*calculated_);
94 }
95
96 private:
97 Result<void> ResultSetup() override final {
98 if constexpr (std::is_void_v<T>) {
99 CF_EXPECT(!calculated_, "precondition violation");
101 calculated_ = true;
102 } else {
103 CF_EXPECT(!calculated_.has_value(), "precondition violation");
105 }
106 return {};
107 }
108
109 virtual Result<T> Calculate() = 0;
110
111 std::conditional_t<std::is_void_v<T>, bool, std::optional<T>> calculated_;
112};
113
114class FlagFeature : public Feature<FlagFeature> {
115 public:
116 static Result<void> ProcessFlags(const std::vector<FlagFeature*>& features,
117 std::vector<std::string>& flags);
118 static bool WriteGflagsHelpXml(const std::vector<FlagFeature*>& features,
119 std::ostream& out);
120
121 private:
122 // Must be executed in dependency order following Dependencies(). Expected to
123 // mutate the `flags` argument to remove handled flags, and possibly introduce
124 // new flag values (e.g. from a file).
125 virtual Result<void> Process(std::vector<std::string>& flags) = 0;
126
127 // TODO(schuffelen): Migrate the xml help to human-readable help output after
128 // the gflags migration is done.
129
130 // Write an xml fragment that is compatible with gflags' `--helpxml` format.
131 virtual bool WriteGflagsCompatHelpXml(std::ostream& out) const = 0;
132};
133
134template <typename Subclass>
136 const std::unordered_set<Subclass*>& features,
137 const std::function<Result<void>(Subclass*)>& callback) {
138 enum class Status { UNVISITED, VISITING, VISITED };
139 std::unordered_map<Subclass*, Status> features_status;
140 for (const auto& feature : features) {
141 features_status[feature] = Status::UNVISITED;
142 }
143 std::function<Result<void>(Subclass*)> visit;
144 visit = [&callback, &features_status,
145 &visit](Subclass* feature) -> Result<void> {
146 CF_EXPECT(features_status.count(feature) > 0,
147 "Dependency edge to "
148 << feature->Name() << " but it is not part of the feature "
149 << "graph. This feature is either disabled or not correctly "
150 << "registered.");
151 if (features_status[feature] == Status::VISITED) {
152 return {};
153 }
154 CF_EXPECT(features_status[feature] != Status::VISITING,
155 "Cycle detected while visiting " << feature->Name());
156 features_status[feature] = Status::VISITING;
157 for (const auto& dependency : feature->Dependencies()) {
158 CF_EXPECT(dependency != nullptr, "SetupFeature "
159 << feature->Name()
160 << " has a null dependency.");
161 CF_EXPECT(visit(dependency),
162 "Error detected while visiting " << feature->Name());
163 }
164 features_status[feature] = Status::VISITED;
165 CF_EXPECT(callback(feature), "Callback error on " << feature->Name());
166 return {};
167 };
168 for (const auto& feature : features) {
169 CF_EXPECT(visit(feature)); // `visit` will log the error chain.
170 }
171 return {};
172}
173
174template <typename... Args>
175std::unordered_set<SetupFeature*> SetupFeatureDeps(
176 const std::tuple<Args...>& args) {
177 std::unordered_set<SetupFeature*> deps;
178 std::apply(
179 [&deps](auto&&... arg) {
180 (
181 [&] {
182 using ArgType = std::remove_reference_t<decltype(arg)>;
183 if constexpr (std::is_base_of_v<SetupFeature, ArgType>) {
184 deps.insert(static_cast<SetupFeature*>(&arg));
185 }
186 }(),
187 ...);
188 },
189 args);
190 return deps;
191}
192
193template <auto Fn, typename R, typename... Args>
195 public:
197 : args_(std::forward_as_tuple(args...)) {}
198
199 bool Enabled() const override { return true; }
200
201 std::string Name() const override {
202 static constexpr auto kName = ValueName<Fn>();
203 return std::string(kName);
204 }
205
206 std::unordered_set<SetupFeature*> Dependencies() const override {
207 return SetupFeatureDeps(args_);
208 }
209
210 private:
211 Result<R> Calculate() override {
212 if constexpr (std::is_void_v<R>) {
213 CF_EXPECT(std::apply(Fn, args_));
214 return {};
215 } else {
216 return CF_EXPECT(std::apply(Fn, args_));
217 }
218 }
219 std::tuple<Args...> args_;
220};
221
222template <auto Fn1, typename Fn2>
224
225template <auto Fn, typename R, typename... Args>
226struct GenericSetupImpl<Fn, Result<R> (*)(Args...)> {
228
229 static fruit::Component<
230 fruit::Required<typename std::remove_reference_t<Args>...>, Type>
232 return fruit::createComponent()
233 .template addMultibinding<SetupFeature, Type>();
234 }
235};
236
237template <auto Fn>
238using AutoSetup = GenericSetupImpl<Fn, decltype(Fn)>;
239
240} // namespace cuttlefish
Definition: feature.h:34
virtual std::string Name() const =0
virtual ~Feature()=default
virtual std::unordered_set< Subclass * > Dependencies() const =0
static Result< void > TopologicalVisit(const std::unordered_set< Subclass * > &features, const std::function< Result< void >(Subclass *)> &callback)
Definition: feature.h:135
Definition: feature.h:114
static bool WriteGflagsHelpXml(const std::vector< FlagFeature * > &features, std::ostream &out)
Definition: feature.cpp:73
virtual Result< void > Process(std::vector< std::string > &flags)=0
static Result< void > ProcessFlags(const std::vector< FlagFeature * > &features, std::vector< std::string > &flags)
Definition: feature.cpp:58
virtual bool WriteGflagsCompatHelpXml(std::ostream &out) const =0
std::string Name() const override
Definition: feature.h:201
std::unordered_set< SetupFeature * > Dependencies() const override
Definition: feature.h:206
Result< R > Calculate() override
Definition: feature.h:211
std::tuple< Args... > args_
Definition: feature.h:219
Definition: feature.h:61
ReturningSetupFeature()
Definition: feature.h:63
std::enable_if_t<!std::is_void_v< S >, S * > operator->()
Definition: feature.h:81
virtual Result< T > Calculate()=0
std::enable_if_t<!std::is_void_v< S >, const S & > operator*() const
Definition: feature.h:76
Result< void > ResultSetup() override final
Definition: feature.h:97
std::conditional_t< std::is_void_v< T >, bool, std::optional< T > > calculated_
Definition: feature.h:111
std::enable_if_t<!std::is_void_v< S >, const S * > operator->() const
Definition: feature.h:86
std::enable_if_t<!std::is_void_v< S >, S & > operator*()
Definition: feature.h:71
std::enable_if_t<!std::is_void_v< S >, S > Move()
Definition: feature.h:91
Definition: feature.h:48
virtual ~SetupFeature()
Definition: feature.cpp:30
virtual bool Enabled() const
Definition: feature.h:54
static Result< void > RunSetup(const std::vector< SetupFeature * > &features)
Definition: feature.cpp:32
virtual Result< void > ResultSetup()=0
#define CF_EXPECT(...)
Definition: expect.h:149
__le32 feature
Definition: f2fs_fs.h:35
@ INJECT
Definition: f2fs_fs.h:443
#define CHECK(x)
Definition: logging.h:251
Definition: alloc_driver.h:20
static constexpr std::string_view kName
Definition: efi_loader.cc:30
Status
Definition: buffers.h:24
std::unordered_set< SetupFeature * > SetupFeatureDeps(const std::tuple< Args... > &args)
Definition: feature.h:175
tl::expected< T, StackTraceError > Result
Definition: result_type.h:30
std::vector< std::string_view > Args
Definition: incremental.h:28
#define S(x, n)
Definition: sha512.c:85
#define R(x, n)
Definition: sha512.c:86
static fruit::Component< fruit::Required< typename std::remove_reference_t< Args >... >, Type > Component()
Definition: feature.h:231
Definition: feature.h:223
Definition: f2fs_fs.h:1999