blob: 71ea1bc3c89982337e28277d1772d4ce505f1a9e [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 Hong9a8b1a72017-08-25 17:55:33 -070029
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;
357 // No need to infer when <kernel> tag is missing.
358 if (!manifest->kernel().has_value()) return;
359 // Kernel FCM already set.
360 if (manifest->kernel()->level() != Level::UNSPECIFIED) return;
361
362 manifest->device.mKernel->mLevel = manifest->level();
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 Hong08a48712019-12-11 14:56:00 -0800403
404 inferDeviceManifestKernelFcm(halManifest);
Yifan Hong9aa63702017-05-16 16:37:50 -0700405 }
406
Yifan Hongfeb454e2018-01-09 16:16:40 -0800407 if (halManifest->mType == SchemaType::FRAMEWORK) {
Yifan Honga28729e2018-01-17 13:40:35 -0800408 for (auto&& v : getEnvList("PROVIDED_VNDK_VERSIONS")) {
409 halManifest->framework.mVendorNdks.emplace_back(std::move(v));
410 }
411
Yifan Hong8b8492b2018-01-22 14:08:40 -0800412 for (auto&& v : getEnvList("PLATFORM_SYSTEMSDK_VERSIONS")) {
Yifan Honga28729e2018-01-17 13:40:35 -0800413 halManifest->framework.mSystemSdk.mVersions.emplace(std::move(v));
Yifan Hongfeb454e2018-01-09 16:16:40 -0800414 }
415 }
416
Steven Moreland5875afc2018-04-05 13:14:08 -0700417 outputInputs(*halManifests);
418
Yifan Hong9aa63702017-05-16 16:37:50 -0700419 if (mOutputMatrix) {
420 CompatibilityMatrix generatedMatrix = halManifest->generateCompatibleMatrix();
Yifan Hong3f743912019-12-17 14:12:04 -0800421 if (!halManifest->checkCompatibility(generatedMatrix, &error, mCheckFlags)) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700422 std::cerr << "FATAL ERROR: cannot generate a compatible matrix: " << error
423 << std::endl;
424 }
425 out() << "<!-- \n"
426 " Autogenerated skeleton compatibility matrix. \n"
427 " Use with caution. Modify it to suit your needs.\n"
428 " All HALs are set to optional.\n"
429 " Many entries other than HALs are zero-filled and\n"
430 " require human attention. \n"
431 "-->\n"
Yifan Honga2635c42017-12-12 13:20:33 -0800432 << gCompatibilityMatrixConverter(generatedMatrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700433 } else {
Yifan Honga2635c42017-12-12 13:20:33 -0800434 out() << gHalManifestConverter(*halManifest, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700435 }
436 out().flush();
437
Yifan Hongdf357172020-04-13 17:24:54 -0700438 if (mCheckFile.hasStream()) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700439 CompatibilityMatrix checkMatrix;
Yifan Hongdf357172020-04-13 17:24:54 -0700440 checkMatrix.setFileName(mCheckFile.name());
441 if (!gCompatibilityMatrixConverter(&checkMatrix, read(mCheckFile.stream()), &error)) {
Yifan Hong94757062018-02-09 16:36:31 -0800442 std::cerr << "Cannot parse check file as a compatibility matrix: " << error
443 << std::endl;
Yifan Hong9aa63702017-05-16 16:37:50 -0700444 return false;
445 }
Yifan Hong1044fd12018-02-27 14:51:54 -0800446 if (!checkDualFile(*halManifest, checkMatrix)) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700447 return false;
448 }
449 }
450
451 return true;
452 }
453
Yifan Honge7837b12018-10-11 10:38:57 -0700454 // Parse --kernel arguments and write to output matrix.
Yifan Honge88e1672017-08-24 14:42:54 -0700455 bool assembleFrameworkCompatibilityMatrixKernels(CompatibilityMatrix* matrix) {
Yifan Hong8302cea2017-12-18 20:17:05 -0800456 for (auto& pair : mKernels) {
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700457 std::vector<ConditionedConfig> conditionedConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800458 if (!parseFilesForKernelConfigs(&pair.second, &conditionedConfigs)) {
Yifan Honge88e1672017-08-24 14:42:54 -0700459 return false;
460 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700461 for (ConditionedConfig& conditionedConfig : conditionedConfigs) {
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800462 MatrixKernel kernel(KernelVersion{pair.first}, std::move(conditionedConfig.second));
Yifan Hong079ec242017-08-25 18:53:38 -0700463 if (conditionedConfig.first != nullptr)
464 kernel.mConditions.push_back(std::move(*conditionedConfig.first));
Yifan Honge7837b12018-10-11 10:38:57 -0700465 std::string error;
466 if (!matrix->addKernel(std::move(kernel), &error)) {
467 std::cerr << "Error:" << error << std::endl;
468 return false;
469 };
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700470 }
Yifan Honge88e1672017-08-24 14:42:54 -0700471 }
472 return true;
473 }
474
Yifan Hongdbe9db32017-12-11 19:06:11 -0800475 bool setDeviceFcmVersion(HalManifest* manifest) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700476 // Not needed for generating empty manifest for DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE.
Yifan Hong2eee1982018-03-29 10:55:30 -0700477 if (getBooleanFlag("VINTF_IGNORE_TARGET_FCM_VERSION")) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700478 return true;
479 }
480
Yifan Hongdbe9db32017-12-11 19:06:11 -0800481 size_t shippingApiLevel = getIntegerFlag("PRODUCT_SHIPPING_API_LEVEL");
Yifan Hong9aa63702017-05-16 16:37:50 -0700482
Yifan Hongdbe9db32017-12-11 19:06:11 -0800483 if (manifest->level() != Level::UNSPECIFIED) {
484 return true;
485 }
486 if (!getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
487 manifest->mLevel = Level::LEGACY;
488 return true;
489 }
490 // TODO(b/70628538): Do not infer from Shipping API level.
491 if (shippingApiLevel) {
492 std::cerr << "Warning: Shipping FCM Version is inferred from Shipping API level. "
493 << "Declare Shipping FCM Version in device manifest directly." << std::endl;
Yifan Hongafae1982018-01-11 18:15:24 -0800494 manifest->mLevel = details::convertFromApiLevel(shippingApiLevel);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800495 if (manifest->mLevel == Level::UNSPECIFIED) {
496 std::cerr << "Error: Shipping FCM Version cannot be inferred from Shipping API "
497 << "level " << shippingApiLevel << "."
498 << "Declare Shipping FCM Version in device manifest directly."
499 << std::endl;
500 return false;
501 }
502 return true;
503 }
504 // TODO(b/69638851): should be an error if Shipping API level is not defined.
505 // For now, just leave it empty; when framework compatibility matrix is built,
506 // lowest FCM Version is assumed.
507 std::cerr << "Warning: Shipping FCM Version cannot be inferred, because:" << std::endl
508 << " (1) It is not explicitly declared in device manifest;" << std::endl
509 << " (2) PRODUCT_ENFORCE_VINTF_MANIFEST is set to true;" << std::endl
510 << " (3) PRODUCT_SHIPPING_API_LEVEL is undefined." << std::endl
511 << "Assuming 'unspecified' Shipping FCM Version. " << std::endl
512 << "To remove this warning, define 'level' attribute in device manifest."
513 << std::endl;
514 return true;
515 }
516
517 Level getLowestFcmVersion(const CompatibilityMatrices& matrices) {
518 Level ret = Level::UNSPECIFIED;
519 for (const auto& e : matrices) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700520 if (ret == Level::UNSPECIFIED || ret > e.level()) {
521 ret = e.level();
Yifan Hongdbe9db32017-12-11 19:06:11 -0800522 }
523 }
524 return ret;
525 }
526
527 bool assembleCompatibilityMatrix(CompatibilityMatrices* matrices) {
528 std::string error;
529 CompatibilityMatrix* matrix = nullptr;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800530 std::unique_ptr<HalManifest> checkManifest;
Yifan Honge7837b12018-10-11 10:38:57 -0700531 std::unique_ptr<CompatibilityMatrix> builtMatrix;
532
Yifan Hongdf357172020-04-13 17:24:54 -0700533 if (mCheckFile.hasStream()) {
Yifan Honge7837b12018-10-11 10:38:57 -0700534 checkManifest = std::make_unique<HalManifest>();
Yifan Hongdf357172020-04-13 17:24:54 -0700535 checkManifest->setFileName(mCheckFile.name());
536 if (!gHalManifestConverter(checkManifest.get(), read(mCheckFile.stream()), &error)) {
Yifan Honge7837b12018-10-11 10:38:57 -0700537 std::cerr << "Cannot parse check file as a HAL manifest: " << error << std::endl;
538 return false;
539 }
540 }
541
Yifan Honga83d0e42020-04-13 13:07:31 -0700542 if (matrices->front().mType == SchemaType::DEVICE) {
Yifan Hong5a51ea52019-04-22 14:54:57 -0700543 builtMatrix = CompatibilityMatrix::combineDeviceMatrices(matrices, &error);
544 matrix = builtMatrix.get();
545
546 if (matrix == nullptr) {
547 std::cerr << error << std::endl;
Yifan Honge7837b12018-10-11 10:38:57 -0700548 return false;
549 }
550
Yifan Hongfeb454e2018-01-09 16:16:40 -0800551 auto vndkVersion = base::Trim(getEnv("REQUIRED_VNDK_VERSION"));
552 if (!vndkVersion.empty()) {
553 auto& valueInMatrix = matrix->device.mVendorNdk;
554 if (!valueInMatrix.version().empty() && valueInMatrix.version() != vndkVersion) {
555 std::cerr << "Hard-coded <vendor-ndk> version in device compatibility matrix ("
Yifan Honga83d0e42020-04-13 13:07:31 -0700556 << matrices->front().fileName() << "), '" << valueInMatrix.version()
Yifan Hongfeb454e2018-01-09 16:16:40 -0800557 << "', does not match value inferred "
558 << "from BOARD_VNDK_VERSION '" << vndkVersion << "'" << std::endl;
559 return false;
560 }
561 valueInMatrix = VendorNdk{std::move(vndkVersion)};
562 }
Yifan Honga28729e2018-01-17 13:40:35 -0800563
564 for (auto&& v : getEnvList("BOARD_SYSTEMSDK_VERSIONS")) {
565 matrix->device.mSystemSdk.mVersions.emplace(std::move(v));
566 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800567 }
568
Yifan Honga83d0e42020-04-13 13:07:31 -0700569 if (matrices->front().mType == SchemaType::FRAMEWORK) {
Yifan Honge7837b12018-10-11 10:38:57 -0700570 Level deviceLevel =
571 checkManifest != nullptr ? checkManifest->level() : Level::UNSPECIFIED;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800572 if (deviceLevel == Level::UNSPECIFIED) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800573 deviceLevel = getLowestFcmVersion(*matrices);
Yifan Honge7837b12018-10-11 10:38:57 -0700574 if (checkManifest != nullptr && deviceLevel != Level::UNSPECIFIED) {
575 std::cerr << "Warning: No Target FCM Version for device. Assuming \""
576 << to_string(deviceLevel)
577 << "\" when building final framework compatibility matrix."
578 << std::endl;
579 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800580 }
Yifan Honge7837b12018-10-11 10:38:57 -0700581 builtMatrix = CompatibilityMatrix::combine(deviceLevel, matrices, &error);
582 matrix = builtMatrix.get();
Yifan Hongdbe9db32017-12-11 19:06:11 -0800583
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700584 if (matrix == nullptr) {
585 std::cerr << error << std::endl;
586 return false;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800587 }
588
Yifan Honge88e1672017-08-24 14:42:54 -0700589 if (!assembleFrameworkCompatibilityMatrixKernels(matrix)) {
590 return false;
Yifan Hong79efa8a2017-07-06 14:10:28 -0700591 }
Yifan Honge88e1672017-08-24 14:42:54 -0700592
Yifan Hong321ee222018-02-02 12:46:52 -0800593 // Add PLATFORM_SEPOLICY_* to sepolicy.sepolicy-version. Remove dupes.
594 std::set<Version> sepolicyVersions;
595 auto sepolicyVersionStrings = getEnvList("PLATFORM_SEPOLICY_COMPAT_VERSIONS");
596 auto currentSepolicyVersionString = getEnv("PLATFORM_SEPOLICY_VERSION");
597 if (!currentSepolicyVersionString.empty()) {
598 sepolicyVersionStrings.push_back(currentSepolicyVersionString);
599 }
600 for (auto&& s : sepolicyVersionStrings) {
601 Version v;
602 if (!parse(s, &v)) {
603 std::cerr << "Error: unknown sepolicy version '" << s << "' specified by "
604 << (s == currentSepolicyVersionString
605 ? "PLATFORM_SEPOLICY_VERSION"
606 : "PLATFORM_SEPOLICY_COMPAT_VERSIONS")
607 << ".";
608 return false;
609 }
610 sepolicyVersions.insert(v);
611 }
612 for (auto&& v : sepolicyVersions) {
613 matrix->framework.mSepolicy.mSepolicyVersionRanges.emplace_back(v.majorVer,
614 v.minorVer);
Yifan Hong7f6c00c2017-07-06 19:50:29 +0000615 }
Yifan Hongf4135012017-12-18 17:27:19 -0800616
Yifan Hongce355902019-06-26 17:06:52 +0000617 if (!getFlagIfUnset("POLICYVERS",
618 &matrix->framework.mSepolicy.mKernelSepolicyVersion)) {
619 return false;
620 }
621 if (!getFlagIfUnset("FRAMEWORK_VBMETA_VERSION", &matrix->framework.mAvbMetaVersion)) {
622 return false;
623 }
Yifan Hong4dec91d2018-08-13 12:37:47 -0700624 // Hard-override existing AVB version
625 getFlag("FRAMEWORK_VBMETA_VERSION_OVERRIDE", &matrix->framework.mAvbMetaVersion,
626 false /* log */);
Yifan Hong9aa63702017-05-16 16:37:50 -0700627 }
Steven Moreland5875afc2018-04-05 13:14:08 -0700628 outputInputs(*matrices);
Yifan Honga2635c42017-12-12 13:20:33 -0800629 out() << gCompatibilityMatrixConverter(*matrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700630 out().flush();
631
Yifan Hong1044fd12018-02-27 14:51:54 -0800632 if (checkManifest != nullptr && !checkDualFile(*checkManifest, *matrix)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800633 return false;
Yifan Hong9aa63702017-05-16 16:37:50 -0700634 }
635
636 return true;
637 }
638
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700639 enum AssembleStatus { SUCCESS, FAIL_AND_EXIT, TRY_NEXT };
640 template <typename Schema, typename AssembleFunc>
641 AssembleStatus tryAssemble(const XmlConverter<Schema>& converter, const std::string& schemaName,
Yifan Hong94757062018-02-09 16:36:31 -0800642 AssembleFunc assemble, std::string* error) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700643 std::vector<Schema> schemas;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700644 Schema schema;
Yifan Honga83d0e42020-04-13 13:07:31 -0700645 schema.setFileName(mInFiles.front().name());
Yifan Hong94757062018-02-09 16:36:31 -0800646 if (!converter(&schema, read(mInFiles.front().stream()), error)) {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700647 return TRY_NEXT;
648 }
649 auto firstType = schema.type();
Yifan Honga83d0e42020-04-13 13:07:31 -0700650 schemas.emplace_back(std::move(schema));
Yifan Hongdbe9db32017-12-11 19:06:11 -0800651
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700652 for (auto it = mInFiles.begin() + 1; it != mInFiles.end(); ++it) {
653 Schema additionalSchema;
Yifan Hongaa219f52017-12-18 18:51:59 -0800654 const std::string& fileName = it->name();
Yifan Honga83d0e42020-04-13 13:07:31 -0700655 additionalSchema.setFileName(fileName);
Yifan Hong94757062018-02-09 16:36:31 -0800656 if (!converter(&additionalSchema, read(it->stream()), error)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800657 std::cerr << "File \"" << fileName << "\" is not a valid " << firstType << " "
658 << schemaName << " (but the first file is a valid " << firstType << " "
Yifan Hong94757062018-02-09 16:36:31 -0800659 << schemaName << "). Error: " << *error << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700660 return FAIL_AND_EXIT;
661 }
662 if (additionalSchema.type() != firstType) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800663 std::cerr << "File \"" << fileName << "\" is a " << additionalSchema.type() << " "
664 << schemaName << " (but a " << firstType << " " << schemaName
665 << " is expected)." << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700666 return FAIL_AND_EXIT;
667 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800668
Yifan Honga83d0e42020-04-13 13:07:31 -0700669 schemas.emplace_back(std::move(additionalSchema));
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700670 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800671 return assemble(&schemas) ? SUCCESS : FAIL_AND_EXIT;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700672 }
673
Yifan Hong8302cea2017-12-18 20:17:05 -0800674 bool assemble() override {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700675 using std::placeholders::_1;
676 if (mInFiles.empty()) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700677 std::cerr << "Missing input file." << std::endl;
678 return false;
679 }
680
Yifan Hong94757062018-02-09 16:36:31 -0800681 std::string manifestError;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700682 auto status = tryAssemble(gHalManifestConverter, "manifest",
Yifan Hong94757062018-02-09 16:36:31 -0800683 std::bind(&AssembleVintfImpl::assembleHalManifest, this, _1),
684 &manifestError);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700685 if (status == SUCCESS) return true;
686 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000687
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700688 resetInFiles();
Yifan Honga59d2562017-04-18 18:01:16 -0700689
Yifan Hong94757062018-02-09 16:36:31 -0800690 std::string matrixError;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700691 status = tryAssemble(gCompatibilityMatrixConverter, "compatibility matrix",
Yifan Hong94757062018-02-09 16:36:31 -0800692 std::bind(&AssembleVintfImpl::assembleCompatibilityMatrix, this, _1),
693 &matrixError);
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 Hong959ee1b2017-04-28 14:37:56 -0700697 std::cerr << "Input file has unknown format." << std::endl
Yifan Hong94757062018-02-09 16:36:31 -0800698 << "Error when attempting to convert to manifest: " << manifestError << std::endl
699 << "Error when attempting to convert to compatibility matrix: " << matrixError
700 << std::endl;
Yifan Hong959ee1b2017-04-28 14:37:56 -0700701 return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000702 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700703
Yifan Hong8302cea2017-12-18 20:17:05 -0800704 std::ostream& setOutputStream(Ostream&& out) override {
705 mOutRef = std::move(out);
706 return *mOutRef;
Yifan Hong9aa63702017-05-16 16:37:50 -0700707 }
708
Yifan Hong8302cea2017-12-18 20:17:05 -0800709 std::istream& addInputStream(const std::string& name, Istream&& in) override {
710 auto it = mInFiles.emplace(mInFiles.end(), name, std::move(in));
711 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700712 }
713
Yifan Hongdf357172020-04-13 17:24:54 -0700714 std::istream& setCheckInputStream(const std::string& name, Istream&& in) override {
715 mCheckFile = NamedIstream(name, std::move(in));
716 return mCheckFile.stream();
Yifan Hong8302cea2017-12-18 20:17:05 -0800717 }
718
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800719 bool hasKernelVersion(const KernelVersion& kernelVer) const override {
Yifan Hong8302cea2017-12-18 20:17:05 -0800720 return mKernels.find(kernelVer) != mKernels.end();
721 }
722
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800723 std::istream& addKernelConfigInputStream(const KernelVersion& kernelVer,
724 const std::string& name, Istream&& in) override {
Yifan Hong8302cea2017-12-18 20:17:05 -0800725 auto&& kernel = mKernels[kernelVer];
726 auto it = kernel.emplace(kernel.end(), name, std::move(in));
727 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700728 }
729
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700730 void resetInFiles() {
731 for (auto& inFile : mInFiles) {
Yifan Hongaa219f52017-12-18 18:51:59 -0800732 inFile.stream().clear();
733 inFile.stream().seekg(0);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700734 }
735 }
736
Yifan Hong8302cea2017-12-18 20:17:05 -0800737 void setOutputMatrix() override { mOutputMatrix = true; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700738
Yifan Hong8302cea2017-12-18 20:17:05 -0800739 bool setHalsOnly() override {
Yifan Hong5c9ff702018-08-07 17:26:12 -0700740 if (mHasSetHalsOnlyFlag) {
741 std::cerr << "Error: Cannot set --hals-only with --no-hals." << std::endl;
742 return false;
743 }
744 // Just override it with HALS_ONLY because other flags that modify mSerializeFlags
745 // does not interfere with this (except --no-hals).
746 mSerializeFlags = SerializeFlags::HALS_ONLY;
747 mHasSetHalsOnlyFlag = true;
Yifan Honga2635c42017-12-12 13:20:33 -0800748 return true;
749 }
750
Yifan Hong8302cea2017-12-18 20:17:05 -0800751 bool setNoHals() override {
Yifan Hong5c9ff702018-08-07 17:26:12 -0700752 if (mHasSetHalsOnlyFlag) {
753 std::cerr << "Error: Cannot set --hals-only with --no-hals." << std::endl;
754 return false;
755 }
756 mSerializeFlags = mSerializeFlags.disableHals();
757 mHasSetHalsOnlyFlag = true;
Yifan Honga2635c42017-12-12 13:20:33 -0800758 return true;
759 }
760
Yifan Hong86678e42018-07-26 11:38:54 -0700761 bool setNoKernelRequirements() override {
Yifan Hong5c9ff702018-08-07 17:26:12 -0700762 mSerializeFlags = mSerializeFlags.disableKernelConfigs().disableKernelMinorRevision();
Yifan Hong3f743912019-12-17 14:12:04 -0800763 mCheckFlags = mCheckFlags.disableKernel();
Yifan Hong86678e42018-07-26 11:38:54 -0700764 return true;
765 }
766
Yifan Hong9aa63702017-05-16 16:37:50 -0700767 private:
Yifan Hongaa219f52017-12-18 18:51:59 -0800768 std::vector<NamedIstream> mInFiles;
Yifan Hong8302cea2017-12-18 20:17:05 -0800769 Ostream mOutRef;
Yifan Hongdf357172020-04-13 17:24:54 -0700770 NamedIstream mCheckFile;
Yifan Hong9aa63702017-05-16 16:37:50 -0700771 bool mOutputMatrix = false;
Yifan Hong5c9ff702018-08-07 17:26:12 -0700772 bool mHasSetHalsOnlyFlag = false;
Yifan Hongba588bc2018-08-08 14:19:41 -0700773 SerializeFlags::Type mSerializeFlags = SerializeFlags::EVERYTHING;
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800774 std::map<KernelVersion, std::vector<NamedIstream>> mKernels;
Yifan Hong8302cea2017-12-18 20:17:05 -0800775 std::map<std::string, std::string> mFakeEnv;
Yifan Hong3f743912019-12-17 14:12:04 -0800776 CheckFlags::Type mCheckFlags = CheckFlags::DEFAULT;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000777};
778
Yifan Hong8302cea2017-12-18 20:17:05 -0800779bool AssembleVintf::openOutFile(const std::string& path) {
780 return static_cast<std::ofstream&>(setOutputStream(std::make_unique<std::ofstream>(path)))
781 .is_open();
782}
783
784bool AssembleVintf::openInFile(const std::string& path) {
785 return static_cast<std::ifstream&>(addInputStream(path, std::make_unique<std::ifstream>(path)))
786 .is_open();
787}
788
789bool AssembleVintf::openCheckFile(const std::string& path) {
Yifan Hongdf357172020-04-13 17:24:54 -0700790 return static_cast<std::ifstream&>(
791 setCheckInputStream(path, std::make_unique<std::ifstream>(path)))
Yifan Hong8302cea2017-12-18 20:17:05 -0800792 .is_open();
793}
794
795bool AssembleVintf::addKernel(const std::string& kernelArg) {
Yifan Hongb83e56b2018-01-09 14:36:30 -0800796 auto tokens = base::Split(kernelArg, ":");
Yifan Hong8302cea2017-12-18 20:17:05 -0800797 if (tokens.size() <= 1) {
798 std::cerr << "Unrecognized --kernel option '" << kernelArg << "'" << std::endl;
799 return false;
800 }
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800801 KernelVersion kernelVer;
Yifan Hong8302cea2017-12-18 20:17:05 -0800802 if (!parse(tokens.front(), &kernelVer)) {
803 std::cerr << "Unrecognized kernel version '" << tokens.front() << "'" << std::endl;
804 return false;
805 }
806 if (hasKernelVersion(kernelVer)) {
807 std::cerr << "Multiple --kernel for " << kernelVer << " is specified." << std::endl;
808 return false;
809 }
810 for (auto it = tokens.begin() + 1; it != tokens.end(); ++it) {
811 bool opened =
812 static_cast<std::ifstream&>(
813 addKernelConfigInputStream(kernelVer, *it, std::make_unique<std::ifstream>(*it)))
814 .is_open();
815 if (!opened) {
816 std::cerr << "Cannot open file '" << *it << "'." << std::endl;
817 return false;
818 }
819 }
820 return true;
821}
822
823std::unique_ptr<AssembleVintf> AssembleVintf::newInstance() {
824 return std::make_unique<AssembleVintfImpl>();
825}
826
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000827} // namespace vintf
828} // namespace android