blob: 4665f680a1caf68187f9bf1215d31e7689fdc921 [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-";
42static const std::string gConfigSuffix = ".cfg";
43static const std::string gBaseConfig = "android-base.cfg";
44
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
79 template <typename T>
80 bool getFlag(const std::string& key, T* value) const {
81 std::string envValue = getEnv(key);
82 if (envValue.empty()) {
83 std::cerr << "Warning: " << key << " is missing, defaulted to " << (*value) << "."
Yifan Hong488e16a2017-07-11 13:50:41 -070084 << std::endl;
85 return true;
Yifan Hong4d18bcc2017-04-07 21:47:16 +000086 }
87
88 if (!parse(envValue, value)) {
89 std::cerr << "Cannot parse " << envValue << "." << std::endl;
90 return false;
91 }
92 return true;
93 }
94
Yifan Hongeff04662017-12-18 16:27:24 -080095 /**
96 * Set *out to environment variable if *out is not a dummy value (i.e. default constructed).
97 */
98 template <typename T>
Yifan Hong8302cea2017-12-18 20:17:05 -080099 bool getFlagIfUnset(const std::string& envKey, T* out) const {
Yifan Hongeff04662017-12-18 16:27:24 -0800100 bool hasExistingValue = !(*out == T{});
101
102 bool hasEnvValue = false;
103 T envValue;
Yifan Hong8302cea2017-12-18 20:17:05 -0800104 std::string envStrValue = getEnv(envKey);
105 if (!envStrValue.empty()) {
106 if (!parse(envStrValue, &envValue)) {
107 std::cerr << "Cannot parse " << envValue << "." << std::endl;
Yifan Hongeff04662017-12-18 16:27:24 -0800108 return false;
109 }
110 hasEnvValue = true;
111 }
112
113 if (hasExistingValue) {
114 if (hasEnvValue) {
115 std::cerr << "Warning: cannot override existing value " << *out << " with "
116 << envKey << " (which is " << envValue << ")." << std::endl;
117 }
118 return false;
119 }
120 if (!hasEnvValue) {
121 std::cerr << "Warning: " << envKey << " is not specified. Default to " << T{} << "."
122 << std::endl;
123 return false;
124 }
125 *out = envValue;
126 return true;
127 }
128
Yifan Hong8302cea2017-12-18 20:17:05 -0800129 bool getBooleanFlag(const std::string& key) const { return getEnv(key) == std::string("true"); }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800130
Yifan Hong8302cea2017-12-18 20:17:05 -0800131 size_t getIntegerFlag(const std::string& key, size_t defaultValue = 0) const {
132 std::string envValue = getEnv(key);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800133 if (envValue.empty()) {
134 return defaultValue;
135 }
136 size_t value;
137 if (!base::ParseUint(envValue, &value)) {
138 std::cerr << "Error: " << key << " must be a number." << std::endl;
139 return defaultValue;
140 }
141 return value;
142 }
143
Yifan Hong4650ad82017-05-01 17:28:02 -0700144 static std::string read(std::basic_istream<char>& is) {
145 std::stringstream ss;
146 ss << is.rdbuf();
147 return ss.str();
148 }
149
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700150 static bool isCommonConfig(const std::string& path) {
151 return ::android::base::Basename(path) == gBaseConfig;
152 }
153
Yifan Hongdbe9db32017-12-11 19:06:11 -0800154 static Level convertFromApiLevel(size_t apiLevel) {
155 if (apiLevel < 26) {
156 return Level::LEGACY;
157 } else if (apiLevel == 26) {
158 return Level::O;
159 } else if (apiLevel == 27) {
160 return Level::O_MR1;
161 } else {
162 return Level::UNSPECIFIED;
163 }
164 }
165
Yifan Hong079ec242017-08-25 18:53:38 -0700166 // nullptr on any error, otherwise the condition.
167 static Condition generateCondition(const std::string& path) {
168 std::string fname = ::android::base::Basename(path);
169 if (fname.size() <= gConfigPrefix.size() + gConfigSuffix.size() ||
170 !std::equal(gConfigPrefix.begin(), gConfigPrefix.end(), fname.begin()) ||
171 !std::equal(gConfigSuffix.rbegin(), gConfigSuffix.rend(), fname.rbegin())) {
172 return nullptr;
173 }
174
175 std::string sub = fname.substr(gConfigPrefix.size(),
176 fname.size() - gConfigPrefix.size() - gConfigSuffix.size());
177 if (sub.empty()) {
178 return nullptr; // should not happen
179 }
180 for (size_t i = 0; i < sub.size(); ++i) {
181 if (sub[i] == '-') {
182 sub[i] = '_';
183 continue;
184 }
185 if (isalnum(sub[i])) {
186 sub[i] = toupper(sub[i]);
187 continue;
188 }
189 std::cerr << "'" << fname << "' (in " << path
190 << ") is not a valid kernel config file name. Must match regex: "
191 << "android-base(-[0-9a-zA-Z-]+)?\\.cfg" << std::endl;
192 return nullptr;
193 }
194 sub.insert(0, "CONFIG_");
195 return std::make_unique<KernelConfig>(std::move(sub), Tristate::YES);
196 }
197
Yifan Hong8302cea2017-12-18 20:17:05 -0800198 static bool parseFileForKernelConfigs(std::basic_istream<char>& stream,
199 std::vector<KernelConfig>* out) {
Yifan Hong02e94002017-07-10 15:41:56 -0700200 KernelConfigParser parser(true /* processComments */, true /* relaxedFormat */);
Yifan Hong8302cea2017-12-18 20:17:05 -0800201 std::string content = read(stream);
Yifan Hong79efa8a2017-07-06 14:10:28 -0700202 status_t err = parser.process(content.c_str(), content.size());
203 if (err != OK) {
Yifan Hongae53a0e2017-07-07 15:19:06 -0700204 std::cerr << parser.error();
Yifan Hong79efa8a2017-07-06 14:10:28 -0700205 return false;
206 }
207 err = parser.finish();
208 if (err != OK) {
Yifan Hongae53a0e2017-07-07 15:19:06 -0700209 std::cerr << parser.error();
Yifan Hong79efa8a2017-07-06 14:10:28 -0700210 return false;
211 }
212
213 for (auto& configPair : parser.configs()) {
214 out->push_back({});
215 KernelConfig& config = out->back();
216 config.first = std::move(configPair.first);
217 if (!parseKernelConfigTypedValue(configPair.second, &config.second)) {
218 std::cerr << "Unknown value type for key = '" << config.first << "', value = '"
219 << configPair.second << "'\n";
220 return false;
221 }
222 }
223 return true;
224 }
225
Yifan Hong8302cea2017-12-18 20:17:05 -0800226 static bool parseFilesForKernelConfigs(std::vector<NamedIstream>* streams,
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700227 std::vector<ConditionedConfig>* out) {
228 out->clear();
229 ConditionedConfig commonConfig;
230 bool foundCommonConfig = false;
Steve Muckle0bef8682017-07-31 15:47:15 -0700231 bool ret = true;
Yifan Hong8302cea2017-12-18 20:17:05 -0800232
233 for (auto& namedStream : *streams) {
234 if (isCommonConfig(namedStream.name())) {
235 ret &= parseFileForKernelConfigs(namedStream.stream(), &commonConfig.second);
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700236 foundCommonConfig = true;
237 } else {
Yifan Hong8302cea2017-12-18 20:17:05 -0800238 Condition condition = generateCondition(namedStream.name());
Yifan Hong079ec242017-08-25 18:53:38 -0700239 ret &= (condition != nullptr);
240
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700241 std::vector<KernelConfig> kernelConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800242 if ((ret &= parseFileForKernelConfigs(namedStream.stream(), &kernelConfigs)))
Yifan Hong079ec242017-08-25 18:53:38 -0700243 out->emplace_back(std::move(condition), std::move(kernelConfigs));
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700244 }
Steve Muckle0bef8682017-07-31 15:47:15 -0700245 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700246
247 if (!foundCommonConfig) {
Yifan Hong8302cea2017-12-18 20:17:05 -0800248 std::cerr << "No android-base.cfg is found in these paths:" << std::endl;
249 for (auto& namedStream : *streams) {
250 std::cerr << " " << namedStream.name() << std::endl;
251 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700252 }
253 ret &= foundCommonConfig;
254 // first element is always common configs (no conditions).
255 out->insert(out->begin(), std::move(commonConfig));
Steve Muckle0bef8682017-07-31 15:47:15 -0700256 return ret;
257 }
258
Yifan Hongdbe9db32017-12-11 19:06:11 -0800259 static std::string getFileNameFromPath(std::string path) {
260 auto idx = path.find_last_of("\\/");
261 if (idx != std::string::npos) {
262 path.erase(0, idx + 1);
263 }
264 return path;
265 }
266
Yifan Hong8302cea2017-12-18 20:17:05 -0800267 std::basic_ostream<char>& out() const { return mOutRef == nullptr ? std::cout : *mOutRef; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700268
Yifan Hongdbe9db32017-12-11 19:06:11 -0800269 template <typename S>
270 using Schemas = std::vector<std::pair<std::string, S>>;
271 using HalManifests = Schemas<HalManifest>;
272 using CompatibilityMatrices = Schemas<CompatibilityMatrix>;
273
274 bool assembleHalManifest(HalManifests* halManifests) {
Yifan Hong4650ad82017-05-01 17:28:02 -0700275 std::string error;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800276 HalManifest* halManifest = &halManifests->front().second;
277 for (auto it = halManifests->begin() + 1; it != halManifests->end(); ++it) {
278 const std::string& path = it->first;
279 HalManifest& halToAdd = it->second;
280
281 if (halToAdd.level() != Level::UNSPECIFIED) {
282 if (halManifest->level() == Level::UNSPECIFIED) {
283 halManifest->mLevel = halToAdd.level();
284 } else if (halManifest->level() != halToAdd.level()) {
285 std::cerr << "Inconsistent FCM Version in HAL manifests:" << std::endl
286 << " File '" << halManifests->front().first << "' has level "
287 << halManifest->level() << std::endl
288 << " File '" << path << "' has level " << halToAdd.level()
289 << std::endl;
290 return false;
291 }
292 }
293
Yifan Hongea25dd42017-12-18 17:03:24 -0800294 if (!halManifest->addAllHals(&halToAdd, &error)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800295 std::cerr << "File \"" << path << "\" cannot be added: conflict on HAL \"" << error
296 << "\" with an existing HAL. See <hal> with the same name "
297 << "in previously parsed files or previously declared in this file."
298 << std::endl;
299 return false;
300 }
301 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700302
303 if (halManifest->mType == SchemaType::DEVICE) {
304 if (!getFlag("BOARD_SEPOLICY_VERS", &halManifest->device.mSepolicyVersion)) {
305 return false;
306 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800307 if (!setDeviceFcmVersion(halManifest)) {
308 return false;
309 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700310 }
311
312 if (mOutputMatrix) {
313 CompatibilityMatrix generatedMatrix = halManifest->generateCompatibleMatrix();
314 if (!halManifest->checkCompatibility(generatedMatrix, &error)) {
315 std::cerr << "FATAL ERROR: cannot generate a compatible matrix: " << error
316 << std::endl;
317 }
318 out() << "<!-- \n"
319 " Autogenerated skeleton compatibility matrix. \n"
320 " Use with caution. Modify it to suit your needs.\n"
321 " All HALs are set to optional.\n"
322 " Many entries other than HALs are zero-filled and\n"
323 " require human attention. \n"
324 "-->\n"
Yifan Honga2635c42017-12-12 13:20:33 -0800325 << gCompatibilityMatrixConverter(generatedMatrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700326 } else {
Yifan Honga2635c42017-12-12 13:20:33 -0800327 out() << gHalManifestConverter(*halManifest, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700328 }
329 out().flush();
330
Yifan Hong8302cea2017-12-18 20:17:05 -0800331 if (mCheckFile != nullptr) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700332 CompatibilityMatrix checkMatrix;
Yifan Hong8302cea2017-12-18 20:17:05 -0800333 if (!gCompatibilityMatrixConverter(&checkMatrix, read(*mCheckFile))) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700334 std::cerr << "Cannot parse check file as a compatibility matrix: "
335 << gCompatibilityMatrixConverter.lastError() << std::endl;
336 return false;
337 }
338 if (!halManifest->checkCompatibility(checkMatrix, &error)) {
339 std::cerr << "Not compatible: " << error << std::endl;
340 return false;
341 }
342 }
343
344 return true;
345 }
346
Yifan Honge88e1672017-08-24 14:42:54 -0700347 bool assembleFrameworkCompatibilityMatrixKernels(CompatibilityMatrix* matrix) {
Yifan Hong8302cea2017-12-18 20:17:05 -0800348 for (auto& pair : mKernels) {
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700349 std::vector<ConditionedConfig> conditionedConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800350 if (!parseFilesForKernelConfigs(&pair.second, &conditionedConfigs)) {
Yifan Honge88e1672017-08-24 14:42:54 -0700351 return false;
352 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700353 for (ConditionedConfig& conditionedConfig : conditionedConfigs) {
Yifan Hong48602df2017-08-28 13:04:12 -0700354 MatrixKernel kernel(KernelVersion{pair.first.majorVer, pair.first.minorVer, 0u},
355 std::move(conditionedConfig.second));
Yifan Hong079ec242017-08-25 18:53:38 -0700356 if (conditionedConfig.first != nullptr)
357 kernel.mConditions.push_back(std::move(*conditionedConfig.first));
358 matrix->framework.mKernels.push_back(std::move(kernel));
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700359 }
Yifan Honge88e1672017-08-24 14:42:54 -0700360 }
361 return true;
362 }
363
Yifan Hongdbe9db32017-12-11 19:06:11 -0800364 bool setDeviceFcmVersion(HalManifest* manifest) {
365 size_t shippingApiLevel = getIntegerFlag("PRODUCT_SHIPPING_API_LEVEL");
Yifan Hong9aa63702017-05-16 16:37:50 -0700366
Yifan Hongdbe9db32017-12-11 19:06:11 -0800367 if (manifest->level() != Level::UNSPECIFIED) {
368 return true;
369 }
370 if (!getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
371 manifest->mLevel = Level::LEGACY;
372 return true;
373 }
374 // TODO(b/70628538): Do not infer from Shipping API level.
375 if (shippingApiLevel) {
376 std::cerr << "Warning: Shipping FCM Version is inferred from Shipping API level. "
377 << "Declare Shipping FCM Version in device manifest directly." << std::endl;
378 manifest->mLevel = convertFromApiLevel(shippingApiLevel);
379 if (manifest->mLevel == Level::UNSPECIFIED) {
380 std::cerr << "Error: Shipping FCM Version cannot be inferred from Shipping API "
381 << "level " << shippingApiLevel << "."
382 << "Declare Shipping FCM Version in device manifest directly."
383 << std::endl;
384 return false;
385 }
386 return true;
387 }
388 // TODO(b/69638851): should be an error if Shipping API level is not defined.
389 // For now, just leave it empty; when framework compatibility matrix is built,
390 // lowest FCM Version is assumed.
391 std::cerr << "Warning: Shipping FCM Version cannot be inferred, because:" << std::endl
392 << " (1) It is not explicitly declared in device manifest;" << std::endl
393 << " (2) PRODUCT_ENFORCE_VINTF_MANIFEST is set to true;" << std::endl
394 << " (3) PRODUCT_SHIPPING_API_LEVEL is undefined." << std::endl
395 << "Assuming 'unspecified' Shipping FCM Version. " << std::endl
396 << "To remove this warning, define 'level' attribute in device manifest."
397 << std::endl;
398 return true;
399 }
400
401 Level getLowestFcmVersion(const CompatibilityMatrices& matrices) {
402 Level ret = Level::UNSPECIFIED;
403 for (const auto& e : matrices) {
404 if (ret == Level::UNSPECIFIED || ret > e.second.level()) {
405 ret = e.second.level();
406 }
407 }
408 return ret;
409 }
410
411 bool assembleCompatibilityMatrix(CompatibilityMatrices* matrices) {
412 std::string error;
413 CompatibilityMatrix* matrix = nullptr;
Yifan Hong9aa63702017-05-16 16:37:50 -0700414 KernelSepolicyVersion kernelSepolicyVers;
415 Version sepolicyVers;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800416 std::unique_ptr<HalManifest> checkManifest;
417 if (matrices->front().second.mType == SchemaType::DEVICE) {
418 matrix = &matrices->front().second;
419 }
420
421 if (matrices->front().second.mType == SchemaType::FRAMEWORK) {
422 Level deviceLevel = Level::UNSPECIFIED;
423 std::vector<std::string> fileList;
Yifan Hong8302cea2017-12-18 20:17:05 -0800424 if (mCheckFile != nullptr) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800425 checkManifest = std::make_unique<HalManifest>();
Yifan Hong8302cea2017-12-18 20:17:05 -0800426 if (!gHalManifestConverter(checkManifest.get(), read(*mCheckFile))) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800427 std::cerr << "Cannot parse check file as a HAL manifest: "
428 << gHalManifestConverter.lastError() << std::endl;
429 return false;
430 }
431 deviceLevel = checkManifest->level();
432 }
433
434 if (deviceLevel == Level::UNSPECIFIED) {
435 // For GSI build, legacy devices that do not have a HAL manifest,
436 // and devices in development, merge all compatibility matrices.
437 deviceLevel = getLowestFcmVersion(*matrices);
438 }
439
440 for (auto& e : *matrices) {
441 if (e.second.level() == deviceLevel) {
442 fileList.push_back(e.first);
443 matrix = &e.second;
444 }
445 }
446 if (matrix == nullptr) {
447 std::cerr << "FATAL ERROR: cannot find matrix with level '" << deviceLevel << "'"
448 << std::endl;
449 return false;
450 }
451 for (auto& e : *matrices) {
452 if (e.second.level() <= deviceLevel) {
453 continue;
454 }
455 fileList.push_back(e.first);
456 if (!matrix->addAllHalsAsOptional(&e.second, &error)) {
457 std::cerr << "File \"" << e.first << "\" cannot be added: " << error
458 << ". See <hal> with the same name "
459 << "in previously parsed files or previously declared in this file."
460 << std::endl;
461 return false;
462 }
463 }
464
Yifan Hong9aa63702017-05-16 16:37:50 -0700465 if (!getFlag("BOARD_SEPOLICY_VERS", &sepolicyVers)) {
466 return false;
467 }
468 if (!getFlag("POLICYVERS", &kernelSepolicyVers)) {
469 return false;
470 }
Yifan Honge88e1672017-08-24 14:42:54 -0700471
472 if (!assembleFrameworkCompatibilityMatrixKernels(matrix)) {
473 return false;
Yifan Hong79efa8a2017-07-06 14:10:28 -0700474 }
Yifan Honge88e1672017-08-24 14:42:54 -0700475
Yifan Hong9aa63702017-05-16 16:37:50 -0700476 matrix->framework.mSepolicy =
477 Sepolicy(kernelSepolicyVers, {{sepolicyVers.majorVer, sepolicyVers.minorVer}});
Yifan Hong7f6c00c2017-07-06 19:50:29 +0000478
479 Version avbMetaVersion;
480 if (!getFlag("FRAMEWORK_VBMETA_VERSION", &avbMetaVersion)) {
481 return false;
482 }
483 matrix->framework.mAvbMetaVersion = avbMetaVersion;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800484
485 out() << "<!--" << std::endl;
486 out() << " Input:" << std::endl;
487 for (const auto& path : fileList) {
488 out() << " " << getFileNameFromPath(path) << std::endl;
489 }
490 out() << "-->" << std::endl;
Yifan Hong9aa63702017-05-16 16:37:50 -0700491 }
Yifan Honga2635c42017-12-12 13:20:33 -0800492 out() << gCompatibilityMatrixConverter(*matrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700493 out().flush();
494
Yifan Hongdbe9db32017-12-11 19:06:11 -0800495 if (checkManifest != nullptr && getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST") &&
496 !checkManifest->checkCompatibility(*matrix, &error)) {
497 std::cerr << "Not compatible: " << error << std::endl;
498 return false;
Yifan Hong9aa63702017-05-16 16:37:50 -0700499 }
500
501 return true;
502 }
503
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700504 enum AssembleStatus { SUCCESS, FAIL_AND_EXIT, TRY_NEXT };
505 template <typename Schema, typename AssembleFunc>
506 AssembleStatus tryAssemble(const XmlConverter<Schema>& converter, const std::string& schemaName,
507 AssembleFunc assemble) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800508 Schemas<Schema> schemas;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700509 Schema schema;
Yifan Hongaa219f52017-12-18 18:51:59 -0800510 if (!converter(&schema, read(mInFiles.front().stream()))) {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700511 return TRY_NEXT;
512 }
513 auto firstType = schema.type();
Yifan Hongaa219f52017-12-18 18:51:59 -0800514 schemas.emplace_back(mInFiles.front().name(), std::move(schema));
Yifan Hongdbe9db32017-12-11 19:06:11 -0800515
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700516 for (auto it = mInFiles.begin() + 1; it != mInFiles.end(); ++it) {
517 Schema additionalSchema;
Yifan Hongaa219f52017-12-18 18:51:59 -0800518 const std::string& fileName = it->name();
519 if (!converter(&additionalSchema, read(it->stream()))) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800520 std::cerr << "File \"" << fileName << "\" is not a valid " << firstType << " "
521 << schemaName << " (but the first file is a valid " << firstType << " "
522 << schemaName << "). Error: " << converter.lastError() << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700523 return FAIL_AND_EXIT;
524 }
525 if (additionalSchema.type() != firstType) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800526 std::cerr << "File \"" << fileName << "\" is a " << additionalSchema.type() << " "
527 << schemaName << " (but a " << firstType << " " << schemaName
528 << " is expected)." << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700529 return FAIL_AND_EXIT;
530 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800531
532 schemas.emplace_back(fileName, std::move(additionalSchema));
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700533 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800534 return assemble(&schemas) ? SUCCESS : FAIL_AND_EXIT;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700535 }
536
Yifan Hong8302cea2017-12-18 20:17:05 -0800537 bool assemble() override {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700538 using std::placeholders::_1;
539 if (mInFiles.empty()) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700540 std::cerr << "Missing input file." << std::endl;
541 return false;
542 }
543
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700544 auto status = tryAssemble(gHalManifestConverter, "manifest",
Yifan Hong8302cea2017-12-18 20:17:05 -0800545 std::bind(&AssembleVintfImpl::assembleHalManifest, this, _1));
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700546 if (status == SUCCESS) return true;
547 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000548
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700549 resetInFiles();
Yifan Honga59d2562017-04-18 18:01:16 -0700550
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700551 status = tryAssemble(gCompatibilityMatrixConverter, "compatibility matrix",
Yifan Hong8302cea2017-12-18 20:17:05 -0800552 std::bind(&AssembleVintfImpl::assembleCompatibilityMatrix, this, _1));
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700553 if (status == SUCCESS) return true;
554 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000555
Yifan Hong959ee1b2017-04-28 14:37:56 -0700556 std::cerr << "Input file has unknown format." << std::endl
557 << "Error when attempting to convert to manifest: "
558 << gHalManifestConverter.lastError() << std::endl
559 << "Error when attempting to convert to compatibility matrix: "
560 << gCompatibilityMatrixConverter.lastError() << std::endl;
561 return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000562 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700563
Yifan Hong8302cea2017-12-18 20:17:05 -0800564 std::ostream& setOutputStream(Ostream&& out) override {
565 mOutRef = std::move(out);
566 return *mOutRef;
Yifan Hong9aa63702017-05-16 16:37:50 -0700567 }
568
Yifan Hong8302cea2017-12-18 20:17:05 -0800569 std::istream& addInputStream(const std::string& name, Istream&& in) override {
570 auto it = mInFiles.emplace(mInFiles.end(), name, std::move(in));
571 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700572 }
573
Yifan Hong8302cea2017-12-18 20:17:05 -0800574 std::istream& setCheckInputStream(Istream&& in) override {
575 mCheckFile = std::move(in);
576 return *mCheckFile;
577 }
578
579 bool hasKernelVersion(const Version& kernelVer) const override {
580 return mKernels.find(kernelVer) != mKernels.end();
581 }
582
583 std::istream& addKernelConfigInputStream(const Version& kernelVer, const std::string& name,
584 Istream&& in) override {
585 auto&& kernel = mKernels[kernelVer];
586 auto it = kernel.emplace(kernel.end(), name, std::move(in));
587 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700588 }
589
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700590 void resetInFiles() {
591 for (auto& inFile : mInFiles) {
Yifan Hongaa219f52017-12-18 18:51:59 -0800592 inFile.stream().clear();
593 inFile.stream().seekg(0);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700594 }
595 }
596
Yifan Hong8302cea2017-12-18 20:17:05 -0800597 void setOutputMatrix() override { mOutputMatrix = true; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700598
Yifan Hong8302cea2017-12-18 20:17:05 -0800599 bool setHalsOnly() override {
Yifan Honga2635c42017-12-12 13:20:33 -0800600 if (mSerializeFlags) return false;
601 mSerializeFlags |= SerializeFlag::HALS_ONLY;
602 return true;
603 }
604
Yifan Hong8302cea2017-12-18 20:17:05 -0800605 bool setNoHals() override {
Yifan Honga2635c42017-12-12 13:20:33 -0800606 if (mSerializeFlags) return false;
607 mSerializeFlags |= SerializeFlag::NO_HALS;
608 return true;
609 }
610
Yifan Hong9aa63702017-05-16 16:37:50 -0700611 private:
Yifan Hongaa219f52017-12-18 18:51:59 -0800612 std::vector<NamedIstream> mInFiles;
Yifan Hong8302cea2017-12-18 20:17:05 -0800613 Ostream mOutRef;
614 Istream mCheckFile;
Yifan Hong9aa63702017-05-16 16:37:50 -0700615 bool mOutputMatrix = false;
Yifan Honga2635c42017-12-12 13:20:33 -0800616 SerializeFlags mSerializeFlags = SerializeFlag::EVERYTHING;
Yifan Hong8302cea2017-12-18 20:17:05 -0800617 std::map<Version, std::vector<NamedIstream>> mKernels;
618 std::map<std::string, std::string> mFakeEnv;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000619};
620
Yifan Hong8302cea2017-12-18 20:17:05 -0800621bool AssembleVintf::openOutFile(const std::string& path) {
622 return static_cast<std::ofstream&>(setOutputStream(std::make_unique<std::ofstream>(path)))
623 .is_open();
624}
625
626bool AssembleVintf::openInFile(const std::string& path) {
627 return static_cast<std::ifstream&>(addInputStream(path, std::make_unique<std::ifstream>(path)))
628 .is_open();
629}
630
631bool AssembleVintf::openCheckFile(const std::string& path) {
632 return static_cast<std::ifstream&>(setCheckInputStream(std::make_unique<std::ifstream>(path)))
633 .is_open();
634}
635
636bool AssembleVintf::addKernel(const std::string& kernelArg) {
Yifan Hongb83e56b2018-01-09 14:36:30 -0800637 auto tokens = base::Split(kernelArg, ":");
Yifan Hong8302cea2017-12-18 20:17:05 -0800638 if (tokens.size() <= 1) {
639 std::cerr << "Unrecognized --kernel option '" << kernelArg << "'" << std::endl;
640 return false;
641 }
642 Version kernelVer;
643 if (!parse(tokens.front(), &kernelVer)) {
644 std::cerr << "Unrecognized kernel version '" << tokens.front() << "'" << std::endl;
645 return false;
646 }
647 if (hasKernelVersion(kernelVer)) {
648 std::cerr << "Multiple --kernel for " << kernelVer << " is specified." << std::endl;
649 return false;
650 }
651 for (auto it = tokens.begin() + 1; it != tokens.end(); ++it) {
652 bool opened =
653 static_cast<std::ifstream&>(
654 addKernelConfigInputStream(kernelVer, *it, std::make_unique<std::ifstream>(*it)))
655 .is_open();
656 if (!opened) {
657 std::cerr << "Cannot open file '" << *it << "'." << std::endl;
658 return false;
659 }
660 }
661 return true;
662}
663
664std::unique_ptr<AssembleVintf> AssembleVintf::newInstance() {
665 return std::make_unique<AssembleVintfImpl>();
666}
667
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000668} // namespace vintf
669} // namespace android