blob: f084076842aeb09c848ad6418dd09bafa0064b47 [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:
50 NamedIstream(const std::string& name, std::unique_ptr<std::istream>&& stream)
51 : mName(name), mStream(std::move(stream)) {}
52 const std::string& name() const { return mName; }
53 std::istream& stream() { return *mStream; }
54
55 private:
56 std::string mName;
57 std::unique_ptr<std::istream> mStream;
58};
59
Yifan Hong4d18bcc2017-04-07 21:47:16 +000060/**
61 * Slurps the device manifest file and add build time flag to it.
62 */
Yifan Hong8302cea2017-12-18 20:17:05 -080063class AssembleVintfImpl : public AssembleVintf {
Yifan Hong9a8b1a72017-08-25 17:55:33 -070064 using Condition = std::unique_ptr<KernelConfig>;
65 using ConditionedConfig = std::pair<Condition, std::vector<KernelConfig> /* configs */>;
66
67 public:
Yifan Hong8302cea2017-12-18 20:17:05 -080068 void setFakeEnv(const std::string& key, const std::string& value) { mFakeEnv[key] = value; }
69
70 std::string getEnv(const std::string& key) const {
71 auto it = mFakeEnv.find(key);
72 if (it != mFakeEnv.end()) {
73 return it->second;
74 }
75 const char* envValue = getenv(key.c_str());
76 return envValue != nullptr ? std::string(envValue) : std::string();
77 }
78
Yifan Honga28729e2018-01-17 13:40:35 -080079 // Get environment variable and split with space.
80 std::vector<std::string> getEnvList(const std::string& key) const {
81 std::vector<std::string> ret;
82 for (auto&& v : base::Split(getEnv(key), " ")) {
83 v = base::Trim(v);
84 if (!v.empty()) {
85 ret.push_back(v);
86 }
87 }
88 return ret;
89 }
90
Yifan Hong8302cea2017-12-18 20:17:05 -080091 template <typename T>
Yifan Hong4dec91d2018-08-13 12:37:47 -070092 bool getFlag(const std::string& key, T* value, bool log = true) const {
Yifan Hong8302cea2017-12-18 20:17:05 -080093 std::string envValue = getEnv(key);
94 if (envValue.empty()) {
Yifan Hong4dec91d2018-08-13 12:37:47 -070095 if (log) {
96 std::cerr << "Warning: " << key << " is missing, defaulted to " << (*value) << "."
97 << std::endl;
98 }
Yifan Hong488e16a2017-07-11 13:50:41 -070099 return true;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000100 }
101
102 if (!parse(envValue, value)) {
103 std::cerr << "Cannot parse " << envValue << "." << std::endl;
104 return false;
105 }
106 return true;
107 }
108
Yifan Hongeff04662017-12-18 16:27:24 -0800109 /**
Yifan Hong0f7f7282018-02-02 13:05:43 -0800110 * Set *out to environment variable only if *out is a dummy value (i.e. default constructed).
Yifan Hongce355902019-06-26 17:06:52 +0000111 * Return false if a fatal error has occurred:
112 * - The environment variable has an unknown format
113 * - The value of the environment variable does not match a predefined variable in the files
Yifan Hongeff04662017-12-18 16:27:24 -0800114 */
115 template <typename T>
Yifan Hongce355902019-06-26 17:06:52 +0000116 bool getFlagIfUnset(const std::string& envKey, T* out) const {
Yifan Hongeff04662017-12-18 16:27:24 -0800117 bool hasExistingValue = !(*out == T{});
118
119 bool hasEnvValue = false;
120 T envValue;
Yifan Hong8302cea2017-12-18 20:17:05 -0800121 std::string envStrValue = getEnv(envKey);
122 if (!envStrValue.empty()) {
123 if (!parse(envStrValue, &envValue)) {
Yifan Hongb36e85f2019-06-26 17:06:52 +0000124 std::cerr << "Cannot parse " << envValue << "." << std::endl;
Yifan Hongce355902019-06-26 17:06:52 +0000125 return false;
Yifan Hongeff04662017-12-18 16:27:24 -0800126 }
127 hasEnvValue = true;
128 }
129
130 if (hasExistingValue) {
Yifan Hongb36e85f2019-06-26 17:06:52 +0000131 if (hasEnvValue && (*out != envValue)) {
Yifan Hongce355902019-06-26 17:06:52 +0000132 std::cerr << "Cannot override existing value " << *out << " with " << envKey
133 << " (which is " << envValue << ")." << std::endl;
134 return false;
Yifan Hongeff04662017-12-18 16:27:24 -0800135 }
Yifan Hongce355902019-06-26 17:06:52 +0000136 return true;
Yifan Hongeff04662017-12-18 16:27:24 -0800137 }
Yifan Hongb36e85f2019-06-26 17:06:52 +0000138 if (hasEnvValue) {
139 *out = envValue;
Yifan Hongeff04662017-12-18 16:27:24 -0800140 }
Yifan Hongce355902019-06-26 17:06:52 +0000141 return true;
Yifan Hongeff04662017-12-18 16:27:24 -0800142 }
143
Yifan Hong8302cea2017-12-18 20:17:05 -0800144 bool getBooleanFlag(const std::string& key) const { return getEnv(key) == std::string("true"); }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800145
Yifan Hong8302cea2017-12-18 20:17:05 -0800146 size_t getIntegerFlag(const std::string& key, size_t defaultValue = 0) const {
147 std::string envValue = getEnv(key);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800148 if (envValue.empty()) {
149 return defaultValue;
150 }
151 size_t value;
152 if (!base::ParseUint(envValue, &value)) {
153 std::cerr << "Error: " << key << " must be a number." << std::endl;
154 return defaultValue;
155 }
156 return value;
157 }
158
Yifan Hong4650ad82017-05-01 17:28:02 -0700159 static std::string read(std::basic_istream<char>& is) {
160 std::stringstream ss;
161 ss << is.rdbuf();
162 return ss.str();
163 }
164
Yifan Hong4072b252020-03-27 16:54:16 -0700165 // Return true if name of file is "android-base.config". This file must be specified
166 // exactly once for each kernel version. These requirements do not have any conditions.
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700167 static bool isCommonConfig(const std::string& path) {
168 return ::android::base::Basename(path) == gBaseConfig;
169 }
170
Yifan Hong4072b252020-03-27 16:54:16 -0700171 // Return true if name of file matches "android-base-foo.config".
172 // Zero or more conditional configs may be specified for each kernel version. These
173 // requirements are conditional on CONFIG_FOO=y.
174 static bool isConditionalConfig(const std::string& path) {
175 auto fname = ::android::base::Basename(path);
176 return ::android::base::StartsWith(fname, gConfigPrefix) &&
177 ::android::base::EndsWith(fname, gConfigSuffix);
178 }
179
180 // Return true for all other file names (i.e. not android-base.config, and not conditional
181 // configs.)
182 // Zero or more conditional configs may be specified for each kernel version.
183 // These requirements do not have any conditions.
184 static bool isExtraCommonConfig(const std::string& path) {
185 return !isCommonConfig(path) && !isConditionalConfig(path);
186 }
187
Yifan Hong079ec242017-08-25 18:53:38 -0700188 // nullptr on any error, otherwise the condition.
189 static Condition generateCondition(const std::string& path) {
Yifan Hong4072b252020-03-27 16:54:16 -0700190 if (!isConditionalConfig(path)) {
Yifan Hong079ec242017-08-25 18:53:38 -0700191 return nullptr;
192 }
Yifan Hong4072b252020-03-27 16:54:16 -0700193 auto fname = ::android::base::Basename(path);
Yifan Hong079ec242017-08-25 18:53:38 -0700194 std::string sub = fname.substr(gConfigPrefix.size(),
195 fname.size() - gConfigPrefix.size() - gConfigSuffix.size());
196 if (sub.empty()) {
197 return nullptr; // should not happen
198 }
199 for (size_t i = 0; i < sub.size(); ++i) {
200 if (sub[i] == '-') {
201 sub[i] = '_';
202 continue;
203 }
204 if (isalnum(sub[i])) {
205 sub[i] = toupper(sub[i]);
206 continue;
207 }
208 std::cerr << "'" << fname << "' (in " << path
209 << ") is not a valid kernel config file name. Must match regex: "
Steve Mucklead6aa912018-07-23 15:44:01 -0700210 << "android-base(-[0-9a-zA-Z-]+)?\\" << gConfigSuffix
211 << std::endl;
Yifan Hong079ec242017-08-25 18:53:38 -0700212 return nullptr;
213 }
214 sub.insert(0, "CONFIG_");
215 return std::make_unique<KernelConfig>(std::move(sub), Tristate::YES);
216 }
217
Yifan Hong8302cea2017-12-18 20:17:05 -0800218 static bool parseFileForKernelConfigs(std::basic_istream<char>& stream,
219 std::vector<KernelConfig>* out) {
Yifan Hong02e94002017-07-10 15:41:56 -0700220 KernelConfigParser parser(true /* processComments */, true /* relaxedFormat */);
Yifan Honga45f77d2018-12-03 16:42:52 -0800221 status_t err = parser.processAndFinish(read(stream));
Yifan Hong79efa8a2017-07-06 14:10:28 -0700222 if (err != OK) {
Yifan Hongae53a0e2017-07-07 15:19:06 -0700223 std::cerr << parser.error();
Yifan Hong79efa8a2017-07-06 14:10:28 -0700224 return false;
225 }
226
227 for (auto& configPair : parser.configs()) {
228 out->push_back({});
229 KernelConfig& config = out->back();
230 config.first = std::move(configPair.first);
231 if (!parseKernelConfigTypedValue(configPair.second, &config.second)) {
232 std::cerr << "Unknown value type for key = '" << config.first << "', value = '"
233 << configPair.second << "'\n";
234 return false;
235 }
236 }
237 return true;
238 }
239
Yifan Hong8302cea2017-12-18 20:17:05 -0800240 static bool parseFilesForKernelConfigs(std::vector<NamedIstream>* streams,
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700241 std::vector<ConditionedConfig>* out) {
242 out->clear();
243 ConditionedConfig commonConfig;
244 bool foundCommonConfig = false;
Steve Muckle0bef8682017-07-31 15:47:15 -0700245 bool ret = true;
Yifan Hong8302cea2017-12-18 20:17:05 -0800246
247 for (auto& namedStream : *streams) {
Yifan Hong4072b252020-03-27 16:54:16 -0700248 if (isCommonConfig(namedStream.name()) || isExtraCommonConfig(namedStream.name())) {
249 if (!parseFileForKernelConfigs(namedStream.stream(), &commonConfig.second)) {
250 std::cerr << "Failed to generate common configs for file "
251 << namedStream.name();
252 ret = false;
253 }
254 if (isCommonConfig(namedStream.name())) {
255 foundCommonConfig = true;
256 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700257 } else {
Yifan Hong8302cea2017-12-18 20:17:05 -0800258 Condition condition = generateCondition(namedStream.name());
Yifan Hong4072b252020-03-27 16:54:16 -0700259 if (condition == nullptr) {
260 std::cerr << "Failed to generate conditional configs for file "
261 << namedStream.name();
262 ret = false;
263 }
Yifan Hong079ec242017-08-25 18:53:38 -0700264
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700265 std::vector<KernelConfig> kernelConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800266 if ((ret &= parseFileForKernelConfigs(namedStream.stream(), &kernelConfigs)))
Yifan Hong079ec242017-08-25 18:53:38 -0700267 out->emplace_back(std::move(condition), std::move(kernelConfigs));
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700268 }
Steve Muckle0bef8682017-07-31 15:47:15 -0700269 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700270
271 if (!foundCommonConfig) {
Steve Mucklead6aa912018-07-23 15:44:01 -0700272 std::cerr << "No " << gBaseConfig << " is found in these paths:" << std::endl;
Yifan Hong8302cea2017-12-18 20:17:05 -0800273 for (auto& namedStream : *streams) {
274 std::cerr << " " << namedStream.name() << std::endl;
275 }
Yifan Hong4072b252020-03-27 16:54:16 -0700276 ret = false;
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700277 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700278 // first element is always common configs (no conditions).
279 out->insert(out->begin(), std::move(commonConfig));
Steve Muckle0bef8682017-07-31 15:47:15 -0700280 return ret;
281 }
282
Yifan Hong8302cea2017-12-18 20:17:05 -0800283 std::basic_ostream<char>& out() const { return mOutRef == nullptr ? std::cout : *mOutRef; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700284
Yifan Hong1044fd12018-02-27 14:51:54 -0800285 // If -c is provided, check it.
286 bool checkDualFile(const HalManifest& manifest, const CompatibilityMatrix& matrix) {
287 if (getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
288 std::string error;
Yifan Hong3f743912019-12-17 14:12:04 -0800289 if (!manifest.checkCompatibility(matrix, &error, mCheckFlags)) {
Yifan Hong1044fd12018-02-27 14:51:54 -0800290 std::cerr << "Not compatible: " << error << std::endl;
291 return false;
292 }
293 }
Yifan Hong1044fd12018-02-27 14:51:54 -0800294 return true;
295 }
296
Yifan Honga83d0e42020-04-13 13:07:31 -0700297 using HalManifests = std::vector<HalManifest>;
298 using CompatibilityMatrices = std::vector<CompatibilityMatrix>;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800299
Steven Moreland5875afc2018-04-05 13:14:08 -0700300 template <typename M>
Yifan Honga83d0e42020-04-13 13:07:31 -0700301 void outputInputs(const std::vector<M>& inputs) {
Steven Moreland5875afc2018-04-05 13:14:08 -0700302 out() << "<!--" << std::endl;
303 out() << " Input:" << std::endl;
304 for (const auto& e : inputs) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700305 if (!e.fileName().empty()) {
306 out() << " " << base::Basename(e.fileName()) << std::endl;
Steven Moreland5875afc2018-04-05 13:14:08 -0700307 }
308 }
309 out() << "-->" << std::endl;
310 }
311
Yifan Honga45f77d2018-12-03 16:42:52 -0800312 // Parse --kernel arguments and write to output manifest.
313 bool setDeviceManifestKernel(HalManifest* manifest) {
314 if (mKernels.empty()) {
315 return true;
316 }
317 if (mKernels.size() > 1) {
318 std::cerr << "Warning: multiple --kernel is specified when building device manifest. "
319 << "Only the first one will be used." << std::endl;
320 }
321 auto& kernelArg = *mKernels.begin();
322 const auto& kernelVer = kernelArg.first;
323 auto& kernelConfigFiles = kernelArg.second;
324 // addKernel() guarantees that !kernelConfigFiles.empty().
325 if (kernelConfigFiles.size() > 1) {
326 std::cerr << "Warning: multiple config files are specified in --kernel when building "
327 << "device manfiest. Only the first one will be used." << std::endl;
328 }
329
330 KernelConfigParser parser(true /* processComments */, false /* relaxedFormat */);
331 status_t err = parser.processAndFinish(read(kernelConfigFiles[0].stream()));
332 if (err != OK) {
333 std::cerr << parser.error();
334 return false;
335 }
Yifan Hong08a48712019-12-11 14:56:00 -0800336
337 // Set version and configs in manifest.
338 auto kernel_info = std::make_optional<KernelInfo>();
339 kernel_info->mVersion = kernelVer;
340 kernel_info->mConfigs = parser.configs();
341 std::string error;
342 if (!manifest->mergeKernel(&kernel_info, &error)) {
343 std::cerr << error << "\n";
344 return false;
345 }
346
Yifan Honga45f77d2018-12-03 16:42:52 -0800347 return true;
348 }
349
Yifan Hong08a48712019-12-11 14:56:00 -0800350 void inferDeviceManifestKernelFcm(HalManifest* manifest) {
351 // No target FCM version.
352 if (manifest->level() == Level::UNSPECIFIED) return;
353 // target FCM version < R: leave value untouched.
354 if (manifest->level() < Level::R) return;
355 // No need to infer when <kernel> tag is missing.
356 if (!manifest->kernel().has_value()) return;
357 // Kernel FCM already set.
358 if (manifest->kernel()->level() != Level::UNSPECIFIED) return;
359
360 manifest->device.mKernel->mLevel = manifest->level();
361 }
362
Yifan Hongdbe9db32017-12-11 19:06:11 -0800363 bool assembleHalManifest(HalManifests* halManifests) {
Yifan Hong4650ad82017-05-01 17:28:02 -0700364 std::string error;
Yifan Honga83d0e42020-04-13 13:07:31 -0700365 HalManifest* halManifest = &halManifests->front();
Yifan Hongdbe9db32017-12-11 19:06:11 -0800366 for (auto it = halManifests->begin() + 1; it != halManifests->end(); ++it) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700367 const std::string& path = it->fileName();
368 HalManifest& manifestToAdd = *it;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800369
Yifan Hong0a35ab12018-06-29 11:32:57 -0700370 if (manifestToAdd.level() != Level::UNSPECIFIED) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800371 if (halManifest->level() == Level::UNSPECIFIED) {
Yifan Hong0a35ab12018-06-29 11:32:57 -0700372 halManifest->mLevel = manifestToAdd.level();
373 } else if (halManifest->level() != manifestToAdd.level()) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800374 std::cerr << "Inconsistent FCM Version in HAL manifests:" << std::endl
Yifan Honga83d0e42020-04-13 13:07:31 -0700375 << " File '" << halManifests->front().fileName() << "' has level "
Yifan Hongdbe9db32017-12-11 19:06:11 -0800376 << halManifest->level() << std::endl
Yifan Hong0a35ab12018-06-29 11:32:57 -0700377 << " File '" << path << "' has level " << manifestToAdd.level()
Yifan Hongdbe9db32017-12-11 19:06:11 -0800378 << std::endl;
379 return false;
380 }
381 }
382
Yifan Hong4c6962d2019-01-30 15:50:46 -0800383 if (!halManifest->addAll(&manifestToAdd, &error)) {
384 std::cerr << "File \"" << path << "\" cannot be added: " << error << std::endl;
Yifan Hong0a35ab12018-06-29 11:32:57 -0700385 return false;
386 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800387 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700388
389 if (halManifest->mType == SchemaType::DEVICE) {
Yifan Hongce355902019-06-26 17:06:52 +0000390 if (!getFlagIfUnset("BOARD_SEPOLICY_VERS", &halManifest->device.mSepolicyVersion)) {
391 return false;
392 }
Steven Morelandb1392772018-05-01 11:21:35 -0700393
Yifan Hongdbe9db32017-12-11 19:06:11 -0800394 if (!setDeviceFcmVersion(halManifest)) {
395 return false;
396 }
Yifan Honga45f77d2018-12-03 16:42:52 -0800397
398 if (!setDeviceManifestKernel(halManifest)) {
399 return false;
400 }
Yifan Hong08a48712019-12-11 14:56:00 -0800401
402 inferDeviceManifestKernelFcm(halManifest);
Yifan Hong9aa63702017-05-16 16:37:50 -0700403 }
404
Yifan Hongfeb454e2018-01-09 16:16:40 -0800405 if (halManifest->mType == SchemaType::FRAMEWORK) {
Yifan Honga28729e2018-01-17 13:40:35 -0800406 for (auto&& v : getEnvList("PROVIDED_VNDK_VERSIONS")) {
407 halManifest->framework.mVendorNdks.emplace_back(std::move(v));
408 }
409
Yifan Hong8b8492b2018-01-22 14:08:40 -0800410 for (auto&& v : getEnvList("PLATFORM_SYSTEMSDK_VERSIONS")) {
Yifan Honga28729e2018-01-17 13:40:35 -0800411 halManifest->framework.mSystemSdk.mVersions.emplace(std::move(v));
Yifan Hongfeb454e2018-01-09 16:16:40 -0800412 }
413 }
414
Steven Moreland5875afc2018-04-05 13:14:08 -0700415 outputInputs(*halManifests);
416
Yifan Hong9aa63702017-05-16 16:37:50 -0700417 if (mOutputMatrix) {
418 CompatibilityMatrix generatedMatrix = halManifest->generateCompatibleMatrix();
Yifan Hong3f743912019-12-17 14:12:04 -0800419 if (!halManifest->checkCompatibility(generatedMatrix, &error, mCheckFlags)) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700420 std::cerr << "FATAL ERROR: cannot generate a compatible matrix: " << error
421 << std::endl;
422 }
423 out() << "<!-- \n"
424 " Autogenerated skeleton compatibility matrix. \n"
425 " Use with caution. Modify it to suit your needs.\n"
426 " All HALs are set to optional.\n"
427 " Many entries other than HALs are zero-filled and\n"
428 " require human attention. \n"
429 "-->\n"
Yifan Honga2635c42017-12-12 13:20:33 -0800430 << gCompatibilityMatrixConverter(generatedMatrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700431 } else {
Yifan Honga2635c42017-12-12 13:20:33 -0800432 out() << gHalManifestConverter(*halManifest, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700433 }
434 out().flush();
435
Yifan Hong8302cea2017-12-18 20:17:05 -0800436 if (mCheckFile != nullptr) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700437 CompatibilityMatrix checkMatrix;
Yifan Hong94757062018-02-09 16:36:31 -0800438 if (!gCompatibilityMatrixConverter(&checkMatrix, read(*mCheckFile), &error)) {
439 std::cerr << "Cannot parse check file as a compatibility matrix: " << error
440 << std::endl;
Yifan Hong9aa63702017-05-16 16:37:50 -0700441 return false;
442 }
Yifan Hong1044fd12018-02-27 14:51:54 -0800443 if (!checkDualFile(*halManifest, checkMatrix)) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700444 return false;
445 }
446 }
447
448 return true;
449 }
450
Yifan Honge7837b12018-10-11 10:38:57 -0700451 // Parse --kernel arguments and write to output matrix.
Yifan Honge88e1672017-08-24 14:42:54 -0700452 bool assembleFrameworkCompatibilityMatrixKernels(CompatibilityMatrix* matrix) {
Yifan Hong8302cea2017-12-18 20:17:05 -0800453 for (auto& pair : mKernels) {
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700454 std::vector<ConditionedConfig> conditionedConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800455 if (!parseFilesForKernelConfigs(&pair.second, &conditionedConfigs)) {
Yifan Honge88e1672017-08-24 14:42:54 -0700456 return false;
457 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700458 for (ConditionedConfig& conditionedConfig : conditionedConfigs) {
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800459 MatrixKernel kernel(KernelVersion{pair.first}, std::move(conditionedConfig.second));
Yifan Hong079ec242017-08-25 18:53:38 -0700460 if (conditionedConfig.first != nullptr)
461 kernel.mConditions.push_back(std::move(*conditionedConfig.first));
Yifan Honge7837b12018-10-11 10:38:57 -0700462 std::string error;
463 if (!matrix->addKernel(std::move(kernel), &error)) {
464 std::cerr << "Error:" << error << std::endl;
465 return false;
466 };
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700467 }
Yifan Honge88e1672017-08-24 14:42:54 -0700468 }
469 return true;
470 }
471
Yifan Hongdbe9db32017-12-11 19:06:11 -0800472 bool setDeviceFcmVersion(HalManifest* manifest) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700473 // Not needed for generating empty manifest for DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE.
Yifan Hong2eee1982018-03-29 10:55:30 -0700474 if (getBooleanFlag("VINTF_IGNORE_TARGET_FCM_VERSION")) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700475 return true;
476 }
477
Yifan Hongdbe9db32017-12-11 19:06:11 -0800478 size_t shippingApiLevel = getIntegerFlag("PRODUCT_SHIPPING_API_LEVEL");
Yifan Hong9aa63702017-05-16 16:37:50 -0700479
Yifan Hongdbe9db32017-12-11 19:06:11 -0800480 if (manifest->level() != Level::UNSPECIFIED) {
481 return true;
482 }
483 if (!getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
484 manifest->mLevel = Level::LEGACY;
485 return true;
486 }
487 // TODO(b/70628538): Do not infer from Shipping API level.
488 if (shippingApiLevel) {
489 std::cerr << "Warning: Shipping FCM Version is inferred from Shipping API level. "
490 << "Declare Shipping FCM Version in device manifest directly." << std::endl;
Yifan Hongafae1982018-01-11 18:15:24 -0800491 manifest->mLevel = details::convertFromApiLevel(shippingApiLevel);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800492 if (manifest->mLevel == Level::UNSPECIFIED) {
493 std::cerr << "Error: Shipping FCM Version cannot be inferred from Shipping API "
494 << "level " << shippingApiLevel << "."
495 << "Declare Shipping FCM Version in device manifest directly."
496 << std::endl;
497 return false;
498 }
499 return true;
500 }
501 // TODO(b/69638851): should be an error if Shipping API level is not defined.
502 // For now, just leave it empty; when framework compatibility matrix is built,
503 // lowest FCM Version is assumed.
504 std::cerr << "Warning: Shipping FCM Version cannot be inferred, because:" << std::endl
505 << " (1) It is not explicitly declared in device manifest;" << std::endl
506 << " (2) PRODUCT_ENFORCE_VINTF_MANIFEST is set to true;" << std::endl
507 << " (3) PRODUCT_SHIPPING_API_LEVEL is undefined." << std::endl
508 << "Assuming 'unspecified' Shipping FCM Version. " << std::endl
509 << "To remove this warning, define 'level' attribute in device manifest."
510 << std::endl;
511 return true;
512 }
513
514 Level getLowestFcmVersion(const CompatibilityMatrices& matrices) {
515 Level ret = Level::UNSPECIFIED;
516 for (const auto& e : matrices) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700517 if (ret == Level::UNSPECIFIED || ret > e.level()) {
518 ret = e.level();
Yifan Hongdbe9db32017-12-11 19:06:11 -0800519 }
520 }
521 return ret;
522 }
523
524 bool assembleCompatibilityMatrix(CompatibilityMatrices* matrices) {
525 std::string error;
526 CompatibilityMatrix* matrix = nullptr;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800527 std::unique_ptr<HalManifest> checkManifest;
Yifan Honge7837b12018-10-11 10:38:57 -0700528 std::unique_ptr<CompatibilityMatrix> builtMatrix;
529
530 if (mCheckFile != nullptr) {
531 checkManifest = std::make_unique<HalManifest>();
532 if (!gHalManifestConverter(checkManifest.get(), read(*mCheckFile), &error)) {
533 std::cerr << "Cannot parse check file as a HAL manifest: " << error << std::endl;
534 return false;
535 }
536 }
537
Yifan Honga83d0e42020-04-13 13:07:31 -0700538 if (matrices->front().mType == SchemaType::DEVICE) {
Yifan Hong5a51ea52019-04-22 14:54:57 -0700539 builtMatrix = CompatibilityMatrix::combineDeviceMatrices(matrices, &error);
540 matrix = builtMatrix.get();
541
542 if (matrix == nullptr) {
543 std::cerr << error << std::endl;
Yifan Honge7837b12018-10-11 10:38:57 -0700544 return false;
545 }
546
Yifan Hongfeb454e2018-01-09 16:16:40 -0800547 auto vndkVersion = base::Trim(getEnv("REQUIRED_VNDK_VERSION"));
548 if (!vndkVersion.empty()) {
549 auto& valueInMatrix = matrix->device.mVendorNdk;
550 if (!valueInMatrix.version().empty() && valueInMatrix.version() != vndkVersion) {
551 std::cerr << "Hard-coded <vendor-ndk> version in device compatibility matrix ("
Yifan Honga83d0e42020-04-13 13:07:31 -0700552 << matrices->front().fileName() << "), '" << valueInMatrix.version()
Yifan Hongfeb454e2018-01-09 16:16:40 -0800553 << "', does not match value inferred "
554 << "from BOARD_VNDK_VERSION '" << vndkVersion << "'" << std::endl;
555 return false;
556 }
557 valueInMatrix = VendorNdk{std::move(vndkVersion)};
558 }
Yifan Honga28729e2018-01-17 13:40:35 -0800559
560 for (auto&& v : getEnvList("BOARD_SYSTEMSDK_VERSIONS")) {
561 matrix->device.mSystemSdk.mVersions.emplace(std::move(v));
562 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800563 }
564
Yifan Honga83d0e42020-04-13 13:07:31 -0700565 if (matrices->front().mType == SchemaType::FRAMEWORK) {
Yifan Honge7837b12018-10-11 10:38:57 -0700566 Level deviceLevel =
567 checkManifest != nullptr ? checkManifest->level() : Level::UNSPECIFIED;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800568 if (deviceLevel == Level::UNSPECIFIED) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800569 deviceLevel = getLowestFcmVersion(*matrices);
Yifan Honge7837b12018-10-11 10:38:57 -0700570 if (checkManifest != nullptr && deviceLevel != Level::UNSPECIFIED) {
571 std::cerr << "Warning: No Target FCM Version for device. Assuming \""
572 << to_string(deviceLevel)
573 << "\" when building final framework compatibility matrix."
574 << std::endl;
575 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800576 }
Yifan Honge7837b12018-10-11 10:38:57 -0700577 builtMatrix = CompatibilityMatrix::combine(deviceLevel, matrices, &error);
578 matrix = builtMatrix.get();
Yifan Hongdbe9db32017-12-11 19:06:11 -0800579
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700580 if (matrix == nullptr) {
581 std::cerr << error << std::endl;
582 return false;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800583 }
584
Yifan Honge88e1672017-08-24 14:42:54 -0700585 if (!assembleFrameworkCompatibilityMatrixKernels(matrix)) {
586 return false;
Yifan Hong79efa8a2017-07-06 14:10:28 -0700587 }
Yifan Honge88e1672017-08-24 14:42:54 -0700588
Yifan Hong321ee222018-02-02 12:46:52 -0800589 // Add PLATFORM_SEPOLICY_* to sepolicy.sepolicy-version. Remove dupes.
590 std::set<Version> sepolicyVersions;
591 auto sepolicyVersionStrings = getEnvList("PLATFORM_SEPOLICY_COMPAT_VERSIONS");
592 auto currentSepolicyVersionString = getEnv("PLATFORM_SEPOLICY_VERSION");
593 if (!currentSepolicyVersionString.empty()) {
594 sepolicyVersionStrings.push_back(currentSepolicyVersionString);
595 }
596 for (auto&& s : sepolicyVersionStrings) {
597 Version v;
598 if (!parse(s, &v)) {
599 std::cerr << "Error: unknown sepolicy version '" << s << "' specified by "
600 << (s == currentSepolicyVersionString
601 ? "PLATFORM_SEPOLICY_VERSION"
602 : "PLATFORM_SEPOLICY_COMPAT_VERSIONS")
603 << ".";
604 return false;
605 }
606 sepolicyVersions.insert(v);
607 }
608 for (auto&& v : sepolicyVersions) {
609 matrix->framework.mSepolicy.mSepolicyVersionRanges.emplace_back(v.majorVer,
610 v.minorVer);
Yifan Hong7f6c00c2017-07-06 19:50:29 +0000611 }
Yifan Hongf4135012017-12-18 17:27:19 -0800612
Yifan Hongce355902019-06-26 17:06:52 +0000613 if (!getFlagIfUnset("POLICYVERS",
614 &matrix->framework.mSepolicy.mKernelSepolicyVersion)) {
615 return false;
616 }
617 if (!getFlagIfUnset("FRAMEWORK_VBMETA_VERSION", &matrix->framework.mAvbMetaVersion)) {
618 return false;
619 }
Yifan Hong4dec91d2018-08-13 12:37:47 -0700620 // Hard-override existing AVB version
621 getFlag("FRAMEWORK_VBMETA_VERSION_OVERRIDE", &matrix->framework.mAvbMetaVersion,
622 false /* log */);
Yifan Hong9aa63702017-05-16 16:37:50 -0700623 }
Steven Moreland5875afc2018-04-05 13:14:08 -0700624 outputInputs(*matrices);
Yifan Honga2635c42017-12-12 13:20:33 -0800625 out() << gCompatibilityMatrixConverter(*matrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700626 out().flush();
627
Yifan Hong1044fd12018-02-27 14:51:54 -0800628 if (checkManifest != nullptr && !checkDualFile(*checkManifest, *matrix)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800629 return false;
Yifan Hong9aa63702017-05-16 16:37:50 -0700630 }
631
632 return true;
633 }
634
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700635 enum AssembleStatus { SUCCESS, FAIL_AND_EXIT, TRY_NEXT };
636 template <typename Schema, typename AssembleFunc>
637 AssembleStatus tryAssemble(const XmlConverter<Schema>& converter, const std::string& schemaName,
Yifan Hong94757062018-02-09 16:36:31 -0800638 AssembleFunc assemble, std::string* error) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700639 std::vector<Schema> schemas;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700640 Schema schema;
Yifan Honga83d0e42020-04-13 13:07:31 -0700641 schema.setFileName(mInFiles.front().name());
Yifan Hong94757062018-02-09 16:36:31 -0800642 if (!converter(&schema, read(mInFiles.front().stream()), error)) {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700643 return TRY_NEXT;
644 }
645 auto firstType = schema.type();
Yifan Honga83d0e42020-04-13 13:07:31 -0700646 schemas.emplace_back(std::move(schema));
Yifan Hongdbe9db32017-12-11 19:06:11 -0800647
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700648 for (auto it = mInFiles.begin() + 1; it != mInFiles.end(); ++it) {
649 Schema additionalSchema;
Yifan Hongaa219f52017-12-18 18:51:59 -0800650 const std::string& fileName = it->name();
Yifan Honga83d0e42020-04-13 13:07:31 -0700651 additionalSchema.setFileName(fileName);
Yifan Hong94757062018-02-09 16:36:31 -0800652 if (!converter(&additionalSchema, read(it->stream()), error)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800653 std::cerr << "File \"" << fileName << "\" is not a valid " << firstType << " "
654 << schemaName << " (but the first file is a valid " << firstType << " "
Yifan Hong94757062018-02-09 16:36:31 -0800655 << schemaName << "). Error: " << *error << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700656 return FAIL_AND_EXIT;
657 }
658 if (additionalSchema.type() != firstType) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800659 std::cerr << "File \"" << fileName << "\" is a " << additionalSchema.type() << " "
660 << schemaName << " (but a " << firstType << " " << schemaName
661 << " is expected)." << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700662 return FAIL_AND_EXIT;
663 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800664
Yifan Honga83d0e42020-04-13 13:07:31 -0700665 schemas.emplace_back(std::move(additionalSchema));
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700666 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800667 return assemble(&schemas) ? SUCCESS : FAIL_AND_EXIT;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700668 }
669
Yifan Hong8302cea2017-12-18 20:17:05 -0800670 bool assemble() override {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700671 using std::placeholders::_1;
672 if (mInFiles.empty()) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700673 std::cerr << "Missing input file." << std::endl;
674 return false;
675 }
676
Yifan Hong94757062018-02-09 16:36:31 -0800677 std::string manifestError;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700678 auto status = tryAssemble(gHalManifestConverter, "manifest",
Yifan Hong94757062018-02-09 16:36:31 -0800679 std::bind(&AssembleVintfImpl::assembleHalManifest, this, _1),
680 &manifestError);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700681 if (status == SUCCESS) return true;
682 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000683
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700684 resetInFiles();
Yifan Honga59d2562017-04-18 18:01:16 -0700685
Yifan Hong94757062018-02-09 16:36:31 -0800686 std::string matrixError;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700687 status = tryAssemble(gCompatibilityMatrixConverter, "compatibility matrix",
Yifan Hong94757062018-02-09 16:36:31 -0800688 std::bind(&AssembleVintfImpl::assembleCompatibilityMatrix, this, _1),
689 &matrixError);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700690 if (status == SUCCESS) return true;
691 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000692
Yifan Hong959ee1b2017-04-28 14:37:56 -0700693 std::cerr << "Input file has unknown format." << std::endl
Yifan Hong94757062018-02-09 16:36:31 -0800694 << "Error when attempting to convert to manifest: " << manifestError << std::endl
695 << "Error when attempting to convert to compatibility matrix: " << matrixError
696 << std::endl;
Yifan Hong959ee1b2017-04-28 14:37:56 -0700697 return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000698 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700699
Yifan Hong8302cea2017-12-18 20:17:05 -0800700 std::ostream& setOutputStream(Ostream&& out) override {
701 mOutRef = std::move(out);
702 return *mOutRef;
Yifan Hong9aa63702017-05-16 16:37:50 -0700703 }
704
Yifan Hong8302cea2017-12-18 20:17:05 -0800705 std::istream& addInputStream(const std::string& name, Istream&& in) override {
706 auto it = mInFiles.emplace(mInFiles.end(), name, std::move(in));
707 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700708 }
709
Yifan Hong8302cea2017-12-18 20:17:05 -0800710 std::istream& setCheckInputStream(Istream&& in) override {
711 mCheckFile = std::move(in);
712 return *mCheckFile;
713 }
714
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800715 bool hasKernelVersion(const KernelVersion& kernelVer) const override {
Yifan Hong8302cea2017-12-18 20:17:05 -0800716 return mKernels.find(kernelVer) != mKernels.end();
717 }
718
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800719 std::istream& addKernelConfigInputStream(const KernelVersion& kernelVer,
720 const std::string& name, Istream&& in) override {
Yifan Hong8302cea2017-12-18 20:17:05 -0800721 auto&& kernel = mKernels[kernelVer];
722 auto it = kernel.emplace(kernel.end(), name, std::move(in));
723 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700724 }
725
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700726 void resetInFiles() {
727 for (auto& inFile : mInFiles) {
Yifan Hongaa219f52017-12-18 18:51:59 -0800728 inFile.stream().clear();
729 inFile.stream().seekg(0);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700730 }
731 }
732
Yifan Hong8302cea2017-12-18 20:17:05 -0800733 void setOutputMatrix() override { mOutputMatrix = true; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700734
Yifan Hong8302cea2017-12-18 20:17:05 -0800735 bool setHalsOnly() override {
Yifan Hong5c9ff702018-08-07 17:26:12 -0700736 if (mHasSetHalsOnlyFlag) {
737 std::cerr << "Error: Cannot set --hals-only with --no-hals." << std::endl;
738 return false;
739 }
740 // Just override it with HALS_ONLY because other flags that modify mSerializeFlags
741 // does not interfere with this (except --no-hals).
742 mSerializeFlags = SerializeFlags::HALS_ONLY;
743 mHasSetHalsOnlyFlag = true;
Yifan Honga2635c42017-12-12 13:20:33 -0800744 return true;
745 }
746
Yifan Hong8302cea2017-12-18 20:17:05 -0800747 bool setNoHals() override {
Yifan Hong5c9ff702018-08-07 17:26:12 -0700748 if (mHasSetHalsOnlyFlag) {
749 std::cerr << "Error: Cannot set --hals-only with --no-hals." << std::endl;
750 return false;
751 }
752 mSerializeFlags = mSerializeFlags.disableHals();
753 mHasSetHalsOnlyFlag = true;
Yifan Honga2635c42017-12-12 13:20:33 -0800754 return true;
755 }
756
Yifan Hong86678e42018-07-26 11:38:54 -0700757 bool setNoKernelRequirements() override {
Yifan Hong5c9ff702018-08-07 17:26:12 -0700758 mSerializeFlags = mSerializeFlags.disableKernelConfigs().disableKernelMinorRevision();
Yifan Hong3f743912019-12-17 14:12:04 -0800759 mCheckFlags = mCheckFlags.disableKernel();
Yifan Hong86678e42018-07-26 11:38:54 -0700760 return true;
761 }
762
Yifan Hong9aa63702017-05-16 16:37:50 -0700763 private:
Yifan Hongaa219f52017-12-18 18:51:59 -0800764 std::vector<NamedIstream> mInFiles;
Yifan Hong8302cea2017-12-18 20:17:05 -0800765 Ostream mOutRef;
766 Istream mCheckFile;
Yifan Hong9aa63702017-05-16 16:37:50 -0700767 bool mOutputMatrix = false;
Yifan Hong5c9ff702018-08-07 17:26:12 -0700768 bool mHasSetHalsOnlyFlag = false;
Yifan Hongba588bc2018-08-08 14:19:41 -0700769 SerializeFlags::Type mSerializeFlags = SerializeFlags::EVERYTHING;
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800770 std::map<KernelVersion, std::vector<NamedIstream>> mKernels;
Yifan Hong8302cea2017-12-18 20:17:05 -0800771 std::map<std::string, std::string> mFakeEnv;
Yifan Hong3f743912019-12-17 14:12:04 -0800772 CheckFlags::Type mCheckFlags = CheckFlags::DEFAULT;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000773};
774
Yifan Hong8302cea2017-12-18 20:17:05 -0800775bool AssembleVintf::openOutFile(const std::string& path) {
776 return static_cast<std::ofstream&>(setOutputStream(std::make_unique<std::ofstream>(path)))
777 .is_open();
778}
779
780bool AssembleVintf::openInFile(const std::string& path) {
781 return static_cast<std::ifstream&>(addInputStream(path, std::make_unique<std::ifstream>(path)))
782 .is_open();
783}
784
785bool AssembleVintf::openCheckFile(const std::string& path) {
786 return static_cast<std::ifstream&>(setCheckInputStream(std::make_unique<std::ifstream>(path)))
787 .is_open();
788}
789
790bool AssembleVintf::addKernel(const std::string& kernelArg) {
Yifan Hongb83e56b2018-01-09 14:36:30 -0800791 auto tokens = base::Split(kernelArg, ":");
Yifan Hong8302cea2017-12-18 20:17:05 -0800792 if (tokens.size() <= 1) {
793 std::cerr << "Unrecognized --kernel option '" << kernelArg << "'" << std::endl;
794 return false;
795 }
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800796 KernelVersion kernelVer;
Yifan Hong8302cea2017-12-18 20:17:05 -0800797 if (!parse(tokens.front(), &kernelVer)) {
798 std::cerr << "Unrecognized kernel version '" << tokens.front() << "'" << std::endl;
799 return false;
800 }
801 if (hasKernelVersion(kernelVer)) {
802 std::cerr << "Multiple --kernel for " << kernelVer << " is specified." << std::endl;
803 return false;
804 }
805 for (auto it = tokens.begin() + 1; it != tokens.end(); ++it) {
806 bool opened =
807 static_cast<std::ifstream&>(
808 addKernelConfigInputStream(kernelVer, *it, std::make_unique<std::ifstream>(*it)))
809 .is_open();
810 if (!opened) {
811 std::cerr << "Cannot open file '" << *it << "'." << std::endl;
812 return false;
813 }
814 }
815 return true;
816}
817
818std::unique_ptr<AssembleVintf> AssembleVintf::newInstance() {
819 return std::make_unique<AssembleVintfImpl>();
820}
821
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000822} // namespace vintf
823} // namespace android