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 | |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 17 | #include <getopt.h> |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 18 | #include <stdlib.h> |
| 19 | #include <unistd.h> |
| 20 | |
| 21 | #include <fstream> |
| 22 | #include <iostream> |
| 23 | #include <unordered_map> |
| 24 | #include <sstream> |
| 25 | #include <string> |
| 26 | |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 27 | #include <android-base/file.h> |
| 28 | |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 29 | #include <vintf/KernelConfigParser.h> |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 30 | #include <vintf/parse_string.h> |
| 31 | #include <vintf/parse_xml.h> |
| 32 | |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 33 | #define BUFFER_SIZE sysconf(_SC_PAGESIZE) |
| 34 | |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 35 | namespace android { |
| 36 | namespace vintf { |
| 37 | |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 38 | static const std::string gConfigPrefix = "android-base-"; |
| 39 | static const std::string gConfigSuffix = ".cfg"; |
| 40 | static const std::string gBaseConfig = "android-base.cfg"; |
| 41 | |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 42 | /** |
| 43 | * Slurps the device manifest file and add build time flag to it. |
| 44 | */ |
| 45 | class AssembleVintf { |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 46 | using Condition = std::unique_ptr<KernelConfig>; |
| 47 | using ConditionedConfig = std::pair<Condition, std::vector<KernelConfig> /* configs */>; |
| 48 | |
| 49 | public: |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 50 | template<typename T> |
| 51 | static bool getFlag(const std::string& key, T* value) { |
| 52 | const char *envValue = getenv(key.c_str()); |
| 53 | if (envValue == NULL) { |
Yifan Hong | 488e16a | 2017-07-11 13:50:41 -0700 | [diff] [blame] | 54 | std::cerr << "Warning: " << key << " is missing, defaulted to " << (*value) |
| 55 | << std::endl; |
| 56 | return true; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | if (!parse(envValue, value)) { |
| 60 | std::cerr << "Cannot parse " << envValue << "." << std::endl; |
| 61 | return false; |
| 62 | } |
| 63 | return true; |
| 64 | } |
| 65 | |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 66 | static std::string read(std::basic_istream<char>& is) { |
| 67 | std::stringstream ss; |
| 68 | ss << is.rdbuf(); |
| 69 | return ss.str(); |
| 70 | } |
| 71 | |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 72 | static bool isCommonConfig(const std::string& path) { |
| 73 | return ::android::base::Basename(path) == gBaseConfig; |
| 74 | } |
| 75 | |
Yifan Hong | 079ec24 | 2017-08-25 18:53:38 -0700 | [diff] [blame] | 76 | // nullptr on any error, otherwise the condition. |
| 77 | static Condition generateCondition(const std::string& path) { |
| 78 | std::string fname = ::android::base::Basename(path); |
| 79 | if (fname.size() <= gConfigPrefix.size() + gConfigSuffix.size() || |
| 80 | !std::equal(gConfigPrefix.begin(), gConfigPrefix.end(), fname.begin()) || |
| 81 | !std::equal(gConfigSuffix.rbegin(), gConfigSuffix.rend(), fname.rbegin())) { |
| 82 | return nullptr; |
| 83 | } |
| 84 | |
| 85 | std::string sub = fname.substr(gConfigPrefix.size(), |
| 86 | fname.size() - gConfigPrefix.size() - gConfigSuffix.size()); |
| 87 | if (sub.empty()) { |
| 88 | return nullptr; // should not happen |
| 89 | } |
| 90 | for (size_t i = 0; i < sub.size(); ++i) { |
| 91 | if (sub[i] == '-') { |
| 92 | sub[i] = '_'; |
| 93 | continue; |
| 94 | } |
| 95 | if (isalnum(sub[i])) { |
| 96 | sub[i] = toupper(sub[i]); |
| 97 | continue; |
| 98 | } |
| 99 | std::cerr << "'" << fname << "' (in " << path |
| 100 | << ") is not a valid kernel config file name. Must match regex: " |
| 101 | << "android-base(-[0-9a-zA-Z-]+)?\\.cfg" << std::endl; |
| 102 | return nullptr; |
| 103 | } |
| 104 | sub.insert(0, "CONFIG_"); |
| 105 | return std::make_unique<KernelConfig>(std::move(sub), Tristate::YES); |
| 106 | } |
| 107 | |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 108 | static bool parseFileForKernelConfigs(const std::string& path, std::vector<KernelConfig>* out) { |
| 109 | std::ifstream ifs{path}; |
| 110 | if (!ifs.is_open()) { |
| 111 | std::cerr << "File '" << path << "' does not exist or cannot be read." << std::endl; |
| 112 | return false; |
| 113 | } |
Yifan Hong | 02e9400 | 2017-07-10 15:41:56 -0700 | [diff] [blame] | 114 | KernelConfigParser parser(true /* processComments */, true /* relaxedFormat */); |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 115 | std::string content = read(ifs); |
| 116 | status_t err = parser.process(content.c_str(), content.size()); |
| 117 | if (err != OK) { |
Yifan Hong | ae53a0e | 2017-07-07 15:19:06 -0700 | [diff] [blame] | 118 | std::cerr << parser.error(); |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | err = parser.finish(); |
| 122 | if (err != OK) { |
Yifan Hong | ae53a0e | 2017-07-07 15:19:06 -0700 | [diff] [blame] | 123 | std::cerr << parser.error(); |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | |
| 127 | for (auto& configPair : parser.configs()) { |
| 128 | out->push_back({}); |
| 129 | KernelConfig& config = out->back(); |
| 130 | config.first = std::move(configPair.first); |
| 131 | if (!parseKernelConfigTypedValue(configPair.second, &config.second)) { |
| 132 | std::cerr << "Unknown value type for key = '" << config.first << "', value = '" |
| 133 | << configPair.second << "'\n"; |
| 134 | return false; |
| 135 | } |
| 136 | } |
| 137 | return true; |
| 138 | } |
| 139 | |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 140 | static bool parseFilesForKernelConfigs(const std::string& path, |
| 141 | std::vector<ConditionedConfig>* out) { |
| 142 | out->clear(); |
| 143 | ConditionedConfig commonConfig; |
| 144 | bool foundCommonConfig = false; |
Steve Muckle | 0bef868 | 2017-07-31 15:47:15 -0700 | [diff] [blame] | 145 | bool ret = true; |
| 146 | char *pathIter; |
| 147 | char *modPath = new char[path.length() + 1]; |
| 148 | strcpy(modPath, path.c_str()); |
| 149 | pathIter = strtok(modPath, ":"); |
| 150 | while (ret && pathIter != NULL) { |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 151 | if (isCommonConfig(pathIter)) { |
| 152 | ret &= parseFileForKernelConfigs(pathIter, &commonConfig.second); |
| 153 | foundCommonConfig = true; |
| 154 | } else { |
Yifan Hong | 079ec24 | 2017-08-25 18:53:38 -0700 | [diff] [blame] | 155 | Condition condition = generateCondition(pathIter); |
| 156 | ret &= (condition != nullptr); |
| 157 | |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 158 | std::vector<KernelConfig> kernelConfigs; |
| 159 | if ((ret &= parseFileForKernelConfigs(pathIter, &kernelConfigs))) |
Yifan Hong | 079ec24 | 2017-08-25 18:53:38 -0700 | [diff] [blame] | 160 | out->emplace_back(std::move(condition), std::move(kernelConfigs)); |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 161 | } |
Steve Muckle | 0bef868 | 2017-07-31 15:47:15 -0700 | [diff] [blame] | 162 | pathIter = strtok(NULL, ":"); |
| 163 | } |
Luis A. Lozano | 82266ae | 2017-08-22 16:30:11 -0700 | [diff] [blame] | 164 | delete[] modPath; |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 165 | |
| 166 | if (!foundCommonConfig) { |
| 167 | std::cerr << "No android-base.cfg is found in these paths: '" << path << "'" |
| 168 | << std::endl; |
| 169 | } |
| 170 | ret &= foundCommonConfig; |
| 171 | // first element is always common configs (no conditions). |
| 172 | out->insert(out->begin(), std::move(commonConfig)); |
Steve Muckle | 0bef868 | 2017-07-31 15:47:15 -0700 | [diff] [blame] | 173 | return ret; |
| 174 | } |
| 175 | |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 176 | std::basic_ostream<char>& out() const { |
| 177 | return mOutFileRef == nullptr ? std::cout : *mOutFileRef; |
| 178 | } |
| 179 | |
| 180 | bool assembleHalManifest(HalManifest* halManifest) { |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 181 | std::string error; |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 182 | |
| 183 | if (halManifest->mType == SchemaType::DEVICE) { |
| 184 | if (!getFlag("BOARD_SEPOLICY_VERS", &halManifest->device.mSepolicyVersion)) { |
| 185 | return false; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | if (mOutputMatrix) { |
| 190 | CompatibilityMatrix generatedMatrix = halManifest->generateCompatibleMatrix(); |
| 191 | if (!halManifest->checkCompatibility(generatedMatrix, &error)) { |
| 192 | std::cerr << "FATAL ERROR: cannot generate a compatible matrix: " << error |
| 193 | << std::endl; |
| 194 | } |
| 195 | out() << "<!-- \n" |
| 196 | " Autogenerated skeleton compatibility matrix. \n" |
| 197 | " Use with caution. Modify it to suit your needs.\n" |
| 198 | " All HALs are set to optional.\n" |
| 199 | " Many entries other than HALs are zero-filled and\n" |
| 200 | " require human attention. \n" |
| 201 | "-->\n" |
| 202 | << gCompatibilityMatrixConverter(generatedMatrix); |
| 203 | } else { |
| 204 | out() << gHalManifestConverter(*halManifest); |
| 205 | } |
| 206 | out().flush(); |
| 207 | |
| 208 | if (mCheckFile.is_open()) { |
| 209 | CompatibilityMatrix checkMatrix; |
| 210 | if (!gCompatibilityMatrixConverter(&checkMatrix, read(mCheckFile))) { |
| 211 | std::cerr << "Cannot parse check file as a compatibility matrix: " |
| 212 | << gCompatibilityMatrixConverter.lastError() << std::endl; |
| 213 | return false; |
| 214 | } |
| 215 | if (!halManifest->checkCompatibility(checkMatrix, &error)) { |
| 216 | std::cerr << "Not compatible: " << error << std::endl; |
| 217 | return false; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | return true; |
| 222 | } |
| 223 | |
Yifan Hong | e88e167 | 2017-08-24 14:42:54 -0700 | [diff] [blame] | 224 | bool assembleFrameworkCompatibilityMatrixKernels(CompatibilityMatrix* matrix) { |
Yifan Hong | 4c34fee | 2017-08-24 16:03:34 -0700 | [diff] [blame] | 225 | if (!matrix->framework.mKernels.empty()) { |
| 226 | // Remove hard-coded <kernel version="x.y.z" /> in legacy files. |
| 227 | std::cerr << "WARNING: framework compatibility matrix has hard-coded kernel" |
| 228 | << " requirements for version"; |
| 229 | for (const auto& kernel : matrix->framework.mKernels) { |
| 230 | std::cerr << " " << kernel.minLts(); |
| 231 | } |
| 232 | std::cerr << ". Hard-coded requirements are removed." << std::endl; |
| 233 | matrix->framework.mKernels.clear(); |
| 234 | } |
Yifan Hong | e88e167 | 2017-08-24 14:42:54 -0700 | [diff] [blame] | 235 | for (const auto& pair : mKernels) { |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 236 | std::vector<ConditionedConfig> conditionedConfigs; |
| 237 | if (!parseFilesForKernelConfigs(pair.second, &conditionedConfigs)) { |
Yifan Hong | e88e167 | 2017-08-24 14:42:54 -0700 | [diff] [blame] | 238 | return false; |
| 239 | } |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 240 | for (ConditionedConfig& conditionedConfig : conditionedConfigs) { |
Yifan Hong | 48602df | 2017-08-28 13:04:12 -0700 | [diff] [blame^] | 241 | MatrixKernel kernel(KernelVersion{pair.first.majorVer, pair.first.minorVer, 0u}, |
| 242 | std::move(conditionedConfig.second)); |
Yifan Hong | 079ec24 | 2017-08-25 18:53:38 -0700 | [diff] [blame] | 243 | if (conditionedConfig.first != nullptr) |
| 244 | kernel.mConditions.push_back(std::move(*conditionedConfig.first)); |
| 245 | matrix->framework.mKernels.push_back(std::move(kernel)); |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 246 | } |
Yifan Hong | e88e167 | 2017-08-24 14:42:54 -0700 | [diff] [blame] | 247 | } |
| 248 | return true; |
| 249 | } |
| 250 | |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 251 | bool assembleCompatibilityMatrix(CompatibilityMatrix* matrix) { |
| 252 | std::string error; |
| 253 | |
| 254 | KernelSepolicyVersion kernelSepolicyVers; |
| 255 | Version sepolicyVers; |
| 256 | if (matrix->mType == SchemaType::FRAMEWORK) { |
| 257 | if (!getFlag("BOARD_SEPOLICY_VERS", &sepolicyVers)) { |
| 258 | return false; |
| 259 | } |
| 260 | if (!getFlag("POLICYVERS", &kernelSepolicyVers)) { |
| 261 | return false; |
| 262 | } |
Yifan Hong | e88e167 | 2017-08-24 14:42:54 -0700 | [diff] [blame] | 263 | |
| 264 | if (!assembleFrameworkCompatibilityMatrixKernels(matrix)) { |
| 265 | return false; |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 266 | } |
Yifan Hong | e88e167 | 2017-08-24 14:42:54 -0700 | [diff] [blame] | 267 | |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 268 | matrix->framework.mSepolicy = |
| 269 | Sepolicy(kernelSepolicyVers, {{sepolicyVers.majorVer, sepolicyVers.minorVer}}); |
Yifan Hong | 7f6c00c | 2017-07-06 19:50:29 +0000 | [diff] [blame] | 270 | |
| 271 | Version avbMetaVersion; |
| 272 | if (!getFlag("FRAMEWORK_VBMETA_VERSION", &avbMetaVersion)) { |
| 273 | return false; |
| 274 | } |
| 275 | matrix->framework.mAvbMetaVersion = avbMetaVersion; |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 276 | } |
| 277 | out() << gCompatibilityMatrixConverter(*matrix); |
| 278 | out().flush(); |
| 279 | |
| 280 | if (mCheckFile.is_open()) { |
| 281 | HalManifest checkManifest; |
| 282 | if (!gHalManifestConverter(&checkManifest, read(mCheckFile))) { |
| 283 | std::cerr << "Cannot parse check file as a HAL manifest: " |
| 284 | << gHalManifestConverter.lastError() << std::endl; |
| 285 | return false; |
| 286 | } |
| 287 | if (!checkManifest.checkCompatibility(*matrix, &error)) { |
| 288 | std::cerr << "Not compatible: " << error << std::endl; |
| 289 | return false; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | return true; |
| 294 | } |
| 295 | |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 296 | enum AssembleStatus { SUCCESS, FAIL_AND_EXIT, TRY_NEXT }; |
| 297 | template <typename Schema, typename AssembleFunc> |
| 298 | AssembleStatus tryAssemble(const XmlConverter<Schema>& converter, const std::string& schemaName, |
| 299 | AssembleFunc assemble) { |
| 300 | Schema schema; |
| 301 | if (!converter(&schema, read(mInFiles.front()))) { |
| 302 | return TRY_NEXT; |
| 303 | } |
| 304 | auto firstType = schema.type(); |
| 305 | for (auto it = mInFiles.begin() + 1; it != mInFiles.end(); ++it) { |
| 306 | Schema additionalSchema; |
| 307 | if (!converter(&additionalSchema, read(*it))) { |
| 308 | std::cerr << "File \"" << mInFilePaths[std::distance(mInFiles.begin(), it)] |
| 309 | << "\" is not a valid " << firstType << " " << schemaName |
| 310 | << " (but the first file is a valid " << firstType << " " << schemaName |
| 311 | << "). Error: " << converter.lastError() << std::endl; |
| 312 | return FAIL_AND_EXIT; |
| 313 | } |
| 314 | if (additionalSchema.type() != firstType) { |
| 315 | std::cerr << "File \"" << mInFilePaths[std::distance(mInFiles.begin(), it)] |
| 316 | << "\" is a " << additionalSchema.type() << " " << schemaName |
| 317 | << " (but a " << firstType << " " << schemaName << " is expected)." |
| 318 | << std::endl; |
| 319 | return FAIL_AND_EXIT; |
| 320 | } |
| 321 | schema.addAll(std::move(additionalSchema)); |
| 322 | } |
| 323 | return assemble(&schema) ? SUCCESS : FAIL_AND_EXIT; |
| 324 | } |
| 325 | |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 326 | bool assemble() { |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 327 | using std::placeholders::_1; |
| 328 | if (mInFiles.empty()) { |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 329 | std::cerr << "Missing input file." << std::endl; |
| 330 | return false; |
| 331 | } |
| 332 | |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 333 | auto status = tryAssemble(gHalManifestConverter, "manifest", |
| 334 | std::bind(&AssembleVintf::assembleHalManifest, this, _1)); |
| 335 | if (status == SUCCESS) return true; |
| 336 | if (status == FAIL_AND_EXIT) return false; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 337 | |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 338 | resetInFiles(); |
Yifan Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 339 | |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 340 | status = tryAssemble(gCompatibilityMatrixConverter, "compatibility matrix", |
| 341 | std::bind(&AssembleVintf::assembleCompatibilityMatrix, this, _1)); |
| 342 | if (status == SUCCESS) return true; |
| 343 | if (status == FAIL_AND_EXIT) return false; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 344 | |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 345 | std::cerr << "Input file has unknown format." << std::endl |
| 346 | << "Error when attempting to convert to manifest: " |
| 347 | << gHalManifestConverter.lastError() << std::endl |
| 348 | << "Error when attempting to convert to compatibility matrix: " |
| 349 | << gCompatibilityMatrixConverter.lastError() << std::endl; |
| 350 | return false; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 351 | } |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 352 | |
| 353 | bool openOutFile(const char* path) { |
| 354 | mOutFileRef = std::make_unique<std::ofstream>(); |
| 355 | mOutFileRef->open(path); |
| 356 | return mOutFileRef->is_open(); |
| 357 | } |
| 358 | |
| 359 | bool openInFile(const char* path) { |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 360 | mInFilePaths.push_back(path); |
| 361 | mInFiles.push_back({}); |
| 362 | mInFiles.back().open(path); |
| 363 | return mInFiles.back().is_open(); |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | bool openCheckFile(const char* path) { |
| 367 | mCheckFile.open(path); |
| 368 | return mCheckFile.is_open(); |
| 369 | } |
| 370 | |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 371 | void resetInFiles() { |
| 372 | for (auto& inFile : mInFiles) { |
| 373 | inFile.clear(); |
| 374 | inFile.seekg(0); |
| 375 | } |
| 376 | } |
| 377 | |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 378 | void setOutputMatrix() { mOutputMatrix = true; } |
| 379 | |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 380 | bool addKernel(const std::string& kernelArg) { |
| 381 | auto ind = kernelArg.find(':'); |
| 382 | if (ind == std::string::npos) { |
| 383 | std::cerr << "Unrecognized --kernel option '" << kernelArg << "'" << std::endl; |
| 384 | return false; |
| 385 | } |
| 386 | std::string kernelVerStr{kernelArg.begin(), kernelArg.begin() + ind}; |
| 387 | std::string kernelConfigPath{kernelArg.begin() + ind + 1, kernelArg.end()}; |
| 388 | Version kernelVer; |
| 389 | if (!parse(kernelVerStr, &kernelVer)) { |
| 390 | std::cerr << "Unrecognized kernel version '" << kernelVerStr << "'" << std::endl; |
| 391 | return false; |
| 392 | } |
Yifan Hong | 48602df | 2017-08-28 13:04:12 -0700 | [diff] [blame^] | 393 | if (mKernels.find(kernelVer) != mKernels.end()) { |
| 394 | std::cerr << "Multiple --kernel for " << kernelVer << " is specified." << std::endl; |
| 395 | return false; |
| 396 | } |
| 397 | mKernels[kernelVer] = kernelConfigPath; |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 398 | return true; |
| 399 | } |
| 400 | |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 401 | private: |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 402 | std::vector<std::string> mInFilePaths; |
| 403 | std::vector<std::ifstream> mInFiles; |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 404 | std::unique_ptr<std::ofstream> mOutFileRef; |
| 405 | std::ifstream mCheckFile; |
| 406 | bool mOutputMatrix = false; |
Yifan Hong | 48602df | 2017-08-28 13:04:12 -0700 | [diff] [blame^] | 407 | std::map<Version, std::string> mKernels; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 408 | }; |
| 409 | |
| 410 | } // namespace vintf |
| 411 | } // namespace android |
| 412 | |
| 413 | void help() { |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 414 | std::cerr << "assemble_vintf: Checks if a given manifest / matrix file is valid and \n" |
| 415 | " fill in build-time flags into the given file.\n" |
| 416 | "assemble_vintf -h\n" |
| 417 | " Display this help text.\n" |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 418 | "assemble_vintf -i <input file>[:<input file>[...]] [-o <output file>] [-m]\n" |
| 419 | " [-c [<check file>]]\n" |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 420 | " Fill in build-time flags into the given file.\n" |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 421 | " -i <input file>[:<input file>[...]]\n" |
| 422 | " A list of input files. Format is automatically detected for the\n" |
| 423 | " first file, and the remaining files must have the same format.\n" |
| 424 | " Files other than the first file should only have <hal> defined;\n" |
| 425 | " other entries are ignored.\n" |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 426 | " -o <output file>\n" |
| 427 | " Optional output file. If not specified, write to stdout.\n" |
| 428 | " -m\n" |
| 429 | " a compatible compatibility matrix is\n" |
| 430 | " generated instead; for example, given a device manifest,\n" |
| 431 | " a framework compatibility matrix is generated. This flag\n" |
| 432 | " is ignored when input is a compatibility matrix.\n" |
| 433 | " -c [<check file>]\n" |
| 434 | " After writing the output file, check compatibility between\n" |
| 435 | " output file and check file.\n" |
| 436 | " If -c is set but the check file is not specified, a warning\n" |
| 437 | " message is written to stderr. Return 0.\n" |
| 438 | " If the check file is specified but is not compatible, an error\n" |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 439 | " message is written to stderr. Return 1.\n" |
Steve Muckle | 0bef868 | 2017-07-31 15:47:15 -0700 | [diff] [blame] | 440 | " --kernel=<version>:<android-base.cfg>[:<android-base-arch.cfg>[...]]\n" |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 441 | " Add a kernel entry to framework compatibility matrix.\n" |
| 442 | " Ignored for other input format.\n" |
| 443 | " <version> has format: 3.18\n" |
Steve Muckle | 0bef868 | 2017-07-31 15:47:15 -0700 | [diff] [blame] | 444 | " <android-base.cfg> is the location of android-base.cfg\n" |
| 445 | " <android-base-arch.cfg> is the location of an optional\n" |
| 446 | " arch-specific config fragment, more than one may be specified\n"; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | int main(int argc, char **argv) { |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 450 | const struct option longopts[] = {{"kernel", required_argument, NULL, 'k'}, {0, 0, 0, 0}}; |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 451 | |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 452 | std::string outFilePath; |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 453 | ::android::vintf::AssembleVintf assembleVintf; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 454 | int res; |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 455 | int optind; |
| 456 | while ((res = getopt_long(argc, argv, "hi:o:mc:", longopts, &optind)) >= 0) { |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 457 | switch (res) { |
| 458 | case 'i': { |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 459 | char* inFilePath = strtok(optarg, ":"); |
| 460 | while (inFilePath != NULL) { |
| 461 | if (!assembleVintf.openInFile(inFilePath)) { |
| 462 | std::cerr << "Failed to open " << optarg << std::endl; |
| 463 | return 1; |
| 464 | } |
| 465 | inFilePath = strtok(NULL, ":"); |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 466 | } |
| 467 | } break; |
| 468 | |
| 469 | case 'o': { |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 470 | outFilePath = optarg; |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 471 | if (!assembleVintf.openOutFile(optarg)) { |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 472 | std::cerr << "Failed to open " << optarg << std::endl; |
| 473 | return 1; |
| 474 | } |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 475 | } break; |
| 476 | |
Yifan Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 477 | case 'm': { |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 478 | assembleVintf.setOutputMatrix(); |
Yifan Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 479 | } break; |
| 480 | |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 481 | case 'c': { |
| 482 | if (strlen(optarg) != 0) { |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 483 | if (!assembleVintf.openCheckFile(optarg)) { |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 484 | std::cerr << "Failed to open " << optarg << std::endl; |
| 485 | return 1; |
| 486 | } |
| 487 | } else { |
| 488 | std::cerr << "WARNING: no compatibility check is done on " |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 489 | << (outFilePath.empty() ? "output" : outFilePath) << std::endl; |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 490 | } |
| 491 | } break; |
| 492 | |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 493 | case 'k': { |
| 494 | if (!assembleVintf.addKernel(optarg)) { |
| 495 | std::cerr << "ERROR: Unrecognized --kernel argument." << std::endl; |
| 496 | return 1; |
| 497 | } |
| 498 | } break; |
| 499 | |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 500 | case 'h': |
| 501 | default: { |
| 502 | help(); |
| 503 | return 1; |
| 504 | } break; |
| 505 | } |
| 506 | } |
| 507 | |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 508 | bool success = assembleVintf.assemble(); |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 509 | |
| 510 | return success ? 0 : 1; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 511 | } |