blob: b2b0ed1487ebcceaa60ff9b828af6df32e87ceeb [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)) {
91 // TODO (b/37342627): get BOARD_VNDK_VERSION and put it here.
92 outFile << gCompatibilityMatrixConverter(matrix);
93 outFile.flush();
94 return true;
95 }
Yifan Hong4d18bcc2017-04-07 21:47:16 +000096
Yifan Hong959ee1b2017-04-28 14:37:56 -070097 std::cerr << "Input file has unknown format." << std::endl
98 << "Error when attempting to convert to manifest: "
99 << gHalManifestConverter.lastError() << std::endl
100 << "Error when attempting to convert to compatibility matrix: "
101 << gCompatibilityMatrixConverter.lastError() << std::endl;
102 return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000103 }
104};
105
106} // namespace vintf
107} // namespace android
108
109void help() {
110 std::cerr <<
Yifan Hong959ee1b2017-04-28 14:37:56 -0700111 "assemble_vintf: Checks if a given manifest / matrix file is valid.\n"
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000112 "assemble_vintf -h\n"
113 " Display this help text.\n"
Yifan Honga59d2562017-04-18 18:01:16 -0700114 "assemble_vintf -i <input file> [-o <output file>] [-m]\n"
Yifan Hong959ee1b2017-04-28 14:37:56 -0700115 " Fill in build-time flags into the given file.\n"
116 " Input file format is automatically detected.\n"
Yifan Honga59d2562017-04-18 18:01:16 -0700117 " If no designated output file, write to stdout.\n"
118 " If -m is set, a compatible compatibility matrix is\n"
Yifan Hong959ee1b2017-04-28 14:37:56 -0700119 " generated instead; for example, given a device manifest,\n"
120 " a framework compatibility matrix is generated. This flag\n"
121 " is ignored when input is a compatibility matrix itself.\n";
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000122}
123
124int main(int argc, char **argv) {
125 std::ifstream inFile;
126 std::ofstream outFile;
127 std::ostream* outFileRef = &std::cout;
Yifan Hong959ee1b2017-04-28 14:37:56 -0700128 bool outputMatrix = false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000129 int res;
Yifan Honga59d2562017-04-18 18:01:16 -0700130 while((res = getopt(argc, argv, "hi:o:m")) >= 0) {
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000131 switch (res) {
132 case 'i': {
133 inFile.open(optarg);
134 if (!inFile.is_open()) {
135 std::cerr << "Failed to open " << optarg << std::endl;
136 return 1;
137 }
138 } break;
139
140 case 'o': {
141 outFile.open(optarg);
142 if (!outFile.is_open()) {
143 std::cerr << "Failed to open " << optarg << std::endl;
144 return 1;
145 }
146 outFileRef = &outFile;
147 } break;
148
Yifan Honga59d2562017-04-18 18:01:16 -0700149 case 'm': {
Yifan Hong959ee1b2017-04-28 14:37:56 -0700150 outputMatrix = true;
Yifan Honga59d2562017-04-18 18:01:16 -0700151 } break;
152
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000153 case 'h':
154 default: {
155 help();
156 return 1;
157 } break;
158 }
159 }
160
161 if (!inFile.is_open()) {
162 std::cerr << "Missing input file" << std::endl;
163 help();
164 return 1;
165 }
166
Yifan Hong959ee1b2017-04-28 14:37:56 -0700167 return ::android::vintf::AssembleVintf::assemble(inFile, *outFileRef, outputMatrix)
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000168 ? 0 : 1;
169}
170