blob: d75e889b6cf9d473bb9b8a1f233f6478f362db79 [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
Steve Muckle0bef8682017-07-31 15:47:15 -070095 static bool parseFilesForKernelConfigs(const std::string& path, std::vector<KernelConfig>* out) {
96 bool ret = true;
97 char *pathIter;
98 char *modPath = new char[path.length() + 1];
99 strcpy(modPath, path.c_str());
100 pathIter = strtok(modPath, ":");
101 while (ret && pathIter != NULL) {
102 ret &= parseFileForKernelConfigs(pathIter, out);
103 pathIter = strtok(NULL, ":");
104 }
Luis A. Lozano82266ae2017-08-22 16:30:11 -0700105 delete[] modPath;
Steve Muckle0bef8682017-07-31 15:47:15 -0700106 return ret;
107 }
108
Yifan Hong9aa63702017-05-16 16:37:50 -0700109 std::basic_ostream<char>& out() const {
110 return mOutFileRef == nullptr ? std::cout : *mOutFileRef;
111 }
112
113 bool assembleHalManifest(HalManifest* halManifest) {
Yifan Hong4650ad82017-05-01 17:28:02 -0700114 std::string error;
Yifan Hong9aa63702017-05-16 16:37:50 -0700115
116 if (halManifest->mType == SchemaType::DEVICE) {
117 if (!getFlag("BOARD_SEPOLICY_VERS", &halManifest->device.mSepolicyVersion)) {
118 return false;
119 }
120 }
121
122 if (mOutputMatrix) {
123 CompatibilityMatrix generatedMatrix = halManifest->generateCompatibleMatrix();
124 if (!halManifest->checkCompatibility(generatedMatrix, &error)) {
125 std::cerr << "FATAL ERROR: cannot generate a compatible matrix: " << error
126 << std::endl;
127 }
128 out() << "<!-- \n"
129 " Autogenerated skeleton compatibility matrix. \n"
130 " Use with caution. Modify it to suit your needs.\n"
131 " All HALs are set to optional.\n"
132 " Many entries other than HALs are zero-filled and\n"
133 " require human attention. \n"
134 "-->\n"
135 << gCompatibilityMatrixConverter(generatedMatrix);
136 } else {
137 out() << gHalManifestConverter(*halManifest);
138 }
139 out().flush();
140
141 if (mCheckFile.is_open()) {
142 CompatibilityMatrix checkMatrix;
143 if (!gCompatibilityMatrixConverter(&checkMatrix, read(mCheckFile))) {
144 std::cerr << "Cannot parse check file as a compatibility matrix: "
145 << gCompatibilityMatrixConverter.lastError() << std::endl;
146 return false;
147 }
148 if (!halManifest->checkCompatibility(checkMatrix, &error)) {
149 std::cerr << "Not compatible: " << error << std::endl;
150 return false;
151 }
152 }
153
154 return true;
155 }
156
157 bool assembleCompatibilityMatrix(CompatibilityMatrix* matrix) {
158 std::string error;
159
160 KernelSepolicyVersion kernelSepolicyVers;
161 Version sepolicyVers;
162 if (matrix->mType == SchemaType::FRAMEWORK) {
163 if (!getFlag("BOARD_SEPOLICY_VERS", &sepolicyVers)) {
164 return false;
165 }
166 if (!getFlag("POLICYVERS", &kernelSepolicyVers)) {
167 return false;
168 }
Yifan Hong79efa8a2017-07-06 14:10:28 -0700169 for (const auto& pair : mKernels) {
170 std::vector<KernelConfig> configs;
Steve Muckle0bef8682017-07-31 15:47:15 -0700171 if (!parseFilesForKernelConfigs(pair.second, &configs)) {
Yifan Hong79efa8a2017-07-06 14:10:28 -0700172 return false;
173 }
174 bool added = false;
175 for (auto& e : matrix->framework.mKernels) {
176 if (e.minLts() == pair.first) {
177 e.mConfigs.insert(e.mConfigs.end(), configs.begin(), configs.end());
178 added = true;
179 break;
180 }
181 }
182 if (!added) {
183 matrix->framework.mKernels.push_back(
184 MatrixKernel{KernelVersion{pair.first}, std::move(configs)});
185 }
186 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700187 matrix->framework.mSepolicy =
188 Sepolicy(kernelSepolicyVers, {{sepolicyVers.majorVer, sepolicyVers.minorVer}});
Yifan Hong7f6c00c2017-07-06 19:50:29 +0000189
190 Version avbMetaVersion;
191 if (!getFlag("FRAMEWORK_VBMETA_VERSION", &avbMetaVersion)) {
192 return false;
193 }
194 matrix->framework.mAvbMetaVersion = avbMetaVersion;
Yifan Hong9aa63702017-05-16 16:37:50 -0700195 }
196 out() << gCompatibilityMatrixConverter(*matrix);
197 out().flush();
198
199 if (mCheckFile.is_open()) {
200 HalManifest checkManifest;
201 if (!gHalManifestConverter(&checkManifest, read(mCheckFile))) {
202 std::cerr << "Cannot parse check file as a HAL manifest: "
203 << gHalManifestConverter.lastError() << std::endl;
204 return false;
205 }
206 if (!checkManifest.checkCompatibility(*matrix, &error)) {
207 std::cerr << "Not compatible: " << error << std::endl;
208 return false;
209 }
210 }
211
212 return true;
213 }
214
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700215 enum AssembleStatus { SUCCESS, FAIL_AND_EXIT, TRY_NEXT };
216 template <typename Schema, typename AssembleFunc>
217 AssembleStatus tryAssemble(const XmlConverter<Schema>& converter, const std::string& schemaName,
218 AssembleFunc assemble) {
219 Schema schema;
220 if (!converter(&schema, read(mInFiles.front()))) {
221 return TRY_NEXT;
222 }
223 auto firstType = schema.type();
224 for (auto it = mInFiles.begin() + 1; it != mInFiles.end(); ++it) {
225 Schema additionalSchema;
226 if (!converter(&additionalSchema, read(*it))) {
227 std::cerr << "File \"" << mInFilePaths[std::distance(mInFiles.begin(), it)]
228 << "\" is not a valid " << firstType << " " << schemaName
229 << " (but the first file is a valid " << firstType << " " << schemaName
230 << "). Error: " << converter.lastError() << std::endl;
231 return FAIL_AND_EXIT;
232 }
233 if (additionalSchema.type() != firstType) {
234 std::cerr << "File \"" << mInFilePaths[std::distance(mInFiles.begin(), it)]
235 << "\" is a " << additionalSchema.type() << " " << schemaName
236 << " (but a " << firstType << " " << schemaName << " is expected)."
237 << std::endl;
238 return FAIL_AND_EXIT;
239 }
240 schema.addAll(std::move(additionalSchema));
241 }
242 return assemble(&schema) ? SUCCESS : FAIL_AND_EXIT;
243 }
244
Yifan Hong9aa63702017-05-16 16:37:50 -0700245 bool assemble() {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700246 using std::placeholders::_1;
247 if (mInFiles.empty()) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700248 std::cerr << "Missing input file." << std::endl;
249 return false;
250 }
251
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700252 auto status = tryAssemble(gHalManifestConverter, "manifest",
253 std::bind(&AssembleVintf::assembleHalManifest, this, _1));
254 if (status == SUCCESS) return true;
255 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000256
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700257 resetInFiles();
Yifan Honga59d2562017-04-18 18:01:16 -0700258
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700259 status = tryAssemble(gCompatibilityMatrixConverter, "compatibility matrix",
260 std::bind(&AssembleVintf::assembleCompatibilityMatrix, this, _1));
261 if (status == SUCCESS) return true;
262 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000263
Yifan Hong959ee1b2017-04-28 14:37:56 -0700264 std::cerr << "Input file has unknown format." << std::endl
265 << "Error when attempting to convert to manifest: "
266 << gHalManifestConverter.lastError() << std::endl
267 << "Error when attempting to convert to compatibility matrix: "
268 << gCompatibilityMatrixConverter.lastError() << std::endl;
269 return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000270 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700271
272 bool openOutFile(const char* path) {
273 mOutFileRef = std::make_unique<std::ofstream>();
274 mOutFileRef->open(path);
275 return mOutFileRef->is_open();
276 }
277
278 bool openInFile(const char* path) {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700279 mInFilePaths.push_back(path);
280 mInFiles.push_back({});
281 mInFiles.back().open(path);
282 return mInFiles.back().is_open();
Yifan Hong9aa63702017-05-16 16:37:50 -0700283 }
284
285 bool openCheckFile(const char* path) {
286 mCheckFile.open(path);
287 return mCheckFile.is_open();
288 }
289
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700290 void resetInFiles() {
291 for (auto& inFile : mInFiles) {
292 inFile.clear();
293 inFile.seekg(0);
294 }
295 }
296
Yifan Hong9aa63702017-05-16 16:37:50 -0700297 void setOutputMatrix() { mOutputMatrix = true; }
298
Yifan Hong79efa8a2017-07-06 14:10:28 -0700299 bool addKernel(const std::string& kernelArg) {
300 auto ind = kernelArg.find(':');
301 if (ind == std::string::npos) {
302 std::cerr << "Unrecognized --kernel option '" << kernelArg << "'" << std::endl;
303 return false;
304 }
305 std::string kernelVerStr{kernelArg.begin(), kernelArg.begin() + ind};
306 std::string kernelConfigPath{kernelArg.begin() + ind + 1, kernelArg.end()};
307 Version kernelVer;
308 if (!parse(kernelVerStr, &kernelVer)) {
309 std::cerr << "Unrecognized kernel version '" << kernelVerStr << "'" << std::endl;
310 return false;
311 }
312 mKernels.push_back({{kernelVer.majorVer, kernelVer.minorVer, 0u}, kernelConfigPath});
313 return true;
314 }
315
Yifan Hong9aa63702017-05-16 16:37:50 -0700316 private:
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700317 std::vector<std::string> mInFilePaths;
318 std::vector<std::ifstream> mInFiles;
Yifan Hong9aa63702017-05-16 16:37:50 -0700319 std::unique_ptr<std::ofstream> mOutFileRef;
320 std::ifstream mCheckFile;
321 bool mOutputMatrix = false;
Yifan Hong79efa8a2017-07-06 14:10:28 -0700322 std::vector<std::pair<KernelVersion, std::string>> mKernels;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000323};
324
325} // namespace vintf
326} // namespace android
327
328void help() {
Yifan Hong9aa63702017-05-16 16:37:50 -0700329 std::cerr << "assemble_vintf: Checks if a given manifest / matrix file is valid and \n"
330 " fill in build-time flags into the given file.\n"
331 "assemble_vintf -h\n"
332 " Display this help text.\n"
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700333 "assemble_vintf -i <input file>[:<input file>[...]] [-o <output file>] [-m]\n"
334 " [-c [<check file>]]\n"
Yifan Hong9aa63702017-05-16 16:37:50 -0700335 " Fill in build-time flags into the given file.\n"
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700336 " -i <input file>[:<input file>[...]]\n"
337 " A list of input files. Format is automatically detected for the\n"
338 " first file, and the remaining files must have the same format.\n"
339 " Files other than the first file should only have <hal> defined;\n"
340 " other entries are ignored.\n"
Yifan Hong9aa63702017-05-16 16:37:50 -0700341 " -o <output file>\n"
342 " Optional output file. If not specified, write to stdout.\n"
343 " -m\n"
344 " a compatible compatibility matrix is\n"
345 " generated instead; for example, given a device manifest,\n"
346 " a framework compatibility matrix is generated. This flag\n"
347 " is ignored when input is a compatibility matrix.\n"
348 " -c [<check file>]\n"
349 " After writing the output file, check compatibility between\n"
350 " output file and check file.\n"
351 " If -c is set but the check file is not specified, a warning\n"
352 " message is written to stderr. Return 0.\n"
353 " If the check file is specified but is not compatible, an error\n"
Yifan Hong79efa8a2017-07-06 14:10:28 -0700354 " message is written to stderr. Return 1.\n"
Steve Muckle0bef8682017-07-31 15:47:15 -0700355 " --kernel=<version>:<android-base.cfg>[:<android-base-arch.cfg>[...]]\n"
Yifan Hong79efa8a2017-07-06 14:10:28 -0700356 " Add a kernel entry to framework compatibility matrix.\n"
357 " Ignored for other input format.\n"
358 " <version> has format: 3.18\n"
Steve Muckle0bef8682017-07-31 15:47:15 -0700359 " <android-base.cfg> is the location of android-base.cfg\n"
360 " <android-base-arch.cfg> is the location of an optional\n"
361 " arch-specific config fragment, more than one may be specified\n";
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000362}
363
364int main(int argc, char **argv) {
Yifan Hong79efa8a2017-07-06 14:10:28 -0700365 const struct option longopts[] = {{"kernel", required_argument, NULL, 'k'}, {0, 0, 0, 0}};
Yifan Hong9aa63702017-05-16 16:37:50 -0700366
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700367 std::string outFilePath;
Yifan Hong9aa63702017-05-16 16:37:50 -0700368 ::android::vintf::AssembleVintf assembleVintf;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000369 int res;
Yifan Hong9aa63702017-05-16 16:37:50 -0700370 int optind;
371 while ((res = getopt_long(argc, argv, "hi:o:mc:", longopts, &optind)) >= 0) {
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000372 switch (res) {
373 case 'i': {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700374 char* inFilePath = strtok(optarg, ":");
375 while (inFilePath != NULL) {
376 if (!assembleVintf.openInFile(inFilePath)) {
377 std::cerr << "Failed to open " << optarg << std::endl;
378 return 1;
379 }
380 inFilePath = strtok(NULL, ":");
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000381 }
382 } break;
383
384 case 'o': {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700385 outFilePath = optarg;
Yifan Hong9aa63702017-05-16 16:37:50 -0700386 if (!assembleVintf.openOutFile(optarg)) {
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000387 std::cerr << "Failed to open " << optarg << std::endl;
388 return 1;
389 }
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000390 } break;
391
Yifan Honga59d2562017-04-18 18:01:16 -0700392 case 'm': {
Yifan Hong9aa63702017-05-16 16:37:50 -0700393 assembleVintf.setOutputMatrix();
Yifan Honga59d2562017-04-18 18:01:16 -0700394 } break;
395
Yifan Hong4650ad82017-05-01 17:28:02 -0700396 case 'c': {
397 if (strlen(optarg) != 0) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700398 if (!assembleVintf.openCheckFile(optarg)) {
Yifan Hong4650ad82017-05-01 17:28:02 -0700399 std::cerr << "Failed to open " << optarg << std::endl;
400 return 1;
401 }
402 } else {
403 std::cerr << "WARNING: no compatibility check is done on "
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700404 << (outFilePath.empty() ? "output" : outFilePath) << std::endl;
Yifan Hong4650ad82017-05-01 17:28:02 -0700405 }
406 } break;
407
Yifan Hong79efa8a2017-07-06 14:10:28 -0700408 case 'k': {
409 if (!assembleVintf.addKernel(optarg)) {
410 std::cerr << "ERROR: Unrecognized --kernel argument." << std::endl;
411 return 1;
412 }
413 } break;
414
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000415 case 'h':
416 default: {
417 help();
418 return 1;
419 } break;
420 }
421 }
422
Yifan Hong9aa63702017-05-16 16:37:50 -0700423 bool success = assembleVintf.assemble();
Yifan Hong4650ad82017-05-01 17:28:02 -0700424
425 return success ? 0 : 1;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000426}