blob: 8b7e609a570354139473061292586e700fb86176 [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 Hong79efa8a2017-07-06 14:10:28 -070027#include <vintf/KernelConfigParser.h>
Yifan Hong4d18bcc2017-04-07 21:47:16 +000028#include <vintf/parse_string.h>
29#include <vintf/parse_xml.h>
30
Yifan Hong79efa8a2017-07-06 14:10:28 -070031#define BUFFER_SIZE sysconf(_SC_PAGESIZE)
32
Yifan Hong4d18bcc2017-04-07 21:47:16 +000033namespace android {
34namespace vintf {
35
36/**
37 * Slurps the device manifest file and add build time flag to it.
38 */
39class AssembleVintf {
40public:
41 template<typename T>
42 static bool getFlag(const std::string& key, T* value) {
43 const char *envValue = getenv(key.c_str());
44 if (envValue == NULL) {
Yifan Hong488e16a2017-07-11 13:50:41 -070045 std::cerr << "Warning: " << key << " is missing, defaulted to " << (*value)
46 << std::endl;
47 return true;
Yifan Hong4d18bcc2017-04-07 21:47:16 +000048 }
49
50 if (!parse(envValue, value)) {
51 std::cerr << "Cannot parse " << envValue << "." << std::endl;
52 return false;
53 }
54 return true;
55 }
56
Yifan Hong4650ad82017-05-01 17:28:02 -070057 static std::string read(std::basic_istream<char>& is) {
58 std::stringstream ss;
59 ss << is.rdbuf();
60 return ss.str();
61 }
62
Yifan Hong79efa8a2017-07-06 14:10:28 -070063 static bool parseFileForKernelConfigs(const std::string& path, std::vector<KernelConfig>* out) {
64 std::ifstream ifs{path};
65 if (!ifs.is_open()) {
66 std::cerr << "File '" << path << "' does not exist or cannot be read." << std::endl;
67 return false;
68 }
Yifan Hong02e94002017-07-10 15:41:56 -070069 KernelConfigParser parser(true /* processComments */, true /* relaxedFormat */);
Yifan Hong79efa8a2017-07-06 14:10:28 -070070 std::string content = read(ifs);
71 status_t err = parser.process(content.c_str(), content.size());
72 if (err != OK) {
Yifan Hongae53a0e2017-07-07 15:19:06 -070073 std::cerr << parser.error();
Yifan Hong79efa8a2017-07-06 14:10:28 -070074 return false;
75 }
76 err = parser.finish();
77 if (err != OK) {
Yifan Hongae53a0e2017-07-07 15:19:06 -070078 std::cerr << parser.error();
Yifan Hong79efa8a2017-07-06 14:10:28 -070079 return false;
80 }
81
82 for (auto& configPair : parser.configs()) {
83 out->push_back({});
84 KernelConfig& config = out->back();
85 config.first = std::move(configPair.first);
86 if (!parseKernelConfigTypedValue(configPair.second, &config.second)) {
87 std::cerr << "Unknown value type for key = '" << config.first << "', value = '"
88 << configPair.second << "'\n";
89 return false;
90 }
91 }
92 return true;
93 }
94
Yifan Hong9aa63702017-05-16 16:37:50 -070095 std::basic_ostream<char>& out() const {
96 return mOutFileRef == nullptr ? std::cout : *mOutFileRef;
97 }
98
99 bool assembleHalManifest(HalManifest* halManifest) {
Yifan Hong4650ad82017-05-01 17:28:02 -0700100 std::string error;
Yifan Hong9aa63702017-05-16 16:37:50 -0700101
102 if (halManifest->mType == SchemaType::DEVICE) {
103 if (!getFlag("BOARD_SEPOLICY_VERS", &halManifest->device.mSepolicyVersion)) {
104 return false;
105 }
106 }
107
108 if (mOutputMatrix) {
109 CompatibilityMatrix generatedMatrix = halManifest->generateCompatibleMatrix();
110 if (!halManifest->checkCompatibility(generatedMatrix, &error)) {
111 std::cerr << "FATAL ERROR: cannot generate a compatible matrix: " << error
112 << std::endl;
113 }
114 out() << "<!-- \n"
115 " Autogenerated skeleton compatibility matrix. \n"
116 " Use with caution. Modify it to suit your needs.\n"
117 " All HALs are set to optional.\n"
118 " Many entries other than HALs are zero-filled and\n"
119 " require human attention. \n"
120 "-->\n"
121 << gCompatibilityMatrixConverter(generatedMatrix);
122 } else {
123 out() << gHalManifestConverter(*halManifest);
124 }
125 out().flush();
126
127 if (mCheckFile.is_open()) {
128 CompatibilityMatrix checkMatrix;
129 if (!gCompatibilityMatrixConverter(&checkMatrix, read(mCheckFile))) {
130 std::cerr << "Cannot parse check file as a compatibility matrix: "
131 << gCompatibilityMatrixConverter.lastError() << std::endl;
132 return false;
133 }
134 if (!halManifest->checkCompatibility(checkMatrix, &error)) {
135 std::cerr << "Not compatible: " << error << std::endl;
136 return false;
137 }
138 }
139
140 return true;
141 }
142
143 bool assembleCompatibilityMatrix(CompatibilityMatrix* matrix) {
144 std::string error;
145
146 KernelSepolicyVersion kernelSepolicyVers;
147 Version sepolicyVers;
148 if (matrix->mType == SchemaType::FRAMEWORK) {
149 if (!getFlag("BOARD_SEPOLICY_VERS", &sepolicyVers)) {
150 return false;
151 }
152 if (!getFlag("POLICYVERS", &kernelSepolicyVers)) {
153 return false;
154 }
Yifan Hong79efa8a2017-07-06 14:10:28 -0700155 for (const auto& pair : mKernels) {
156 std::vector<KernelConfig> configs;
157 if (!parseFileForKernelConfigs(pair.second, &configs)) {
158 return false;
159 }
160 bool added = false;
161 for (auto& e : matrix->framework.mKernels) {
162 if (e.minLts() == pair.first) {
163 e.mConfigs.insert(e.mConfigs.end(), configs.begin(), configs.end());
164 added = true;
165 break;
166 }
167 }
168 if (!added) {
169 matrix->framework.mKernels.push_back(
170 MatrixKernel{KernelVersion{pair.first}, std::move(configs)});
171 }
172 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700173 matrix->framework.mSepolicy =
174 Sepolicy(kernelSepolicyVers, {{sepolicyVers.majorVer, sepolicyVers.minorVer}});
Yifan Hong7f6c00c2017-07-06 19:50:29 +0000175
176 Version avbMetaVersion;
177 if (!getFlag("FRAMEWORK_VBMETA_VERSION", &avbMetaVersion)) {
178 return false;
179 }
180 matrix->framework.mAvbMetaVersion = avbMetaVersion;
Yifan Hong9aa63702017-05-16 16:37:50 -0700181 }
182 out() << gCompatibilityMatrixConverter(*matrix);
183 out().flush();
184
185 if (mCheckFile.is_open()) {
186 HalManifest checkManifest;
187 if (!gHalManifestConverter(&checkManifest, read(mCheckFile))) {
188 std::cerr << "Cannot parse check file as a HAL manifest: "
189 << gHalManifestConverter.lastError() << std::endl;
190 return false;
191 }
192 if (!checkManifest.checkCompatibility(*matrix, &error)) {
193 std::cerr << "Not compatible: " << error << std::endl;
194 return false;
195 }
196 }
197
198 return true;
199 }
200
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700201 enum AssembleStatus { SUCCESS, FAIL_AND_EXIT, TRY_NEXT };
202 template <typename Schema, typename AssembleFunc>
203 AssembleStatus tryAssemble(const XmlConverter<Schema>& converter, const std::string& schemaName,
204 AssembleFunc assemble) {
205 Schema schema;
206 if (!converter(&schema, read(mInFiles.front()))) {
207 return TRY_NEXT;
208 }
209 auto firstType = schema.type();
210 for (auto it = mInFiles.begin() + 1; it != mInFiles.end(); ++it) {
211 Schema additionalSchema;
212 if (!converter(&additionalSchema, read(*it))) {
213 std::cerr << "File \"" << mInFilePaths[std::distance(mInFiles.begin(), it)]
214 << "\" is not a valid " << firstType << " " << schemaName
215 << " (but the first file is a valid " << firstType << " " << schemaName
216 << "). Error: " << converter.lastError() << std::endl;
217 return FAIL_AND_EXIT;
218 }
219 if (additionalSchema.type() != firstType) {
220 std::cerr << "File \"" << mInFilePaths[std::distance(mInFiles.begin(), it)]
221 << "\" is a " << additionalSchema.type() << " " << schemaName
222 << " (but a " << firstType << " " << schemaName << " is expected)."
223 << std::endl;
224 return FAIL_AND_EXIT;
225 }
226 schema.addAll(std::move(additionalSchema));
227 }
228 return assemble(&schema) ? SUCCESS : FAIL_AND_EXIT;
229 }
230
Yifan Hong9aa63702017-05-16 16:37:50 -0700231 bool assemble() {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700232 using std::placeholders::_1;
233 if (mInFiles.empty()) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700234 std::cerr << "Missing input file." << std::endl;
235 return false;
236 }
237
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700238 auto status = tryAssemble(gHalManifestConverter, "manifest",
239 std::bind(&AssembleVintf::assembleHalManifest, this, _1));
240 if (status == SUCCESS) return true;
241 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000242
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700243 resetInFiles();
Yifan Honga59d2562017-04-18 18:01:16 -0700244
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700245 status = tryAssemble(gCompatibilityMatrixConverter, "compatibility matrix",
246 std::bind(&AssembleVintf::assembleCompatibilityMatrix, this, _1));
247 if (status == SUCCESS) return true;
248 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000249
Yifan Hong959ee1b2017-04-28 14:37:56 -0700250 std::cerr << "Input file has unknown format." << std::endl
251 << "Error when attempting to convert to manifest: "
252 << gHalManifestConverter.lastError() << std::endl
253 << "Error when attempting to convert to compatibility matrix: "
254 << gCompatibilityMatrixConverter.lastError() << std::endl;
255 return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000256 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700257
258 bool openOutFile(const char* path) {
259 mOutFileRef = std::make_unique<std::ofstream>();
260 mOutFileRef->open(path);
261 return mOutFileRef->is_open();
262 }
263
264 bool openInFile(const char* path) {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700265 mInFilePaths.push_back(path);
266 mInFiles.push_back({});
267 mInFiles.back().open(path);
268 return mInFiles.back().is_open();
Yifan Hong9aa63702017-05-16 16:37:50 -0700269 }
270
271 bool openCheckFile(const char* path) {
272 mCheckFile.open(path);
273 return mCheckFile.is_open();
274 }
275
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700276 void resetInFiles() {
277 for (auto& inFile : mInFiles) {
278 inFile.clear();
279 inFile.seekg(0);
280 }
281 }
282
Yifan Hong9aa63702017-05-16 16:37:50 -0700283 void setOutputMatrix() { mOutputMatrix = true; }
284
Yifan Hong79efa8a2017-07-06 14:10:28 -0700285 bool addKernel(const std::string& kernelArg) {
286 auto ind = kernelArg.find(':');
287 if (ind == std::string::npos) {
288 std::cerr << "Unrecognized --kernel option '" << kernelArg << "'" << std::endl;
289 return false;
290 }
291 std::string kernelVerStr{kernelArg.begin(), kernelArg.begin() + ind};
292 std::string kernelConfigPath{kernelArg.begin() + ind + 1, kernelArg.end()};
293 Version kernelVer;
294 if (!parse(kernelVerStr, &kernelVer)) {
295 std::cerr << "Unrecognized kernel version '" << kernelVerStr << "'" << std::endl;
296 return false;
297 }
298 mKernels.push_back({{kernelVer.majorVer, kernelVer.minorVer, 0u}, kernelConfigPath});
299 return true;
300 }
301
Yifan Hong9aa63702017-05-16 16:37:50 -0700302 private:
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700303 std::vector<std::string> mInFilePaths;
304 std::vector<std::ifstream> mInFiles;
Yifan Hong9aa63702017-05-16 16:37:50 -0700305 std::unique_ptr<std::ofstream> mOutFileRef;
306 std::ifstream mCheckFile;
307 bool mOutputMatrix = false;
Yifan Hong79efa8a2017-07-06 14:10:28 -0700308 std::vector<std::pair<KernelVersion, std::string>> mKernels;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000309};
310
311} // namespace vintf
312} // namespace android
313
314void help() {
Yifan Hong9aa63702017-05-16 16:37:50 -0700315 std::cerr << "assemble_vintf: Checks if a given manifest / matrix file is valid and \n"
316 " fill in build-time flags into the given file.\n"
317 "assemble_vintf -h\n"
318 " Display this help text.\n"
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700319 "assemble_vintf -i <input file>[:<input file>[...]] [-o <output file>] [-m]\n"
320 " [-c [<check file>]]\n"
Yifan Hong9aa63702017-05-16 16:37:50 -0700321 " Fill in build-time flags into the given file.\n"
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700322 " -i <input file>[:<input file>[...]]\n"
323 " A list of input files. Format is automatically detected for the\n"
324 " first file, and the remaining files must have the same format.\n"
325 " Files other than the first file should only have <hal> defined;\n"
326 " other entries are ignored.\n"
Yifan Hong9aa63702017-05-16 16:37:50 -0700327 " -o <output file>\n"
328 " Optional output file. If not specified, write to stdout.\n"
329 " -m\n"
330 " a compatible compatibility matrix is\n"
331 " generated instead; for example, given a device manifest,\n"
332 " a framework compatibility matrix is generated. This flag\n"
333 " is ignored when input is a compatibility matrix.\n"
334 " -c [<check file>]\n"
335 " After writing the output file, check compatibility between\n"
336 " output file and check file.\n"
337 " If -c is set but the check file is not specified, a warning\n"
338 " message is written to stderr. Return 0.\n"
339 " If the check file is specified but is not compatible, an error\n"
Yifan Hong79efa8a2017-07-06 14:10:28 -0700340 " message is written to stderr. Return 1.\n"
341 " --kernel=<version>:<android-base.cfg>\n"
342 " Add a kernel entry to framework compatibility matrix.\n"
343 " Ignored for other input format.\n"
344 " <version> has format: 3.18\n"
345 " <android-base.cfg> is the location of android-base.cfg\n";
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000346}
347
348int main(int argc, char **argv) {
Yifan Hong79efa8a2017-07-06 14:10:28 -0700349 const struct option longopts[] = {{"kernel", required_argument, NULL, 'k'}, {0, 0, 0, 0}};
Yifan Hong9aa63702017-05-16 16:37:50 -0700350
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700351 std::string outFilePath;
Yifan Hong9aa63702017-05-16 16:37:50 -0700352 ::android::vintf::AssembleVintf assembleVintf;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000353 int res;
Yifan Hong9aa63702017-05-16 16:37:50 -0700354 int optind;
355 while ((res = getopt_long(argc, argv, "hi:o:mc:", longopts, &optind)) >= 0) {
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000356 switch (res) {
357 case 'i': {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700358 char* inFilePath = strtok(optarg, ":");
359 while (inFilePath != NULL) {
360 if (!assembleVintf.openInFile(inFilePath)) {
361 std::cerr << "Failed to open " << optarg << std::endl;
362 return 1;
363 }
364 inFilePath = strtok(NULL, ":");
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000365 }
366 } break;
367
368 case 'o': {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700369 outFilePath = optarg;
Yifan Hong9aa63702017-05-16 16:37:50 -0700370 if (!assembleVintf.openOutFile(optarg)) {
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000371 std::cerr << "Failed to open " << optarg << std::endl;
372 return 1;
373 }
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000374 } break;
375
Yifan Honga59d2562017-04-18 18:01:16 -0700376 case 'm': {
Yifan Hong9aa63702017-05-16 16:37:50 -0700377 assembleVintf.setOutputMatrix();
Yifan Honga59d2562017-04-18 18:01:16 -0700378 } break;
379
Yifan Hong4650ad82017-05-01 17:28:02 -0700380 case 'c': {
381 if (strlen(optarg) != 0) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700382 if (!assembleVintf.openCheckFile(optarg)) {
Yifan Hong4650ad82017-05-01 17:28:02 -0700383 std::cerr << "Failed to open " << optarg << std::endl;
384 return 1;
385 }
386 } else {
387 std::cerr << "WARNING: no compatibility check is done on "
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700388 << (outFilePath.empty() ? "output" : outFilePath) << std::endl;
Yifan Hong4650ad82017-05-01 17:28:02 -0700389 }
390 } break;
391
Yifan Hong79efa8a2017-07-06 14:10:28 -0700392 case 'k': {
393 if (!assembleVintf.addKernel(optarg)) {
394 std::cerr << "ERROR: Unrecognized --kernel argument." << std::endl;
395 return 1;
396 }
397 } break;
398
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000399 case 'h':
400 default: {
401 help();
402 return 1;
403 } break;
404 }
405 }
406
Yifan Hong9aa63702017-05-16 16:37:50 -0700407 bool success = assembleVintf.assemble();
Yifan Hong4650ad82017-05-01 17:28:02 -0700408
409 return success ? 0 : 1;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000410}
411