blob: 19294c61cd2f658d1710a52ea5d4113487b0a7eb [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
Yifan Hong9aa63702017-05-16 16:37:50 -070017#include <getopt.h>
Yifan Hong4d18bcc2017-04-07 21:47:16 +000018#include <stdlib.h>
19#include <unistd.h>
20
21#include <fstream>
22#include <iostream>
23#include <unordered_map>
24#include <sstream>
25#include <string>
26
Yifan Hong9a8b1a72017-08-25 17:55:33 -070027#include <android-base/file.h>
Yifan Hongdbe9db32017-12-11 19:06:11 -080028#include <android-base/parseint.h>
Yifan Hong9a8b1a72017-08-25 17:55:33 -070029
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>
33
Yifan Hong79efa8a2017-07-06 14:10:28 -070034#define BUFFER_SIZE sysconf(_SC_PAGESIZE)
35
Yifan Hong4d18bcc2017-04-07 21:47:16 +000036namespace android {
37namespace vintf {
38
Yifan Hong9a8b1a72017-08-25 17:55:33 -070039static const std::string gConfigPrefix = "android-base-";
40static const std::string gConfigSuffix = ".cfg";
41static const std::string gBaseConfig = "android-base.cfg";
42
Yifan Hong4d18bcc2017-04-07 21:47:16 +000043/**
44 * Slurps the device manifest file and add build time flag to it.
45 */
46class AssembleVintf {
Yifan Hong9a8b1a72017-08-25 17:55:33 -070047 using Condition = std::unique_ptr<KernelConfig>;
48 using ConditionedConfig = std::pair<Condition, std::vector<KernelConfig> /* configs */>;
49
50 public:
Yifan Hong4d18bcc2017-04-07 21:47:16 +000051 template<typename T>
52 static bool getFlag(const std::string& key, T* value) {
53 const char *envValue = getenv(key.c_str());
54 if (envValue == NULL) {
Yifan Hong488e16a2017-07-11 13:50:41 -070055 std::cerr << "Warning: " << key << " is missing, defaulted to " << (*value)
56 << std::endl;
57 return true;
Yifan Hong4d18bcc2017-04-07 21:47:16 +000058 }
59
60 if (!parse(envValue, value)) {
61 std::cerr << "Cannot parse " << envValue << "." << std::endl;
62 return false;
63 }
64 return true;
65 }
66
Yifan Hongdbe9db32017-12-11 19:06:11 -080067 static bool getBooleanFlag(const char* key) {
68 const char* envValue = getenv(key);
69 return envValue != nullptr && strcmp(envValue, "true") == 0;
70 }
71
72 static size_t getIntegerFlag(const char* key, size_t defaultValue = 0) {
73 std::string envValue = getenv(key);
74 if (envValue.empty()) {
75 return defaultValue;
76 }
77 size_t value;
78 if (!base::ParseUint(envValue, &value)) {
79 std::cerr << "Error: " << key << " must be a number." << std::endl;
80 return defaultValue;
81 }
82 return value;
83 }
84
Yifan Hong4650ad82017-05-01 17:28:02 -070085 static std::string read(std::basic_istream<char>& is) {
86 std::stringstream ss;
87 ss << is.rdbuf();
88 return ss.str();
89 }
90
Yifan Hong9a8b1a72017-08-25 17:55:33 -070091 static bool isCommonConfig(const std::string& path) {
92 return ::android::base::Basename(path) == gBaseConfig;
93 }
94
Yifan Hongdbe9db32017-12-11 19:06:11 -080095 static Level convertFromApiLevel(size_t apiLevel) {
96 if (apiLevel < 26) {
97 return Level::LEGACY;
98 } else if (apiLevel == 26) {
99 return Level::O;
100 } else if (apiLevel == 27) {
101 return Level::O_MR1;
102 } else {
103 return Level::UNSPECIFIED;
104 }
105 }
106
Yifan Hong079ec242017-08-25 18:53:38 -0700107 // nullptr on any error, otherwise the condition.
108 static Condition generateCondition(const std::string& path) {
109 std::string fname = ::android::base::Basename(path);
110 if (fname.size() <= gConfigPrefix.size() + gConfigSuffix.size() ||
111 !std::equal(gConfigPrefix.begin(), gConfigPrefix.end(), fname.begin()) ||
112 !std::equal(gConfigSuffix.rbegin(), gConfigSuffix.rend(), fname.rbegin())) {
113 return nullptr;
114 }
115
116 std::string sub = fname.substr(gConfigPrefix.size(),
117 fname.size() - gConfigPrefix.size() - gConfigSuffix.size());
118 if (sub.empty()) {
119 return nullptr; // should not happen
120 }
121 for (size_t i = 0; i < sub.size(); ++i) {
122 if (sub[i] == '-') {
123 sub[i] = '_';
124 continue;
125 }
126 if (isalnum(sub[i])) {
127 sub[i] = toupper(sub[i]);
128 continue;
129 }
130 std::cerr << "'" << fname << "' (in " << path
131 << ") is not a valid kernel config file name. Must match regex: "
132 << "android-base(-[0-9a-zA-Z-]+)?\\.cfg" << std::endl;
133 return nullptr;
134 }
135 sub.insert(0, "CONFIG_");
136 return std::make_unique<KernelConfig>(std::move(sub), Tristate::YES);
137 }
138
Yifan Hong79efa8a2017-07-06 14:10:28 -0700139 static bool parseFileForKernelConfigs(const std::string& path, std::vector<KernelConfig>* out) {
140 std::ifstream ifs{path};
141 if (!ifs.is_open()) {
142 std::cerr << "File '" << path << "' does not exist or cannot be read." << std::endl;
143 return false;
144 }
Yifan Hong02e94002017-07-10 15:41:56 -0700145 KernelConfigParser parser(true /* processComments */, true /* relaxedFormat */);
Yifan Hong79efa8a2017-07-06 14:10:28 -0700146 std::string content = read(ifs);
147 status_t err = parser.process(content.c_str(), content.size());
148 if (err != OK) {
Yifan Hongae53a0e2017-07-07 15:19:06 -0700149 std::cerr << parser.error();
Yifan Hong79efa8a2017-07-06 14:10:28 -0700150 return false;
151 }
152 err = parser.finish();
153 if (err != OK) {
Yifan Hongae53a0e2017-07-07 15:19:06 -0700154 std::cerr << parser.error();
Yifan Hong79efa8a2017-07-06 14:10:28 -0700155 return false;
156 }
157
158 for (auto& configPair : parser.configs()) {
159 out->push_back({});
160 KernelConfig& config = out->back();
161 config.first = std::move(configPair.first);
162 if (!parseKernelConfigTypedValue(configPair.second, &config.second)) {
163 std::cerr << "Unknown value type for key = '" << config.first << "', value = '"
164 << configPair.second << "'\n";
165 return false;
166 }
167 }
168 return true;
169 }
170
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700171 static bool parseFilesForKernelConfigs(const std::string& path,
172 std::vector<ConditionedConfig>* out) {
173 out->clear();
174 ConditionedConfig commonConfig;
175 bool foundCommonConfig = false;
Steve Muckle0bef8682017-07-31 15:47:15 -0700176 bool ret = true;
177 char *pathIter;
178 char *modPath = new char[path.length() + 1];
179 strcpy(modPath, path.c_str());
180 pathIter = strtok(modPath, ":");
181 while (ret && pathIter != NULL) {
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700182 if (isCommonConfig(pathIter)) {
183 ret &= parseFileForKernelConfigs(pathIter, &commonConfig.second);
184 foundCommonConfig = true;
185 } else {
Yifan Hong079ec242017-08-25 18:53:38 -0700186 Condition condition = generateCondition(pathIter);
187 ret &= (condition != nullptr);
188
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700189 std::vector<KernelConfig> kernelConfigs;
190 if ((ret &= parseFileForKernelConfigs(pathIter, &kernelConfigs)))
Yifan Hong079ec242017-08-25 18:53:38 -0700191 out->emplace_back(std::move(condition), std::move(kernelConfigs));
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700192 }
Steve Muckle0bef8682017-07-31 15:47:15 -0700193 pathIter = strtok(NULL, ":");
194 }
Luis A. Lozano82266ae2017-08-22 16:30:11 -0700195 delete[] modPath;
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700196
197 if (!foundCommonConfig) {
198 std::cerr << "No android-base.cfg is found in these paths: '" << path << "'"
199 << std::endl;
200 }
201 ret &= foundCommonConfig;
202 // first element is always common configs (no conditions).
203 out->insert(out->begin(), std::move(commonConfig));
Steve Muckle0bef8682017-07-31 15:47:15 -0700204 return ret;
205 }
206
Yifan Hongdbe9db32017-12-11 19:06:11 -0800207 static std::string getFileNameFromPath(std::string path) {
208 auto idx = path.find_last_of("\\/");
209 if (idx != std::string::npos) {
210 path.erase(0, idx + 1);
211 }
212 return path;
213 }
214
Yifan Hong9aa63702017-05-16 16:37:50 -0700215 std::basic_ostream<char>& out() const {
216 return mOutFileRef == nullptr ? std::cout : *mOutFileRef;
217 }
218
Yifan Hongdbe9db32017-12-11 19:06:11 -0800219 template <typename S>
220 using Schemas = std::vector<std::pair<std::string, S>>;
221 using HalManifests = Schemas<HalManifest>;
222 using CompatibilityMatrices = Schemas<CompatibilityMatrix>;
223
224 bool assembleHalManifest(HalManifests* halManifests) {
Yifan Hong4650ad82017-05-01 17:28:02 -0700225 std::string error;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800226 HalManifest* halManifest = &halManifests->front().second;
227 for (auto it = halManifests->begin() + 1; it != halManifests->end(); ++it) {
228 const std::string& path = it->first;
229 HalManifest& halToAdd = it->second;
230
231 if (halToAdd.level() != Level::UNSPECIFIED) {
232 if (halManifest->level() == Level::UNSPECIFIED) {
233 halManifest->mLevel = halToAdd.level();
234 } else if (halManifest->level() != halToAdd.level()) {
235 std::cerr << "Inconsistent FCM Version in HAL manifests:" << std::endl
236 << " File '" << halManifests->front().first << "' has level "
237 << halManifest->level() << std::endl
238 << " File '" << path << "' has level " << halToAdd.level()
239 << std::endl;
240 return false;
241 }
242 }
243
244 if (!halManifest->addAll(std::move(halToAdd), &error)) {
245 std::cerr << "File \"" << path << "\" cannot be added: conflict on HAL \"" << error
246 << "\" with an existing HAL. See <hal> with the same name "
247 << "in previously parsed files or previously declared in this file."
248 << std::endl;
249 return false;
250 }
251 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700252
253 if (halManifest->mType == SchemaType::DEVICE) {
254 if (!getFlag("BOARD_SEPOLICY_VERS", &halManifest->device.mSepolicyVersion)) {
255 return false;
256 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800257 if (!setDeviceFcmVersion(halManifest)) {
258 return false;
259 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700260 }
261
262 if (mOutputMatrix) {
263 CompatibilityMatrix generatedMatrix = halManifest->generateCompatibleMatrix();
264 if (!halManifest->checkCompatibility(generatedMatrix, &error)) {
265 std::cerr << "FATAL ERROR: cannot generate a compatible matrix: " << error
266 << std::endl;
267 }
268 out() << "<!-- \n"
269 " Autogenerated skeleton compatibility matrix. \n"
270 " Use with caution. Modify it to suit your needs.\n"
271 " All HALs are set to optional.\n"
272 " Many entries other than HALs are zero-filled and\n"
273 " require human attention. \n"
274 "-->\n"
Yifan Honga2635c42017-12-12 13:20:33 -0800275 << gCompatibilityMatrixConverter(generatedMatrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700276 } else {
Yifan Honga2635c42017-12-12 13:20:33 -0800277 out() << gHalManifestConverter(*halManifest, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700278 }
279 out().flush();
280
281 if (mCheckFile.is_open()) {
282 CompatibilityMatrix checkMatrix;
283 if (!gCompatibilityMatrixConverter(&checkMatrix, read(mCheckFile))) {
284 std::cerr << "Cannot parse check file as a compatibility matrix: "
285 << gCompatibilityMatrixConverter.lastError() << std::endl;
286 return false;
287 }
288 if (!halManifest->checkCompatibility(checkMatrix, &error)) {
289 std::cerr << "Not compatible: " << error << std::endl;
290 return false;
291 }
292 }
293
294 return true;
295 }
296
Yifan Honge88e1672017-08-24 14:42:54 -0700297 bool assembleFrameworkCompatibilityMatrixKernels(CompatibilityMatrix* matrix) {
Yifan Hong4c34fee2017-08-24 16:03:34 -0700298 if (!matrix->framework.mKernels.empty()) {
299 // Remove hard-coded <kernel version="x.y.z" /> in legacy files.
300 std::cerr << "WARNING: framework compatibility matrix has hard-coded kernel"
301 << " requirements for version";
302 for (const auto& kernel : matrix->framework.mKernels) {
303 std::cerr << " " << kernel.minLts();
304 }
305 std::cerr << ". Hard-coded requirements are removed." << std::endl;
306 matrix->framework.mKernels.clear();
307 }
Yifan Honge88e1672017-08-24 14:42:54 -0700308 for (const auto& pair : mKernels) {
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700309 std::vector<ConditionedConfig> conditionedConfigs;
310 if (!parseFilesForKernelConfigs(pair.second, &conditionedConfigs)) {
Yifan Honge88e1672017-08-24 14:42:54 -0700311 return false;
312 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700313 for (ConditionedConfig& conditionedConfig : conditionedConfigs) {
Yifan Hong48602df2017-08-28 13:04:12 -0700314 MatrixKernel kernel(KernelVersion{pair.first.majorVer, pair.first.minorVer, 0u},
315 std::move(conditionedConfig.second));
Yifan Hong079ec242017-08-25 18:53:38 -0700316 if (conditionedConfig.first != nullptr)
317 kernel.mConditions.push_back(std::move(*conditionedConfig.first));
318 matrix->framework.mKernels.push_back(std::move(kernel));
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700319 }
Yifan Honge88e1672017-08-24 14:42:54 -0700320 }
321 return true;
322 }
323
Yifan Hongdbe9db32017-12-11 19:06:11 -0800324 bool setDeviceFcmVersion(HalManifest* manifest) {
325 size_t shippingApiLevel = getIntegerFlag("PRODUCT_SHIPPING_API_LEVEL");
Yifan Hong9aa63702017-05-16 16:37:50 -0700326
Yifan Hongdbe9db32017-12-11 19:06:11 -0800327 if (manifest->level() != Level::UNSPECIFIED) {
328 return true;
329 }
330 if (!getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
331 manifest->mLevel = Level::LEGACY;
332 return true;
333 }
334 // TODO(b/70628538): Do not infer from Shipping API level.
335 if (shippingApiLevel) {
336 std::cerr << "Warning: Shipping FCM Version is inferred from Shipping API level. "
337 << "Declare Shipping FCM Version in device manifest directly." << std::endl;
338 manifest->mLevel = convertFromApiLevel(shippingApiLevel);
339 if (manifest->mLevel == Level::UNSPECIFIED) {
340 std::cerr << "Error: Shipping FCM Version cannot be inferred from Shipping API "
341 << "level " << shippingApiLevel << "."
342 << "Declare Shipping FCM Version in device manifest directly."
343 << std::endl;
344 return false;
345 }
346 return true;
347 }
348 // TODO(b/69638851): should be an error if Shipping API level is not defined.
349 // For now, just leave it empty; when framework compatibility matrix is built,
350 // lowest FCM Version is assumed.
351 std::cerr << "Warning: Shipping FCM Version cannot be inferred, because:" << std::endl
352 << " (1) It is not explicitly declared in device manifest;" << std::endl
353 << " (2) PRODUCT_ENFORCE_VINTF_MANIFEST is set to true;" << std::endl
354 << " (3) PRODUCT_SHIPPING_API_LEVEL is undefined." << std::endl
355 << "Assuming 'unspecified' Shipping FCM Version. " << std::endl
356 << "To remove this warning, define 'level' attribute in device manifest."
357 << std::endl;
358 return true;
359 }
360
361 Level getLowestFcmVersion(const CompatibilityMatrices& matrices) {
362 Level ret = Level::UNSPECIFIED;
363 for (const auto& e : matrices) {
364 if (ret == Level::UNSPECIFIED || ret > e.second.level()) {
365 ret = e.second.level();
366 }
367 }
368 return ret;
369 }
370
371 bool assembleCompatibilityMatrix(CompatibilityMatrices* matrices) {
372 std::string error;
373 CompatibilityMatrix* matrix = nullptr;
Yifan Hong9aa63702017-05-16 16:37:50 -0700374 KernelSepolicyVersion kernelSepolicyVers;
375 Version sepolicyVers;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800376 std::unique_ptr<HalManifest> checkManifest;
377 if (matrices->front().second.mType == SchemaType::DEVICE) {
378 matrix = &matrices->front().second;
379 }
380
381 if (matrices->front().second.mType == SchemaType::FRAMEWORK) {
382 Level deviceLevel = Level::UNSPECIFIED;
383 std::vector<std::string> fileList;
384 if (mCheckFile.is_open()) {
385 checkManifest = std::make_unique<HalManifest>();
386 if (!gHalManifestConverter(checkManifest.get(), read(mCheckFile))) {
387 std::cerr << "Cannot parse check file as a HAL manifest: "
388 << gHalManifestConverter.lastError() << std::endl;
389 return false;
390 }
391 deviceLevel = checkManifest->level();
392 }
393
394 if (deviceLevel == Level::UNSPECIFIED) {
395 // For GSI build, legacy devices that do not have a HAL manifest,
396 // and devices in development, merge all compatibility matrices.
397 deviceLevel = getLowestFcmVersion(*matrices);
398 }
399
400 for (auto& e : *matrices) {
401 if (e.second.level() == deviceLevel) {
402 fileList.push_back(e.first);
403 matrix = &e.second;
404 }
405 }
406 if (matrix == nullptr) {
407 std::cerr << "FATAL ERROR: cannot find matrix with level '" << deviceLevel << "'"
408 << std::endl;
409 return false;
410 }
411 for (auto& e : *matrices) {
412 if (e.second.level() <= deviceLevel) {
413 continue;
414 }
415 fileList.push_back(e.first);
416 if (!matrix->addAllHalsAsOptional(&e.second, &error)) {
417 std::cerr << "File \"" << e.first << "\" cannot be added: " << error
418 << ". See <hal> with the same name "
419 << "in previously parsed files or previously declared in this file."
420 << std::endl;
421 return false;
422 }
423 }
424
Yifan Hong9aa63702017-05-16 16:37:50 -0700425 if (!getFlag("BOARD_SEPOLICY_VERS", &sepolicyVers)) {
426 return false;
427 }
428 if (!getFlag("POLICYVERS", &kernelSepolicyVers)) {
429 return false;
430 }
Yifan Honge88e1672017-08-24 14:42:54 -0700431
432 if (!assembleFrameworkCompatibilityMatrixKernels(matrix)) {
433 return false;
Yifan Hong79efa8a2017-07-06 14:10:28 -0700434 }
Yifan Honge88e1672017-08-24 14:42:54 -0700435
Yifan Hong9aa63702017-05-16 16:37:50 -0700436 matrix->framework.mSepolicy =
437 Sepolicy(kernelSepolicyVers, {{sepolicyVers.majorVer, sepolicyVers.minorVer}});
Yifan Hong7f6c00c2017-07-06 19:50:29 +0000438
439 Version avbMetaVersion;
440 if (!getFlag("FRAMEWORK_VBMETA_VERSION", &avbMetaVersion)) {
441 return false;
442 }
443 matrix->framework.mAvbMetaVersion = avbMetaVersion;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800444
445 out() << "<!--" << std::endl;
446 out() << " Input:" << std::endl;
447 for (const auto& path : fileList) {
448 out() << " " << getFileNameFromPath(path) << std::endl;
449 }
450 out() << "-->" << std::endl;
Yifan Hong9aa63702017-05-16 16:37:50 -0700451 }
Yifan Honga2635c42017-12-12 13:20:33 -0800452 out() << gCompatibilityMatrixConverter(*matrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700453 out().flush();
454
Yifan Hongdbe9db32017-12-11 19:06:11 -0800455 if (checkManifest != nullptr && getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST") &&
456 !checkManifest->checkCompatibility(*matrix, &error)) {
457 std::cerr << "Not compatible: " << error << std::endl;
458 return false;
Yifan Hong9aa63702017-05-16 16:37:50 -0700459 }
460
461 return true;
462 }
463
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700464 enum AssembleStatus { SUCCESS, FAIL_AND_EXIT, TRY_NEXT };
465 template <typename Schema, typename AssembleFunc>
466 AssembleStatus tryAssemble(const XmlConverter<Schema>& converter, const std::string& schemaName,
467 AssembleFunc assemble) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800468 Schemas<Schema> schemas;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700469 Schema schema;
470 if (!converter(&schema, read(mInFiles.front()))) {
471 return TRY_NEXT;
472 }
473 auto firstType = schema.type();
Yifan Hongdbe9db32017-12-11 19:06:11 -0800474 schemas.emplace_back(mInFilePaths.front(), std::move(schema));
475
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700476 for (auto it = mInFiles.begin() + 1; it != mInFiles.end(); ++it) {
477 Schema additionalSchema;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800478 const std::string fileName = mInFilePaths[std::distance(mInFiles.begin(), it)];
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700479 if (!converter(&additionalSchema, read(*it))) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800480 std::cerr << "File \"" << fileName << "\" is not a valid " << firstType << " "
481 << schemaName << " (but the first file is a valid " << firstType << " "
482 << schemaName << "). Error: " << converter.lastError() << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700483 return FAIL_AND_EXIT;
484 }
485 if (additionalSchema.type() != firstType) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800486 std::cerr << "File \"" << fileName << "\" is a " << additionalSchema.type() << " "
487 << schemaName << " (but a " << firstType << " " << schemaName
488 << " is expected)." << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700489 return FAIL_AND_EXIT;
490 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800491
492 schemas.emplace_back(fileName, std::move(additionalSchema));
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700493 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800494 return assemble(&schemas) ? SUCCESS : FAIL_AND_EXIT;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700495 }
496
Yifan Hong9aa63702017-05-16 16:37:50 -0700497 bool assemble() {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700498 using std::placeholders::_1;
499 if (mInFiles.empty()) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700500 std::cerr << "Missing input file." << std::endl;
501 return false;
502 }
503
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700504 auto status = tryAssemble(gHalManifestConverter, "manifest",
505 std::bind(&AssembleVintf::assembleHalManifest, this, _1));
506 if (status == SUCCESS) return true;
507 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000508
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700509 resetInFiles();
Yifan Honga59d2562017-04-18 18:01:16 -0700510
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700511 status = tryAssemble(gCompatibilityMatrixConverter, "compatibility matrix",
512 std::bind(&AssembleVintf::assembleCompatibilityMatrix, this, _1));
513 if (status == SUCCESS) return true;
514 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000515
Yifan Hong959ee1b2017-04-28 14:37:56 -0700516 std::cerr << "Input file has unknown format." << std::endl
517 << "Error when attempting to convert to manifest: "
518 << gHalManifestConverter.lastError() << std::endl
519 << "Error when attempting to convert to compatibility matrix: "
520 << gCompatibilityMatrixConverter.lastError() << std::endl;
521 return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000522 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700523
524 bool openOutFile(const char* path) {
525 mOutFileRef = std::make_unique<std::ofstream>();
526 mOutFileRef->open(path);
527 return mOutFileRef->is_open();
528 }
529
530 bool openInFile(const char* path) {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700531 mInFilePaths.push_back(path);
532 mInFiles.push_back({});
533 mInFiles.back().open(path);
534 return mInFiles.back().is_open();
Yifan Hong9aa63702017-05-16 16:37:50 -0700535 }
536
537 bool openCheckFile(const char* path) {
538 mCheckFile.open(path);
539 return mCheckFile.is_open();
540 }
541
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700542 void resetInFiles() {
543 for (auto& inFile : mInFiles) {
544 inFile.clear();
545 inFile.seekg(0);
546 }
547 }
548
Yifan Hong9aa63702017-05-16 16:37:50 -0700549 void setOutputMatrix() { mOutputMatrix = true; }
550
Yifan Honga2635c42017-12-12 13:20:33 -0800551 bool setHalsOnly() {
552 if (mSerializeFlags) return false;
553 mSerializeFlags |= SerializeFlag::HALS_ONLY;
554 return true;
555 }
556
557 bool setNoHals() {
558 if (mSerializeFlags) return false;
559 mSerializeFlags |= SerializeFlag::NO_HALS;
560 return true;
561 }
562
Yifan Hong79efa8a2017-07-06 14:10:28 -0700563 bool addKernel(const std::string& kernelArg) {
564 auto ind = kernelArg.find(':');
565 if (ind == std::string::npos) {
566 std::cerr << "Unrecognized --kernel option '" << kernelArg << "'" << std::endl;
567 return false;
568 }
569 std::string kernelVerStr{kernelArg.begin(), kernelArg.begin() + ind};
570 std::string kernelConfigPath{kernelArg.begin() + ind + 1, kernelArg.end()};
571 Version kernelVer;
572 if (!parse(kernelVerStr, &kernelVer)) {
573 std::cerr << "Unrecognized kernel version '" << kernelVerStr << "'" << std::endl;
574 return false;
575 }
Yifan Hong48602df2017-08-28 13:04:12 -0700576 if (mKernels.find(kernelVer) != mKernels.end()) {
577 std::cerr << "Multiple --kernel for " << kernelVer << " is specified." << std::endl;
578 return false;
579 }
580 mKernels[kernelVer] = kernelConfigPath;
Yifan Hong79efa8a2017-07-06 14:10:28 -0700581 return true;
582 }
583
Yifan Hong9aa63702017-05-16 16:37:50 -0700584 private:
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700585 std::vector<std::string> mInFilePaths;
586 std::vector<std::ifstream> mInFiles;
Yifan Hong9aa63702017-05-16 16:37:50 -0700587 std::unique_ptr<std::ofstream> mOutFileRef;
588 std::ifstream mCheckFile;
589 bool mOutputMatrix = false;
Yifan Honga2635c42017-12-12 13:20:33 -0800590 SerializeFlags mSerializeFlags = SerializeFlag::EVERYTHING;
Yifan Hong48602df2017-08-28 13:04:12 -0700591 std::map<Version, std::string> mKernels;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000592};
593
594} // namespace vintf
595} // namespace android
596
597void help() {
Yifan Hong9aa63702017-05-16 16:37:50 -0700598 std::cerr << "assemble_vintf: Checks if a given manifest / matrix file is valid and \n"
599 " fill in build-time flags into the given file.\n"
600 "assemble_vintf -h\n"
601 " Display this help text.\n"
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700602 "assemble_vintf -i <input file>[:<input file>[...]] [-o <output file>] [-m]\n"
603 " [-c [<check file>]]\n"
Yifan Hong9aa63702017-05-16 16:37:50 -0700604 " Fill in build-time flags into the given file.\n"
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700605 " -i <input file>[:<input file>[...]]\n"
606 " A list of input files. Format is automatically detected for the\n"
607 " first file, and the remaining files must have the same format.\n"
608 " Files other than the first file should only have <hal> defined;\n"
609 " other entries are ignored.\n"
Yifan Hong9aa63702017-05-16 16:37:50 -0700610 " -o <output file>\n"
611 " Optional output file. If not specified, write to stdout.\n"
612 " -m\n"
613 " a compatible compatibility matrix is\n"
614 " generated instead; for example, given a device manifest,\n"
615 " a framework compatibility matrix is generated. This flag\n"
616 " is ignored when input is a compatibility matrix.\n"
617 " -c [<check file>]\n"
618 " After writing the output file, check compatibility between\n"
619 " output file and check file.\n"
620 " If -c is set but the check file is not specified, a warning\n"
621 " message is written to stderr. Return 0.\n"
622 " If the check file is specified but is not compatible, an error\n"
Yifan Hong79efa8a2017-07-06 14:10:28 -0700623 " message is written to stderr. Return 1.\n"
Steve Muckle0bef8682017-07-31 15:47:15 -0700624 " --kernel=<version>:<android-base.cfg>[:<android-base-arch.cfg>[...]]\n"
Yifan Hong79efa8a2017-07-06 14:10:28 -0700625 " Add a kernel entry to framework compatibility matrix.\n"
626 " Ignored for other input format.\n"
627 " <version> has format: 3.18\n"
Steve Muckle0bef8682017-07-31 15:47:15 -0700628 " <android-base.cfg> is the location of android-base.cfg\n"
629 " <android-base-arch.cfg> is the location of an optional\n"
Yifan Honga2635c42017-12-12 13:20:33 -0800630 " arch-specific config fragment, more than one may be specified\n"
631 " -l, --hals-only\n"
632 " Output has only <hal> entries. Cannot be used with -n.\n"
633 " -n, --no-hals\n"
634 " Output has no <hal> entries (but all other entries).\n"
635 " Cannot be used with -l.\n";
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000636}
637
638int main(int argc, char **argv) {
Yifan Honga2635c42017-12-12 13:20:33 -0800639 const struct option longopts[] = {{"kernel", required_argument, NULL, 'k'},
640 {"hals-only", no_argument, NULL, 'l'},
641 {"no-hals", no_argument, NULL, 'n'},
642 {0, 0, 0, 0}};
Yifan Hong9aa63702017-05-16 16:37:50 -0700643
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700644 std::string outFilePath;
Yifan Hong9aa63702017-05-16 16:37:50 -0700645 ::android::vintf::AssembleVintf assembleVintf;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000646 int res;
Yifan Hong9aa63702017-05-16 16:37:50 -0700647 int optind;
Yifan Honga2635c42017-12-12 13:20:33 -0800648 while ((res = getopt_long(argc, argv, "hi:o:mc:nl", longopts, &optind)) >= 0) {
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000649 switch (res) {
650 case 'i': {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700651 char* inFilePath = strtok(optarg, ":");
652 while (inFilePath != NULL) {
653 if (!assembleVintf.openInFile(inFilePath)) {
654 std::cerr << "Failed to open " << optarg << std::endl;
655 return 1;
656 }
657 inFilePath = strtok(NULL, ":");
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000658 }
659 } break;
660
661 case 'o': {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700662 outFilePath = optarg;
Yifan Hong9aa63702017-05-16 16:37:50 -0700663 if (!assembleVintf.openOutFile(optarg)) {
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000664 std::cerr << "Failed to open " << optarg << std::endl;
665 return 1;
666 }
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000667 } break;
668
Yifan Honga59d2562017-04-18 18:01:16 -0700669 case 'm': {
Yifan Hong9aa63702017-05-16 16:37:50 -0700670 assembleVintf.setOutputMatrix();
Yifan Honga59d2562017-04-18 18:01:16 -0700671 } break;
672
Yifan Hong4650ad82017-05-01 17:28:02 -0700673 case 'c': {
674 if (strlen(optarg) != 0) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700675 if (!assembleVintf.openCheckFile(optarg)) {
Yifan Hong4650ad82017-05-01 17:28:02 -0700676 std::cerr << "Failed to open " << optarg << std::endl;
677 return 1;
678 }
679 } else {
680 std::cerr << "WARNING: no compatibility check is done on "
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700681 << (outFilePath.empty() ? "output" : outFilePath) << std::endl;
Yifan Hong4650ad82017-05-01 17:28:02 -0700682 }
683 } break;
684
Yifan Hong79efa8a2017-07-06 14:10:28 -0700685 case 'k': {
686 if (!assembleVintf.addKernel(optarg)) {
687 std::cerr << "ERROR: Unrecognized --kernel argument." << std::endl;
688 return 1;
689 }
690 } break;
691
Yifan Honga2635c42017-12-12 13:20:33 -0800692 case 'l': {
693 if (!assembleVintf.setHalsOnly()) {
694 return 1;
695 }
696 } break;
697
698 case 'n': {
699 if (!assembleVintf.setNoHals()) {
700 return 1;
701 }
702 } break;
703
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000704 case 'h':
705 default: {
706 help();
707 return 1;
708 } break;
709 }
710 }
711
Yifan Hong9aa63702017-05-16 16:37:50 -0700712 bool success = assembleVintf.assemble();
Yifan Hong4650ad82017-05-01 17:28:02 -0700713
714 return success ? 0 : 1;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000715}