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 | } |
Yifan Hong | eb14e30 | 2017-10-17 16:35:14 -0700 | [diff] [blame] | 321 | std::string error; |
| 322 | if (!schema.addAll(std::move(additionalSchema), &error)) { |
| 323 | std::cerr << "File \"" << mInFilePaths[std::distance(mInFiles.begin(), it)] |
| 324 | << "\" cannot be added: conflict on HAL \"" << error |
| 325 | << "\" with an existing HAL. See <hal> with the same name " |
| 326 | << "in previously parsed files or previously declared in this file." |
| 327 | << std::endl; |
| 328 | return FAIL_AND_EXIT; |
| 329 | } |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 330 | } |
| 331 | return assemble(&schema) ? SUCCESS : FAIL_AND_EXIT; |
| 332 | } |
| 333 | |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 334 | bool assemble() { |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 335 | using std::placeholders::_1; |
| 336 | if (mInFiles.empty()) { |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 337 | std::cerr << "Missing input file." << std::endl; |
| 338 | return false; |
| 339 | } |
| 340 | |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 341 | auto status = tryAssemble(gHalManifestConverter, "manifest", |
| 342 | std::bind(&AssembleVintf::assembleHalManifest, this, _1)); |
| 343 | if (status == SUCCESS) return true; |
| 344 | if (status == FAIL_AND_EXIT) return false; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 345 | |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 346 | resetInFiles(); |
Yifan Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 347 | |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 348 | status = tryAssemble(gCompatibilityMatrixConverter, "compatibility matrix", |
| 349 | std::bind(&AssembleVintf::assembleCompatibilityMatrix, this, _1)); |
| 350 | if (status == SUCCESS) return true; |
| 351 | if (status == FAIL_AND_EXIT) return false; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 352 | |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 353 | std::cerr << "Input file has unknown format." << std::endl |
| 354 | << "Error when attempting to convert to manifest: " |
| 355 | << gHalManifestConverter.lastError() << std::endl |
| 356 | << "Error when attempting to convert to compatibility matrix: " |
| 357 | << gCompatibilityMatrixConverter.lastError() << std::endl; |
| 358 | return false; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 359 | } |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 360 | |
| 361 | bool openOutFile(const char* path) { |
| 362 | mOutFileRef = std::make_unique<std::ofstream>(); |
| 363 | mOutFileRef->open(path); |
| 364 | return mOutFileRef->is_open(); |
| 365 | } |
| 366 | |
| 367 | bool openInFile(const char* path) { |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 368 | mInFilePaths.push_back(path); |
| 369 | mInFiles.push_back({}); |
| 370 | mInFiles.back().open(path); |
| 371 | return mInFiles.back().is_open(); |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | bool openCheckFile(const char* path) { |
| 375 | mCheckFile.open(path); |
| 376 | return mCheckFile.is_open(); |
| 377 | } |
| 378 | |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 379 | void resetInFiles() { |
| 380 | for (auto& inFile : mInFiles) { |
| 381 | inFile.clear(); |
| 382 | inFile.seekg(0); |
| 383 | } |
| 384 | } |
| 385 | |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 386 | void setOutputMatrix() { mOutputMatrix = true; } |
| 387 | |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 388 | bool addKernel(const std::string& kernelArg) { |
| 389 | auto ind = kernelArg.find(':'); |
| 390 | if (ind == std::string::npos) { |
| 391 | std::cerr << "Unrecognized --kernel option '" << kernelArg << "'" << std::endl; |
| 392 | return false; |
| 393 | } |
| 394 | std::string kernelVerStr{kernelArg.begin(), kernelArg.begin() + ind}; |
| 395 | std::string kernelConfigPath{kernelArg.begin() + ind + 1, kernelArg.end()}; |
| 396 | Version kernelVer; |
| 397 | if (!parse(kernelVerStr, &kernelVer)) { |
| 398 | std::cerr << "Unrecognized kernel version '" << kernelVerStr << "'" << std::endl; |
| 399 | return false; |
| 400 | } |
Yifan Hong | 48602df | 2017-08-28 13:04:12 -0700 | [diff] [blame] | 401 | if (mKernels.find(kernelVer) != mKernels.end()) { |
| 402 | std::cerr << "Multiple --kernel for " << kernelVer << " is specified." << std::endl; |
| 403 | return false; |
| 404 | } |
| 405 | mKernels[kernelVer] = kernelConfigPath; |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 406 | return true; |
| 407 | } |
| 408 | |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 409 | private: |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 410 | std::vector<std::string> mInFilePaths; |
| 411 | std::vector<std::ifstream> mInFiles; |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 412 | std::unique_ptr<std::ofstream> mOutFileRef; |
| 413 | std::ifstream mCheckFile; |
| 414 | bool mOutputMatrix = false; |
Yifan Hong | 48602df | 2017-08-28 13:04:12 -0700 | [diff] [blame] | 415 | std::map<Version, std::string> mKernels; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 416 | }; |
| 417 | |
| 418 | } // namespace vintf |
| 419 | } // namespace android |
| 420 | |
| 421 | void help() { |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 422 | std::cerr << "assemble_vintf: Checks if a given manifest / matrix file is valid and \n" |
| 423 | " fill in build-time flags into the given file.\n" |
| 424 | "assemble_vintf -h\n" |
| 425 | " Display this help text.\n" |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 426 | "assemble_vintf -i <input file>[:<input file>[...]] [-o <output file>] [-m]\n" |
| 427 | " [-c [<check file>]]\n" |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 428 | " Fill in build-time flags into the given file.\n" |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 429 | " -i <input file>[:<input file>[...]]\n" |
| 430 | " A list of input files. Format is automatically detected for the\n" |
| 431 | " first file, and the remaining files must have the same format.\n" |
| 432 | " Files other than the first file should only have <hal> defined;\n" |
| 433 | " other entries are ignored.\n" |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 434 | " -o <output file>\n" |
| 435 | " Optional output file. If not specified, write to stdout.\n" |
| 436 | " -m\n" |
| 437 | " a compatible compatibility matrix is\n" |
| 438 | " generated instead; for example, given a device manifest,\n" |
| 439 | " a framework compatibility matrix is generated. This flag\n" |
| 440 | " is ignored when input is a compatibility matrix.\n" |
| 441 | " -c [<check file>]\n" |
| 442 | " After writing the output file, check compatibility between\n" |
| 443 | " output file and check file.\n" |
| 444 | " If -c is set but the check file is not specified, a warning\n" |
| 445 | " message is written to stderr. Return 0.\n" |
| 446 | " 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] | 447 | " message is written to stderr. Return 1.\n" |
Steve Muckle | 0bef868 | 2017-07-31 15:47:15 -0700 | [diff] [blame] | 448 | " --kernel=<version>:<android-base.cfg>[:<android-base-arch.cfg>[...]]\n" |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 449 | " Add a kernel entry to framework compatibility matrix.\n" |
| 450 | " Ignored for other input format.\n" |
| 451 | " <version> has format: 3.18\n" |
Steve Muckle | 0bef868 | 2017-07-31 15:47:15 -0700 | [diff] [blame] | 452 | " <android-base.cfg> is the location of android-base.cfg\n" |
| 453 | " <android-base-arch.cfg> is the location of an optional\n" |
| 454 | " arch-specific config fragment, more than one may be specified\n"; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | int main(int argc, char **argv) { |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 458 | 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] | 459 | |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 460 | std::string outFilePath; |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 461 | ::android::vintf::AssembleVintf assembleVintf; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 462 | int res; |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 463 | int optind; |
| 464 | while ((res = getopt_long(argc, argv, "hi:o:mc:", longopts, &optind)) >= 0) { |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 465 | switch (res) { |
| 466 | case 'i': { |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 467 | char* inFilePath = strtok(optarg, ":"); |
| 468 | while (inFilePath != NULL) { |
| 469 | if (!assembleVintf.openInFile(inFilePath)) { |
| 470 | std::cerr << "Failed to open " << optarg << std::endl; |
| 471 | return 1; |
| 472 | } |
| 473 | inFilePath = strtok(NULL, ":"); |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 474 | } |
| 475 | } break; |
| 476 | |
| 477 | case 'o': { |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 478 | outFilePath = optarg; |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 479 | if (!assembleVintf.openOutFile(optarg)) { |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 480 | std::cerr << "Failed to open " << optarg << std::endl; |
| 481 | return 1; |
| 482 | } |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 483 | } break; |
| 484 | |
Yifan Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 485 | case 'm': { |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 486 | assembleVintf.setOutputMatrix(); |
Yifan Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 487 | } break; |
| 488 | |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 489 | case 'c': { |
| 490 | if (strlen(optarg) != 0) { |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 491 | if (!assembleVintf.openCheckFile(optarg)) { |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 492 | std::cerr << "Failed to open " << optarg << std::endl; |
| 493 | return 1; |
| 494 | } |
| 495 | } else { |
| 496 | std::cerr << "WARNING: no compatibility check is done on " |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 497 | << (outFilePath.empty() ? "output" : outFilePath) << std::endl; |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 498 | } |
| 499 | } break; |
| 500 | |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 501 | case 'k': { |
| 502 | if (!assembleVintf.addKernel(optarg)) { |
| 503 | std::cerr << "ERROR: Unrecognized --kernel argument." << std::endl; |
| 504 | return 1; |
| 505 | } |
| 506 | } break; |
| 507 | |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 508 | case 'h': |
| 509 | default: { |
| 510 | help(); |
| 511 | return 1; |
| 512 | } break; |
| 513 | } |
| 514 | } |
| 515 | |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 516 | bool success = assembleVintf.assemble(); |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 517 | |
| 518 | return success ? 0 : 1; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 519 | } |