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
17 #include "host/libs/config/feature.h"
18
19 #include <unordered_set>
20
21 #include "common/libs/utils/result.h"
22
23 namespace cuttlefish {
24
~SetupFeature()25 SetupFeature::~SetupFeature() {}
26
ResultSetup()27 Result<void> SetupFeature::ResultSetup() {
28 CF_EXPECT(Setup());
29 return {};
30 }
31
Setup()32 bool SetupFeature::Setup() {
33 LOG(ERROR) << "Missing ResultSetup implementation";
34 return false;
35 }
36
RunSetup(const std::vector<SetupFeature * > & features)37 /* static */ Result<void> SetupFeature::RunSetup(
38 const std::vector<SetupFeature*>& features) {
39 std::unordered_set<SetupFeature*> enabled;
40 for (const auto& feature : features) {
41 CF_EXPECT(feature != nullptr, "Received null feature");
42 if (feature->Enabled()) {
43 enabled.insert(feature);
44 }
45 }
46 // Collect these in a vector first to trigger any obvious dependency issues.
47 std::vector<SetupFeature*> ordered_features;
48 auto add_feature = [&ordered_features](SetupFeature* feature) -> bool {
49 ordered_features.push_back(feature);
50 return true;
51 };
52 CF_EXPECT(Feature<SetupFeature>::TopologicalVisit(enabled, add_feature),
53 "Dependency issue detected, not performing any setup.");
54 // TODO(b/189153501): This can potentially be parallelized.
55 for (auto& feature : ordered_features) {
56 LOG(DEBUG) << "Running setup for " << feature->Name();
57 CF_EXPECT(feature->ResultSetup(), "Setup failed for " << feature->Name());
58 }
59 return {};
60 }
61
ProcessFlags(const std::vector<FlagFeature * > & features,std::vector<std::string> & flags)62 Result<void> FlagFeature::ProcessFlags(
63 const std::vector<FlagFeature*>& features,
64 std::vector<std::string>& flags) {
65 std::unordered_set<FlagFeature*> features_set(features.begin(),
66 features.end());
67 CF_EXPECT(features_set.count(nullptr) == 0, "Received null feature");
68 auto handle = [&flags](FlagFeature* feature) -> bool {
69 return feature->Process(flags);
70 };
71 CF_EXPECT(
72 Feature<FlagFeature>::TopologicalVisit(features_set, handle),
73 "Unable to parse flags.");
74 return {};
75 }
76
WriteGflagsHelpXml(const std::vector<FlagFeature * > & features,std::ostream & out)77 bool FlagFeature::WriteGflagsHelpXml(const std::vector<FlagFeature*>& features,
78 std::ostream& out) {
79 // Lifted from external/gflags/src/gflags_reporting.cc:ShowXMLOfFlags
80 out << "<?xml version=\"1.0\"?>\n";
81 out << "<AllFlags>\n";
82 out << " <program>program</program>\n";
83 out << " <usage>usage</usage>\n";
84 for (const auto& feature : features) {
85 if (!feature) {
86 LOG(ERROR) << "Received null feature";
87 return false;
88 }
89 if (!feature->WriteGflagsCompatHelpXml(out)) {
90 LOG(ERROR) << "Failure to write xml";
91 return false;
92 }
93 }
94 out << "</AllFlags>";
95 return true;
96 }
97
98 } // namespace cuttlefish
99