blob: 7b01c8df3e67abeb7c9334f85f69601a5ecde0dc [file] [log] [blame]
Yifan Hong4d18bcc2017-04-07 21:47:16 +00001/*
2 * Copyright (C) 2017 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 <stdlib.h>
18#include <unistd.h>
19
20#include <fstream>
21#include <iostream>
Yifan Hong4d18bcc2017-04-07 21:47:16 +000022#include <sstream>
23#include <string>
Yifan Hong8302cea2017-12-18 20:17:05 -080024#include <unordered_map>
Yifan Hong4d18bcc2017-04-07 21:47:16 +000025
Yifan Hong9a8b1a72017-08-25 17:55:33 -070026#include <android-base/file.h>
Yifan Hongdbe9db32017-12-11 19:06:11 -080027#include <android-base/parseint.h>
Yifan Hongb83e56b2018-01-09 14:36:30 -080028#include <android-base/strings.h>
Yifan Honga82bbdf2020-05-21 15:56:38 -070029#include <libvts_vintf_test_common/common.h>
Yifan Hong8302cea2017-12-18 20:17:05 -080030#include <vintf/AssembleVintf.h>
Yifan Hong79efa8a2017-07-06 14:10:28 -070031#include <vintf/KernelConfigParser.h>
Yifan Hong4d18bcc2017-04-07 21:47:16 +000032#include <vintf/parse_string.h>
33#include <vintf/parse_xml.h>
Yifan Hong953f2062021-06-02 14:52:08 -070034#include "constants-private.h"
Yifan Hong8302cea2017-12-18 20:17:05 -080035#include "utils.h"
Yifan Hong4d18bcc2017-04-07 21:47:16 +000036
Yifan Hong79efa8a2017-07-06 14:10:28 -070037#define BUFFER_SIZE sysconf(_SC_PAGESIZE)
38
Yifan Hong4d18bcc2017-04-07 21:47:16 +000039namespace android {
40namespace vintf {
41
Yifan Hong9a8b1a72017-08-25 17:55:33 -070042static const std::string gConfigPrefix = "android-base-";
Steve Mucklead6aa912018-07-23 15:44:01 -070043static const std::string gConfigSuffix = ".config";
44static const std::string gBaseConfig = "android-base.config";
Yifan Hong9a8b1a72017-08-25 17:55:33 -070045
Yifan Hongaa219f52017-12-18 18:51:59 -080046// An input stream with a name.
47// The input stream may be an actual file, or a stringstream for testing.
48// It takes ownership on the istream.
49class NamedIstream {
50 public:
Yifan Hongdf357172020-04-13 17:24:54 -070051 NamedIstream() = default;
Yifan Hongaa219f52017-12-18 18:51:59 -080052 NamedIstream(const std::string& name, std::unique_ptr<std::istream>&& stream)
53 : mName(name), mStream(std::move(stream)) {}
54 const std::string& name() const { return mName; }
55 std::istream& stream() { return *mStream; }
Yifan Hongdf357172020-04-13 17:24:54 -070056 bool hasStream() { return mStream != nullptr; }
Yifan Hongaa219f52017-12-18 18:51:59 -080057
58 private:
59 std::string mName;
60 std::unique_ptr<std::istream> mStream;
61};
62
Yifan Hong4d18bcc2017-04-07 21:47:16 +000063/**
64 * Slurps the device manifest file and add build time flag to it.
65 */
Yifan Hong8302cea2017-12-18 20:17:05 -080066class AssembleVintfImpl : public AssembleVintf {
Yifan Hong9a8b1a72017-08-25 17:55:33 -070067 using Condition = std::unique_ptr<KernelConfig>;
68 using ConditionedConfig = std::pair<Condition, std::vector<KernelConfig> /* configs */>;
69
70 public:
Yifan Hong8302cea2017-12-18 20:17:05 -080071 void setFakeEnv(const std::string& key, const std::string& value) { mFakeEnv[key] = value; }
72
73 std::string getEnv(const std::string& key) const {
74 auto it = mFakeEnv.find(key);
75 if (it != mFakeEnv.end()) {
76 return it->second;
77 }
78 const char* envValue = getenv(key.c_str());
79 return envValue != nullptr ? std::string(envValue) : std::string();
80 }
81
Yifan Honga28729e2018-01-17 13:40:35 -080082 // Get environment variable and split with space.
83 std::vector<std::string> getEnvList(const std::string& key) const {
84 std::vector<std::string> ret;
85 for (auto&& v : base::Split(getEnv(key), " ")) {
86 v = base::Trim(v);
87 if (!v.empty()) {
88 ret.push_back(v);
89 }
90 }
91 return ret;
92 }
93
Yifan Hong8302cea2017-12-18 20:17:05 -080094 template <typename T>
Yifan Hong4dec91d2018-08-13 12:37:47 -070095 bool getFlag(const std::string& key, T* value, bool log = true) const {
Yifan Hong8302cea2017-12-18 20:17:05 -080096 std::string envValue = getEnv(key);
97 if (envValue.empty()) {
Yifan Hong4dec91d2018-08-13 12:37:47 -070098 if (log) {
99 std::cerr << "Warning: " << key << " is missing, defaulted to " << (*value) << "."
100 << std::endl;
101 }
Yifan Hong488e16a2017-07-11 13:50:41 -0700102 return true;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000103 }
104
105 if (!parse(envValue, value)) {
106 std::cerr << "Cannot parse " << envValue << "." << std::endl;
107 return false;
108 }
109 return true;
110 }
111
Yifan Hongeff04662017-12-18 16:27:24 -0800112 /**
Yifan Hong195a5a82020-07-29 11:24:50 -0700113 * Set *out to environment variable only if *out is default constructed.
Yifan Hongce355902019-06-26 17:06:52 +0000114 * Return false if a fatal error has occurred:
115 * - The environment variable has an unknown format
116 * - The value of the environment variable does not match a predefined variable in the files
Yifan Hongeff04662017-12-18 16:27:24 -0800117 */
118 template <typename T>
Yifan Hongce355902019-06-26 17:06:52 +0000119 bool getFlagIfUnset(const std::string& envKey, T* out) const {
Yifan Hongeff04662017-12-18 16:27:24 -0800120 bool hasExistingValue = !(*out == T{});
121
122 bool hasEnvValue = false;
123 T envValue;
Yifan Hong8302cea2017-12-18 20:17:05 -0800124 std::string envStrValue = getEnv(envKey);
125 if (!envStrValue.empty()) {
126 if (!parse(envStrValue, &envValue)) {
Yifan Hongb36e85f2019-06-26 17:06:52 +0000127 std::cerr << "Cannot parse " << envValue << "." << std::endl;
Yifan Hongce355902019-06-26 17:06:52 +0000128 return false;
Yifan Hongeff04662017-12-18 16:27:24 -0800129 }
130 hasEnvValue = true;
131 }
132
133 if (hasExistingValue) {
Yifan Hongb36e85f2019-06-26 17:06:52 +0000134 if (hasEnvValue && (*out != envValue)) {
Yifan Hongce355902019-06-26 17:06:52 +0000135 std::cerr << "Cannot override existing value " << *out << " with " << envKey
136 << " (which is " << envValue << ")." << std::endl;
137 return false;
Yifan Hongeff04662017-12-18 16:27:24 -0800138 }
Yifan Hongce355902019-06-26 17:06:52 +0000139 return true;
Yifan Hongeff04662017-12-18 16:27:24 -0800140 }
Yifan Hongb36e85f2019-06-26 17:06:52 +0000141 if (hasEnvValue) {
142 *out = envValue;
Yifan Hongeff04662017-12-18 16:27:24 -0800143 }
Yifan Hongce355902019-06-26 17:06:52 +0000144 return true;
Yifan Hongeff04662017-12-18 16:27:24 -0800145 }
146
Yifan Hong8302cea2017-12-18 20:17:05 -0800147 bool getBooleanFlag(const std::string& key) const { return getEnv(key) == std::string("true"); }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800148
Yifan Hong8302cea2017-12-18 20:17:05 -0800149 size_t getIntegerFlag(const std::string& key, size_t defaultValue = 0) const {
150 std::string envValue = getEnv(key);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800151 if (envValue.empty()) {
152 return defaultValue;
153 }
154 size_t value;
155 if (!base::ParseUint(envValue, &value)) {
156 std::cerr << "Error: " << key << " must be a number." << std::endl;
157 return defaultValue;
158 }
159 return value;
160 }
161
Yifan Hong4650ad82017-05-01 17:28:02 -0700162 static std::string read(std::basic_istream<char>& is) {
163 std::stringstream ss;
164 ss << is.rdbuf();
165 return ss.str();
166 }
167
Yifan Hong4072b252020-03-27 16:54:16 -0700168 // Return true if name of file is "android-base.config". This file must be specified
169 // exactly once for each kernel version. These requirements do not have any conditions.
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700170 static bool isCommonConfig(const std::string& path) {
171 return ::android::base::Basename(path) == gBaseConfig;
172 }
173
Yifan Hong4072b252020-03-27 16:54:16 -0700174 // Return true if name of file matches "android-base-foo.config".
175 // Zero or more conditional configs may be specified for each kernel version. These
176 // requirements are conditional on CONFIG_FOO=y.
177 static bool isConditionalConfig(const std::string& path) {
178 auto fname = ::android::base::Basename(path);
179 return ::android::base::StartsWith(fname, gConfigPrefix) &&
180 ::android::base::EndsWith(fname, gConfigSuffix);
181 }
182
183 // Return true for all other file names (i.e. not android-base.config, and not conditional
184 // configs.)
185 // Zero or more conditional configs may be specified for each kernel version.
186 // These requirements do not have any conditions.
187 static bool isExtraCommonConfig(const std::string& path) {
188 return !isCommonConfig(path) && !isConditionalConfig(path);
189 }
190
Yifan Hong079ec242017-08-25 18:53:38 -0700191 // nullptr on any error, otherwise the condition.
192 static Condition generateCondition(const std::string& path) {
Yifan Hong4072b252020-03-27 16:54:16 -0700193 if (!isConditionalConfig(path)) {
Yifan Hong079ec242017-08-25 18:53:38 -0700194 return nullptr;
195 }
Yifan Hong4072b252020-03-27 16:54:16 -0700196 auto fname = ::android::base::Basename(path);
Yifan Hong079ec242017-08-25 18:53:38 -0700197 std::string sub = fname.substr(gConfigPrefix.size(),
198 fname.size() - gConfigPrefix.size() - gConfigSuffix.size());
199 if (sub.empty()) {
200 return nullptr; // should not happen
201 }
202 for (size_t i = 0; i < sub.size(); ++i) {
203 if (sub[i] == '-') {
204 sub[i] = '_';
205 continue;
206 }
207 if (isalnum(sub[i])) {
208 sub[i] = toupper(sub[i]);
209 continue;
210 }
211 std::cerr << "'" << fname << "' (in " << path
212 << ") is not a valid kernel config file name. Must match regex: "
Steve Mucklead6aa912018-07-23 15:44:01 -0700213 << "android-base(-[0-9a-zA-Z-]+)?\\" << gConfigSuffix
214 << std::endl;
Yifan Hong079ec242017-08-25 18:53:38 -0700215 return nullptr;
216 }
217 sub.insert(0, "CONFIG_");
218 return std::make_unique<KernelConfig>(std::move(sub), Tristate::YES);
219 }
220
Yifan Hong8302cea2017-12-18 20:17:05 -0800221 static bool parseFileForKernelConfigs(std::basic_istream<char>& stream,
222 std::vector<KernelConfig>* out) {
Yifan Hong02e94002017-07-10 15:41:56 -0700223 KernelConfigParser parser(true /* processComments */, true /* relaxedFormat */);
Yifan Honga45f77d2018-12-03 16:42:52 -0800224 status_t err = parser.processAndFinish(read(stream));
Yifan Hong79efa8a2017-07-06 14:10:28 -0700225 if (err != OK) {
Yifan Hongae53a0e2017-07-07 15:19:06 -0700226 std::cerr << parser.error();
Yifan Hong79efa8a2017-07-06 14:10:28 -0700227 return false;
228 }
229
230 for (auto& configPair : parser.configs()) {
231 out->push_back({});
232 KernelConfig& config = out->back();
233 config.first = std::move(configPair.first);
234 if (!parseKernelConfigTypedValue(configPair.second, &config.second)) {
235 std::cerr << "Unknown value type for key = '" << config.first << "', value = '"
236 << configPair.second << "'\n";
237 return false;
238 }
239 }
240 return true;
241 }
242
Yifan Hong8302cea2017-12-18 20:17:05 -0800243 static bool parseFilesForKernelConfigs(std::vector<NamedIstream>* streams,
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700244 std::vector<ConditionedConfig>* out) {
245 out->clear();
246 ConditionedConfig commonConfig;
247 bool foundCommonConfig = false;
Steve Muckle0bef8682017-07-31 15:47:15 -0700248 bool ret = true;
Yifan Hong8302cea2017-12-18 20:17:05 -0800249
250 for (auto& namedStream : *streams) {
Yifan Hong4072b252020-03-27 16:54:16 -0700251 if (isCommonConfig(namedStream.name()) || isExtraCommonConfig(namedStream.name())) {
252 if (!parseFileForKernelConfigs(namedStream.stream(), &commonConfig.second)) {
253 std::cerr << "Failed to generate common configs for file "
254 << namedStream.name();
255 ret = false;
256 }
257 if (isCommonConfig(namedStream.name())) {
258 foundCommonConfig = true;
259 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700260 } else {
Yifan Hong8302cea2017-12-18 20:17:05 -0800261 Condition condition = generateCondition(namedStream.name());
Yifan Hong4072b252020-03-27 16:54:16 -0700262 if (condition == nullptr) {
263 std::cerr << "Failed to generate conditional configs for file "
264 << namedStream.name();
265 ret = false;
266 }
Yifan Hong079ec242017-08-25 18:53:38 -0700267
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700268 std::vector<KernelConfig> kernelConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800269 if ((ret &= parseFileForKernelConfigs(namedStream.stream(), &kernelConfigs)))
Yifan Hong079ec242017-08-25 18:53:38 -0700270 out->emplace_back(std::move(condition), std::move(kernelConfigs));
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700271 }
Steve Muckle0bef8682017-07-31 15:47:15 -0700272 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700273
274 if (!foundCommonConfig) {
Steve Mucklead6aa912018-07-23 15:44:01 -0700275 std::cerr << "No " << gBaseConfig << " is found in these paths:" << std::endl;
Yifan Hong8302cea2017-12-18 20:17:05 -0800276 for (auto& namedStream : *streams) {
277 std::cerr << " " << namedStream.name() << std::endl;
278 }
Yifan Hong4072b252020-03-27 16:54:16 -0700279 ret = false;
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700280 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700281 // first element is always common configs (no conditions).
282 out->insert(out->begin(), std::move(commonConfig));
Steve Muckle0bef8682017-07-31 15:47:15 -0700283 return ret;
284 }
285
Yifan Hong8302cea2017-12-18 20:17:05 -0800286 std::basic_ostream<char>& out() const { return mOutRef == nullptr ? std::cout : *mOutRef; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700287
Yifan Hong1044fd12018-02-27 14:51:54 -0800288 // If -c is provided, check it.
289 bool checkDualFile(const HalManifest& manifest, const CompatibilityMatrix& matrix) {
290 if (getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
291 std::string error;
Yifan Hong3f743912019-12-17 14:12:04 -0800292 if (!manifest.checkCompatibility(matrix, &error, mCheckFlags)) {
Yifan Hong1044fd12018-02-27 14:51:54 -0800293 std::cerr << "Not compatible: " << error << std::endl;
294 return false;
295 }
296 }
Yifan Hong1044fd12018-02-27 14:51:54 -0800297 return true;
298 }
299
Yifan Honga83d0e42020-04-13 13:07:31 -0700300 using HalManifests = std::vector<HalManifest>;
301 using CompatibilityMatrices = std::vector<CompatibilityMatrix>;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800302
Steven Moreland5875afc2018-04-05 13:14:08 -0700303 template <typename M>
Yifan Honga83d0e42020-04-13 13:07:31 -0700304 void outputInputs(const std::vector<M>& inputs) {
Steven Moreland5875afc2018-04-05 13:14:08 -0700305 out() << "<!--" << std::endl;
306 out() << " Input:" << std::endl;
307 for (const auto& e : inputs) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700308 if (!e.fileName().empty()) {
309 out() << " " << base::Basename(e.fileName()) << std::endl;
Steven Moreland5875afc2018-04-05 13:14:08 -0700310 }
311 }
312 out() << "-->" << std::endl;
313 }
314
Yifan Honga45f77d2018-12-03 16:42:52 -0800315 // Parse --kernel arguments and write to output manifest.
316 bool setDeviceManifestKernel(HalManifest* manifest) {
317 if (mKernels.empty()) {
318 return true;
319 }
320 if (mKernels.size() > 1) {
321 std::cerr << "Warning: multiple --kernel is specified when building device manifest. "
322 << "Only the first one will be used." << std::endl;
323 }
324 auto& kernelArg = *mKernels.begin();
325 const auto& kernelVer = kernelArg.first;
326 auto& kernelConfigFiles = kernelArg.second;
327 // addKernel() guarantees that !kernelConfigFiles.empty().
328 if (kernelConfigFiles.size() > 1) {
329 std::cerr << "Warning: multiple config files are specified in --kernel when building "
330 << "device manfiest. Only the first one will be used." << std::endl;
331 }
332
333 KernelConfigParser parser(true /* processComments */, false /* relaxedFormat */);
334 status_t err = parser.processAndFinish(read(kernelConfigFiles[0].stream()));
335 if (err != OK) {
336 std::cerr << parser.error();
337 return false;
338 }
Yifan Hong08a48712019-12-11 14:56:00 -0800339
340 // Set version and configs in manifest.
341 auto kernel_info = std::make_optional<KernelInfo>();
342 kernel_info->mVersion = kernelVer;
343 kernel_info->mConfigs = parser.configs();
344 std::string error;
345 if (!manifest->mergeKernel(&kernel_info, &error)) {
346 std::cerr << error << "\n";
347 return false;
348 }
Yifan Hong953f2062021-06-02 14:52:08 -0700349 return true;
350 }
Yifan Hong08a48712019-12-11 14:56:00 -0800351
Yifan Hong953f2062021-06-02 14:52:08 -0700352 bool checkDeviceManifestNoKernelLevel(const HalManifest& manifest) {
353 if (manifest.level() != Level::UNSPECIFIED &&
354 manifest.level() >= details::kEnforceDeviceManifestNoKernelLevel &&
355 // Use manifest.kernel()->level() directly because inferredKernelLevel()
356 // reads manifest.level().
357 manifest.kernel().has_value() && manifest.kernel()->level() != Level::UNSPECIFIED) {
358 std::cerr << "Error: Device manifest with level " << manifest.level()
359 << " must not set kernel level " << manifest.kernel()->level() << std::endl;
360 return false;
361 }
Yifan Honga45f77d2018-12-03 16:42:52 -0800362 return true;
363 }
364
Yifan Hongdbe9db32017-12-11 19:06:11 -0800365 bool assembleHalManifest(HalManifests* halManifests) {
Yifan Hong4650ad82017-05-01 17:28:02 -0700366 std::string error;
Yifan Honga83d0e42020-04-13 13:07:31 -0700367 HalManifest* halManifest = &halManifests->front();
Yifan Hongdbe9db32017-12-11 19:06:11 -0800368 for (auto it = halManifests->begin() + 1; it != halManifests->end(); ++it) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700369 const std::string& path = it->fileName();
370 HalManifest& manifestToAdd = *it;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800371
Yifan Hong0a35ab12018-06-29 11:32:57 -0700372 if (manifestToAdd.level() != Level::UNSPECIFIED) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800373 if (halManifest->level() == Level::UNSPECIFIED) {
Yifan Hong0a35ab12018-06-29 11:32:57 -0700374 halManifest->mLevel = manifestToAdd.level();
375 } else if (halManifest->level() != manifestToAdd.level()) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800376 std::cerr << "Inconsistent FCM Version in HAL manifests:" << std::endl
Yifan Honga83d0e42020-04-13 13:07:31 -0700377 << " File '" << halManifests->front().fileName() << "' has level "
Yifan Hongdbe9db32017-12-11 19:06:11 -0800378 << halManifest->level() << std::endl
Yifan Hong0a35ab12018-06-29 11:32:57 -0700379 << " File '" << path << "' has level " << manifestToAdd.level()
Yifan Hongdbe9db32017-12-11 19:06:11 -0800380 << std::endl;
381 return false;
382 }
383 }
384
Yifan Hong4c6962d2019-01-30 15:50:46 -0800385 if (!halManifest->addAll(&manifestToAdd, &error)) {
386 std::cerr << "File \"" << path << "\" cannot be added: " << error << std::endl;
Yifan Hong0a35ab12018-06-29 11:32:57 -0700387 return false;
388 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800389 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700390
391 if (halManifest->mType == SchemaType::DEVICE) {
Yifan Hongce355902019-06-26 17:06:52 +0000392 if (!getFlagIfUnset("BOARD_SEPOLICY_VERS", &halManifest->device.mSepolicyVersion)) {
393 return false;
394 }
Steven Morelandb1392772018-05-01 11:21:35 -0700395
Yifan Hongdbe9db32017-12-11 19:06:11 -0800396 if (!setDeviceFcmVersion(halManifest)) {
397 return false;
398 }
Yifan Honga45f77d2018-12-03 16:42:52 -0800399
400 if (!setDeviceManifestKernel(halManifest)) {
401 return false;
402 }
Yifan Hong953f2062021-06-02 14:52:08 -0700403
404 if (!checkDeviceManifestNoKernelLevel(*halManifest)) {
405 return false;
406 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700407 }
408
Yifan Hongfeb454e2018-01-09 16:16:40 -0800409 if (halManifest->mType == SchemaType::FRAMEWORK) {
Yifan Honga28729e2018-01-17 13:40:35 -0800410 for (auto&& v : getEnvList("PROVIDED_VNDK_VERSIONS")) {
411 halManifest->framework.mVendorNdks.emplace_back(std::move(v));
412 }
413
Yifan Hong8b8492b2018-01-22 14:08:40 -0800414 for (auto&& v : getEnvList("PLATFORM_SYSTEMSDK_VERSIONS")) {
Yifan Honga28729e2018-01-17 13:40:35 -0800415 halManifest->framework.mSystemSdk.mVersions.emplace(std::move(v));
Yifan Hongfeb454e2018-01-09 16:16:40 -0800416 }
417 }
418
Steven Moreland5875afc2018-04-05 13:14:08 -0700419 outputInputs(*halManifests);
420
Yifan Hong9aa63702017-05-16 16:37:50 -0700421 if (mOutputMatrix) {
422 CompatibilityMatrix generatedMatrix = halManifest->generateCompatibleMatrix();
Yifan Hong3f743912019-12-17 14:12:04 -0800423 if (!halManifest->checkCompatibility(generatedMatrix, &error, mCheckFlags)) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700424 std::cerr << "FATAL ERROR: cannot generate a compatible matrix: " << error
425 << std::endl;
426 }
427 out() << "<!-- \n"
428 " Autogenerated skeleton compatibility matrix. \n"
429 " Use with caution. Modify it to suit your needs.\n"
430 " All HALs are set to optional.\n"
431 " Many entries other than HALs are zero-filled and\n"
432 " require human attention. \n"
433 "-->\n"
Yifan Hong25e2a772021-04-16 18:34:28 -0700434 << toXml(generatedMatrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700435 } else {
Yifan Hong25e2a772021-04-16 18:34:28 -0700436 out() << toXml(*halManifest, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700437 }
438 out().flush();
439
Yifan Hongdf357172020-04-13 17:24:54 -0700440 if (mCheckFile.hasStream()) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700441 CompatibilityMatrix checkMatrix;
Yifan Hongdf357172020-04-13 17:24:54 -0700442 checkMatrix.setFileName(mCheckFile.name());
Yifan Hong25e2a772021-04-16 18:34:28 -0700443 if (!fromXml(&checkMatrix, read(mCheckFile.stream()), &error)) {
Yifan Hong94757062018-02-09 16:36:31 -0800444 std::cerr << "Cannot parse check file as a compatibility matrix: " << error
445 << std::endl;
Yifan Hong9aa63702017-05-16 16:37:50 -0700446 return false;
447 }
Yifan Hong1044fd12018-02-27 14:51:54 -0800448 if (!checkDualFile(*halManifest, checkMatrix)) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700449 return false;
450 }
451 }
452
453 return true;
454 }
455
Yifan Honge7837b12018-10-11 10:38:57 -0700456 // Parse --kernel arguments and write to output matrix.
Yifan Honge88e1672017-08-24 14:42:54 -0700457 bool assembleFrameworkCompatibilityMatrixKernels(CompatibilityMatrix* matrix) {
Yifan Hong8302cea2017-12-18 20:17:05 -0800458 for (auto& pair : mKernels) {
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700459 std::vector<ConditionedConfig> conditionedConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800460 if (!parseFilesForKernelConfigs(&pair.second, &conditionedConfigs)) {
Yifan Honge88e1672017-08-24 14:42:54 -0700461 return false;
462 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700463 for (ConditionedConfig& conditionedConfig : conditionedConfigs) {
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800464 MatrixKernel kernel(KernelVersion{pair.first}, std::move(conditionedConfig.second));
Yifan Hong079ec242017-08-25 18:53:38 -0700465 if (conditionedConfig.first != nullptr)
466 kernel.mConditions.push_back(std::move(*conditionedConfig.first));
Yifan Honge7837b12018-10-11 10:38:57 -0700467 std::string error;
468 if (!matrix->addKernel(std::move(kernel), &error)) {
469 std::cerr << "Error:" << error << std::endl;
470 return false;
471 };
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700472 }
Yifan Honge88e1672017-08-24 14:42:54 -0700473 }
474 return true;
475 }
476
Yifan Hongdbe9db32017-12-11 19:06:11 -0800477 bool setDeviceFcmVersion(HalManifest* manifest) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700478 // Not needed for generating empty manifest for DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE.
Yifan Hong2eee1982018-03-29 10:55:30 -0700479 if (getBooleanFlag("VINTF_IGNORE_TARGET_FCM_VERSION")) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700480 return true;
481 }
482
Yifan Hongdbe9db32017-12-11 19:06:11 -0800483 size_t shippingApiLevel = getIntegerFlag("PRODUCT_SHIPPING_API_LEVEL");
Yifan Hong9aa63702017-05-16 16:37:50 -0700484
Yifan Hongdbe9db32017-12-11 19:06:11 -0800485 if (manifest->level() != Level::UNSPECIFIED) {
Yifan Honga82bbdf2020-05-21 15:56:38 -0700486 if (shippingApiLevel != 0) {
487 auto res = android::vintf::testing::TestTargetFcmVersion(manifest->level(),
488 shippingApiLevel);
489 if (!res.ok()) std::cerr << "Warning: " << res.error() << std::endl;
490 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800491 return true;
492 }
493 if (!getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
494 manifest->mLevel = Level::LEGACY;
495 return true;
496 }
497 // TODO(b/70628538): Do not infer from Shipping API level.
498 if (shippingApiLevel) {
499 std::cerr << "Warning: Shipping FCM Version is inferred from Shipping API level. "
500 << "Declare Shipping FCM Version in device manifest directly." << std::endl;
Yifan Hongafae1982018-01-11 18:15:24 -0800501 manifest->mLevel = details::convertFromApiLevel(shippingApiLevel);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800502 if (manifest->mLevel == Level::UNSPECIFIED) {
503 std::cerr << "Error: Shipping FCM Version cannot be inferred from Shipping API "
504 << "level " << shippingApiLevel << "."
505 << "Declare Shipping FCM Version in device manifest directly."
506 << std::endl;
507 return false;
508 }
509 return true;
510 }
511 // TODO(b/69638851): should be an error if Shipping API level is not defined.
512 // For now, just leave it empty; when framework compatibility matrix is built,
513 // lowest FCM Version is assumed.
514 std::cerr << "Warning: Shipping FCM Version cannot be inferred, because:" << std::endl
515 << " (1) It is not explicitly declared in device manifest;" << std::endl
516 << " (2) PRODUCT_ENFORCE_VINTF_MANIFEST is set to true;" << std::endl
517 << " (3) PRODUCT_SHIPPING_API_LEVEL is undefined." << std::endl
518 << "Assuming 'unspecified' Shipping FCM Version. " << std::endl
519 << "To remove this warning, define 'level' attribute in device manifest."
520 << std::endl;
521 return true;
522 }
523
524 Level getLowestFcmVersion(const CompatibilityMatrices& matrices) {
525 Level ret = Level::UNSPECIFIED;
526 for (const auto& e : matrices) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700527 if (ret == Level::UNSPECIFIED || ret > e.level()) {
528 ret = e.level();
Yifan Hongdbe9db32017-12-11 19:06:11 -0800529 }
530 }
531 return ret;
532 }
533
534 bool assembleCompatibilityMatrix(CompatibilityMatrices* matrices) {
535 std::string error;
536 CompatibilityMatrix* matrix = nullptr;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800537 std::unique_ptr<HalManifest> checkManifest;
Yifan Honge7837b12018-10-11 10:38:57 -0700538 std::unique_ptr<CompatibilityMatrix> builtMatrix;
539
Yifan Hongdf357172020-04-13 17:24:54 -0700540 if (mCheckFile.hasStream()) {
Yifan Honge7837b12018-10-11 10:38:57 -0700541 checkManifest = std::make_unique<HalManifest>();
Yifan Hongdf357172020-04-13 17:24:54 -0700542 checkManifest->setFileName(mCheckFile.name());
Yifan Hong25e2a772021-04-16 18:34:28 -0700543 if (!fromXml(checkManifest.get(), read(mCheckFile.stream()), &error)) {
Yifan Honge7837b12018-10-11 10:38:57 -0700544 std::cerr << "Cannot parse check file as a HAL manifest: " << error << std::endl;
545 return false;
546 }
547 }
548
Yifan Honga83d0e42020-04-13 13:07:31 -0700549 if (matrices->front().mType == SchemaType::DEVICE) {
Yifan Hong5a51ea52019-04-22 14:54:57 -0700550 builtMatrix = CompatibilityMatrix::combineDeviceMatrices(matrices, &error);
551 matrix = builtMatrix.get();
552
553 if (matrix == nullptr) {
554 std::cerr << error << std::endl;
Yifan Honge7837b12018-10-11 10:38:57 -0700555 return false;
556 }
557
Yifan Hongfeb454e2018-01-09 16:16:40 -0800558 auto vndkVersion = base::Trim(getEnv("REQUIRED_VNDK_VERSION"));
559 if (!vndkVersion.empty()) {
560 auto& valueInMatrix = matrix->device.mVendorNdk;
561 if (!valueInMatrix.version().empty() && valueInMatrix.version() != vndkVersion) {
562 std::cerr << "Hard-coded <vendor-ndk> version in device compatibility matrix ("
Yifan Honga83d0e42020-04-13 13:07:31 -0700563 << matrices->front().fileName() << "), '" << valueInMatrix.version()
Yifan Hongfeb454e2018-01-09 16:16:40 -0800564 << "', does not match value inferred "
565 << "from BOARD_VNDK_VERSION '" << vndkVersion << "'" << std::endl;
566 return false;
567 }
568 valueInMatrix = VendorNdk{std::move(vndkVersion)};
569 }
Yifan Honga28729e2018-01-17 13:40:35 -0800570
571 for (auto&& v : getEnvList("BOARD_SYSTEMSDK_VERSIONS")) {
572 matrix->device.mSystemSdk.mVersions.emplace(std::move(v));
573 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800574 }
575
Yifan Honga83d0e42020-04-13 13:07:31 -0700576 if (matrices->front().mType == SchemaType::FRAMEWORK) {
Yifan Honge7837b12018-10-11 10:38:57 -0700577 Level deviceLevel =
578 checkManifest != nullptr ? checkManifest->level() : Level::UNSPECIFIED;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800579 if (deviceLevel == Level::UNSPECIFIED) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800580 deviceLevel = getLowestFcmVersion(*matrices);
Yifan Honge7837b12018-10-11 10:38:57 -0700581 if (checkManifest != nullptr && deviceLevel != Level::UNSPECIFIED) {
582 std::cerr << "Warning: No Target FCM Version for device. Assuming \""
583 << to_string(deviceLevel)
584 << "\" when building final framework compatibility matrix."
585 << std::endl;
586 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800587 }
Yifan Hong507815b2021-05-21 16:48:37 -0700588 // No <kernel> tags to assemble at this point
589 const auto kernelLevel = Level::UNSPECIFIED;
590 builtMatrix = CompatibilityMatrix::combine(deviceLevel, kernelLevel, matrices, &error);
Yifan Honge7837b12018-10-11 10:38:57 -0700591 matrix = builtMatrix.get();
Yifan Hongdbe9db32017-12-11 19:06:11 -0800592
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700593 if (matrix == nullptr) {
594 std::cerr << error << std::endl;
595 return false;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800596 }
597
Yifan Honge88e1672017-08-24 14:42:54 -0700598 if (!assembleFrameworkCompatibilityMatrixKernels(matrix)) {
599 return false;
Yifan Hong79efa8a2017-07-06 14:10:28 -0700600 }
Yifan Honge88e1672017-08-24 14:42:54 -0700601
Yifan Hong321ee222018-02-02 12:46:52 -0800602 // Add PLATFORM_SEPOLICY_* to sepolicy.sepolicy-version. Remove dupes.
603 std::set<Version> sepolicyVersions;
604 auto sepolicyVersionStrings = getEnvList("PLATFORM_SEPOLICY_COMPAT_VERSIONS");
605 auto currentSepolicyVersionString = getEnv("PLATFORM_SEPOLICY_VERSION");
606 if (!currentSepolicyVersionString.empty()) {
607 sepolicyVersionStrings.push_back(currentSepolicyVersionString);
608 }
609 for (auto&& s : sepolicyVersionStrings) {
610 Version v;
611 if (!parse(s, &v)) {
612 std::cerr << "Error: unknown sepolicy version '" << s << "' specified by "
613 << (s == currentSepolicyVersionString
614 ? "PLATFORM_SEPOLICY_VERSION"
615 : "PLATFORM_SEPOLICY_COMPAT_VERSIONS")
616 << ".";
617 return false;
618 }
619 sepolicyVersions.insert(v);
620 }
621 for (auto&& v : sepolicyVersions) {
622 matrix->framework.mSepolicy.mSepolicyVersionRanges.emplace_back(v.majorVer,
623 v.minorVer);
Yifan Hong7f6c00c2017-07-06 19:50:29 +0000624 }
Yifan Hongf4135012017-12-18 17:27:19 -0800625
Yifan Hongce355902019-06-26 17:06:52 +0000626 if (!getFlagIfUnset("POLICYVERS",
627 &matrix->framework.mSepolicy.mKernelSepolicyVersion)) {
628 return false;
629 }
630 if (!getFlagIfUnset("FRAMEWORK_VBMETA_VERSION", &matrix->framework.mAvbMetaVersion)) {
631 return false;
632 }
Yifan Hong4dec91d2018-08-13 12:37:47 -0700633 // Hard-override existing AVB version
634 getFlag("FRAMEWORK_VBMETA_VERSION_OVERRIDE", &matrix->framework.mAvbMetaVersion,
635 false /* log */);
Yifan Hong9aa63702017-05-16 16:37:50 -0700636 }
Steven Moreland5875afc2018-04-05 13:14:08 -0700637 outputInputs(*matrices);
Yifan Hong25e2a772021-04-16 18:34:28 -0700638 out() << toXml(*matrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700639 out().flush();
640
Yifan Hong1044fd12018-02-27 14:51:54 -0800641 if (checkManifest != nullptr && !checkDualFile(*checkManifest, *matrix)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800642 return false;
Yifan Hong9aa63702017-05-16 16:37:50 -0700643 }
644
645 return true;
646 }
647
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700648 enum AssembleStatus { SUCCESS, FAIL_AND_EXIT, TRY_NEXT };
649 template <typename Schema, typename AssembleFunc>
Yifan Hong25e2a772021-04-16 18:34:28 -0700650 AssembleStatus tryAssemble(const std::string& schemaName, AssembleFunc assemble,
651 std::string* error) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700652 std::vector<Schema> schemas;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700653 Schema schema;
Yifan Honga83d0e42020-04-13 13:07:31 -0700654 schema.setFileName(mInFiles.front().name());
Yifan Hong25e2a772021-04-16 18:34:28 -0700655 if (!fromXml(&schema, read(mInFiles.front().stream()), error)) {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700656 return TRY_NEXT;
657 }
658 auto firstType = schema.type();
Yifan Honga83d0e42020-04-13 13:07:31 -0700659 schemas.emplace_back(std::move(schema));
Yifan Hongdbe9db32017-12-11 19:06:11 -0800660
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700661 for (auto it = mInFiles.begin() + 1; it != mInFiles.end(); ++it) {
662 Schema additionalSchema;
Yifan Hongaa219f52017-12-18 18:51:59 -0800663 const std::string& fileName = it->name();
Yifan Honga83d0e42020-04-13 13:07:31 -0700664 additionalSchema.setFileName(fileName);
Yifan Hong25e2a772021-04-16 18:34:28 -0700665 if (!fromXml(&additionalSchema, read(it->stream()), error)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800666 std::cerr << "File \"" << fileName << "\" is not a valid " << firstType << " "
667 << schemaName << " (but the first file is a valid " << firstType << " "
Yifan Hong94757062018-02-09 16:36:31 -0800668 << schemaName << "). Error: " << *error << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700669 return FAIL_AND_EXIT;
670 }
671 if (additionalSchema.type() != firstType) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800672 std::cerr << "File \"" << fileName << "\" is a " << additionalSchema.type() << " "
673 << schemaName << " (but a " << firstType << " " << schemaName
674 << " is expected)." << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700675 return FAIL_AND_EXIT;
676 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800677
Yifan Honga83d0e42020-04-13 13:07:31 -0700678 schemas.emplace_back(std::move(additionalSchema));
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700679 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800680 return assemble(&schemas) ? SUCCESS : FAIL_AND_EXIT;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700681 }
682
Yifan Hong8302cea2017-12-18 20:17:05 -0800683 bool assemble() override {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700684 using std::placeholders::_1;
685 if (mInFiles.empty()) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700686 std::cerr << "Missing input file." << std::endl;
687 return false;
688 }
689
Yifan Hong94757062018-02-09 16:36:31 -0800690 std::string manifestError;
Yifan Hong25e2a772021-04-16 18:34:28 -0700691 auto status = tryAssemble<HalManifest>(
692 "manifest", std::bind(&AssembleVintfImpl::assembleHalManifest, this, _1),
693 &manifestError);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700694 if (status == SUCCESS) return true;
695 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000696
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700697 resetInFiles();
Yifan Honga59d2562017-04-18 18:01:16 -0700698
Yifan Hong94757062018-02-09 16:36:31 -0800699 std::string matrixError;
Yifan Hong25e2a772021-04-16 18:34:28 -0700700 status = tryAssemble<CompatibilityMatrix>(
701 "compatibility matrix",
702 std::bind(&AssembleVintfImpl::assembleCompatibilityMatrix, this, _1), &matrixError);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700703 if (status == SUCCESS) return true;
704 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000705
Yifan Hong959ee1b2017-04-28 14:37:56 -0700706 std::cerr << "Input file has unknown format." << std::endl
Yifan Hong94757062018-02-09 16:36:31 -0800707 << "Error when attempting to convert to manifest: " << manifestError << std::endl
708 << "Error when attempting to convert to compatibility matrix: " << matrixError
709 << std::endl;
Yifan Hong959ee1b2017-04-28 14:37:56 -0700710 return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000711 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700712
Yifan Hong8302cea2017-12-18 20:17:05 -0800713 std::ostream& setOutputStream(Ostream&& out) override {
714 mOutRef = std::move(out);
715 return *mOutRef;
Yifan Hong9aa63702017-05-16 16:37:50 -0700716 }
717
Yifan Hong8302cea2017-12-18 20:17:05 -0800718 std::istream& addInputStream(const std::string& name, Istream&& in) override {
719 auto it = mInFiles.emplace(mInFiles.end(), name, std::move(in));
720 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700721 }
722
Yifan Hongdf357172020-04-13 17:24:54 -0700723 std::istream& setCheckInputStream(const std::string& name, Istream&& in) override {
724 mCheckFile = NamedIstream(name, std::move(in));
725 return mCheckFile.stream();
Yifan Hong8302cea2017-12-18 20:17:05 -0800726 }
727
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800728 bool hasKernelVersion(const KernelVersion& kernelVer) const override {
Yifan Hong8302cea2017-12-18 20:17:05 -0800729 return mKernels.find(kernelVer) != mKernels.end();
730 }
731
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800732 std::istream& addKernelConfigInputStream(const KernelVersion& kernelVer,
733 const std::string& name, Istream&& in) override {
Yifan Hong8302cea2017-12-18 20:17:05 -0800734 auto&& kernel = mKernels[kernelVer];
735 auto it = kernel.emplace(kernel.end(), name, std::move(in));
736 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700737 }
738
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700739 void resetInFiles() {
740 for (auto& inFile : mInFiles) {
Yifan Hongaa219f52017-12-18 18:51:59 -0800741 inFile.stream().clear();
742 inFile.stream().seekg(0);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700743 }
744 }
745
Yifan Hong8302cea2017-12-18 20:17:05 -0800746 void setOutputMatrix() override { mOutputMatrix = true; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700747
Yifan Hong8302cea2017-12-18 20:17:05 -0800748 bool setHalsOnly() override {
Yifan Hong5c9ff702018-08-07 17:26:12 -0700749 if (mHasSetHalsOnlyFlag) {
750 std::cerr << "Error: Cannot set --hals-only with --no-hals." << std::endl;
751 return false;
752 }
753 // Just override it with HALS_ONLY because other flags that modify mSerializeFlags
754 // does not interfere with this (except --no-hals).
755 mSerializeFlags = SerializeFlags::HALS_ONLY;
756 mHasSetHalsOnlyFlag = true;
Yifan Honga2635c42017-12-12 13:20:33 -0800757 return true;
758 }
759
Yifan Hong8302cea2017-12-18 20:17:05 -0800760 bool setNoHals() override {
Yifan Hong5c9ff702018-08-07 17:26:12 -0700761 if (mHasSetHalsOnlyFlag) {
762 std::cerr << "Error: Cannot set --hals-only with --no-hals." << std::endl;
763 return false;
764 }
765 mSerializeFlags = mSerializeFlags.disableHals();
766 mHasSetHalsOnlyFlag = true;
Yifan Honga2635c42017-12-12 13:20:33 -0800767 return true;
768 }
769
Yifan Hong86678e42018-07-26 11:38:54 -0700770 bool setNoKernelRequirements() override {
Yifan Hong5c9ff702018-08-07 17:26:12 -0700771 mSerializeFlags = mSerializeFlags.disableKernelConfigs().disableKernelMinorRevision();
Yifan Hong3f743912019-12-17 14:12:04 -0800772 mCheckFlags = mCheckFlags.disableKernel();
Yifan Hong86678e42018-07-26 11:38:54 -0700773 return true;
774 }
775
Yifan Hong9aa63702017-05-16 16:37:50 -0700776 private:
Yifan Hongaa219f52017-12-18 18:51:59 -0800777 std::vector<NamedIstream> mInFiles;
Yifan Hong8302cea2017-12-18 20:17:05 -0800778 Ostream mOutRef;
Yifan Hongdf357172020-04-13 17:24:54 -0700779 NamedIstream mCheckFile;
Yifan Hong9aa63702017-05-16 16:37:50 -0700780 bool mOutputMatrix = false;
Yifan Hong5c9ff702018-08-07 17:26:12 -0700781 bool mHasSetHalsOnlyFlag = false;
Yifan Hongba588bc2018-08-08 14:19:41 -0700782 SerializeFlags::Type mSerializeFlags = SerializeFlags::EVERYTHING;
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800783 std::map<KernelVersion, std::vector<NamedIstream>> mKernels;
Yifan Hong8302cea2017-12-18 20:17:05 -0800784 std::map<std::string, std::string> mFakeEnv;
Yifan Hong3f743912019-12-17 14:12:04 -0800785 CheckFlags::Type mCheckFlags = CheckFlags::DEFAULT;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000786};
787
Yifan Hong8302cea2017-12-18 20:17:05 -0800788bool AssembleVintf::openOutFile(const std::string& path) {
789 return static_cast<std::ofstream&>(setOutputStream(std::make_unique<std::ofstream>(path)))
790 .is_open();
791}
792
793bool AssembleVintf::openInFile(const std::string& path) {
794 return static_cast<std::ifstream&>(addInputStream(path, std::make_unique<std::ifstream>(path)))
795 .is_open();
796}
797
798bool AssembleVintf::openCheckFile(const std::string& path) {
Yifan Hongdf357172020-04-13 17:24:54 -0700799 return static_cast<std::ifstream&>(
800 setCheckInputStream(path, std::make_unique<std::ifstream>(path)))
Yifan Hong8302cea2017-12-18 20:17:05 -0800801 .is_open();
802}
803
804bool AssembleVintf::addKernel(const std::string& kernelArg) {
Yifan Hongb83e56b2018-01-09 14:36:30 -0800805 auto tokens = base::Split(kernelArg, ":");
Yifan Hong8302cea2017-12-18 20:17:05 -0800806 if (tokens.size() <= 1) {
807 std::cerr << "Unrecognized --kernel option '" << kernelArg << "'" << std::endl;
808 return false;
809 }
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800810 KernelVersion kernelVer;
Yifan Hong8302cea2017-12-18 20:17:05 -0800811 if (!parse(tokens.front(), &kernelVer)) {
812 std::cerr << "Unrecognized kernel version '" << tokens.front() << "'" << std::endl;
813 return false;
814 }
815 if (hasKernelVersion(kernelVer)) {
816 std::cerr << "Multiple --kernel for " << kernelVer << " is specified." << std::endl;
817 return false;
818 }
819 for (auto it = tokens.begin() + 1; it != tokens.end(); ++it) {
820 bool opened =
821 static_cast<std::ifstream&>(
822 addKernelConfigInputStream(kernelVer, *it, std::make_unique<std::ifstream>(*it)))
823 .is_open();
824 if (!opened) {
825 std::cerr << "Cannot open file '" << *it << "'." << std::endl;
826 return false;
827 }
828 }
829 return true;
830}
831
832std::unique_ptr<AssembleVintf> AssembleVintf::newInstance() {
833 return std::make_unique<AssembleVintfImpl>();
834}
835
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000836} // namespace vintf
837} // namespace android