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: " |
| 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 Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 105 | return true; |
Yifan Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 108 | CompatibilityMatrix matrix; |
| 109 | if (gCompatibilityMatrixConverter(&matrix, fileContent)) { |
Yifan Hong | 1e5a054 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 110 | 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 Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 122 | outFile << gCompatibilityMatrixConverter(matrix); |
| 123 | outFile.flush(); |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame^] | 124 | |
| 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 Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 138 | return true; |
| 139 | } |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 140 | |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 141 | 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 Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 147 | } |
| 148 | }; |
| 149 | |
| 150 | } // namespace vintf |
| 151 | } // namespace android |
| 152 | |
| 153 | void help() { |
| 154 | std::cerr << |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame^] | 155 | "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 Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | int main(int argc, char **argv) { |
| 180 | std::ifstream inFile; |
| 181 | std::ofstream outFile; |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame^] | 182 | std::ifstream checkFile; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 183 | std::ostream* outFileRef = &std::cout; |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 184 | bool outputMatrix = false; |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame^] | 185 | std::string inFilePath; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 186 | int res; |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame^] | 187 | while((res = getopt(argc, argv, "hi:o:mc:")) >= 0) { |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 188 | switch (res) { |
| 189 | case 'i': { |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame^] | 190 | inFilePath = optarg; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 191 | 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 Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 207 | case 'm': { |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 208 | outputMatrix = true; |
Yifan Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 209 | } break; |
| 210 | |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame^] | 211 | 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 Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 224 | 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 Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame^] | 238 | bool success = ::android::vintf::AssembleVintf::assemble( |
| 239 | inFile, *outFileRef, checkFile, outputMatrix); |
| 240 | |
| 241 | |
| 242 | return success ? 0 : 1; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 243 | } |
| 244 | |