blob: a6241573126746cea52b1571f9967b6dc41880d9 [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 Hong9a8b1a72017-08-25 17:55:33 -070028
Yifan Hong8302cea2017-12-18 20:17:05 -080029#include <vintf/AssembleVintf.h>
Yifan Hong79efa8a2017-07-06 14:10:28 -070030#include <vintf/KernelConfigParser.h>
Yifan Hong4d18bcc2017-04-07 21:47:16 +000031#include <vintf/parse_string.h>
32#include <vintf/parse_xml.h>
Yifan Hong8302cea2017-12-18 20:17:05 -080033#include "utils.h"
Yifan Hong4d18bcc2017-04-07 21:47:16 +000034
Yifan Hong79efa8a2017-07-06 14:10:28 -070035#define BUFFER_SIZE sysconf(_SC_PAGESIZE)
36
Yifan Hong4d18bcc2017-04-07 21:47:16 +000037namespace android {
38namespace vintf {
39
Yifan Hong9a8b1a72017-08-25 17:55:33 -070040static const std::string gConfigPrefix = "android-base-";
41static const std::string gConfigSuffix = ".cfg";
42static const std::string gBaseConfig = "android-base.cfg";
43
Yifan Hongaa219f52017-12-18 18:51:59 -080044// An input stream with a name.
45// The input stream may be an actual file, or a stringstream for testing.
46// It takes ownership on the istream.
47class NamedIstream {
48 public:
49 NamedIstream(const std::string& name, std::unique_ptr<std::istream>&& stream)
50 : mName(name), mStream(std::move(stream)) {}
51 const std::string& name() const { return mName; }
52 std::istream& stream() { return *mStream; }
53
54 private:
55 std::string mName;
56 std::unique_ptr<std::istream> mStream;
57};
58
Yifan Hong4d18bcc2017-04-07 21:47:16 +000059/**
60 * Slurps the device manifest file and add build time flag to it.
61 */
Yifan Hong8302cea2017-12-18 20:17:05 -080062class AssembleVintfImpl : public AssembleVintf {
Yifan Hong9a8b1a72017-08-25 17:55:33 -070063 using Condition = std::unique_ptr<KernelConfig>;
64 using ConditionedConfig = std::pair<Condition, std::vector<KernelConfig> /* configs */>;
65
66 public:
Yifan Hong8302cea2017-12-18 20:17:05 -080067 void setFakeEnv(const std::string& key, const std::string& value) { mFakeEnv[key] = value; }
68
69 std::string getEnv(const std::string& key) const {
70 auto it = mFakeEnv.find(key);
71 if (it != mFakeEnv.end()) {
72 return it->second;
73 }
74 const char* envValue = getenv(key.c_str());
75 return envValue != nullptr ? std::string(envValue) : std::string();
76 }
77
78 template <typename T>
79 bool getFlag(const std::string& key, T* value) const {
80 std::string envValue = getEnv(key);
81 if (envValue.empty()) {
82 std::cerr << "Warning: " << key << " is missing, defaulted to " << (*value) << "."
Yifan Hong488e16a2017-07-11 13:50:41 -070083 << std::endl;
84 return true;
Yifan Hong4d18bcc2017-04-07 21:47:16 +000085 }
86
87 if (!parse(envValue, value)) {
88 std::cerr << "Cannot parse " << envValue << "." << std::endl;
89 return false;
90 }
91 return true;
92 }
93
Yifan Hongeff04662017-12-18 16:27:24 -080094 /**
95 * Set *out to environment variable if *out is not a dummy value (i.e. default constructed).
96 */
97 template <typename T>
Yifan Hong8302cea2017-12-18 20:17:05 -080098 bool getFlagIfUnset(const std::string& envKey, T* out) const {
Yifan Hongeff04662017-12-18 16:27:24 -080099 bool hasExistingValue = !(*out == T{});
100
101 bool hasEnvValue = false;
102 T envValue;
Yifan Hong8302cea2017-12-18 20:17:05 -0800103 std::string envStrValue = getEnv(envKey);
104 if (!envStrValue.empty()) {
105 if (!parse(envStrValue, &envValue)) {
106 std::cerr << "Cannot parse " << envValue << "." << std::endl;
Yifan Hongeff04662017-12-18 16:27:24 -0800107 return false;
108 }
109 hasEnvValue = true;
110 }
111
112 if (hasExistingValue) {
113 if (hasEnvValue) {
114 std::cerr << "Warning: cannot override existing value " << *out << " with "
115 << envKey << " (which is " << envValue << ")." << std::endl;
116 }
117 return false;
118 }
119 if (!hasEnvValue) {
120 std::cerr << "Warning: " << envKey << " is not specified. Default to " << T{} << "."
121 << std::endl;
122 return false;
123 }
124 *out = envValue;
125 return true;
126 }
127
Yifan Hong8302cea2017-12-18 20:17:05 -0800128 bool getBooleanFlag(const std::string& key) const { return getEnv(key) == std::string("true"); }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800129
Yifan Hong8302cea2017-12-18 20:17:05 -0800130 size_t getIntegerFlag(const std::string& key, size_t defaultValue = 0) const {
131 std::string envValue = getEnv(key);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800132 if (envValue.empty()) {
133 return defaultValue;
134 }
135 size_t value;
136 if (!base::ParseUint(envValue, &value)) {
137 std::cerr << "Error: " << key << " must be a number." << std::endl;
138 return defaultValue;
139 }
140 return value;
141 }
142
Yifan Hong4650ad82017-05-01 17:28:02 -0700143 static std::string read(std::basic_istream<char>& is) {
144 std::stringstream ss;
145 ss << is.rdbuf();
146 return ss.str();
147 }
148
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700149 static bool isCommonConfig(const std::string& path) {
150 return ::android::base::Basename(path) == gBaseConfig;
151 }
152
Yifan Hongdbe9db32017-12-11 19:06:11 -0800153 static Level convertFromApiLevel(size_t apiLevel) {
154 if (apiLevel < 26) {
155 return Level::LEGACY;
156 } else if (apiLevel == 26) {
157 return Level::O;
158 } else if (apiLevel == 27) {
159 return Level::O_MR1;
160 } else {
161 return Level::UNSPECIFIED;
162 }
163 }
164
Yifan Hong079ec242017-08-25 18:53:38 -0700165 // nullptr on any error, otherwise the condition.
166 static Condition generateCondition(const std::string& path) {
167 std::string fname = ::android::base::Basename(path);
168 if (fname.size() <= gConfigPrefix.size() + gConfigSuffix.size() ||
169 !std::equal(gConfigPrefix.begin(), gConfigPrefix.end(), fname.begin()) ||
170 !std::equal(gConfigSuffix.rbegin(), gConfigSuffix.rend(), fname.rbegin())) {
171 return nullptr;
172 }
173
174 std::string sub = fname.substr(gConfigPrefix.size(),
175 fname.size() - gConfigPrefix.size() - gConfigSuffix.size());
176 if (sub.empty()) {
177 return nullptr; // should not happen
178 }
179 for (size_t i = 0; i < sub.size(); ++i) {
180 if (sub[i] == '-') {
181 sub[i] = '_';
182 continue;
183 }
184 if (isalnum(sub[i])) {
185 sub[i] = toupper(sub[i]);
186 continue;
187 }
188 std::cerr << "'" << fname << "' (in " << path
189 << ") is not a valid kernel config file name. Must match regex: "
190 << "android-base(-[0-9a-zA-Z-]+)?\\.cfg" << std::endl;
191 return nullptr;
192 }
193 sub.insert(0, "CONFIG_");
194 return std::make_unique<KernelConfig>(std::move(sub), Tristate::YES);
195 }
196
Yifan Hong8302cea2017-12-18 20:17:05 -0800197 static bool parseFileForKernelConfigs(std::basic_istream<char>& stream,
198 std::vector<KernelConfig>* out) {
Yifan Hong02e94002017-07-10 15:41:56 -0700199 KernelConfigParser parser(true /* processComments */, true /* relaxedFormat */);
Yifan Hong8302cea2017-12-18 20:17:05 -0800200 std::string content = read(stream);
Yifan Hong79efa8a2017-07-06 14:10:28 -0700201 status_t err = parser.process(content.c_str(), content.size());
202 if (err != OK) {
Yifan Hongae53a0e2017-07-07 15:19:06 -0700203 std::cerr << parser.error();
Yifan Hong79efa8a2017-07-06 14:10:28 -0700204 return false;
205 }
206 err = parser.finish();
207 if (err != OK) {
Yifan Hongae53a0e2017-07-07 15:19:06 -0700208 std::cerr << parser.error();
Yifan Hong79efa8a2017-07-06 14:10:28 -0700209 return false;
210 }
211
212 for (auto& configPair : parser.configs()) {
213 out->push_back({});
214 KernelConfig& config = out->back();
215 config.first = std::move(configPair.first);
216 if (!parseKernelConfigTypedValue(configPair.second, &config.second)) {
217 std::cerr << "Unknown value type for key = '" << config.first << "', value = '"
218 << configPair.second << "'\n";
219 return false;
220 }
221 }
222 return true;
223 }
224
Yifan Hong8302cea2017-12-18 20:17:05 -0800225 static bool parseFilesForKernelConfigs(std::vector<NamedIstream>* streams,
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700226 std::vector<ConditionedConfig>* out) {
227 out->clear();
228 ConditionedConfig commonConfig;
229 bool foundCommonConfig = false;
Steve Muckle0bef8682017-07-31 15:47:15 -0700230 bool ret = true;
Yifan Hong8302cea2017-12-18 20:17:05 -0800231
232 for (auto& namedStream : *streams) {
233 if (isCommonConfig(namedStream.name())) {
234 ret &= parseFileForKernelConfigs(namedStream.stream(), &commonConfig.second);
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700235 foundCommonConfig = true;
236 } else {
Yifan Hong8302cea2017-12-18 20:17:05 -0800237 Condition condition = generateCondition(namedStream.name());
Yifan Hong079ec242017-08-25 18:53:38 -0700238 ret &= (condition != nullptr);
239
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700240 std::vector<KernelConfig> kernelConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800241 if ((ret &= parseFileForKernelConfigs(namedStream.stream(), &kernelConfigs)))
Yifan Hong079ec242017-08-25 18:53:38 -0700242 out->emplace_back(std::move(condition), std::move(kernelConfigs));
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700243 }
Steve Muckle0bef8682017-07-31 15:47:15 -0700244 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700245
246 if (!foundCommonConfig) {
Yifan Hong8302cea2017-12-18 20:17:05 -0800247 std::cerr << "No android-base.cfg is found in these paths:" << std::endl;
248 for (auto& namedStream : *streams) {
249 std::cerr << " " << namedStream.name() << std::endl;
250 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700251 }
252 ret &= foundCommonConfig;
253 // first element is always common configs (no conditions).
254 out->insert(out->begin(), std::move(commonConfig));
Steve Muckle0bef8682017-07-31 15:47:15 -0700255 return ret;
256 }
257
Yifan Hongdbe9db32017-12-11 19:06:11 -0800258 static std::string getFileNameFromPath(std::string path) {
259 auto idx = path.find_last_of("\\/");
260 if (idx != std::string::npos) {
261 path.erase(0, idx + 1);
262 }
263 return path;
264 }
265
Yifan Hong8302cea2017-12-18 20:17:05 -0800266 std::basic_ostream<char>& out() const { return mOutRef == nullptr ? std::cout : *mOutRef; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700267
Yifan Hongdbe9db32017-12-11 19:06:11 -0800268 template <typename S>
269 using Schemas = std::vector<std::pair<std::string, S>>;
270 using HalManifests = Schemas<HalManifest>;
271 using CompatibilityMatrices = Schemas<CompatibilityMatrix>;
272
273 bool assembleHalManifest(HalManifests* halManifests) {
Yifan Hong4650ad82017-05-01 17:28:02 -0700274 std::string error;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800275 HalManifest* halManifest = &halManifests->front().second;
276 for (auto it = halManifests->begin() + 1; it != halManifests->end(); ++it) {
277 const std::string& path = it->first;
278 HalManifest& halToAdd = it->second;
279
280 if (halToAdd.level() != Level::UNSPECIFIED) {
281 if (halManifest->level() == Level::UNSPECIFIED) {
282 halManifest->mLevel = halToAdd.level();
283 } else if (halManifest->level() != halToAdd.level()) {
284 std::cerr << "Inconsistent FCM Version in HAL manifests:" << std::endl
285 << " File '" << halManifests->front().first << "' has level "
286 << halManifest->level() << std::endl
287 << " File '" << path << "' has level " << halToAdd.level()
288 << std::endl;
289 return false;
290 }
291 }
292
Yifan Hongea25dd42017-12-18 17:03:24 -0800293 if (!halManifest->addAllHals(&halToAdd, &error)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800294 std::cerr << "File \"" << path << "\" cannot be added: conflict on HAL \"" << error
295 << "\" with an existing HAL. See <hal> with the same name "
296 << "in previously parsed files or previously declared in this file."
297 << std::endl;
298 return false;
299 }
300 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700301
302 if (halManifest->mType == SchemaType::DEVICE) {
303 if (!getFlag("BOARD_SEPOLICY_VERS", &halManifest->device.mSepolicyVersion)) {
304 return false;
305 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800306 if (!setDeviceFcmVersion(halManifest)) {
307 return false;
308 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700309 }
310
311 if (mOutputMatrix) {
312 CompatibilityMatrix generatedMatrix = halManifest->generateCompatibleMatrix();
313 if (!halManifest->checkCompatibility(generatedMatrix, &error)) {
314 std::cerr << "FATAL ERROR: cannot generate a compatible matrix: " << error
315 << std::endl;
316 }
317 out() << "<!-- \n"
318 " Autogenerated skeleton compatibility matrix. \n"
319 " Use with caution. Modify it to suit your needs.\n"
320 " All HALs are set to optional.\n"
321 " Many entries other than HALs are zero-filled and\n"
322 " require human attention. \n"
323 "-->\n"
Yifan Honga2635c42017-12-12 13:20:33 -0800324 << gCompatibilityMatrixConverter(generatedMatrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700325 } else {
Yifan Honga2635c42017-12-12 13:20:33 -0800326 out() << gHalManifestConverter(*halManifest, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700327 }
328 out().flush();
329
Yifan Hong8302cea2017-12-18 20:17:05 -0800330 if (mCheckFile != nullptr) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700331 CompatibilityMatrix checkMatrix;
Yifan Hong8302cea2017-12-18 20:17:05 -0800332 if (!gCompatibilityMatrixConverter(&checkMatrix, read(*mCheckFile))) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700333 std::cerr << "Cannot parse check file as a compatibility matrix: "
334 << gCompatibilityMatrixConverter.lastError() << std::endl;
335 return false;
336 }
337 if (!halManifest->checkCompatibility(checkMatrix, &error)) {
338 std::cerr << "Not compatible: " << error << std::endl;
339 return false;
340 }
341 }
342
343 return true;
344 }
345
Yifan Honge88e1672017-08-24 14:42:54 -0700346 bool assembleFrameworkCompatibilityMatrixKernels(CompatibilityMatrix* matrix) {
Yifan Hong8302cea2017-12-18 20:17:05 -0800347 for (auto& pair : mKernels) {
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700348 std::vector<ConditionedConfig> conditionedConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800349 if (!parseFilesForKernelConfigs(&pair.second, &conditionedConfigs)) {
Yifan Honge88e1672017-08-24 14:42:54 -0700350 return false;
351 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700352 for (ConditionedConfig& conditionedConfig : conditionedConfigs) {
Yifan Hong48602df2017-08-28 13:04:12 -0700353 MatrixKernel kernel(KernelVersion{pair.first.majorVer, pair.first.minorVer, 0u},
354 std::move(conditionedConfig.second));
Yifan Hong079ec242017-08-25 18:53:38 -0700355 if (conditionedConfig.first != nullptr)
356 kernel.mConditions.push_back(std::move(*conditionedConfig.first));
357 matrix->framework.mKernels.push_back(std::move(kernel));
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700358 }
Yifan Honge88e1672017-08-24 14:42:54 -0700359 }
360 return true;
361 }
362
Yifan Hongdbe9db32017-12-11 19:06:11 -0800363 bool setDeviceFcmVersion(HalManifest* manifest) {
364 size_t shippingApiLevel = getIntegerFlag("PRODUCT_SHIPPING_API_LEVEL");
Yifan Hong9aa63702017-05-16 16:37:50 -0700365
Yifan Hongdbe9db32017-12-11 19:06:11 -0800366 if (manifest->level() != Level::UNSPECIFIED) {
367 return true;
368 }
369 if (!getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
370 manifest->mLevel = Level::LEGACY;
371 return true;
372 }
373 // TODO(b/70628538): Do not infer from Shipping API level.
374 if (shippingApiLevel) {
375 std::cerr << "Warning: Shipping FCM Version is inferred from Shipping API level. "
376 << "Declare Shipping FCM Version in device manifest directly." << std::endl;
377 manifest->mLevel = convertFromApiLevel(shippingApiLevel);
378 if (manifest->mLevel == Level::UNSPECIFIED) {
379 std::cerr << "Error: Shipping FCM Version cannot be inferred from Shipping API "
380 << "level " << shippingApiLevel << "."
381 << "Declare Shipping FCM Version in device manifest directly."
382 << std::endl;
383 return false;
384 }
385 return true;
386 }
387 // TODO(b/69638851): should be an error if Shipping API level is not defined.
388 // For now, just leave it empty; when framework compatibility matrix is built,
389 // lowest FCM Version is assumed.
390 std::cerr << "Warning: Shipping FCM Version cannot be inferred, because:" << std::endl
391 << " (1) It is not explicitly declared in device manifest;" << std::endl
392 << " (2) PRODUCT_ENFORCE_VINTF_MANIFEST is set to true;" << std::endl
393 << " (3) PRODUCT_SHIPPING_API_LEVEL is undefined." << std::endl
394 << "Assuming 'unspecified' Shipping FCM Version. " << std::endl
395 << "To remove this warning, define 'level' attribute in device manifest."
396 << std::endl;
397 return true;
398 }
399
400 Level getLowestFcmVersion(const CompatibilityMatrices& matrices) {
401 Level ret = Level::UNSPECIFIED;
402 for (const auto& e : matrices) {
403 if (ret == Level::UNSPECIFIED || ret > e.second.level()) {
404 ret = e.second.level();
405 }
406 }
407 return ret;
408 }
409
410 bool assembleCompatibilityMatrix(CompatibilityMatrices* matrices) {
411 std::string error;
412 CompatibilityMatrix* matrix = nullptr;
Yifan Hong9aa63702017-05-16 16:37:50 -0700413 KernelSepolicyVersion kernelSepolicyVers;
414 Version sepolicyVers;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800415 std::unique_ptr<HalManifest> checkManifest;
416 if (matrices->front().second.mType == SchemaType::DEVICE) {
417 matrix = &matrices->front().second;
418 }
419
420 if (matrices->front().second.mType == SchemaType::FRAMEWORK) {
421 Level deviceLevel = Level::UNSPECIFIED;
422 std::vector<std::string> fileList;
Yifan Hong8302cea2017-12-18 20:17:05 -0800423 if (mCheckFile != nullptr) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800424 checkManifest = std::make_unique<HalManifest>();
Yifan Hong8302cea2017-12-18 20:17:05 -0800425 if (!gHalManifestConverter(checkManifest.get(), read(*mCheckFile))) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800426 std::cerr << "Cannot parse check file as a HAL manifest: "
427 << gHalManifestConverter.lastError() << std::endl;
428 return false;
429 }
430 deviceLevel = checkManifest->level();
431 }
432
433 if (deviceLevel == Level::UNSPECIFIED) {
434 // For GSI build, legacy devices that do not have a HAL manifest,
435 // and devices in development, merge all compatibility matrices.
436 deviceLevel = getLowestFcmVersion(*matrices);
437 }
438
439 for (auto& e : *matrices) {
440 if (e.second.level() == deviceLevel) {
441 fileList.push_back(e.first);
442 matrix = &e.second;
443 }
444 }
445 if (matrix == nullptr) {
446 std::cerr << "FATAL ERROR: cannot find matrix with level '" << deviceLevel << "'"
447 << std::endl;
448 return false;
449 }
450 for (auto& e : *matrices) {
451 if (e.second.level() <= deviceLevel) {
452 continue;
453 }
454 fileList.push_back(e.first);
455 if (!matrix->addAllHalsAsOptional(&e.second, &error)) {
456 std::cerr << "File \"" << e.first << "\" cannot be added: " << error
457 << ". See <hal> with the same name "
458 << "in previously parsed files or previously declared in this file."
459 << std::endl;
460 return false;
461 }
462 }
463
Yifan Hong9aa63702017-05-16 16:37:50 -0700464 if (!getFlag("BOARD_SEPOLICY_VERS", &sepolicyVers)) {
465 return false;
466 }
467 if (!getFlag("POLICYVERS", &kernelSepolicyVers)) {
468 return false;
469 }
Yifan Honge88e1672017-08-24 14:42:54 -0700470
471 if (!assembleFrameworkCompatibilityMatrixKernels(matrix)) {
472 return false;
Yifan Hong79efa8a2017-07-06 14:10:28 -0700473 }
Yifan Honge88e1672017-08-24 14:42:54 -0700474
Yifan Hong9aa63702017-05-16 16:37:50 -0700475 matrix->framework.mSepolicy =
476 Sepolicy(kernelSepolicyVers, {{sepolicyVers.majorVer, sepolicyVers.minorVer}});
Yifan Hong7f6c00c2017-07-06 19:50:29 +0000477
478 Version avbMetaVersion;
479 if (!getFlag("FRAMEWORK_VBMETA_VERSION", &avbMetaVersion)) {
480 return false;
481 }
482 matrix->framework.mAvbMetaVersion = avbMetaVersion;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800483
484 out() << "<!--" << std::endl;
485 out() << " Input:" << std::endl;
486 for (const auto& path : fileList) {
487 out() << " " << getFileNameFromPath(path) << std::endl;
488 }
489 out() << "-->" << std::endl;
Yifan Hong9aa63702017-05-16 16:37:50 -0700490 }
Yifan Honga2635c42017-12-12 13:20:33 -0800491 out() << gCompatibilityMatrixConverter(*matrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700492 out().flush();
493
Yifan Hongdbe9db32017-12-11 19:06:11 -0800494 if (checkManifest != nullptr && getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST") &&
495 !checkManifest->checkCompatibility(*matrix, &error)) {
496 std::cerr << "Not compatible: " << error << std::endl;
497 return false;
Yifan Hong9aa63702017-05-16 16:37:50 -0700498 }
499
500 return true;
501 }
502
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700503 enum AssembleStatus { SUCCESS, FAIL_AND_EXIT, TRY_NEXT };
504 template <typename Schema, typename AssembleFunc>
505 AssembleStatus tryAssemble(const XmlConverter<Schema>& converter, const std::string& schemaName,
506 AssembleFunc assemble) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800507 Schemas<Schema> schemas;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700508 Schema schema;
Yifan Hongaa219f52017-12-18 18:51:59 -0800509 if (!converter(&schema, read(mInFiles.front().stream()))) {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700510 return TRY_NEXT;
511 }
512 auto firstType = schema.type();
Yifan Hongaa219f52017-12-18 18:51:59 -0800513 schemas.emplace_back(mInFiles.front().name(), std::move(schema));
Yifan Hongdbe9db32017-12-11 19:06:11 -0800514
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700515 for (auto it = mInFiles.begin() + 1; it != mInFiles.end(); ++it) {
516 Schema additionalSchema;
Yifan Hongaa219f52017-12-18 18:51:59 -0800517 const std::string& fileName = it->name();
518 if (!converter(&additionalSchema, read(it->stream()))) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800519 std::cerr << "File \"" << fileName << "\" is not a valid " << firstType << " "
520 << schemaName << " (but the first file is a valid " << firstType << " "
521 << schemaName << "). Error: " << converter.lastError() << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700522 return FAIL_AND_EXIT;
523 }
524 if (additionalSchema.type() != firstType) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800525 std::cerr << "File \"" << fileName << "\" is a " << additionalSchema.type() << " "
526 << schemaName << " (but a " << firstType << " " << schemaName
527 << " is expected)." << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700528 return FAIL_AND_EXIT;
529 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800530
531 schemas.emplace_back(fileName, std::move(additionalSchema));
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700532 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800533 return assemble(&schemas) ? SUCCESS : FAIL_AND_EXIT;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700534 }
535
Yifan Hong8302cea2017-12-18 20:17:05 -0800536 bool assemble() override {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700537 using std::placeholders::_1;
538 if (mInFiles.empty()) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700539 std::cerr << "Missing input file." << std::endl;
540 return false;
541 }
542
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700543 auto status = tryAssemble(gHalManifestConverter, "manifest",
Yifan Hong8302cea2017-12-18 20:17:05 -0800544 std::bind(&AssembleVintfImpl::assembleHalManifest, this, _1));
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700545 if (status == SUCCESS) return true;
546 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000547
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700548 resetInFiles();
Yifan Honga59d2562017-04-18 18:01:16 -0700549
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700550 status = tryAssemble(gCompatibilityMatrixConverter, "compatibility matrix",
Yifan Hong8302cea2017-12-18 20:17:05 -0800551 std::bind(&AssembleVintfImpl::assembleCompatibilityMatrix, this, _1));
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700552 if (status == SUCCESS) return true;
553 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000554
Yifan Hong959ee1b2017-04-28 14:37:56 -0700555 std::cerr << "Input file has unknown format." << std::endl
556 << "Error when attempting to convert to manifest: "
557 << gHalManifestConverter.lastError() << std::endl
558 << "Error when attempting to convert to compatibility matrix: "
559 << gCompatibilityMatrixConverter.lastError() << std::endl;
560 return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000561 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700562
Yifan Hong8302cea2017-12-18 20:17:05 -0800563 std::ostream& setOutputStream(Ostream&& out) override {
564 mOutRef = std::move(out);
565 return *mOutRef;
Yifan Hong9aa63702017-05-16 16:37:50 -0700566 }
567
Yifan Hong8302cea2017-12-18 20:17:05 -0800568 std::istream& addInputStream(const std::string& name, Istream&& in) override {
569 auto it = mInFiles.emplace(mInFiles.end(), name, std::move(in));
570 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700571 }
572
Yifan Hong8302cea2017-12-18 20:17:05 -0800573 std::istream& setCheckInputStream(Istream&& in) override {
574 mCheckFile = std::move(in);
575 return *mCheckFile;
576 }
577
578 bool hasKernelVersion(const Version& kernelVer) const override {
579 return mKernels.find(kernelVer) != mKernels.end();
580 }
581
582 std::istream& addKernelConfigInputStream(const Version& kernelVer, const std::string& name,
583 Istream&& in) override {
584 auto&& kernel = mKernels[kernelVer];
585 auto it = kernel.emplace(kernel.end(), name, std::move(in));
586 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700587 }
588
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700589 void resetInFiles() {
590 for (auto& inFile : mInFiles) {
Yifan Hongaa219f52017-12-18 18:51:59 -0800591 inFile.stream().clear();
592 inFile.stream().seekg(0);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700593 }
594 }
595
Yifan Hong8302cea2017-12-18 20:17:05 -0800596 void setOutputMatrix() override { mOutputMatrix = true; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700597
Yifan Hong8302cea2017-12-18 20:17:05 -0800598 bool setHalsOnly() override {
Yifan Honga2635c42017-12-12 13:20:33 -0800599 if (mSerializeFlags) return false;
600 mSerializeFlags |= SerializeFlag::HALS_ONLY;
601 return true;
602 }
603
Yifan Hong8302cea2017-12-18 20:17:05 -0800604 bool setNoHals() override {
Yifan Honga2635c42017-12-12 13:20:33 -0800605 if (mSerializeFlags) return false;
606 mSerializeFlags |= SerializeFlag::NO_HALS;
607 return true;
608 }
609
Yifan Hong9aa63702017-05-16 16:37:50 -0700610 private:
Yifan Hongaa219f52017-12-18 18:51:59 -0800611 std::vector<NamedIstream> mInFiles;
Yifan Hong8302cea2017-12-18 20:17:05 -0800612 Ostream mOutRef;
613 Istream mCheckFile;
Yifan Hong9aa63702017-05-16 16:37:50 -0700614 bool mOutputMatrix = false;
Yifan Honga2635c42017-12-12 13:20:33 -0800615 SerializeFlags mSerializeFlags = SerializeFlag::EVERYTHING;
Yifan Hong8302cea2017-12-18 20:17:05 -0800616 std::map<Version, std::vector<NamedIstream>> mKernels;
617 std::map<std::string, std::string> mFakeEnv;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000618};
619
Yifan Hong8302cea2017-12-18 20:17:05 -0800620bool AssembleVintf::openOutFile(const std::string& path) {
621 return static_cast<std::ofstream&>(setOutputStream(std::make_unique<std::ofstream>(path)))
622 .is_open();
623}
624
625bool AssembleVintf::openInFile(const std::string& path) {
626 return static_cast<std::ifstream&>(addInputStream(path, std::make_unique<std::ifstream>(path)))
627 .is_open();
628}
629
630bool AssembleVintf::openCheckFile(const std::string& path) {
631 return static_cast<std::ifstream&>(setCheckInputStream(std::make_unique<std::ifstream>(path)))
632 .is_open();
633}
634
635bool AssembleVintf::addKernel(const std::string& kernelArg) {
636 auto tokens = details::tokenize(kernelArg);
637 if (tokens.size() <= 1) {
638 std::cerr << "Unrecognized --kernel option '" << kernelArg << "'" << std::endl;
639 return false;
640 }
641 Version kernelVer;
642 if (!parse(tokens.front(), &kernelVer)) {
643 std::cerr << "Unrecognized kernel version '" << tokens.front() << "'" << std::endl;
644 return false;
645 }
646 if (hasKernelVersion(kernelVer)) {
647 std::cerr << "Multiple --kernel for " << kernelVer << " is specified." << std::endl;
648 return false;
649 }
650 for (auto it = tokens.begin() + 1; it != tokens.end(); ++it) {
651 bool opened =
652 static_cast<std::ifstream&>(
653 addKernelConfigInputStream(kernelVer, *it, std::make_unique<std::ifstream>(*it)))
654 .is_open();
655 if (!opened) {
656 std::cerr << "Cannot open file '" << *it << "'." << std::endl;
657 return false;
658 }
659 }
660 return true;
661}
662
663std::unique_ptr<AssembleVintf> AssembleVintf::newInstance() {
664 return std::make_unique<AssembleVintfImpl>();
665}
666
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000667} // namespace vintf
668} // namespace android