Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 29 | namespace android { |
| 30 | namespace vintf { |
| 31 | |
| 32 | /** |
| 33 | * Slurps the device manifest file and add build time flag to it. |
| 34 | */ |
| 35 | class AssembleVintf { |
| 36 | public: |
| 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 Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 52 | static std::string read(std::basic_istream<char>& is) { |
| 53 | std::stringstream ss; |
| 54 | ss << is.rdbuf(); |
| 55 | return ss.str(); |
| 56 | } |
| 57 | |
Yifan Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 58 | static bool assemble(std::basic_istream<char>& inFile, |
| 59 | std::basic_ostream<char>& outFile, |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 60 | std::ifstream& checkFile, |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 61 | bool outputMatrix) { |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 62 | std::string error; |
| 63 | std::string fileContent = read(inFile); |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 64 | |
| 65 | HalManifest halManifest; |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 66 | if (gHalManifestConverter(&halManifest, fileContent)) { |
| 67 | if (halManifest.mType == SchemaType::DEVICE) { |
| 68 | if (!getFlag("BOARD_SEPOLICY_VERS", &halManifest.device.mSepolicyVersion)) { |
| 69 | return false; |
| 70 | } |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 71 | } |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 72 | |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 73 | if (outputMatrix) { |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 74 | CompatibilityMatrix generatedMatrix = halManifest.generateCompatibleMatrix(); |
| 75 | if (!halManifest.checkCompatibility(generatedMatrix, &error)) { |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 76 | 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 Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 86 | << gCompatibilityMatrixConverter(generatedMatrix); |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 87 | } else { |
| 88 | outFile << gHalManifestConverter(halManifest); |
Yifan Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 89 | } |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 90 | outFile.flush(); |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 91 | |
| 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 Hong | e34ac2a | 2017-05-03 16:21:28 -0700 | [diff] [blame] | 96 | << gCompatibilityMatrixConverter.lastError() |
| 97 | << std::endl; |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 98 | return false; |
| 99 | } |
| 100 | if (!halManifest.checkCompatibility(checkMatrix, &error)) { |
Yifan Hong | e34ac2a | 2017-05-03 16:21:28 -0700 | [diff] [blame] | 101 | std::cerr << "Not compatible: " << error << std::endl; |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 102 | return false; |
| 103 | } |
| 104 | } |
| 105 | |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 106 | return true; |
Yifan Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 109 | CompatibilityMatrix matrix; |
| 110 | if (gCompatibilityMatrixConverter(&matrix, fileContent)) { |
Yifan Hong | 1e5a054 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 111 | 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 Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 123 | outFile << gCompatibilityMatrixConverter(matrix); |
| 124 | outFile.flush(); |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 125 | |
| 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 Hong | e34ac2a | 2017-05-03 16:21:28 -0700 | [diff] [blame] | 130 | << gHalManifestConverter.lastError() |
| 131 | << std::endl; |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 132 | return false; |
| 133 | } |
| 134 | if (!checkManifest.checkCompatibility(matrix, &error)) { |
Yifan Hong | e34ac2a | 2017-05-03 16:21:28 -0700 | [diff] [blame] | 135 | std::cerr << "Not compatible: " << error << std::endl; |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 136 | return false; |
| 137 | } |
| 138 | } |
| 139 | |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 140 | return true; |
| 141 | } |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 142 | |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 143 | 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 Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 149 | } |
| 150 | }; |
| 151 | |
| 152 | } // namespace vintf |
| 153 | } // namespace android |
| 154 | |
| 155 | void help() { |
| 156 | std::cerr << |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 157 | "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 Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | int main(int argc, char **argv) { |
| 182 | std::ifstream inFile; |
| 183 | std::ofstream outFile; |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 184 | std::ifstream checkFile; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 185 | std::ostream* outFileRef = &std::cout; |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 186 | bool outputMatrix = false; |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 187 | std::string inFilePath; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 188 | int res; |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 189 | while((res = getopt(argc, argv, "hi:o:mc:")) >= 0) { |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 190 | switch (res) { |
| 191 | case 'i': { |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 192 | inFilePath = optarg; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 193 | 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 Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 209 | case 'm': { |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 210 | outputMatrix = true; |
Yifan Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 211 | } break; |
| 212 | |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 213 | 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 Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 226 | 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 Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 240 | bool success = ::android::vintf::AssembleVintf::assemble( |
| 241 | inFile, *outFileRef, checkFile, outputMatrix); |
| 242 | |
| 243 | |
| 244 | return success ? 0 : 1; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 245 | } |
| 246 | |