blob: 4b0b35e14f7d8f74936fe60f34dddfcb639efe35 [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 Hong8302cea2017-12-18 20:17:05 -080034#include "utils.h"
Yifan Hong4d18bcc2017-04-07 21:47:16 +000035
Yifan Hong79efa8a2017-07-06 14:10:28 -070036#define BUFFER_SIZE sysconf(_SC_PAGESIZE)
37
Yifan Hong4d18bcc2017-04-07 21:47:16 +000038namespace android {
39namespace vintf {
40
Yifan Hong9a8b1a72017-08-25 17:55:33 -070041static const std::string gConfigPrefix = "android-base-";
Steve Mucklead6aa912018-07-23 15:44:01 -070042static const std::string gConfigSuffix = ".config";
43static const std::string gBaseConfig = "android-base.config";
Yifan Hong9a8b1a72017-08-25 17:55:33 -070044
Yifan Hongaa219f52017-12-18 18:51:59 -080045// An input stream with a name.
46// The input stream may be an actual file, or a stringstream for testing.
47// It takes ownership on the istream.
48class NamedIstream {
49 public:
Yifan Hongdf357172020-04-13 17:24:54 -070050 NamedIstream() = default;
Yifan Hongaa219f52017-12-18 18:51:59 -080051 NamedIstream(const std::string& name, std::unique_ptr<std::istream>&& stream)
52 : mName(name), mStream(std::move(stream)) {}
53 const std::string& name() const { return mName; }
54 std::istream& stream() { return *mStream; }
Yifan Hongdf357172020-04-13 17:24:54 -070055 bool hasStream() { return mStream != nullptr; }
Yifan Hongaa219f52017-12-18 18:51:59 -080056
57 private:
58 std::string mName;
59 std::unique_ptr<std::istream> mStream;
60};
61
Yifan Hong4d18bcc2017-04-07 21:47:16 +000062/**
63 * Slurps the device manifest file and add build time flag to it.
64 */
Yifan Hong8302cea2017-12-18 20:17:05 -080065class AssembleVintfImpl : public AssembleVintf {
Yifan Hong9a8b1a72017-08-25 17:55:33 -070066 using Condition = std::unique_ptr<KernelConfig>;
67 using ConditionedConfig = std::pair<Condition, std::vector<KernelConfig> /* configs */>;
68
69 public:
Yifan Hong8302cea2017-12-18 20:17:05 -080070 void setFakeEnv(const std::string& key, const std::string& value) { mFakeEnv[key] = value; }
71
72 std::string getEnv(const std::string& key) const {
73 auto it = mFakeEnv.find(key);
74 if (it != mFakeEnv.end()) {
75 return it->second;
76 }
77 const char* envValue = getenv(key.c_str());
78 return envValue != nullptr ? std::string(envValue) : std::string();
79 }
80
Yifan Honga28729e2018-01-17 13:40:35 -080081 // Get environment variable and split with space.
82 std::vector<std::string> getEnvList(const std::string& key) const {
83 std::vector<std::string> ret;
84 for (auto&& v : base::Split(getEnv(key), " ")) {
85 v = base::Trim(v);
86 if (!v.empty()) {
87 ret.push_back(v);
88 }
89 }
90 return ret;
91 }
92
Yifan Hong8302cea2017-12-18 20:17:05 -080093 template <typename T>
Yifan Hong4dec91d2018-08-13 12:37:47 -070094 bool getFlag(const std::string& key, T* value, bool log = true) const {
Yifan Hong8302cea2017-12-18 20:17:05 -080095 std::string envValue = getEnv(key);
96 if (envValue.empty()) {
Yifan Hong4dec91d2018-08-13 12:37:47 -070097 if (log) {
98 std::cerr << "Warning: " << key << " is missing, defaulted to " << (*value) << "."
99 << std::endl;
100 }
Yifan Hong488e16a2017-07-11 13:50:41 -0700101 return true;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000102 }
103
104 if (!parse(envValue, value)) {
105 std::cerr << "Cannot parse " << envValue << "." << std::endl;
106 return false;
107 }
108 return true;
109 }
110
Yifan Hongeff04662017-12-18 16:27:24 -0800111 /**
Yifan Hong0f7f7282018-02-02 13:05:43 -0800112 * Set *out to environment variable only if *out is a dummy value (i.e. default constructed).
Yifan Hongce355902019-06-26 17:06:52 +0000113 * Return false if a fatal error has occurred:
114 * - The environment variable has an unknown format
115 * - The value of the environment variable does not match a predefined variable in the files
Yifan Hongeff04662017-12-18 16:27:24 -0800116 */
117 template <typename T>
Yifan Hongce355902019-06-26 17:06:52 +0000118 bool getFlagIfUnset(const std::string& envKey, T* out) const {
Yifan Hongeff04662017-12-18 16:27:24 -0800119 bool hasExistingValue = !(*out == T{});
120
121 bool hasEnvValue = false;
122 T envValue;
Yifan Hong8302cea2017-12-18 20:17:05 -0800123 std::string envStrValue = getEnv(envKey);
124 if (!envStrValue.empty()) {
125 if (!parse(envStrValue, &envValue)) {
Yifan Hongb36e85f2019-06-26 17:06:52 +0000126 std::cerr << "Cannot parse " << envValue << "." << std::endl;
Yifan Hongce355902019-06-26 17:06:52 +0000127 return false;
Yifan Hongeff04662017-12-18 16:27:24 -0800128 }
129 hasEnvValue = true;
130 }
131
132 if (hasExistingValue) {
Yifan Hongb36e85f2019-06-26 17:06:52 +0000133 if (hasEnvValue && (*out != envValue)) {
Yifan Hongce355902019-06-26 17:06:52 +0000134 std::cerr << "Cannot override existing value " << *out << " with " << envKey
135 << " (which is " << envValue << ")." << std::endl;
136 return false;
Yifan Hongeff04662017-12-18 16:27:24 -0800137 }
Yifan Hongce355902019-06-26 17:06:52 +0000138 return true;
Yifan Hongeff04662017-12-18 16:27:24 -0800139 }
Yifan Hongb36e85f2019-06-26 17:06:52 +0000140 if (hasEnvValue) {
141 *out = envValue;
Yifan Hongeff04662017-12-18 16:27:24 -0800142 }
Yifan Hongce355902019-06-26 17:06:52 +0000143 return true;
Yifan Hongeff04662017-12-18 16:27:24 -0800144 }
145
Yifan Hong8302cea2017-12-18 20:17:05 -0800146 bool getBooleanFlag(const std::string& key) const { return getEnv(key) == std::string("true"); }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800147
Yifan Hong8302cea2017-12-18 20:17:05 -0800148 size_t getIntegerFlag(const std::string& key, size_t defaultValue = 0) const {
149 std::string envValue = getEnv(key);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800150 if (envValue.empty()) {
151 return defaultValue;
152 }
153 size_t value;
154 if (!base::ParseUint(envValue, &value)) {
155 std::cerr << "Error: " << key << " must be a number." << std::endl;
156 return defaultValue;
157 }
158 return value;
159 }
160
Yifan Hong4650ad82017-05-01 17:28:02 -0700161 static std::string read(std::basic_istream<char>& is) {
162 std::stringstream ss;
163 ss << is.rdbuf();
164 return ss.str();
165 }
166
Yifan Hong4072b252020-03-27 16:54:16 -0700167 // Return true if name of file is "android-base.config". This file must be specified
168 // exactly once for each kernel version. These requirements do not have any conditions.
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700169 static bool isCommonConfig(const std::string& path) {
170 return ::android::base::Basename(path) == gBaseConfig;
171 }
172
Yifan Hong4072b252020-03-27 16:54:16 -0700173 // Return true if name of file matches "android-base-foo.config".
174 // Zero or more conditional configs may be specified for each kernel version. These
175 // requirements are conditional on CONFIG_FOO=y.
176 static bool isConditionalConfig(const std::string& path) {
177 auto fname = ::android::base::Basename(path);
178 return ::android::base::StartsWith(fname, gConfigPrefix) &&
179 ::android::base::EndsWith(fname, gConfigSuffix);
180 }
181
182 // Return true for all other file names (i.e. not android-base.config, and not conditional
183 // configs.)
184 // Zero or more conditional configs may be specified for each kernel version.
185 // These requirements do not have any conditions.
186 static bool isExtraCommonConfig(const std::string& path) {
187 return !isCommonConfig(path) && !isConditionalConfig(path);
188 }
189
Yifan Hong079ec242017-08-25 18:53:38 -0700190 // nullptr on any error, otherwise the condition.
191 static Condition generateCondition(const std::string& path) {
Yifan Hong4072b252020-03-27 16:54:16 -0700192 if (!isConditionalConfig(path)) {
Yifan Hong079ec242017-08-25 18:53:38 -0700193 return nullptr;
194 }
Yifan Hong4072b252020-03-27 16:54:16 -0700195 auto fname = ::android::base::Basename(path);
Yifan Hong079ec242017-08-25 18:53:38 -0700196 std::string sub = fname.substr(gConfigPrefix.size(),
197 fname.size() - gConfigPrefix.size() - gConfigSuffix.size());
198 if (sub.empty()) {
199 return nullptr; // should not happen
200 }
201 for (size_t i = 0; i < sub.size(); ++i) {
202 if (sub[i] == '-') {
203 sub[i] = '_';
204 continue;
205 }
206 if (isalnum(sub[i])) {
207 sub[i] = toupper(sub[i]);
208 continue;
209 }
210 std::cerr << "'" << fname << "' (in " << path
211 << ") is not a valid kernel config file name. Must match regex: "
Steve Mucklead6aa912018-07-23 15:44:01 -0700212 << "android-base(-[0-9a-zA-Z-]+)?\\" << gConfigSuffix
213 << std::endl;
Yifan Hong079ec242017-08-25 18:53:38 -0700214 return nullptr;
215 }
216 sub.insert(0, "CONFIG_");
217 return std::make_unique<KernelConfig>(std::move(sub), Tristate::YES);
218 }
219
Yifan Hong8302cea2017-12-18 20:17:05 -0800220 static bool parseFileForKernelConfigs(std::basic_istream<char>& stream,
221 std::vector<KernelConfig>* out) {
Yifan Hong02e94002017-07-10 15:41:56 -0700222 KernelConfigParser parser(true /* processComments */, true /* relaxedFormat */);
Yifan Honga45f77d2018-12-03 16:42:52 -0800223 status_t err = parser.processAndFinish(read(stream));
Yifan Hong79efa8a2017-07-06 14:10:28 -0700224 if (err != OK) {
Yifan Hongae53a0e2017-07-07 15:19:06 -0700225 std::cerr << parser.error();
Yifan Hong79efa8a2017-07-06 14:10:28 -0700226 return false;
227 }
228
229 for (auto& configPair : parser.configs()) {
230 out->push_back({});
231 KernelConfig& config = out->back();
232 config.first = std::move(configPair.first);
233 if (!parseKernelConfigTypedValue(configPair.second, &config.second)) {
234 std::cerr << "Unknown value type for key = '" << config.first << "', value = '"
235 << configPair.second << "'\n";
236 return false;
237 }
238 }
239 return true;
240 }
241
Yifan Hong8302cea2017-12-18 20:17:05 -0800242 static bool parseFilesForKernelConfigs(std::vector<NamedIstream>* streams,
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700243 std::vector<ConditionedConfig>* out) {
244 out->clear();
245 ConditionedConfig commonConfig;
246 bool foundCommonConfig = false;
Steve Muckle0bef8682017-07-31 15:47:15 -0700247 bool ret = true;
Yifan Hong8302cea2017-12-18 20:17:05 -0800248
249 for (auto& namedStream : *streams) {
Yifan Hong4072b252020-03-27 16:54:16 -0700250 if (isCommonConfig(namedStream.name()) || isExtraCommonConfig(namedStream.name())) {
251 if (!parseFileForKernelConfigs(namedStream.stream(), &commonConfig.second)) {
252 std::cerr << "Failed to generate common configs for file "
253 << namedStream.name();
254 ret = false;
255 }
256 if (isCommonConfig(namedStream.name())) {
257 foundCommonConfig = true;
258 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700259 } else {
Yifan Hong8302cea2017-12-18 20:17:05 -0800260 Condition condition = generateCondition(namedStream.name());
Yifan Hong4072b252020-03-27 16:54:16 -0700261 if (condition == nullptr) {
262 std::cerr << "Failed to generate conditional configs for file "
263 << namedStream.name();
264 ret = false;
265 }
Yifan Hong079ec242017-08-25 18:53:38 -0700266
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700267 std::vector<KernelConfig> kernelConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800268 if ((ret &= parseFileForKernelConfigs(namedStream.stream(), &kernelConfigs)))
Yifan Hong079ec242017-08-25 18:53:38 -0700269 out->emplace_back(std::move(condition), std::move(kernelConfigs));
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700270 }
Steve Muckle0bef8682017-07-31 15:47:15 -0700271 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700272
273 if (!foundCommonConfig) {
Steve Mucklead6aa912018-07-23 15:44:01 -0700274 std::cerr << "No " << gBaseConfig << " is found in these paths:" << std::endl;
Yifan Hong8302cea2017-12-18 20:17:05 -0800275 for (auto& namedStream : *streams) {
276 std::cerr << " " << namedStream.name() << std::endl;
277 }
Yifan Hong4072b252020-03-27 16:54:16 -0700278 ret = false;
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700279 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700280 // first element is always common configs (no conditions).
281 out->insert(out->begin(), std::move(commonConfig));
Steve Muckle0bef8682017-07-31 15:47:15 -0700282 return ret;
283 }
284
Yifan Hong8302cea2017-12-18 20:17:05 -0800285 std::basic_ostream<char>& out() const { return mOutRef == nullptr ? std::cout : *mOutRef; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700286
Yifan Hong1044fd12018-02-27 14:51:54 -0800287 // If -c is provided, check it.
288 bool checkDualFile(const HalManifest& manifest, const CompatibilityMatrix& matrix) {
289 if (getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
290 std::string error;
Yifan Hong3f743912019-12-17 14:12:04 -0800291 if (!manifest.checkCompatibility(matrix, &error, mCheckFlags)) {
Yifan Hong1044fd12018-02-27 14:51:54 -0800292 std::cerr << "Not compatible: " << error << std::endl;
293 return false;
294 }
295 }
Yifan Hong1044fd12018-02-27 14:51:54 -0800296 return true;
297 }
298
Yifan Honga83d0e42020-04-13 13:07:31 -0700299 using HalManifests = std::vector<HalManifest>;
300 using CompatibilityMatrices = std::vector<CompatibilityMatrix>;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800301
Steven Moreland5875afc2018-04-05 13:14:08 -0700302 template <typename M>
Yifan Honga83d0e42020-04-13 13:07:31 -0700303 void outputInputs(const std::vector<M>& inputs) {
Steven Moreland5875afc2018-04-05 13:14:08 -0700304 out() << "<!--" << std::endl;
305 out() << " Input:" << std::endl;
306 for (const auto& e : inputs) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700307 if (!e.fileName().empty()) {
308 out() << " " << base::Basename(e.fileName()) << std::endl;
Steven Moreland5875afc2018-04-05 13:14:08 -0700309 }
310 }
311 out() << "-->" << std::endl;
312 }
313
Yifan Honga45f77d2018-12-03 16:42:52 -0800314 // Parse --kernel arguments and write to output manifest.
315 bool setDeviceManifestKernel(HalManifest* manifest) {
316 if (mKernels.empty()) {
317 return true;
318 }
319 if (mKernels.size() > 1) {
320 std::cerr << "Warning: multiple --kernel is specified when building device manifest. "
321 << "Only the first one will be used." << std::endl;
322 }
323 auto& kernelArg = *mKernels.begin();
324 const auto& kernelVer = kernelArg.first;
325 auto& kernelConfigFiles = kernelArg.second;
326 // addKernel() guarantees that !kernelConfigFiles.empty().
327 if (kernelConfigFiles.size() > 1) {
328 std::cerr << "Warning: multiple config files are specified in --kernel when building "
329 << "device manfiest. Only the first one will be used." << std::endl;
330 }
331
332 KernelConfigParser parser(true /* processComments */, false /* relaxedFormat */);
333 status_t err = parser.processAndFinish(read(kernelConfigFiles[0].stream()));
334 if (err != OK) {
335 std::cerr << parser.error();
336 return false;
337 }
Yifan Hong08a48712019-12-11 14:56:00 -0800338
339 // Set version and configs in manifest.
340 auto kernel_info = std::make_optional<KernelInfo>();
341 kernel_info->mVersion = kernelVer;
342 kernel_info->mConfigs = parser.configs();
343 std::string error;
344 if (!manifest->mergeKernel(&kernel_info, &error)) {
345 std::cerr << error << "\n";
346 return false;
347 }
348
Yifan Honga45f77d2018-12-03 16:42:52 -0800349 return true;
350 }
351
Yifan Hong08a48712019-12-11 14:56:00 -0800352 void inferDeviceManifestKernelFcm(HalManifest* manifest) {
353 // No target FCM version.
354 if (manifest->level() == Level::UNSPECIFIED) return;
355 // target FCM version < R: leave value untouched.
356 if (manifest->level() < Level::R) return;
Yifan Honge48263a2020-05-14 13:16:31 -0700357 // Inject empty <kernel> tag if missing.
358 if (!manifest->kernel().has_value()) {
359 manifest->device.mKernel = std::make_optional<KernelInfo>();
360 }
Yifan Hong08a48712019-12-11 14:56:00 -0800361 // Kernel FCM already set.
362 if (manifest->kernel()->level() != Level::UNSPECIFIED) return;
363
364 manifest->device.mKernel->mLevel = manifest->level();
365 }
366
Yifan Hongdbe9db32017-12-11 19:06:11 -0800367 bool assembleHalManifest(HalManifests* halManifests) {
Yifan Hong4650ad82017-05-01 17:28:02 -0700368 std::string error;
Yifan Honga83d0e42020-04-13 13:07:31 -0700369 HalManifest* halManifest = &halManifests->front();
Yifan Hongdbe9db32017-12-11 19:06:11 -0800370 for (auto it = halManifests->begin() + 1; it != halManifests->end(); ++it) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700371 const std::string& path = it->fileName();
372 HalManifest& manifestToAdd = *it;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800373
Yifan Hong0a35ab12018-06-29 11:32:57 -0700374 if (manifestToAdd.level() != Level::UNSPECIFIED) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800375 if (halManifest->level() == Level::UNSPECIFIED) {
Yifan Hong0a35ab12018-06-29 11:32:57 -0700376 halManifest->mLevel = manifestToAdd.level();
377 } else if (halManifest->level() != manifestToAdd.level()) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800378 std::cerr << "Inconsistent FCM Version in HAL manifests:" << std::endl
Yifan Honga83d0e42020-04-13 13:07:31 -0700379 << " File '" << halManifests->front().fileName() << "' has level "
Yifan Hongdbe9db32017-12-11 19:06:11 -0800380 << halManifest->level() << std::endl
Yifan Hong0a35ab12018-06-29 11:32:57 -0700381 << " File '" << path << "' has level " << manifestToAdd.level()
Yifan Hongdbe9db32017-12-11 19:06:11 -0800382 << std::endl;
383 return false;
384 }
385 }
386
Yifan Hong4c6962d2019-01-30 15:50:46 -0800387 if (!halManifest->addAll(&manifestToAdd, &error)) {
388 std::cerr << "File \"" << path << "\" cannot be added: " << error << std::endl;
Yifan Hong0a35ab12018-06-29 11:32:57 -0700389 return false;
390 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800391 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700392
393 if (halManifest->mType == SchemaType::DEVICE) {
Yifan Hongce355902019-06-26 17:06:52 +0000394 if (!getFlagIfUnset("BOARD_SEPOLICY_VERS", &halManifest->device.mSepolicyVersion)) {
395 return false;
396 }
Steven Morelandb1392772018-05-01 11:21:35 -0700397
Yifan Hongdbe9db32017-12-11 19:06:11 -0800398 if (!setDeviceFcmVersion(halManifest)) {
399 return false;
400 }
Yifan Honga45f77d2018-12-03 16:42:52 -0800401
402 if (!setDeviceManifestKernel(halManifest)) {
403 return false;
404 }
Yifan Hong08a48712019-12-11 14:56:00 -0800405
406 inferDeviceManifestKernelFcm(halManifest);
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 Honga2635c42017-12-12 13:20:33 -0800434 << gCompatibilityMatrixConverter(generatedMatrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700435 } else {
Yifan Honga2635c42017-12-12 13:20:33 -0800436 out() << gHalManifestConverter(*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());
443 if (!gCompatibilityMatrixConverter(&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());
543 if (!gHalManifestConverter(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 Honge7837b12018-10-11 10:38:57 -0700588 builtMatrix = CompatibilityMatrix::combine(deviceLevel, matrices, &error);
589 matrix = builtMatrix.get();
Yifan Hongdbe9db32017-12-11 19:06:11 -0800590
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700591 if (matrix == nullptr) {
592 std::cerr << error << std::endl;
593 return false;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800594 }
595
Yifan Honge88e1672017-08-24 14:42:54 -0700596 if (!assembleFrameworkCompatibilityMatrixKernels(matrix)) {
597 return false;
Yifan Hong79efa8a2017-07-06 14:10:28 -0700598 }
Yifan Honge88e1672017-08-24 14:42:54 -0700599
Yifan Hong321ee222018-02-02 12:46:52 -0800600 // Add PLATFORM_SEPOLICY_* to sepolicy.sepolicy-version. Remove dupes.
601 std::set<Version> sepolicyVersions;
602 auto sepolicyVersionStrings = getEnvList("PLATFORM_SEPOLICY_COMPAT_VERSIONS");
603 auto currentSepolicyVersionString = getEnv("PLATFORM_SEPOLICY_VERSION");
604 if (!currentSepolicyVersionString.empty()) {
605 sepolicyVersionStrings.push_back(currentSepolicyVersionString);
606 }
607 for (auto&& s : sepolicyVersionStrings) {
608 Version v;
609 if (!parse(s, &v)) {
610 std::cerr << "Error: unknown sepolicy version '" << s << "' specified by "
611 << (s == currentSepolicyVersionString
612 ? "PLATFORM_SEPOLICY_VERSION"
613 : "PLATFORM_SEPOLICY_COMPAT_VERSIONS")
614 << ".";
615 return false;
616 }
617 sepolicyVersions.insert(v);
618 }
619 for (auto&& v : sepolicyVersions) {
620 matrix->framework.mSepolicy.mSepolicyVersionRanges.emplace_back(v.majorVer,
621 v.minorVer);
Yifan Hong7f6c00c2017-07-06 19:50:29 +0000622 }
Yifan Hongf4135012017-12-18 17:27:19 -0800623
Yifan Hongce355902019-06-26 17:06:52 +0000624 if (!getFlagIfUnset("POLICYVERS",
625 &matrix->framework.mSepolicy.mKernelSepolicyVersion)) {
626 return false;
627 }
628 if (!getFlagIfUnset("FRAMEWORK_VBMETA_VERSION", &matrix->framework.mAvbMetaVersion)) {
629 return false;
630 }
Yifan Hong4dec91d2018-08-13 12:37:47 -0700631 // Hard-override existing AVB version
632 getFlag("FRAMEWORK_VBMETA_VERSION_OVERRIDE", &matrix->framework.mAvbMetaVersion,
633 false /* log */);
Yifan Hong9aa63702017-05-16 16:37:50 -0700634 }
Steven Moreland5875afc2018-04-05 13:14:08 -0700635 outputInputs(*matrices);
Yifan Honga2635c42017-12-12 13:20:33 -0800636 out() << gCompatibilityMatrixConverter(*matrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700637 out().flush();
638
Yifan Hong1044fd12018-02-27 14:51:54 -0800639 if (checkManifest != nullptr && !checkDualFile(*checkManifest, *matrix)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800640 return false;
Yifan Hong9aa63702017-05-16 16:37:50 -0700641 }
642
643 return true;
644 }
645
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700646 enum AssembleStatus { SUCCESS, FAIL_AND_EXIT, TRY_NEXT };
647 template <typename Schema, typename AssembleFunc>
648 AssembleStatus tryAssemble(const XmlConverter<Schema>& converter, const std::string& schemaName,
Yifan Hong94757062018-02-09 16:36:31 -0800649 AssembleFunc assemble, std::string* error) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700650 std::vector<Schema> schemas;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700651 Schema schema;
Yifan Honga83d0e42020-04-13 13:07:31 -0700652 schema.setFileName(mInFiles.front().name());
Yifan Hong94757062018-02-09 16:36:31 -0800653 if (!converter(&schema, read(mInFiles.front().stream()), error)) {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700654 return TRY_NEXT;
655 }
656 auto firstType = schema.type();
Yifan Honga83d0e42020-04-13 13:07:31 -0700657 schemas.emplace_back(std::move(schema));
Yifan Hongdbe9db32017-12-11 19:06:11 -0800658
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700659 for (auto it = mInFiles.begin() + 1; it != mInFiles.end(); ++it) {
660 Schema additionalSchema;
Yifan Hongaa219f52017-12-18 18:51:59 -0800661 const std::string& fileName = it->name();
Yifan Honga83d0e42020-04-13 13:07:31 -0700662 additionalSchema.setFileName(fileName);
Yifan Hong94757062018-02-09 16:36:31 -0800663 if (!converter(&additionalSchema, read(it->stream()), error)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800664 std::cerr << "File \"" << fileName << "\" is not a valid " << firstType << " "
665 << schemaName << " (but the first file is a valid " << firstType << " "
Yifan Hong94757062018-02-09 16:36:31 -0800666 << schemaName << "). Error: " << *error << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700667 return FAIL_AND_EXIT;
668 }
669 if (additionalSchema.type() != firstType) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800670 std::cerr << "File \"" << fileName << "\" is a " << additionalSchema.type() << " "
671 << schemaName << " (but a " << firstType << " " << schemaName
672 << " is expected)." << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700673 return FAIL_AND_EXIT;
674 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800675
Yifan Honga83d0e42020-04-13 13:07:31 -0700676 schemas.emplace_back(std::move(additionalSchema));
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700677 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800678 return assemble(&schemas) ? SUCCESS : FAIL_AND_EXIT;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700679 }
680
Yifan Hong8302cea2017-12-18 20:17:05 -0800681 bool assemble() override {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700682 using std::placeholders::_1;
683 if (mInFiles.empty()) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700684 std::cerr << "Missing input file." << std::endl;
685 return false;
686 }
687
Yifan Hong94757062018-02-09 16:36:31 -0800688 std::string manifestError;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700689 auto status = tryAssemble(gHalManifestConverter, "manifest",
Yifan Hong94757062018-02-09 16:36:31 -0800690 std::bind(&AssembleVintfImpl::assembleHalManifest, this, _1),
691 &manifestError);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700692 if (status == SUCCESS) return true;
693 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000694
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700695 resetInFiles();
Yifan Honga59d2562017-04-18 18:01:16 -0700696
Yifan Hong94757062018-02-09 16:36:31 -0800697 std::string matrixError;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700698 status = tryAssemble(gCompatibilityMatrixConverter, "compatibility matrix",
Yifan Hong94757062018-02-09 16:36:31 -0800699 std::bind(&AssembleVintfImpl::assembleCompatibilityMatrix, this, _1),
700 &matrixError);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700701 if (status == SUCCESS) return true;
702 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000703
Yifan Hong959ee1b2017-04-28 14:37:56 -0700704 std::cerr << "Input file has unknown format." << std::endl
Yifan Hong94757062018-02-09 16:36:31 -0800705 << "Error when attempting to convert to manifest: " << manifestError << std::endl
706 << "Error when attempting to convert to compatibility matrix: " << matrixError
707 << std::endl;
Yifan Hong959ee1b2017-04-28 14:37:56 -0700708 return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000709 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700710
Yifan Hong8302cea2017-12-18 20:17:05 -0800711 std::ostream& setOutputStream(Ostream&& out) override {
712 mOutRef = std::move(out);
713 return *mOutRef;
Yifan Hong9aa63702017-05-16 16:37:50 -0700714 }
715
Yifan Hong8302cea2017-12-18 20:17:05 -0800716 std::istream& addInputStream(const std::string& name, Istream&& in) override {
717 auto it = mInFiles.emplace(mInFiles.end(), name, std::move(in));
718 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700719 }
720
Yifan Hongdf357172020-04-13 17:24:54 -0700721 std::istream& setCheckInputStream(const std::string& name, Istream&& in) override {
722 mCheckFile = NamedIstream(name, std::move(in));
723 return mCheckFile.stream();
Yifan Hong8302cea2017-12-18 20:17:05 -0800724 }
725
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800726 bool hasKernelVersion(const KernelVersion& kernelVer) const override {
Yifan Hong8302cea2017-12-18 20:17:05 -0800727 return mKernels.find(kernelVer) != mKernels.end();
728 }
729
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800730 std::istream& addKernelConfigInputStream(const KernelVersion& kernelVer,
731 const std::string& name, Istream&& in) override {
Yifan Hong8302cea2017-12-18 20:17:05 -0800732 auto&& kernel = mKernels[kernelVer];
733 auto it = kernel.emplace(kernel.end(), name, std::move(in));
734 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700735 }
736
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700737 void resetInFiles() {
738 for (auto& inFile : mInFiles) {
Yifan Hongaa219f52017-12-18 18:51:59 -0800739 inFile.stream().clear();
740 inFile.stream().seekg(0);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700741 }
742 }
743
Yifan Hong8302cea2017-12-18 20:17:05 -0800744 void setOutputMatrix() override { mOutputMatrix = true; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700745
Yifan Hong8302cea2017-12-18 20:17:05 -0800746 bool setHalsOnly() override {
Yifan Hong5c9ff702018-08-07 17:26:12 -0700747 if (mHasSetHalsOnlyFlag) {
748 std::cerr << "Error: Cannot set --hals-only with --no-hals." << std::endl;
749 return false;
750 }
751 // Just override it with HALS_ONLY because other flags that modify mSerializeFlags
752 // does not interfere with this (except --no-hals).
753 mSerializeFlags = SerializeFlags::HALS_ONLY;
754 mHasSetHalsOnlyFlag = true;
Yifan Honga2635c42017-12-12 13:20:33 -0800755 return true;
756 }
757
Yifan Hong8302cea2017-12-18 20:17:05 -0800758 bool setNoHals() override {
Yifan Hong5c9ff702018-08-07 17:26:12 -0700759 if (mHasSetHalsOnlyFlag) {
760 std::cerr << "Error: Cannot set --hals-only with --no-hals." << std::endl;
761 return false;
762 }
763 mSerializeFlags = mSerializeFlags.disableHals();
764 mHasSetHalsOnlyFlag = true;
Yifan Honga2635c42017-12-12 13:20:33 -0800765 return true;
766 }
767
Yifan Hong86678e42018-07-26 11:38:54 -0700768 bool setNoKernelRequirements() override {
Yifan Hong5c9ff702018-08-07 17:26:12 -0700769 mSerializeFlags = mSerializeFlags.disableKernelConfigs().disableKernelMinorRevision();
Yifan Hong3f743912019-12-17 14:12:04 -0800770 mCheckFlags = mCheckFlags.disableKernel();
Yifan Hong86678e42018-07-26 11:38:54 -0700771 return true;
772 }
773
Yifan Hong9aa63702017-05-16 16:37:50 -0700774 private:
Yifan Hongaa219f52017-12-18 18:51:59 -0800775 std::vector<NamedIstream> mInFiles;
Yifan Hong8302cea2017-12-18 20:17:05 -0800776 Ostream mOutRef;
Yifan Hongdf357172020-04-13 17:24:54 -0700777 NamedIstream mCheckFile;
Yifan Hong9aa63702017-05-16 16:37:50 -0700778 bool mOutputMatrix = false;
Yifan Hong5c9ff702018-08-07 17:26:12 -0700779 bool mHasSetHalsOnlyFlag = false;
Yifan Hongba588bc2018-08-08 14:19:41 -0700780 SerializeFlags::Type mSerializeFlags = SerializeFlags::EVERYTHING;
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800781 std::map<KernelVersion, std::vector<NamedIstream>> mKernels;
Yifan Hong8302cea2017-12-18 20:17:05 -0800782 std::map<std::string, std::string> mFakeEnv;
Yifan Hong3f743912019-12-17 14:12:04 -0800783 CheckFlags::Type mCheckFlags = CheckFlags::DEFAULT;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000784};
785
Yifan Hong8302cea2017-12-18 20:17:05 -0800786bool AssembleVintf::openOutFile(const std::string& path) {
787 return static_cast<std::ofstream&>(setOutputStream(std::make_unique<std::ofstream>(path)))
788 .is_open();
789}
790
791bool AssembleVintf::openInFile(const std::string& path) {
792 return static_cast<std::ifstream&>(addInputStream(path, std::make_unique<std::ifstream>(path)))
793 .is_open();
794}
795
796bool AssembleVintf::openCheckFile(const std::string& path) {
Yifan Hongdf357172020-04-13 17:24:54 -0700797 return static_cast<std::ifstream&>(
798 setCheckInputStream(path, std::make_unique<std::ifstream>(path)))
Yifan Hong8302cea2017-12-18 20:17:05 -0800799 .is_open();
800}
801
802bool AssembleVintf::addKernel(const std::string& kernelArg) {
Yifan Hongb83e56b2018-01-09 14:36:30 -0800803 auto tokens = base::Split(kernelArg, ":");
Yifan Hong8302cea2017-12-18 20:17:05 -0800804 if (tokens.size() <= 1) {
805 std::cerr << "Unrecognized --kernel option '" << kernelArg << "'" << std::endl;
806 return false;
807 }
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800808 KernelVersion kernelVer;
Yifan Hong8302cea2017-12-18 20:17:05 -0800809 if (!parse(tokens.front(), &kernelVer)) {
810 std::cerr << "Unrecognized kernel version '" << tokens.front() << "'" << std::endl;
811 return false;
812 }
813 if (hasKernelVersion(kernelVer)) {
814 std::cerr << "Multiple --kernel for " << kernelVer << " is specified." << std::endl;
815 return false;
816 }
817 for (auto it = tokens.begin() + 1; it != tokens.end(); ++it) {
818 bool opened =
819 static_cast<std::ifstream&>(
820 addKernelConfigInputStream(kernelVer, *it, std::make_unique<std::ifstream>(*it)))
821 .is_open();
822 if (!opened) {
823 std::cerr << "Cannot open file '" << *it << "'." << std::endl;
824 return false;
825 }
826 }
827 return true;
828}
829
830std::unique_ptr<AssembleVintf> AssembleVintf::newInstance() {
831 return std::make_unique<AssembleVintfImpl>();
832}
833
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000834} // namespace vintf
835} // namespace android