blob: 9a23eeb06fb19b94a9f96ebd305effa54c0dc977 [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: "
96 << gCompatibilityMatrixConverter.lastError();
97 return false;
98 }
99 if (!halManifest.checkCompatibility(checkMatrix, &error)) {
100 std::cerr << "Not compatible: " << error;
101 return false;
102 }
103 }
104
Yifan Hong959ee1b2017-04-28 14:37:56 -0700105 return true;
Yifan Honga59d2562017-04-18 18:01:16 -0700106 }
107
Yifan Hong959ee1b2017-04-28 14:37:56 -0700108 CompatibilityMatrix matrix;
109 if (gCompatibilityMatrixConverter(&matrix, fileContent)) {
Yifan Hong1e5a0542017-04-28 14:37:56 -0700110 KernelSepolicyVersion kernelSepolicyVers;
111 Version sepolicyVers;
112 if (matrix.mType == SchemaType::FRAMEWORK) {
113 if (!getFlag("BOARD_SEPOLICY_VERS", &sepolicyVers)) {
114 return false;
115 }
116 if (!getFlag("POLICYVERS", &kernelSepolicyVers)) {
117 return false;
118 }
119 matrix.framework.mSepolicy = Sepolicy(kernelSepolicyVers,
120 {{sepolicyVers.majorVer,sepolicyVers.minorVer}});
121 }
Yifan Hong959ee1b2017-04-28 14:37:56 -0700122 outFile << gCompatibilityMatrixConverter(matrix);
123 outFile.flush();
Yifan Hong4650ad82017-05-01 17:28:02 -0700124
125 if (checkFile.is_open()) {
126 HalManifest checkManifest;
127 if (!gHalManifestConverter(&checkManifest, read(checkFile))) {
128 std::cerr << "Cannot parse check file as a HAL manifest: "
129 << gHalManifestConverter.lastError();
130 return false;
131 }
132 if (!checkManifest.checkCompatibility(matrix, &error)) {
133 std::cerr << "Not compatible: " << error;
134 return false;
135 }
136 }
137
Yifan Hong959ee1b2017-04-28 14:37:56 -0700138 return true;
139 }
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000140
Yifan Hong959ee1b2017-04-28 14:37:56 -0700141 std::cerr << "Input file has unknown format." << std::endl
142 << "Error when attempting to convert to manifest: "
143 << gHalManifestConverter.lastError() << std::endl
144 << "Error when attempting to convert to compatibility matrix: "
145 << gCompatibilityMatrixConverter.lastError() << std::endl;
146 return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000147 }
148};
149
150} // namespace vintf
151} // namespace android
152
153void help() {
154 std::cerr <<
Yifan Hong4650ad82017-05-01 17:28:02 -0700155"assemble_vintf: Checks if a given manifest / matrix file is valid and \n"
156" fill in build-time flags into the given file.\n"
157"assemble_vintf -h\n"
158" Display this help text.\n"
159"assemble_vintf -i <input file> [-o <output file>] [-m] [-c [<check file>]]\n"
160" Fill in build-time flags into the given file.\n"
161" -i <input file>\n"
162" Input file. Format is automatically detected.\n"
163" -o <input file>\n"
164" Optional output file. If not specified, write to stdout.\n"
165" -m\n"
166" a compatible compatibility matrix is\n"
167" generated instead; for example, given a device manifest,\n"
168" a framework compatibility matrix is generated. This flag\n"
169" is ignored when input is a compatibility matrix.\n"
170" -c [<check file>]\n"
171" After writing the output file, check compatibility between\n"
172" output file and check file.\n"
173" If -c is set but the check file is not specified, a warning\n"
174" message is written to stderr. Return 0.\n"
175" If the check file is specified but is not compatible, an error\n"
176" message is written to stderr. Return 1.\n";
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000177}
178
179int main(int argc, char **argv) {
180 std::ifstream inFile;
181 std::ofstream outFile;
Yifan Hong4650ad82017-05-01 17:28:02 -0700182 std::ifstream checkFile;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000183 std::ostream* outFileRef = &std::cout;
Yifan Hong959ee1b2017-04-28 14:37:56 -0700184 bool outputMatrix = false;
Yifan Hong4650ad82017-05-01 17:28:02 -0700185 std::string inFilePath;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000186 int res;
Yifan Hong4650ad82017-05-01 17:28:02 -0700187 while((res = getopt(argc, argv, "hi:o:mc:")) >= 0) {
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000188 switch (res) {
189 case 'i': {
Yifan Hong4650ad82017-05-01 17:28:02 -0700190 inFilePath = optarg;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000191 inFile.open(optarg);
192 if (!inFile.is_open()) {
193 std::cerr << "Failed to open " << optarg << std::endl;
194 return 1;
195 }
196 } break;
197
198 case 'o': {
199 outFile.open(optarg);
200 if (!outFile.is_open()) {
201 std::cerr << "Failed to open " << optarg << std::endl;
202 return 1;
203 }
204 outFileRef = &outFile;
205 } break;
206
Yifan Honga59d2562017-04-18 18:01:16 -0700207 case 'm': {
Yifan Hong959ee1b2017-04-28 14:37:56 -0700208 outputMatrix = true;
Yifan Honga59d2562017-04-18 18:01:16 -0700209 } break;
210
Yifan Hong4650ad82017-05-01 17:28:02 -0700211 case 'c': {
212 if (strlen(optarg) != 0) {
213 checkFile.open(optarg);
214 if (!checkFile.is_open()) {
215 std::cerr << "Failed to open " << optarg << std::endl;
216 return 1;
217 }
218 } else {
219 std::cerr << "WARNING: no compatibility check is done on "
220 << inFilePath << std::endl;
221 }
222 } break;
223
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000224 case 'h':
225 default: {
226 help();
227 return 1;
228 } break;
229 }
230 }
231
232 if (!inFile.is_open()) {
233 std::cerr << "Missing input file" << std::endl;
234 help();
235 return 1;
236 }
237
Yifan Hong4650ad82017-05-01 17:28:02 -0700238 bool success = ::android::vintf::AssembleVintf::assemble(
239 inFile, *outFileRef, checkFile, outputMatrix);
240
241
242 return success ? 0 : 1;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000243}
244