blob: 663b864b26d2e53b70282f2272f9c9ea18858df7 [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 Honga59d2562017-04-18 18:01:16 -070052 static bool assemble(std::basic_istream<char>& inFile,
53 std::basic_ostream<char>& outFile,
Yifan Hong959ee1b2017-04-28 14:37:56 -070054 bool outputMatrix) {
Yifan Hong4d18bcc2017-04-07 21:47:16 +000055 std::stringstream ss;
56 ss << inFile.rdbuf();
57 std::string fileContent = ss.str();
58
59 HalManifest halManifest;
Yifan Hong959ee1b2017-04-28 14:37:56 -070060 if (gHalManifestConverter(&halManifest, fileContent)) {
61 if (halManifest.mType == SchemaType::DEVICE) {
62 if (!getFlag("BOARD_SEPOLICY_VERS", &halManifest.device.mSepolicyVersion)) {
63 return false;
64 }
Yifan Hong4d18bcc2017-04-07 21:47:16 +000065 }
Yifan Hong4d18bcc2017-04-07 21:47:16 +000066
Yifan Hong959ee1b2017-04-28 14:37:56 -070067 if (outputMatrix) {
68 CompatibilityMatrix mat = halManifest.generateCompatibleMatrix();
69 std::string error;
70 if (!halManifest.checkCompatibility(mat, &error)) {
71 std::cerr << "FATAL ERROR: cannot generate a compatible matrix: "
72 << error << std::endl;
73 }
74 outFile << "<!-- \n"
75 " Autogenerated skeleton compatibility matrix. \n"
76 " Use with caution. Modify it to suit your needs.\n"
77 " All HALs are set to optional.\n"
78 " Many entries other than HALs are zero-filled and\n"
79 " require human attention. \n"
80 "-->\n"
81 << gCompatibilityMatrixConverter(mat);
82 } else {
83 outFile << gHalManifestConverter(halManifest);
Yifan Honga59d2562017-04-18 18:01:16 -070084 }
Yifan Hong959ee1b2017-04-28 14:37:56 -070085 outFile.flush();
86 return true;
Yifan Honga59d2562017-04-18 18:01:16 -070087 }
88
Yifan Hong959ee1b2017-04-28 14:37:56 -070089 CompatibilityMatrix matrix;
90 if (gCompatibilityMatrixConverter(&matrix, fileContent)) {
Yifan Hong1e5a0542017-04-28 14:37:56 -070091 KernelSepolicyVersion kernelSepolicyVers;
92 Version sepolicyVers;
93 if (matrix.mType == SchemaType::FRAMEWORK) {
94 if (!getFlag("BOARD_SEPOLICY_VERS", &sepolicyVers)) {
95 return false;
96 }
97 if (!getFlag("POLICYVERS", &kernelSepolicyVers)) {
98 return false;
99 }
100 matrix.framework.mSepolicy = Sepolicy(kernelSepolicyVers,
101 {{sepolicyVers.majorVer,sepolicyVers.minorVer}});
102 }
Yifan Hong959ee1b2017-04-28 14:37:56 -0700103 outFile << gCompatibilityMatrixConverter(matrix);
104 outFile.flush();
105 return true;
106 }
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000107
Yifan Hong959ee1b2017-04-28 14:37:56 -0700108 std::cerr << "Input file has unknown format." << std::endl
109 << "Error when attempting to convert to manifest: "
110 << gHalManifestConverter.lastError() << std::endl
111 << "Error when attempting to convert to compatibility matrix: "
112 << gCompatibilityMatrixConverter.lastError() << std::endl;
113 return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000114 }
115};
116
117} // namespace vintf
118} // namespace android
119
120void help() {
121 std::cerr <<
Yifan Hong959ee1b2017-04-28 14:37:56 -0700122 "assemble_vintf: Checks if a given manifest / matrix file is valid.\n"
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000123 "assemble_vintf -h\n"
124 " Display this help text.\n"
Yifan Honga59d2562017-04-18 18:01:16 -0700125 "assemble_vintf -i <input file> [-o <output file>] [-m]\n"
Yifan Hong959ee1b2017-04-28 14:37:56 -0700126 " Fill in build-time flags into the given file.\n"
127 " Input file format is automatically detected.\n"
Yifan Honga59d2562017-04-18 18:01:16 -0700128 " If no designated output file, write to stdout.\n"
129 " If -m is set, a compatible compatibility matrix is\n"
Yifan Hong959ee1b2017-04-28 14:37:56 -0700130 " generated instead; for example, given a device manifest,\n"
131 " a framework compatibility matrix is generated. This flag\n"
132 " is ignored when input is a compatibility matrix itself.\n";
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000133}
134
135int main(int argc, char **argv) {
136 std::ifstream inFile;
137 std::ofstream outFile;
138 std::ostream* outFileRef = &std::cout;
Yifan Hong959ee1b2017-04-28 14:37:56 -0700139 bool outputMatrix = false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000140 int res;
Yifan Honga59d2562017-04-18 18:01:16 -0700141 while((res = getopt(argc, argv, "hi:o:m")) >= 0) {
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000142 switch (res) {
143 case 'i': {
144 inFile.open(optarg);
145 if (!inFile.is_open()) {
146 std::cerr << "Failed to open " << optarg << std::endl;
147 return 1;
148 }
149 } break;
150
151 case 'o': {
152 outFile.open(optarg);
153 if (!outFile.is_open()) {
154 std::cerr << "Failed to open " << optarg << std::endl;
155 return 1;
156 }
157 outFileRef = &outFile;
158 } break;
159
Yifan Honga59d2562017-04-18 18:01:16 -0700160 case 'm': {
Yifan Hong959ee1b2017-04-28 14:37:56 -0700161 outputMatrix = true;
Yifan Honga59d2562017-04-18 18:01:16 -0700162 } break;
163
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000164 case 'h':
165 default: {
166 help();
167 return 1;
168 } break;
169 }
170 }
171
172 if (!inFile.is_open()) {
173 std::cerr << "Missing input file" << std::endl;
174 help();
175 return 1;
176 }
177
Yifan Hong959ee1b2017-04-28 14:37:56 -0700178 return ::android::vintf::AssembleVintf::assemble(inFile, *outFileRef, outputMatrix)
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000179 ? 0 : 1;
180}
181