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