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 #include "host/commands/assemble_cvd/flag_feature.h"
17 
18 #include <android-base/strings.h>
19 #include <fruit/fruit.h>
20 #include <gflags/gflags.h>
21 #include <string.h>
22 #include <string>
23 #include <unordered_set>
24 #include <vector>
25 
26 #include "host/libs/config/feature.h"
27 
28 namespace cuttlefish {
29 
XmlEscape(const std::string & s)30 static std::string XmlEscape(const std::string& s) {
31   using android::base::StringReplace;
32   return StringReplace(StringReplace(s, "<", "&lt;", true), ">", "&gt;", true);
33 }
34 
35 class ParseGflagsImpl : public ParseGflags {
36  public:
INJECT(ParseGflagsImpl (ConfigFlag & config))37   INJECT(ParseGflagsImpl(ConfigFlag& config)) : config_(config) {}
38 
Name() const39   std::string Name() const override { return "ParseGflags"; }
Dependencies() const40   std::unordered_set<FlagFeature*> Dependencies() const override {
41     return {static_cast<FlagFeature*>(&config_)};
42   }
Process(std::vector<std::string> & args)43   Result<void> Process(std::vector<std::string>& args) override {
44     std::string process_name = "assemble_cvd";
45     std::vector<char*> pseudo_argv = {process_name.data()};
46     for (auto& arg : args) {
47       pseudo_argv.push_back(arg.data());
48     }
49     int argc = pseudo_argv.size();
50     auto argv = pseudo_argv.data();
51     gflags::AllowCommandLineReparsing();  // Support future non-gflags flags
52     gflags::ParseCommandLineNonHelpFlags(&argc, &argv,
53                                          /* remove_flags */ false);
54     return {};
55   }
WriteGflagsCompatHelpXml(std::ostream & out) const56   bool WriteGflagsCompatHelpXml(std::ostream& out) const override {
57     // Lifted from external/gflags/src/gflags_reporting.cc:ShowXMLOfFlags
58     std::vector<gflags::CommandLineFlagInfo> flags;
59     gflags::GetAllFlags(&flags);
60     for (const auto& flag : flags) {
61       // From external/gflags/src/gflags_reporting.cc:DescribeOneFlagInXML
62       out << "<flag>\n";
63       out << "  <file>" << XmlEscape(flag.filename) << "</file>\n";
64       out << "  <name>" << XmlEscape(flag.name) << "</name>\n";
65       out << "  <meaning>" << XmlEscape(flag.description) << "</meaning>\n";
66       out << "  <default>" << XmlEscape(flag.default_value) << "</default>\n";
67       out << "  <current>" << XmlEscape(flag.current_value) << "</current>\n";
68       out << "  <type>" << XmlEscape(flag.type) << "</type>\n";
69       out << "</flag>\n";
70     }
71     return true;
72   }
73 
74  private:
75   ConfigFlag& config_;
76 };
77 
GflagsComponent()78 fruit::Component<fruit::Required<ConfigFlag>, ParseGflags> GflagsComponent() {
79   return fruit::createComponent()
80       .bind<ParseGflags, ParseGflagsImpl>()
81       .addMultibinding<FlagFeature, ParseGflags>();
82 }
83 
84 }  // namespace cuttlefish
85