blob: f6df2d34df37e67ace676fb55aa85fa626e92812 [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>
22#include <unordered_map>
23#include <sstream>
24#include <string>
25
26#include <vintf/parse_string.h>
27#include <vintf/parse_xml.h>
28
29namespace android {
30namespace vintf {
31
32/**
33 * Slurps the device manifest file and add build time flag to it.
34 */
35class AssembleVintf {
36public:
37 template<typename T>
38 static bool getFlag(const std::string& key, T* value) {
39 const char *envValue = getenv(key.c_str());
40 if (envValue == NULL) {
41 std::cerr << "Required " << key << " flag." << std::endl;
42 return false;
43 }
44
45 if (!parse(envValue, value)) {
46 std::cerr << "Cannot parse " << envValue << "." << std::endl;
47 return false;
48 }
49 return true;
50 }
51
Yifan Hong4650ad82017-05-01 17:28:02 -070052 static std::string read(std::basic_istream<char>& is) {
53 std::stringstream ss;
54 ss << is.rdbuf();
55 return ss.str();
56 }
57
Yifan Honga59d2562017-04-18 18:01:16 -070058 static bool assemble(std::basic_istream<char>& inFile,
59 std::basic_ostream<char>& outFile,
Yifan Hong4650ad82017-05-01 17:28:02 -070060 std::ifstream& checkFile,
Yifan Hong959ee1b2017-04-28 14:37:56 -070061 bool outputMatrix) {
Yifan Hong4650ad82017-05-01 17:28:02 -070062 std::string error;
63 std::string fileContent = read(inFile);
Yifan Hong4d18bcc2017-04-07 21:47:16 +000064
65 HalManifest halManifest;
Yifan Hong959ee1b2017-04-28 14:37:56 -070066 if (gHalManifestConverter(&halManifest, fileContent)) {
67 if (halManifest.mType == SchemaType::DEVICE) {
68 if (!getFlag("BOARD_SEPOLICY_VERS", &halManifest.device.mSepolicyVersion)) {
69 return false;
70 }
Yifan Hong4d18bcc2017-04-07 21:47:16 +000071 }
Yifan Hong4d18bcc2017-04-07 21:47:16 +000072
Yifan Hong959ee1b2017-04-28 14:37:56 -070073 if (outputMatrix) {
Yifan Hong4650ad82017-05-01 17:28:02 -070074 CompatibilityMatrix generatedMatrix = halManifest.generateCompatibleMatrix();
75 if (!halManifest.checkCompatibility(generatedMatrix, &error)) {
Yifan Hong959ee1b2017-04-28 14:37:56 -070076 std::cerr << "FATAL ERROR: cannot generate a compatible matrix: "
77 << error << std::endl;
78 }
79 outFile << "<!-- \n"
80 " Autogenerated skeleton compatibility matrix. \n"
81 " Use with caution. Modify it to suit your needs.\n"
82 " All HALs are set to optional.\n"
83 " Many entries other than HALs are zero-filled and\n"
84 " require human attention. \n"
85 "-->\n"
Yifan Hong4650ad82017-05-01 17:28:02 -070086 << gCompatibilityMatrixConverter(generatedMatrix);
Yifan Hong959ee1b2017-04-28 14:37:56 -070087 } else {
88 outFile << gHalManifestConverter(halManifest);
Yifan Honga59d2562017-04-18 18:01:16 -070089 }
Yifan Hong959ee1b2017-04-28 14:37:56 -070090 outFile.flush();
Yifan Hong4650ad82017-05-01 17:28:02 -070091
92 if (checkFile.is_open()) {
93 CompatibilityMatrix checkMatrix;
94 if (!gCompatibilityMatrixConverter(&checkMatrix, read(checkFile))) {
95 std::cerr << "Cannot parse check file as a compatibility matrix: "
Yifan Honge34ac2a2017-05-03 16:21:28 -070096 << gCompatibilityMatrixConverter.lastError()
97 << std::endl;
Yifan Hong4650ad82017-05-01 17:28:02 -070098 return false;
99 }
100 if (!halManifest.checkCompatibility(checkMatrix, &error)) {
Yifan Honge34ac2a2017-05-03 16:21:28 -0700101 std::cerr << "Not compatible: " << error << std::endl;
Yifan Hong4650ad82017-05-01 17:28:02 -0700102 return false;
103 }
104 }
105
Yifan Hong959ee1b2017-04-28 14:37:56 -0700106 return true;
Yifan Honga59d2562017-04-18 18:01:16 -0700107 }
108
Yifan Hong959ee1b2017-04-28 14:37:56 -0700109 CompatibilityMatrix matrix;
110 if (gCompatibilityMatrixConverter(&matrix, fileContent)) {
Yifan Hong1e5a0542017-04-28 14:37:56 -0700111 KernelSepolicyVersion kernelSepolicyVers;
112 Version sepolicyVers;
113 if (matrix.mType == SchemaType::FRAMEWORK) {
114 if (!getFlag("BOARD_SEPOLICY_VERS", &sepolicyVers)) {
115 return false;
116 }
117 if (!getFlag("POLICYVERS", &kernelSepolicyVers)) {
118 return false;
119 }
120 matrix.framework.mSepolicy = Sepolicy(kernelSepolicyVers,
121 {{sepolicyVers.majorVer,sepolicyVers.minorVer}});
122 }
Yifan Hong959ee1b2017-04-28 14:37:56 -0700123 outFile << gCompatibilityMatrixConverter(matrix);
124 outFile.flush();
Yifan Hong4650ad82017-05-01 17:28:02 -0700125
126 if (checkFile.is_open()) {
127 HalManifest checkManifest;
128 if (!gHalManifestConverter(&checkManifest, read(checkFile))) {
129 std::cerr << "Cannot parse check file as a HAL manifest: "
Yifan Honge34ac2a2017-05-03 16:21:28 -0700130 << gHalManifestConverter.lastError()
131 << std::endl;
Yifan Hong4650ad82017-05-01 17:28:02 -0700132 return false;
133 }
134 if (!checkManifest.checkCompatibility(matrix, &error)) {
Yifan Honge34ac2a2017-05-03 16:21:28 -0700135 std::cerr << "Not compatible: " << error << std::endl;
Yifan Hong4650ad82017-05-01 17:28:02 -0700136 return false;
137 }
138 }
139
Yifan Hong959ee1b2017-04-28 14:37:56 -0700140 return true;
141 }
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000142
Yifan Hong959ee1b2017-04-28 14:37:56 -0700143 std::cerr << "Input file has unknown format." << std::endl
144 << "Error when attempting to convert to manifest: "
145 << gHalManifestConverter.lastError() << std::endl
146 << "Error when attempting to convert to compatibility matrix: "
147 << gCompatibilityMatrixConverter.lastError() << std::endl;
148 return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000149 }
150};
151
152} // namespace vintf
153} // namespace android
154
155void help() {
156 std::cerr <<
Yifan Hong4650ad82017-05-01 17:28:02 -0700157"assemble_vintf: Checks if a given manifest / matrix file is valid and \n"
158" fill in build-time flags into the given file.\n"
159"assemble_vintf -h\n"
160" Display this help text.\n"
161"assemble_vintf -i <input file> [-o <output file>] [-m] [-c [<check file>]]\n"
162" Fill in build-time flags into the given file.\n"
163" -i <input file>\n"
164" Input file. Format is automatically detected.\n"
165" -o <input file>\n"
166" Optional output file. If not specified, write to stdout.\n"
167" -m\n"
168" a compatible compatibility matrix is\n"
169" generated instead; for example, given a device manifest,\n"
170" a framework compatibility matrix is generated. This flag\n"
171" is ignored when input is a compatibility matrix.\n"
172" -c [<check file>]\n"
173" After writing the output file, check compatibility between\n"
174" output file and check file.\n"
175" If -c is set but the check file is not specified, a warning\n"
176" message is written to stderr. Return 0.\n"
177" If the check file is specified but is not compatible, an error\n"
178" message is written to stderr. Return 1.\n";
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000179}
180
181int main(int argc, char **argv) {
182 std::ifstream inFile;
183 std::ofstream outFile;
Yifan Hong4650ad82017-05-01 17:28:02 -0700184 std::ifstream checkFile;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000185 std::ostream* outFileRef = &std::cout;
Yifan Hong959ee1b2017-04-28 14:37:56 -0700186 bool outputMatrix = false;
Yifan Hong4650ad82017-05-01 17:28:02 -0700187 std::string inFilePath;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000188 int res;
Yifan Hong4650ad82017-05-01 17:28:02 -0700189 while((res = getopt(argc, argv, "hi:o:mc:")) >= 0) {
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000190 switch (res) {
191 case 'i': {
Yifan Hong4650ad82017-05-01 17:28:02 -0700192 inFilePath = optarg;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000193 inFile.open(optarg);
194 if (!inFile.is_open()) {
195 std::cerr << "Failed to open " << optarg << std::endl;
196 return 1;
197 }
198 } break;
199
200 case 'o': {
201 outFile.open(optarg);
202 if (!outFile.is_open()) {
203 std::cerr << "Failed to open " << optarg << std::endl;
204 return 1;
205 }
206 outFileRef = &outFile;
207 } break;
208
Yifan Honga59d2562017-04-18 18:01:16 -0700209 case 'm': {
Yifan Hong959ee1b2017-04-28 14:37:56 -0700210 outputMatrix = true;
Yifan Honga59d2562017-04-18 18:01:16 -0700211 } break;
212
Yifan Hong4650ad82017-05-01 17:28:02 -0700213 case 'c': {
214 if (strlen(optarg) != 0) {
215 checkFile.open(optarg);
216 if (!checkFile.is_open()) {
217 std::cerr << "Failed to open " << optarg << std::endl;
218 return 1;
219 }
220 } else {
221 std::cerr << "WARNING: no compatibility check is done on "
222 << inFilePath << std::endl;
223 }
224 } break;
225
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000226 case 'h':
227 default: {
228 help();
229 return 1;
230 } break;
231 }
232 }
233
234 if (!inFile.is_open()) {
235 std::cerr << "Missing input file" << std::endl;
236 help();
237 return 1;
238 }
239
Yifan Hong4650ad82017-05-01 17:28:02 -0700240 bool success = ::android::vintf::AssembleVintf::assemble(
241 inFile, *outFileRef, checkFile, outputMatrix);
242
243
244 return success ? 0 : 1;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000245}
246