xref: /aosp_15_r20/system/libvintf/analyze_matrix/analyze_matrix.cpp (revision 70a7ec852fcefd15a4fb57f8f183a8b1c3aacb08)
1 /*
2  * Copyright (C) 2018 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 <optional>
18 #include <set>
19 
20 #include <android-base/logging.h>
21 #include <gflags/gflags.h>
22 #include <vintf/FileSystem.h>
23 #include <vintf/parse_string.h>
24 #include <vintf/parse_xml.h>
25 
26 namespace android {
27 namespace vintf {
28 
29 namespace {
30 
31 template <typename T>
readObject(const std::string & path)32 std::optional<T> readObject(const std::string& path) {
33     std::string xml;
34     std::string error;
35     status_t err = details::FileSystemImpl().fetch(path, &xml, &error);
36     if (err != OK) {
37         LOG(ERROR) << "Cannot read '" << path << "': " << error;
38         return std::nullopt;
39     }
40     auto ret = std::make_optional<T>();
41     if (!fromXml(&ret.value(), xml, &error)) {
42         LOG(ERROR) << "Cannot parse '" << path << "': " << error;
43         return std::nullopt;
44     }
45     return ret;
46 }
47 
48 template <typename F>
getDescription(const CompatibilityMatrix & mat,F descriptionFn,bool emitReq)49 std::set<std::string> getDescription(const CompatibilityMatrix& mat, F descriptionFn,
50                                      bool emitReq) {
51     std::set<std::string> set;
52     mat.forEachInstance([&set, descriptionFn, emitReq](const auto& matrixInstance) {
53         for (auto minorVer = matrixInstance.versionRange().minMinor;
54              minorVer >= matrixInstance.versionRange().minMinor &&
55              minorVer <= matrixInstance.versionRange().maxMinor;
56              ++minorVer) {
57             Version version{matrixInstance.versionRange().majorVer, minorVer};
58             std::string s = std::invoke(descriptionFn, matrixInstance, version);
59             if (emitReq) {
60                 s += (matrixInstance.optional() ? " optional" : " required");
61             }
62             set.insert(s);
63         }
64         return true;  // continue
65     });
66     return set;
67 }
68 
69 }  // namespace
70 
GetDescription(Level level)71 std::string GetDescription(Level level) {
72     switch (level) {
73         case Level::LEGACY:
74             return "Level legacy";
75         case Level::O:
76             return "Android 8.0 (O)";
77         case Level::O_MR1:
78             return "Android 8.1 (O-MR1)";
79         case Level::P:
80             return "Android 9 (P)";
81         case Level::Q:
82             return "Android 10 (Q)";
83         case Level::R:
84             return "Android 11 (R)";
85         case Level::S:
86             return "Android 12 (S)";
87         case Level::T:
88             return "Android 13 (T)";
89         case Level::U:
90             return "Android 14 (U)";
91         case Level::V:
92             return "Android 15 (V)";
93         case Level::W:
94             // TODO(b/346861728) verify name/number once decided
95             return "Android 16 (W)";
96         case Level::UNSPECIFIED:
97             return "Level unspecified";
98         default:
99             return "Level " + to_string(level);
100     }
101 }
102 
103 }  // namespace vintf
104 }  // namespace android
105 
106 DEFINE_string(input, "", "Input compatibility matrix file");
ValidateInput(const char *,const std::string & value)107 static bool ValidateInput(const char* /* flagname */, const std::string& value) {
108     return !value.empty();
109 }
110 DEFINE_validator(input, &ValidateInput);
111 
112 DEFINE_bool(level, false, "Write level (FCM version) of the compatibility matrix.");
113 DEFINE_bool(level_name, false, "Write level name (FCM version) of the compatibility matrix.");
114 DEFINE_bool(interfaces, false, "Write strings like \"[email protected]::IFoo\".");
115 DEFINE_bool(instances, false, "Write strings like \"[email protected]::IFoo/default\".");
116 DEFINE_bool(requirement, false, "Append optional/required after each interface / instance.");
117 
main(int argc,char ** argv)118 int main(int argc, char** argv) {
119     using namespace android::vintf;
120 
121     gflags::ParseCommandLineFlags(&argc, &argv, true /* remove flags */);
122 
123     auto mat = readObject<CompatibilityMatrix>(FLAGS_input);
124     if (!mat) {
125         return 1;
126     }
127 
128     bool written = false;
129 
130     if (FLAGS_level) {
131         if (mat->level() == Level::UNSPECIFIED) {
132             LOG(WARNING) << "FCM version is unspecified.";
133         }
134         std::cout << mat->level() << std::endl;
135 
136         written = true;
137     }
138 
139     if (FLAGS_level_name) {
140         if (mat->level() == Level::UNSPECIFIED) {
141             LOG(WARNING) << "FCM version is unspecified.";
142         }
143         std::cout << GetDescription(mat->level()) << std::endl;
144 
145         written = true;
146     }
147 
148     if (FLAGS_interfaces) {
149         auto interfaces =
150             getDescription(*mat, &MatrixInstance::interfaceDescription, FLAGS_requirement);
151         if (interfaces.empty()) {
152             LOG(WARNING) << "No interfaces are found.";
153         }
154 
155         for (const auto& interface : interfaces) {
156             std::cout << interface << std::endl;
157         }
158 
159         written = true;
160     }
161 
162     if (FLAGS_instances) {
163         auto instances = getDescription(*mat, &MatrixInstance::description, FLAGS_requirement);
164         if (instances.empty()) {
165             LOG(WARNING) << "No instances are found.";
166         }
167 
168         for (const auto& instance : instances) {
169             std::cout << instance << std::endl;
170         }
171 
172         written = true;
173     }
174 
175     if (!written) {
176         LOG(ERROR) << "No output format is set.";
177         return 1;
178     }
179 
180     return 0;
181 }
182