Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [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 "CompatibilityMatrix.h" |
| 18 | |
Yifan Hong | b6c7f49 | 2018-02-27 14:07:57 -0800 | [diff] [blame] | 19 | #include <iostream> |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 20 | #include <utility> |
| 21 | |
Yifan Hong | 1e8febd | 2019-08-07 16:17:19 -0700 | [diff] [blame] | 22 | #include <android-base/logging.h> |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 23 | #include <android-base/strings.h> |
| 24 | |
Yifan Hong | bbfff30 | 2017-06-06 17:10:13 -0700 | [diff] [blame] | 25 | #include "parse_string.h" |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 26 | #include "parse_xml.h" |
Yifan Hong | b6c7f49 | 2018-02-27 14:07:57 -0800 | [diff] [blame] | 27 | #include "utils.h" |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 28 | |
Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 29 | namespace android { |
| 30 | namespace vintf { |
| 31 | |
Yifan Hong | 4c6962d | 2019-01-30 15:50:46 -0800 | [diff] [blame] | 32 | using details::mergeField; |
| 33 | |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 34 | bool CompatibilityMatrix::addKernel(MatrixKernel&& kernel, std::string* error) { |
Yifan Hong | 7c7d706 | 2017-04-04 16:26:51 -0700 | [diff] [blame] | 35 | if (mType != SchemaType::FRAMEWORK) { |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 36 | if (error) { |
| 37 | *error = "Cannot add <kernel> to a " + to_string(mType) + " compatibility matrix."; |
| 38 | } |
Yifan Hong | 7c7d706 | 2017-04-04 16:26:51 -0700 | [diff] [blame] | 39 | return false; |
| 40 | } |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 41 | |
Yifan Hong | b343c5b | 2019-12-11 18:13:59 -0800 | [diff] [blame] | 42 | if (kernel.getSourceMatrixLevel() == Level::UNSPECIFIED) { |
| 43 | kernel.setSourceMatrixLevel(level()); |
| 44 | } |
| 45 | |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 46 | auto it = framework.mKernels.begin(); |
| 47 | for (; it != framework.mKernels.end(); ++it) { |
| 48 | if (it->minLts() == kernel.minLts()) { |
| 49 | break; |
| 50 | } |
Yifan Hong | 89093ed | 2019-12-12 12:16:01 -0800 | [diff] [blame^] | 51 | if (it->minLts().dropMinor() == kernel.minLts().dropMinor()) { |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 52 | if (error) { |
| 53 | *error = "Kernel version mismatch; cannot add " + to_string(kernel.minLts()) + |
| 54 | " because " + to_string(it->minLts()) + " was added."; |
| 55 | } |
| 56 | return false; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | bool seenVersion = it != framework.mKernels.end(); |
| 61 | |
| 62 | if (seenVersion) { |
| 63 | // If no conditions, must be the first among the same minLts |
| 64 | // because O libvintf only checks the first <kernel> tag that version matches. |
| 65 | if (kernel.conditions().empty()) { |
| 66 | // Found first <kernel> with the same minLts. |
| 67 | // Append config if it does not have <condition>s, else error. |
| 68 | if (it->conditions().empty()) { |
| 69 | const auto& configs = kernel.configs(); |
| 70 | it->mConfigs.insert(it->mConfigs.end(), configs.begin(), configs.end()); |
| 71 | } else { |
| 72 | if (error) { |
| 73 | *error = |
| 74 | "Base compatibility matrix has <condition> for the first <kernel> " |
| 75 | "with minlts " + |
| 76 | to_string(kernel.minLts()) + " for unknown reason."; |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | return true; |
| 81 | } |
| 82 | } else { |
| 83 | // First <kernel> of a minLts must not have <condition>'s for backwards compatibility |
| 84 | // with O libvintf. |
| 85 | if (!kernel.conditions().empty()) { |
| 86 | framework.mKernels.push_back(MatrixKernel(KernelVersion{kernel.minLts()}, {})); |
| 87 | } |
| 88 | } |
| 89 | |
Yifan Hong | 7c7d706 | 2017-04-04 16:26:51 -0700 | [diff] [blame] | 90 | framework.mKernels.push_back(std::move(kernel)); |
Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 91 | return true; |
| 92 | } |
| 93 | |
Yifan Hong | 398f4c7 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 94 | SchemaType CompatibilityMatrix::type() const { |
| 95 | return mType; |
| 96 | } |
| 97 | |
Yifan Hong | 2027a49 | 2017-12-11 15:21:19 -0800 | [diff] [blame] | 98 | Level CompatibilityMatrix::level() const { |
| 99 | return mLevel; |
| 100 | } |
| 101 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 102 | status_t CompatibilityMatrix::fetchAllInformation(const FileSystem* fileSystem, |
| 103 | const std::string& path, std::string* error) { |
| 104 | return details::fetchAllInformation(fileSystem, path, gCompatibilityMatrixConverter, this, |
| 105 | error); |
Yifan Hong | 1e5a054 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Yifan Hong | bbfff30 | 2017-06-06 17:10:13 -0700 | [diff] [blame] | 108 | std::string CompatibilityMatrix::getXmlSchemaPath(const std::string& xmlFileName, |
| 109 | const Version& version) const { |
| 110 | using std::literals::string_literals::operator""s; |
| 111 | auto range = getXmlFiles(xmlFileName); |
| 112 | for (auto it = range.first; it != range.second; ++it) { |
| 113 | const MatrixXmlFile& matrixXmlFile = it->second; |
| 114 | if (matrixXmlFile.versionRange().contains(version)) { |
| 115 | if (!matrixXmlFile.overriddenPath().empty()) { |
| 116 | return matrixXmlFile.overriddenPath(); |
| 117 | } |
| 118 | return "/"s + (type() == SchemaType::DEVICE ? "vendor" : "system") + "/etc/" + |
| 119 | xmlFileName + "_V" + std::to_string(matrixXmlFile.versionRange().majorVer) + |
| 120 | "_" + std::to_string(matrixXmlFile.versionRange().maxMinor) + "." + |
| 121 | to_string(matrixXmlFile.format()); |
| 122 | } |
| 123 | } |
| 124 | return ""; |
| 125 | } |
| 126 | |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 127 | // Split existingHal into a HAL that contains only interface/instance and a HAL |
| 128 | // that does not contain it. Return the HAL that contains only interface/instance. |
| 129 | // - Return nullptr if existingHal does not contain interface/instance |
| 130 | // - Return existingHal if existingHal contains only interface/instance |
| 131 | // - Remove interface/instance from existingHal, and return a new MatrixHal (that is added |
| 132 | // to "this") that contains only interface/instance. |
| 133 | MatrixHal* CompatibilityMatrix::splitInstance(MatrixHal* existingHal, const std::string& interface, |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 134 | const std::string& instanceOrPattern, bool isRegex) { |
| 135 | bool found = false; |
| 136 | bool foundOthers = false; |
| 137 | existingHal->forEachInstance([&](const auto& matrixInstance) { |
| 138 | bool interfaceMatch = matrixInstance.interface() == interface; |
| 139 | bool instanceMatch = false; |
| 140 | if (matrixInstance.isRegex() && isRegex) { |
| 141 | instanceMatch = (matrixInstance.regexPattern() == instanceOrPattern); |
| 142 | } else if (!matrixInstance.isRegex() && !isRegex) { |
| 143 | instanceMatch = (matrixInstance.exactInstance() == instanceOrPattern); |
| 144 | } |
| 145 | |
| 146 | bool match = interfaceMatch && instanceMatch; |
| 147 | |
| 148 | found |= match; |
| 149 | foundOthers |= (!match); |
| 150 | |
| 151 | return !found || !foundOthers; |
| 152 | }); |
| 153 | |
| 154 | if (!found) { |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 155 | return nullptr; |
| 156 | } |
| 157 | |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 158 | if (!foundOthers) { |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 159 | return existingHal; |
| 160 | } |
| 161 | |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 162 | existingHal->removeInstance(interface, instanceOrPattern, isRegex); |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 163 | MatrixHal copy = *existingHal; |
| 164 | copy.clearInstances(); |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 165 | copy.insertInstance(interface, instanceOrPattern, isRegex); |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 166 | |
| 167 | return addInternal(std::move(copy)); |
| 168 | } |
| 169 | |
Yifan Hong | 7967d7b | 2018-03-15 17:08:58 -0700 | [diff] [blame] | 170 | // Add all package@other_version::interface/instance as an optional instance. |
| 171 | // If package@this_version::interface/instance is in this (that is, some instance |
| 172 | // with the same package and interface and instance exists), then other_version is |
| 173 | // considered a possible replacement to this_version. |
| 174 | // See LibVintfTest.AddOptionalHal* tests for details. |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 175 | bool CompatibilityMatrix::addAllHalsAsOptional(CompatibilityMatrix* other, std::string* error) { |
| 176 | if (other == nullptr || other->level() <= level()) { |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | for (auto& pair : other->mHals) { |
| 181 | const std::string& name = pair.first; |
| 182 | MatrixHal& halToAdd = pair.second; |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 183 | |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 184 | std::set<std::pair<std::string, std::string>> insertedInstances; |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 185 | std::set<std::pair<std::string, std::string>> insertedRegex; |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 186 | auto existingHals = getHals(name); |
| 187 | |
| 188 | halToAdd.forEachInstance([&](const std::vector<VersionRange>& versionRanges, |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 189 | const std::string& interface, |
| 190 | const std::string& instanceOrPattern, bool isRegex) { |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 191 | for (auto* existingHal : existingHals) { |
Yifan Hong | 5bb4fbf | 2019-09-10 14:47:28 -0700 | [diff] [blame] | 192 | // Ignore HALs with different format. |
| 193 | if (halToAdd.format != existingHal->format) { |
| 194 | continue; |
| 195 | } |
| 196 | |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 197 | MatrixHal* splitInstance = |
| 198 | this->splitInstance(existingHal, interface, instanceOrPattern, isRegex); |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 199 | if (splitInstance != nullptr) { |
| 200 | splitInstance->insertVersionRanges(versionRanges); |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 201 | if (isRegex) { |
| 202 | insertedRegex.insert(std::make_pair(interface, instanceOrPattern)); |
| 203 | } else { |
| 204 | insertedInstances.insert(std::make_pair(interface, instanceOrPattern)); |
| 205 | } |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 206 | } |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 207 | } |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 208 | return true; |
| 209 | }); |
| 210 | |
| 211 | // Add the remaining instances. |
| 212 | for (const auto& pair : insertedInstances) { |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 213 | halToAdd.removeInstance(pair.first, pair.second, false /* isRegex */); |
| 214 | } |
| 215 | for (const auto& pair : insertedRegex) { |
| 216 | halToAdd.removeInstance(pair.first, pair.second, true /* isRegex */); |
Yifan Hong | 7967d7b | 2018-03-15 17:08:58 -0700 | [diff] [blame] | 217 | } |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 218 | |
Yifan Hong | 7e9e04d | 2018-03-20 13:06:00 -0700 | [diff] [blame] | 219 | if (halToAdd.instancesCount() > 0) { |
Yifan Hong | 7967d7b | 2018-03-15 17:08:58 -0700 | [diff] [blame] | 220 | halToAdd.setOptional(true); |
| 221 | if (!add(std::move(halToAdd))) { |
| 222 | if (error) { |
| 223 | *error = "Cannot add HAL " + name + " for unknown reason."; |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 224 | } |
| 225 | return false; |
| 226 | } |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | return true; |
| 230 | } |
| 231 | |
Yifan Hong | d4b92fe | 2017-12-20 15:29:03 -0800 | [diff] [blame] | 232 | bool CompatibilityMatrix::addAllXmlFilesAsOptional(CompatibilityMatrix* other, std::string* error) { |
| 233 | if (other == nullptr || other->level() <= level()) { |
| 234 | return true; |
| 235 | } |
| 236 | for (auto& pair : other->mXmlFiles) { |
| 237 | const std::string& name = pair.first; |
| 238 | MatrixXmlFile& xmlFileToAdd = pair.second; |
| 239 | |
| 240 | xmlFileToAdd.mOptional = true; |
| 241 | if (!addXmlFile(std::move(xmlFileToAdd))) { |
| 242 | if (error) { |
| 243 | *error = "Cannot add XML File " + name + " for unknown reason."; |
| 244 | } |
| 245 | return false; |
| 246 | } |
| 247 | } |
| 248 | return true; |
| 249 | } |
| 250 | |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 251 | // Merge Kernel. |
| 252 | // Add <kernel> from exact "level", then optionally add <kernel> from high levels to low levels. |
| 253 | // For example, (each letter is a kernel version x.y.z) |
| 254 | // 1.xml: A1, B1 |
| 255 | // 2.xml: B2, C2, D2 |
| 256 | // 3.xml: D3, E3 |
| 257 | // Then the combined 1.xml should have |
| 258 | // A1, B1 (from 1.xml, required), C2, D2, E3 (optional, use earliest possible). |
| 259 | bool CompatibilityMatrix::addAllKernels(CompatibilityMatrix* other, std::string* error) { |
| 260 | for (MatrixKernel& kernel : other->framework.mKernels) { |
Yifan Hong | a39ecfe | 2019-12-11 17:55:30 -0800 | [diff] [blame] | 261 | if (kernel.getSourceMatrixLevel() == Level::UNSPECIFIED) { |
| 262 | kernel.setSourceMatrixLevel(other->level()); |
| 263 | } |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 264 | KernelVersion ver = kernel.minLts(); |
| 265 | if (!addKernel(std::move(kernel), error)) { |
| 266 | if (error) { |
| 267 | *error = "Cannot add kernel version " + to_string(ver) + ": " + *error; |
| 268 | } |
| 269 | return false; |
| 270 | } |
| 271 | } |
| 272 | return true; |
| 273 | } |
| 274 | |
Yifan Hong | d6de7f6 | 2018-04-26 18:40:02 -0700 | [diff] [blame] | 275 | bool CompatibilityMatrix::addAllKernelsAsOptional(CompatibilityMatrix* other, std::string* error) { |
| 276 | if (other == nullptr || other->level() <= level()) { |
| 277 | return true; |
| 278 | } |
| 279 | |
| 280 | for (MatrixKernel& kernelToAdd : other->framework.mKernels) { |
| 281 | bool exists = |
| 282 | std::any_of(this->framework.mKernels.begin(), this->framework.mKernels.end(), |
| 283 | [&kernelToAdd](const MatrixKernel& existing) { |
| 284 | return kernelToAdd.minLts().version == existing.minLts().version && |
| 285 | kernelToAdd.minLts().majorRev == existing.minLts().majorRev; |
| 286 | }); |
| 287 | |
| 288 | if (exists) { |
| 289 | // Shouldn't retroactively add requirements to minLts(), so ignore this. |
| 290 | // This happens even when kernelToAdd.conditions() != existing.conditions(). |
| 291 | continue; |
| 292 | } |
| 293 | |
Yifan Hong | a39ecfe | 2019-12-11 17:55:30 -0800 | [diff] [blame] | 294 | if (kernelToAdd.getSourceMatrixLevel() == Level::UNSPECIFIED) { |
| 295 | kernelToAdd.setSourceMatrixLevel(other->level()); |
| 296 | } |
Yifan Hong | 1e8febd | 2019-08-07 16:17:19 -0700 | [diff] [blame] | 297 | |
Yifan Hong | d6de7f6 | 2018-04-26 18:40:02 -0700 | [diff] [blame] | 298 | KernelVersion minLts = kernelToAdd.minLts(); |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 299 | if (!addKernel(std::move(kernelToAdd), error)) { |
Yifan Hong | d6de7f6 | 2018-04-26 18:40:02 -0700 | [diff] [blame] | 300 | if (error) { |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 301 | *error = "Cannot add " + to_string(minLts) + ": " + *error; |
Yifan Hong | d6de7f6 | 2018-04-26 18:40:02 -0700 | [diff] [blame] | 302 | } |
| 303 | return false; |
| 304 | } |
| 305 | } |
| 306 | return true; |
| 307 | } |
| 308 | |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 309 | bool CompatibilityMatrix::addSepolicy(CompatibilityMatrix* other, std::string* error) { |
| 310 | bool success = mergeField(&this->framework.mSepolicy, &other->framework.mSepolicy); |
| 311 | if (!success && error) *error = "<sepolicy> is already defined"; |
| 312 | return success; |
| 313 | } |
| 314 | |
| 315 | bool CompatibilityMatrix::addAvbMetaVersion(CompatibilityMatrix* other, std::string* error) { |
| 316 | bool success = mergeField(&this->framework.mAvbMetaVersion, &other->framework.mAvbMetaVersion); |
| 317 | if (!success && error) *error = "<avb><vbmeta-version> is already defined"; |
| 318 | return success; |
| 319 | } |
| 320 | |
Yifan Hong | 5a51ea5 | 2019-04-22 14:54:57 -0700 | [diff] [blame] | 321 | bool CompatibilityMatrix::addVndk(CompatibilityMatrix* other, std::string* error) { |
| 322 | #pragma clang diagnostic push |
| 323 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 324 | bool success = mergeField(&this->device.mVndk, &other->device.mVndk); |
| 325 | #pragma clang diagnostic pop |
| 326 | if (!success && error) *error = "<vndk> is already defined"; |
| 327 | return success; |
| 328 | } |
| 329 | |
| 330 | bool CompatibilityMatrix::addVendorNdk(CompatibilityMatrix* other, std::string* error) { |
| 331 | bool success = mergeField(&this->device.mVendorNdk, &other->device.mVendorNdk); |
| 332 | if (!success && error) *error = "<vendor-ndk> is already defined"; |
| 333 | return success; |
| 334 | } |
| 335 | |
| 336 | bool CompatibilityMatrix::addSystemSdk(CompatibilityMatrix* other, std::string* /* error */) { |
| 337 | this->device.mSystemSdk.addAll(&other->device.mSystemSdk); |
| 338 | return true; |
| 339 | } |
| 340 | |
Yifan Hong | fb7469c | 2017-04-05 19:15:21 -0700 | [diff] [blame] | 341 | bool operator==(const CompatibilityMatrix &lft, const CompatibilityMatrix &rgt) { |
Yifan Hong | 2027a49 | 2017-12-11 15:21:19 -0800 | [diff] [blame] | 342 | return lft.mType == rgt.mType && lft.mLevel == rgt.mLevel && lft.mHals == rgt.mHals && |
| 343 | lft.mXmlFiles == rgt.mXmlFiles && |
Yifan Hong | feb454e | 2018-01-09 16:16:40 -0800 | [diff] [blame] | 344 | (lft.mType != SchemaType::DEVICE || |
| 345 | ( |
Yifan Hong | 0f529fa | 2018-01-10 14:51:59 -0800 | [diff] [blame] | 346 | #pragma clang diagnostic push |
| 347 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
Yifan Hong | feb454e | 2018-01-09 16:16:40 -0800 | [diff] [blame] | 348 | lft.device.mVndk == rgt.device.mVndk && |
Yifan Hong | 0f529fa | 2018-01-10 14:51:59 -0800 | [diff] [blame] | 349 | #pragma clang diagnostic pop |
Yifan Hong | a28729e | 2018-01-17 13:40:35 -0800 | [diff] [blame] | 350 | lft.device.mVendorNdk == rgt.device.mVendorNdk && |
| 351 | lft.device.mSystemSdk == rgt.device.mSystemSdk)) && |
Yifan Hong | d485790 | 2017-06-13 14:13:56 -0700 | [diff] [blame] | 352 | (lft.mType != SchemaType::FRAMEWORK || |
| 353 | (lft.framework.mKernels == rgt.framework.mKernels && |
| 354 | lft.framework.mSepolicy == rgt.framework.mSepolicy && |
| 355 | lft.framework.mAvbMetaVersion == rgt.framework.mAvbMetaVersion)); |
Yifan Hong | fb7469c | 2017-04-05 19:15:21 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 358 | std::unique_ptr<CompatibilityMatrix> CompatibilityMatrix::combine( |
| 359 | Level deviceLevel, std::vector<Named<CompatibilityMatrix>>* matrices, std::string* error) { |
| 360 | // Check type. |
| 361 | for (const auto& e : *matrices) { |
| 362 | if (e.object.type() != SchemaType::FRAMEWORK) { |
| 363 | if (error) { |
| 364 | *error = "File \"" + e.name + "\" is not a framework compatibility matrix."; |
| 365 | return nullptr; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // Matrices with unspecified (empty) level are auto-filled with deviceLevel. |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 371 | for (auto& e : *matrices) { |
Yifan Hong | ffcaf99 | 2018-01-09 17:08:51 -0800 | [diff] [blame] | 372 | if (e.object.level() == Level::UNSPECIFIED) { |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 373 | e.object.mLevel = deviceLevel; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 377 | // Add from low to high FCM version so that optional <kernel> requirements are added correctly. |
| 378 | // See comment in addAllAsOptional. |
| 379 | std::sort(matrices->begin(), matrices->end(), |
| 380 | [](const auto& x, const auto& y) { return x.object.level() < y.object.level(); }); |
| 381 | |
| 382 | auto baseMatrix = std::make_unique<CompatibilityMatrix>(); |
| 383 | baseMatrix->mLevel = deviceLevel; |
| 384 | baseMatrix->mType = SchemaType::FRAMEWORK; |
| 385 | |
| 386 | std::vector<std::string> parsedFiles; |
| 387 | for (auto& e : *matrices) { |
| 388 | if (e.object.level() < deviceLevel) { |
| 389 | continue; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 390 | } |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 391 | |
| 392 | bool success = false; |
| 393 | if (e.object.level() == deviceLevel) { |
| 394 | success = baseMatrix->addAll(&e, error); |
| 395 | } else { |
| 396 | success = baseMatrix->addAllAsOptional(&e, error); |
| 397 | } |
| 398 | if (!success) { |
| 399 | if (error) { |
| 400 | *error = "Conflict when merging \"" + e.name + "\": " + *error + "\n" + |
| 401 | "Previous files:\n" + base::Join(parsedFiles, "\n"); |
| 402 | } |
| 403 | return nullptr; |
| 404 | } |
| 405 | parsedFiles.push_back(e.name); |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 406 | } |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 407 | |
| 408 | return baseMatrix; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 409 | } |
| 410 | |
Yifan Hong | 5a51ea5 | 2019-04-22 14:54:57 -0700 | [diff] [blame] | 411 | std::unique_ptr<CompatibilityMatrix> CompatibilityMatrix::combineDeviceMatrices( |
| 412 | std::vector<Named<CompatibilityMatrix>>* matrices, std::string* error) { |
| 413 | auto baseMatrix = std::make_unique<CompatibilityMatrix>(); |
| 414 | baseMatrix->mType = SchemaType::DEVICE; |
| 415 | |
| 416 | std::vector<std::string> parsedFiles; |
| 417 | for (auto& e : *matrices) { |
| 418 | bool success = baseMatrix->addAll(&e, error); |
| 419 | if (!success) { |
| 420 | if (error) { |
| 421 | *error = "Conflict when merging \"" + e.name + "\": " + *error + "\n" + |
| 422 | "Previous files:\n" + base::Join(parsedFiles, "\n"); |
| 423 | } |
| 424 | return nullptr; |
| 425 | } |
| 426 | parsedFiles.push_back(e.name); |
| 427 | } |
| 428 | return baseMatrix; |
| 429 | } |
| 430 | |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 431 | bool CompatibilityMatrix::addAll(Named<CompatibilityMatrix>* inputMatrix, std::string* error) { |
| 432 | if (!addAllHals(&inputMatrix->object, error) || !addAllXmlFiles(&inputMatrix->object, error) || |
| 433 | !addAllKernels(&inputMatrix->object, error) || !addSepolicy(&inputMatrix->object, error) || |
Yifan Hong | 5a51ea5 | 2019-04-22 14:54:57 -0700 | [diff] [blame] | 434 | !addAvbMetaVersion(&inputMatrix->object, error) || !addVndk(&inputMatrix->object, error) || |
| 435 | !addVendorNdk(&inputMatrix->object, error) || !addSystemSdk(&inputMatrix->object, error)) { |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 436 | if (error) { |
| 437 | *error = "File \"" + inputMatrix->name + "\" cannot be added: " + *error + "."; |
Yifan Hong | d6de7f6 | 2018-04-26 18:40:02 -0700 | [diff] [blame] | 438 | } |
Yifan Hong | 5390e3b | 2019-01-31 14:13:00 -0800 | [diff] [blame] | 439 | return false; |
Yifan Hong | d6de7f6 | 2018-04-26 18:40:02 -0700 | [diff] [blame] | 440 | } |
| 441 | return true; |
| 442 | } |
| 443 | |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 444 | bool CompatibilityMatrix::addAllAsOptional(Named<CompatibilityMatrix>* inputMatrix, |
| 445 | std::string* error) { |
| 446 | if (!addAllHalsAsOptional(&inputMatrix->object, error) || |
| 447 | !addAllXmlFilesAsOptional(&inputMatrix->object, error) || |
| 448 | !addAllKernelsAsOptional(&inputMatrix->object, error)) { |
| 449 | if (error) { |
| 450 | *error = "File \"" + inputMatrix->name + "\" cannot be added: " + *error; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 451 | } |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 452 | return false; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 453 | } |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 454 | // ignore <sepolicy> requirement from higher level |
| 455 | // ignore <avb> requirement from higher level |
| 456 | return true; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 457 | } |
| 458 | |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 459 | bool CompatibilityMatrix::forEachInstanceOfVersion( |
Yifan Hong | fc7ad27 | 2019-09-10 19:49:15 -0700 | [diff] [blame] | 460 | HalFormat format, const std::string& package, const Version& expectVersion, |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 461 | const std::function<bool(const MatrixInstance&)>& func) const { |
| 462 | for (const MatrixHal* hal : getHals(package)) { |
| 463 | bool cont = hal->forEachInstance([&](const MatrixInstance& matrixInstance) { |
Yifan Hong | fc7ad27 | 2019-09-10 19:49:15 -0700 | [diff] [blame] | 464 | if (matrixInstance.format() == format && |
| 465 | matrixInstance.versionRange().contains(expectVersion)) { |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 466 | return func(matrixInstance); |
Yifan Hong | e3a9234 | 2018-01-25 17:00:16 -0800 | [diff] [blame] | 467 | } |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 468 | return true; |
| 469 | }); |
| 470 | if (!cont) return false; |
Yifan Hong | e3a9234 | 2018-01-25 17:00:16 -0800 | [diff] [blame] | 471 | } |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 472 | return true; |
Yifan Hong | e3a9234 | 2018-01-25 17:00:16 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Yifan Hong | 1dc5a47 | 2019-09-10 19:40:06 -0700 | [diff] [blame] | 475 | bool CompatibilityMatrix::matchInstance(HalFormat format, const std::string& halName, |
| 476 | const Version& version, const std::string& interfaceName, |
Yifan Hong | def7e7f | 2018-03-20 13:27:36 -0700 | [diff] [blame] | 477 | const std::string& instance) const { |
| 478 | bool found = false; |
Yifan Hong | 1dc5a47 | 2019-09-10 19:40:06 -0700 | [diff] [blame] | 479 | (void)forEachInstanceOfInterface(format, halName, version, interfaceName, |
Yifan Hong | def7e7f | 2018-03-20 13:27:36 -0700 | [diff] [blame] | 480 | [&found, &instance](const auto& e) { |
| 481 | found |= (e.matchInstance(instance)); |
| 482 | return !found; // if not found, continue |
| 483 | }); |
| 484 | return found; |
| 485 | } |
| 486 | |
Yifan Hong | 52b7fae | 2018-05-22 16:21:31 -0700 | [diff] [blame] | 487 | std::string CompatibilityMatrix::getVendorNdkVersion() const { |
| 488 | return type() == SchemaType::DEVICE ? device.mVendorNdk.version() : ""; |
| 489 | } |
| 490 | |
Yifan Hong | 1e8febd | 2019-08-07 16:17:19 -0700 | [diff] [blame] | 491 | Level CompatibilityMatrix::getSourceMatrixLevel(const MatrixKernel* matrixKernel) const { |
| 492 | CHECK(std::find_if(framework.mKernels.begin(), framework.mKernels.end(), |
| 493 | [matrixKernel](const auto& e) { return &e == matrixKernel; }) != |
| 494 | framework.mKernels.end()); |
| 495 | Level ret = matrixKernel->getSourceMatrixLevel(); |
| 496 | if (ret != Level::UNSPECIFIED) return ret; |
| 497 | return level(); |
| 498 | } |
| 499 | |
Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 500 | } // namespace vintf |
| 501 | } // namespace android |