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) placeholder";
91 case Level::UNSPECIFIED:
92 return "Level unspecified";
93 default:
94 return "Level " + std::to_string(level);
95 }
96 }
97
98 } // namespace vintf
99 } // namespace android
100
101 DEFINE_string(input, "", "Input compatibility matrix file");
ValidateInput(const char *,const std::string & value)102 static bool ValidateInput(const char* /* flagname */, const std::string& value) {
103 return !value.empty();
104 }
105 DEFINE_validator(input, &ValidateInput);
106
107 DEFINE_bool(level, false, "Write level (FCM version) of the compatibility matrix.");
108 DEFINE_bool(level_name, false, "Write level name (FCM version) of the compatibility matrix.");
109 DEFINE_bool(interfaces, false, "Write strings like \"android.hardware.foo@1.0::IFoo\".");
110 DEFINE_bool(instances, false, "Write strings like \"android.hardware.foo@1.0::IFoo/default\".");
111 DEFINE_bool(requirement, false, "Append optional/required after each interface / instance.");
112
main(int argc,char ** argv)113 int main(int argc, char** argv) {
114 using namespace android::vintf;
115
116 gflags::ParseCommandLineFlags(&argc, &argv, true /* remove flags */);
117
118 auto mat = readObject<CompatibilityMatrix>(FLAGS_input);
119 if (!mat) {
120 return 1;
121 }
122
123 bool written = false;
124
125 if (FLAGS_level) {
126 if (mat->level() == Level::UNSPECIFIED) {
127 LOG(WARNING) << "FCM version is unspecified.";
128 }
129 std::cout << mat->level() << std::endl;
130
131 written = true;
132 }
133
134 if (FLAGS_level_name) {
135 if (mat->level() == Level::UNSPECIFIED) {
136 LOG(WARNING) << "FCM version is unspecified.";
137 }
138 std::cout << GetDescription(mat->level()) << std::endl;
139
140 written = true;
141 }
142
143 if (FLAGS_interfaces) {
144 auto interfaces =
145 getDescription(*mat, &MatrixInstance::interfaceDescription, FLAGS_requirement);
146 if (interfaces.empty()) {
147 LOG(WARNING) << "No interfaces are found.";
148 }
149
150 for (const auto& interface : interfaces) {
151 std::cout << interface << std::endl;
152 }
153
154 written = true;
155 }
156
157 if (FLAGS_instances) {
158 auto instances = getDescription(*mat, &MatrixInstance::description, FLAGS_requirement);
159 if (instances.empty()) {
160 LOG(WARNING) << "No instances are found.";
161 }
162
163 for (const auto& instance : instances) {
164 std::cout << instance << std::endl;
165 }
166
167 written = true;
168 }
169
170 if (!written) {
171 LOG(ERROR) << "No output format is set.";
172 return 1;
173 }
174
175 return 0;
176 }
177