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> |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 22 | #include <sstream> |
| 23 | #include <string> |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 24 | #include <unordered_map> |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 25 | |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 26 | #include <android-base/file.h> |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 27 | #include <android-base/parseint.h> |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 28 | |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 29 | #include <vintf/AssembleVintf.h> |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 30 | #include <vintf/KernelConfigParser.h> |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 31 | #include <vintf/parse_string.h> |
| 32 | #include <vintf/parse_xml.h> |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 33 | #include "utils.h" |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 34 | |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 35 | #define BUFFER_SIZE sysconf(_SC_PAGESIZE) |
| 36 | |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 37 | namespace android { |
| 38 | namespace vintf { |
| 39 | |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 40 | static const std::string gConfigPrefix = "android-base-"; |
| 41 | static const std::string gConfigSuffix = ".cfg"; |
| 42 | static const std::string gBaseConfig = "android-base.cfg"; |
| 43 | |
Yifan Hong | aa219f5 | 2017-12-18 18:51:59 -0800 | [diff] [blame] | 44 | // An input stream with a name. |
| 45 | // The input stream may be an actual file, or a stringstream for testing. |
| 46 | // It takes ownership on the istream. |
| 47 | class NamedIstream { |
| 48 | public: |
| 49 | NamedIstream(const std::string& name, std::unique_ptr<std::istream>&& stream) |
| 50 | : mName(name), mStream(std::move(stream)) {} |
| 51 | const std::string& name() const { return mName; } |
| 52 | std::istream& stream() { return *mStream; } |
| 53 | |
| 54 | private: |
| 55 | std::string mName; |
| 56 | std::unique_ptr<std::istream> mStream; |
| 57 | }; |
| 58 | |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 59 | /** |
| 60 | * Slurps the device manifest file and add build time flag to it. |
| 61 | */ |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 62 | class AssembleVintfImpl : public AssembleVintf { |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 63 | using Condition = std::unique_ptr<KernelConfig>; |
| 64 | using ConditionedConfig = std::pair<Condition, std::vector<KernelConfig> /* configs */>; |
| 65 | |
| 66 | public: |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 67 | void setFakeEnv(const std::string& key, const std::string& value) { mFakeEnv[key] = value; } |
| 68 | |
| 69 | std::string getEnv(const std::string& key) const { |
| 70 | auto it = mFakeEnv.find(key); |
| 71 | if (it != mFakeEnv.end()) { |
| 72 | return it->second; |
| 73 | } |
| 74 | const char* envValue = getenv(key.c_str()); |
| 75 | return envValue != nullptr ? std::string(envValue) : std::string(); |
| 76 | } |
| 77 | |
| 78 | template <typename T> |
| 79 | bool getFlag(const std::string& key, T* value) const { |
| 80 | std::string envValue = getEnv(key); |
| 81 | if (envValue.empty()) { |
| 82 | std::cerr << "Warning: " << key << " is missing, defaulted to " << (*value) << "." |
Yifan Hong | 488e16a | 2017-07-11 13:50:41 -0700 | [diff] [blame] | 83 | << std::endl; |
| 84 | return true; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | if (!parse(envValue, value)) { |
| 88 | std::cerr << "Cannot parse " << envValue << "." << std::endl; |
| 89 | return false; |
| 90 | } |
| 91 | return true; |
| 92 | } |
| 93 | |
Yifan Hong | eff0466 | 2017-12-18 16:27:24 -0800 | [diff] [blame] | 94 | /** |
| 95 | * Set *out to environment variable if *out is not a dummy value (i.e. default constructed). |
| 96 | */ |
| 97 | template <typename T> |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 98 | bool getFlagIfUnset(const std::string& envKey, T* out) const { |
Yifan Hong | eff0466 | 2017-12-18 16:27:24 -0800 | [diff] [blame] | 99 | bool hasExistingValue = !(*out == T{}); |
| 100 | |
| 101 | bool hasEnvValue = false; |
| 102 | T envValue; |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 103 | std::string envStrValue = getEnv(envKey); |
| 104 | if (!envStrValue.empty()) { |
| 105 | if (!parse(envStrValue, &envValue)) { |
| 106 | std::cerr << "Cannot parse " << envValue << "." << std::endl; |
Yifan Hong | eff0466 | 2017-12-18 16:27:24 -0800 | [diff] [blame] | 107 | return false; |
| 108 | } |
| 109 | hasEnvValue = true; |
| 110 | } |
| 111 | |
| 112 | if (hasExistingValue) { |
| 113 | if (hasEnvValue) { |
| 114 | std::cerr << "Warning: cannot override existing value " << *out << " with " |
| 115 | << envKey << " (which is " << envValue << ")." << std::endl; |
| 116 | } |
| 117 | return false; |
| 118 | } |
| 119 | if (!hasEnvValue) { |
| 120 | std::cerr << "Warning: " << envKey << " is not specified. Default to " << T{} << "." |
| 121 | << std::endl; |
| 122 | return false; |
| 123 | } |
| 124 | *out = envValue; |
| 125 | return true; |
| 126 | } |
| 127 | |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 128 | bool getBooleanFlag(const std::string& key) const { return getEnv(key) == std::string("true"); } |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 129 | |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 130 | size_t getIntegerFlag(const std::string& key, size_t defaultValue = 0) const { |
| 131 | std::string envValue = getEnv(key); |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 132 | if (envValue.empty()) { |
| 133 | return defaultValue; |
| 134 | } |
| 135 | size_t value; |
| 136 | if (!base::ParseUint(envValue, &value)) { |
| 137 | std::cerr << "Error: " << key << " must be a number." << std::endl; |
| 138 | return defaultValue; |
| 139 | } |
| 140 | return value; |
| 141 | } |
| 142 | |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 143 | static std::string read(std::basic_istream<char>& is) { |
| 144 | std::stringstream ss; |
| 145 | ss << is.rdbuf(); |
| 146 | return ss.str(); |
| 147 | } |
| 148 | |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 149 | static bool isCommonConfig(const std::string& path) { |
| 150 | return ::android::base::Basename(path) == gBaseConfig; |
| 151 | } |
| 152 | |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 153 | static Level convertFromApiLevel(size_t apiLevel) { |
| 154 | if (apiLevel < 26) { |
| 155 | return Level::LEGACY; |
| 156 | } else if (apiLevel == 26) { |
| 157 | return Level::O; |
| 158 | } else if (apiLevel == 27) { |
| 159 | return Level::O_MR1; |
| 160 | } else { |
| 161 | return Level::UNSPECIFIED; |
| 162 | } |
| 163 | } |
| 164 | |
Yifan Hong | 079ec24 | 2017-08-25 18:53:38 -0700 | [diff] [blame] | 165 | // nullptr on any error, otherwise the condition. |
| 166 | static Condition generateCondition(const std::string& path) { |
| 167 | std::string fname = ::android::base::Basename(path); |
| 168 | if (fname.size() <= gConfigPrefix.size() + gConfigSuffix.size() || |
| 169 | !std::equal(gConfigPrefix.begin(), gConfigPrefix.end(), fname.begin()) || |
| 170 | !std::equal(gConfigSuffix.rbegin(), gConfigSuffix.rend(), fname.rbegin())) { |
| 171 | return nullptr; |
| 172 | } |
| 173 | |
| 174 | std::string sub = fname.substr(gConfigPrefix.size(), |
| 175 | fname.size() - gConfigPrefix.size() - gConfigSuffix.size()); |
| 176 | if (sub.empty()) { |
| 177 | return nullptr; // should not happen |
| 178 | } |
| 179 | for (size_t i = 0; i < sub.size(); ++i) { |
| 180 | if (sub[i] == '-') { |
| 181 | sub[i] = '_'; |
| 182 | continue; |
| 183 | } |
| 184 | if (isalnum(sub[i])) { |
| 185 | sub[i] = toupper(sub[i]); |
| 186 | continue; |
| 187 | } |
| 188 | std::cerr << "'" << fname << "' (in " << path |
| 189 | << ") is not a valid kernel config file name. Must match regex: " |
| 190 | << "android-base(-[0-9a-zA-Z-]+)?\\.cfg" << std::endl; |
| 191 | return nullptr; |
| 192 | } |
| 193 | sub.insert(0, "CONFIG_"); |
| 194 | return std::make_unique<KernelConfig>(std::move(sub), Tristate::YES); |
| 195 | } |
| 196 | |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 197 | static bool parseFileForKernelConfigs(std::basic_istream<char>& stream, |
| 198 | std::vector<KernelConfig>* out) { |
Yifan Hong | 02e9400 | 2017-07-10 15:41:56 -0700 | [diff] [blame] | 199 | KernelConfigParser parser(true /* processComments */, true /* relaxedFormat */); |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 200 | std::string content = read(stream); |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 201 | status_t err = parser.process(content.c_str(), content.size()); |
| 202 | if (err != OK) { |
Yifan Hong | ae53a0e | 2017-07-07 15:19:06 -0700 | [diff] [blame] | 203 | std::cerr << parser.error(); |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 204 | return false; |
| 205 | } |
| 206 | err = parser.finish(); |
| 207 | if (err != OK) { |
Yifan Hong | ae53a0e | 2017-07-07 15:19:06 -0700 | [diff] [blame] | 208 | std::cerr << parser.error(); |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 209 | return false; |
| 210 | } |
| 211 | |
| 212 | for (auto& configPair : parser.configs()) { |
| 213 | out->push_back({}); |
| 214 | KernelConfig& config = out->back(); |
| 215 | config.first = std::move(configPair.first); |
| 216 | if (!parseKernelConfigTypedValue(configPair.second, &config.second)) { |
| 217 | std::cerr << "Unknown value type for key = '" << config.first << "', value = '" |
| 218 | << configPair.second << "'\n"; |
| 219 | return false; |
| 220 | } |
| 221 | } |
| 222 | return true; |
| 223 | } |
| 224 | |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 225 | static bool parseFilesForKernelConfigs(std::vector<NamedIstream>* streams, |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 226 | std::vector<ConditionedConfig>* out) { |
| 227 | out->clear(); |
| 228 | ConditionedConfig commonConfig; |
| 229 | bool foundCommonConfig = false; |
Steve Muckle | 0bef868 | 2017-07-31 15:47:15 -0700 | [diff] [blame] | 230 | bool ret = true; |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 231 | |
| 232 | for (auto& namedStream : *streams) { |
| 233 | if (isCommonConfig(namedStream.name())) { |
| 234 | ret &= parseFileForKernelConfigs(namedStream.stream(), &commonConfig.second); |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 235 | foundCommonConfig = true; |
| 236 | } else { |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 237 | Condition condition = generateCondition(namedStream.name()); |
Yifan Hong | 079ec24 | 2017-08-25 18:53:38 -0700 | [diff] [blame] | 238 | ret &= (condition != nullptr); |
| 239 | |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 240 | std::vector<KernelConfig> kernelConfigs; |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 241 | if ((ret &= parseFileForKernelConfigs(namedStream.stream(), &kernelConfigs))) |
Yifan Hong | 079ec24 | 2017-08-25 18:53:38 -0700 | [diff] [blame] | 242 | out->emplace_back(std::move(condition), std::move(kernelConfigs)); |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 243 | } |
Steve Muckle | 0bef868 | 2017-07-31 15:47:15 -0700 | [diff] [blame] | 244 | } |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 245 | |
| 246 | if (!foundCommonConfig) { |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 247 | std::cerr << "No android-base.cfg is found in these paths:" << std::endl; |
| 248 | for (auto& namedStream : *streams) { |
| 249 | std::cerr << " " << namedStream.name() << std::endl; |
| 250 | } |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 251 | } |
| 252 | ret &= foundCommonConfig; |
| 253 | // first element is always common configs (no conditions). |
| 254 | out->insert(out->begin(), std::move(commonConfig)); |
Steve Muckle | 0bef868 | 2017-07-31 15:47:15 -0700 | [diff] [blame] | 255 | return ret; |
| 256 | } |
| 257 | |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 258 | static std::string getFileNameFromPath(std::string path) { |
| 259 | auto idx = path.find_last_of("\\/"); |
| 260 | if (idx != std::string::npos) { |
| 261 | path.erase(0, idx + 1); |
| 262 | } |
| 263 | return path; |
| 264 | } |
| 265 | |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 266 | std::basic_ostream<char>& out() const { return mOutRef == nullptr ? std::cout : *mOutRef; } |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 267 | |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 268 | template <typename S> |
| 269 | using Schemas = std::vector<std::pair<std::string, S>>; |
| 270 | using HalManifests = Schemas<HalManifest>; |
| 271 | using CompatibilityMatrices = Schemas<CompatibilityMatrix>; |
| 272 | |
| 273 | bool assembleHalManifest(HalManifests* halManifests) { |
Yifan Hong | 4650ad8 | 2017-05-01 17:28:02 -0700 | [diff] [blame] | 274 | std::string error; |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 275 | HalManifest* halManifest = &halManifests->front().second; |
| 276 | for (auto it = halManifests->begin() + 1; it != halManifests->end(); ++it) { |
| 277 | const std::string& path = it->first; |
| 278 | HalManifest& halToAdd = it->second; |
| 279 | |
| 280 | if (halToAdd.level() != Level::UNSPECIFIED) { |
| 281 | if (halManifest->level() == Level::UNSPECIFIED) { |
| 282 | halManifest->mLevel = halToAdd.level(); |
| 283 | } else if (halManifest->level() != halToAdd.level()) { |
| 284 | std::cerr << "Inconsistent FCM Version in HAL manifests:" << std::endl |
| 285 | << " File '" << halManifests->front().first << "' has level " |
| 286 | << halManifest->level() << std::endl |
| 287 | << " File '" << path << "' has level " << halToAdd.level() |
| 288 | << std::endl; |
| 289 | return false; |
| 290 | } |
| 291 | } |
| 292 | |
Yifan Hong | ea25dd4 | 2017-12-18 17:03:24 -0800 | [diff] [blame] | 293 | if (!halManifest->addAllHals(&halToAdd, &error)) { |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 294 | std::cerr << "File \"" << path << "\" cannot be added: conflict on HAL \"" << error |
| 295 | << "\" with an existing HAL. See <hal> with the same name " |
| 296 | << "in previously parsed files or previously declared in this file." |
| 297 | << std::endl; |
| 298 | return false; |
| 299 | } |
| 300 | } |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 301 | |
| 302 | if (halManifest->mType == SchemaType::DEVICE) { |
| 303 | if (!getFlag("BOARD_SEPOLICY_VERS", &halManifest->device.mSepolicyVersion)) { |
| 304 | return false; |
| 305 | } |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 306 | if (!setDeviceFcmVersion(halManifest)) { |
| 307 | return false; |
| 308 | } |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | if (mOutputMatrix) { |
| 312 | CompatibilityMatrix generatedMatrix = halManifest->generateCompatibleMatrix(); |
| 313 | if (!halManifest->checkCompatibility(generatedMatrix, &error)) { |
| 314 | std::cerr << "FATAL ERROR: cannot generate a compatible matrix: " << error |
| 315 | << std::endl; |
| 316 | } |
| 317 | out() << "<!-- \n" |
| 318 | " Autogenerated skeleton compatibility matrix. \n" |
| 319 | " Use with caution. Modify it to suit your needs.\n" |
| 320 | " All HALs are set to optional.\n" |
| 321 | " Many entries other than HALs are zero-filled and\n" |
| 322 | " require human attention. \n" |
| 323 | "-->\n" |
Yifan Hong | a2635c4 | 2017-12-12 13:20:33 -0800 | [diff] [blame] | 324 | << gCompatibilityMatrixConverter(generatedMatrix, mSerializeFlags); |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 325 | } else { |
Yifan Hong | a2635c4 | 2017-12-12 13:20:33 -0800 | [diff] [blame] | 326 | out() << gHalManifestConverter(*halManifest, mSerializeFlags); |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 327 | } |
| 328 | out().flush(); |
| 329 | |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 330 | if (mCheckFile != nullptr) { |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 331 | CompatibilityMatrix checkMatrix; |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 332 | if (!gCompatibilityMatrixConverter(&checkMatrix, read(*mCheckFile))) { |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 333 | std::cerr << "Cannot parse check file as a compatibility matrix: " |
| 334 | << gCompatibilityMatrixConverter.lastError() << std::endl; |
| 335 | return false; |
| 336 | } |
| 337 | if (!halManifest->checkCompatibility(checkMatrix, &error)) { |
| 338 | std::cerr << "Not compatible: " << error << std::endl; |
| 339 | return false; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | return true; |
| 344 | } |
| 345 | |
Yifan Hong | e88e167 | 2017-08-24 14:42:54 -0700 | [diff] [blame] | 346 | bool assembleFrameworkCompatibilityMatrixKernels(CompatibilityMatrix* matrix) { |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 347 | for (auto& pair : mKernels) { |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 348 | std::vector<ConditionedConfig> conditionedConfigs; |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 349 | if (!parseFilesForKernelConfigs(&pair.second, &conditionedConfigs)) { |
Yifan Hong | e88e167 | 2017-08-24 14:42:54 -0700 | [diff] [blame] | 350 | return false; |
| 351 | } |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 352 | for (ConditionedConfig& conditionedConfig : conditionedConfigs) { |
Yifan Hong | 48602df | 2017-08-28 13:04:12 -0700 | [diff] [blame] | 353 | MatrixKernel kernel(KernelVersion{pair.first.majorVer, pair.first.minorVer, 0u}, |
| 354 | std::move(conditionedConfig.second)); |
Yifan Hong | 079ec24 | 2017-08-25 18:53:38 -0700 | [diff] [blame] | 355 | if (conditionedConfig.first != nullptr) |
| 356 | kernel.mConditions.push_back(std::move(*conditionedConfig.first)); |
| 357 | matrix->framework.mKernels.push_back(std::move(kernel)); |
Yifan Hong | 9a8b1a7 | 2017-08-25 17:55:33 -0700 | [diff] [blame] | 358 | } |
Yifan Hong | e88e167 | 2017-08-24 14:42:54 -0700 | [diff] [blame] | 359 | } |
| 360 | return true; |
| 361 | } |
| 362 | |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 363 | bool setDeviceFcmVersion(HalManifest* manifest) { |
| 364 | size_t shippingApiLevel = getIntegerFlag("PRODUCT_SHIPPING_API_LEVEL"); |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 365 | |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 366 | if (manifest->level() != Level::UNSPECIFIED) { |
| 367 | return true; |
| 368 | } |
| 369 | if (!getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) { |
| 370 | manifest->mLevel = Level::LEGACY; |
| 371 | return true; |
| 372 | } |
| 373 | // TODO(b/70628538): Do not infer from Shipping API level. |
| 374 | if (shippingApiLevel) { |
| 375 | std::cerr << "Warning: Shipping FCM Version is inferred from Shipping API level. " |
| 376 | << "Declare Shipping FCM Version in device manifest directly." << std::endl; |
| 377 | manifest->mLevel = convertFromApiLevel(shippingApiLevel); |
| 378 | if (manifest->mLevel == Level::UNSPECIFIED) { |
| 379 | std::cerr << "Error: Shipping FCM Version cannot be inferred from Shipping API " |
| 380 | << "level " << shippingApiLevel << "." |
| 381 | << "Declare Shipping FCM Version in device manifest directly." |
| 382 | << std::endl; |
| 383 | return false; |
| 384 | } |
| 385 | return true; |
| 386 | } |
| 387 | // TODO(b/69638851): should be an error if Shipping API level is not defined. |
| 388 | // For now, just leave it empty; when framework compatibility matrix is built, |
| 389 | // lowest FCM Version is assumed. |
| 390 | std::cerr << "Warning: Shipping FCM Version cannot be inferred, because:" << std::endl |
| 391 | << " (1) It is not explicitly declared in device manifest;" << std::endl |
| 392 | << " (2) PRODUCT_ENFORCE_VINTF_MANIFEST is set to true;" << std::endl |
| 393 | << " (3) PRODUCT_SHIPPING_API_LEVEL is undefined." << std::endl |
| 394 | << "Assuming 'unspecified' Shipping FCM Version. " << std::endl |
| 395 | << "To remove this warning, define 'level' attribute in device manifest." |
| 396 | << std::endl; |
| 397 | return true; |
| 398 | } |
| 399 | |
| 400 | Level getLowestFcmVersion(const CompatibilityMatrices& matrices) { |
| 401 | Level ret = Level::UNSPECIFIED; |
| 402 | for (const auto& e : matrices) { |
| 403 | if (ret == Level::UNSPECIFIED || ret > e.second.level()) { |
| 404 | ret = e.second.level(); |
| 405 | } |
| 406 | } |
| 407 | return ret; |
| 408 | } |
| 409 | |
| 410 | bool assembleCompatibilityMatrix(CompatibilityMatrices* matrices) { |
| 411 | std::string error; |
| 412 | CompatibilityMatrix* matrix = nullptr; |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 413 | KernelSepolicyVersion kernelSepolicyVers; |
| 414 | Version sepolicyVers; |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 415 | std::unique_ptr<HalManifest> checkManifest; |
| 416 | if (matrices->front().second.mType == SchemaType::DEVICE) { |
| 417 | matrix = &matrices->front().second; |
| 418 | } |
| 419 | |
| 420 | if (matrices->front().second.mType == SchemaType::FRAMEWORK) { |
| 421 | Level deviceLevel = Level::UNSPECIFIED; |
| 422 | std::vector<std::string> fileList; |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 423 | if (mCheckFile != nullptr) { |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 424 | checkManifest = std::make_unique<HalManifest>(); |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 425 | if (!gHalManifestConverter(checkManifest.get(), read(*mCheckFile))) { |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 426 | std::cerr << "Cannot parse check file as a HAL manifest: " |
| 427 | << gHalManifestConverter.lastError() << std::endl; |
| 428 | return false; |
| 429 | } |
| 430 | deviceLevel = checkManifest->level(); |
| 431 | } |
| 432 | |
| 433 | if (deviceLevel == Level::UNSPECIFIED) { |
| 434 | // For GSI build, legacy devices that do not have a HAL manifest, |
| 435 | // and devices in development, merge all compatibility matrices. |
| 436 | deviceLevel = getLowestFcmVersion(*matrices); |
| 437 | } |
| 438 | |
| 439 | for (auto& e : *matrices) { |
| 440 | if (e.second.level() == deviceLevel) { |
| 441 | fileList.push_back(e.first); |
| 442 | matrix = &e.second; |
| 443 | } |
| 444 | } |
| 445 | if (matrix == nullptr) { |
| 446 | std::cerr << "FATAL ERROR: cannot find matrix with level '" << deviceLevel << "'" |
| 447 | << std::endl; |
| 448 | return false; |
| 449 | } |
| 450 | for (auto& e : *matrices) { |
| 451 | if (e.second.level() <= deviceLevel) { |
| 452 | continue; |
| 453 | } |
| 454 | fileList.push_back(e.first); |
| 455 | if (!matrix->addAllHalsAsOptional(&e.second, &error)) { |
| 456 | std::cerr << "File \"" << e.first << "\" cannot be added: " << error |
| 457 | << ". See <hal> with the same name " |
| 458 | << "in previously parsed files or previously declared in this file." |
| 459 | << std::endl; |
| 460 | return false; |
| 461 | } |
| 462 | } |
| 463 | |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 464 | if (!getFlag("BOARD_SEPOLICY_VERS", &sepolicyVers)) { |
| 465 | return false; |
| 466 | } |
| 467 | if (!getFlag("POLICYVERS", &kernelSepolicyVers)) { |
| 468 | return false; |
| 469 | } |
Yifan Hong | e88e167 | 2017-08-24 14:42:54 -0700 | [diff] [blame] | 470 | |
| 471 | if (!assembleFrameworkCompatibilityMatrixKernels(matrix)) { |
| 472 | return false; |
Yifan Hong | 79efa8a | 2017-07-06 14:10:28 -0700 | [diff] [blame] | 473 | } |
Yifan Hong | e88e167 | 2017-08-24 14:42:54 -0700 | [diff] [blame] | 474 | |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 475 | matrix->framework.mSepolicy = |
| 476 | Sepolicy(kernelSepolicyVers, {{sepolicyVers.majorVer, sepolicyVers.minorVer}}); |
Yifan Hong | 7f6c00c | 2017-07-06 19:50:29 +0000 | [diff] [blame] | 477 | |
| 478 | Version avbMetaVersion; |
| 479 | if (!getFlag("FRAMEWORK_VBMETA_VERSION", &avbMetaVersion)) { |
| 480 | return false; |
| 481 | } |
| 482 | matrix->framework.mAvbMetaVersion = avbMetaVersion; |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 483 | |
| 484 | out() << "<!--" << std::endl; |
| 485 | out() << " Input:" << std::endl; |
| 486 | for (const auto& path : fileList) { |
| 487 | out() << " " << getFileNameFromPath(path) << std::endl; |
| 488 | } |
| 489 | out() << "-->" << std::endl; |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 490 | } |
Yifan Hong | a2635c4 | 2017-12-12 13:20:33 -0800 | [diff] [blame] | 491 | out() << gCompatibilityMatrixConverter(*matrix, mSerializeFlags); |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 492 | out().flush(); |
| 493 | |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 494 | if (checkManifest != nullptr && getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST") && |
| 495 | !checkManifest->checkCompatibility(*matrix, &error)) { |
| 496 | std::cerr << "Not compatible: " << error << std::endl; |
| 497 | return false; |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | return true; |
| 501 | } |
| 502 | |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 503 | enum AssembleStatus { SUCCESS, FAIL_AND_EXIT, TRY_NEXT }; |
| 504 | template <typename Schema, typename AssembleFunc> |
| 505 | AssembleStatus tryAssemble(const XmlConverter<Schema>& converter, const std::string& schemaName, |
| 506 | AssembleFunc assemble) { |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 507 | Schemas<Schema> schemas; |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 508 | Schema schema; |
Yifan Hong | aa219f5 | 2017-12-18 18:51:59 -0800 | [diff] [blame] | 509 | if (!converter(&schema, read(mInFiles.front().stream()))) { |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 510 | return TRY_NEXT; |
| 511 | } |
| 512 | auto firstType = schema.type(); |
Yifan Hong | aa219f5 | 2017-12-18 18:51:59 -0800 | [diff] [blame] | 513 | schemas.emplace_back(mInFiles.front().name(), std::move(schema)); |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 514 | |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 515 | for (auto it = mInFiles.begin() + 1; it != mInFiles.end(); ++it) { |
| 516 | Schema additionalSchema; |
Yifan Hong | aa219f5 | 2017-12-18 18:51:59 -0800 | [diff] [blame] | 517 | const std::string& fileName = it->name(); |
| 518 | if (!converter(&additionalSchema, read(it->stream()))) { |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 519 | std::cerr << "File \"" << fileName << "\" is not a valid " << firstType << " " |
| 520 | << schemaName << " (but the first file is a valid " << firstType << " " |
| 521 | << schemaName << "). Error: " << converter.lastError() << std::endl; |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 522 | return FAIL_AND_EXIT; |
| 523 | } |
| 524 | if (additionalSchema.type() != firstType) { |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 525 | std::cerr << "File \"" << fileName << "\" is a " << additionalSchema.type() << " " |
| 526 | << schemaName << " (but a " << firstType << " " << schemaName |
| 527 | << " is expected)." << std::endl; |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 528 | return FAIL_AND_EXIT; |
| 529 | } |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 530 | |
| 531 | schemas.emplace_back(fileName, std::move(additionalSchema)); |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 532 | } |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 533 | return assemble(&schemas) ? SUCCESS : FAIL_AND_EXIT; |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 536 | bool assemble() override { |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 537 | using std::placeholders::_1; |
| 538 | if (mInFiles.empty()) { |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 539 | std::cerr << "Missing input file." << std::endl; |
| 540 | return false; |
| 541 | } |
| 542 | |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 543 | auto status = tryAssemble(gHalManifestConverter, "manifest", |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 544 | std::bind(&AssembleVintfImpl::assembleHalManifest, this, _1)); |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 545 | if (status == SUCCESS) return true; |
| 546 | if (status == FAIL_AND_EXIT) return false; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 547 | |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 548 | resetInFiles(); |
Yifan Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 549 | |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 550 | status = tryAssemble(gCompatibilityMatrixConverter, "compatibility matrix", |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 551 | std::bind(&AssembleVintfImpl::assembleCompatibilityMatrix, this, _1)); |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 552 | if (status == SUCCESS) return true; |
| 553 | if (status == FAIL_AND_EXIT) return false; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 554 | |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 555 | std::cerr << "Input file has unknown format." << std::endl |
| 556 | << "Error when attempting to convert to manifest: " |
| 557 | << gHalManifestConverter.lastError() << std::endl |
| 558 | << "Error when attempting to convert to compatibility matrix: " |
| 559 | << gCompatibilityMatrixConverter.lastError() << std::endl; |
| 560 | return false; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 561 | } |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 562 | |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 563 | std::ostream& setOutputStream(Ostream&& out) override { |
| 564 | mOutRef = std::move(out); |
| 565 | return *mOutRef; |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 568 | std::istream& addInputStream(const std::string& name, Istream&& in) override { |
| 569 | auto it = mInFiles.emplace(mInFiles.end(), name, std::move(in)); |
| 570 | return it->stream(); |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 573 | std::istream& setCheckInputStream(Istream&& in) override { |
| 574 | mCheckFile = std::move(in); |
| 575 | return *mCheckFile; |
| 576 | } |
| 577 | |
| 578 | bool hasKernelVersion(const Version& kernelVer) const override { |
| 579 | return mKernels.find(kernelVer) != mKernels.end(); |
| 580 | } |
| 581 | |
| 582 | std::istream& addKernelConfigInputStream(const Version& kernelVer, const std::string& name, |
| 583 | Istream&& in) override { |
| 584 | auto&& kernel = mKernels[kernelVer]; |
| 585 | auto it = kernel.emplace(kernel.end(), name, std::move(in)); |
| 586 | return it->stream(); |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 589 | void resetInFiles() { |
| 590 | for (auto& inFile : mInFiles) { |
Yifan Hong | aa219f5 | 2017-12-18 18:51:59 -0800 | [diff] [blame] | 591 | inFile.stream().clear(); |
| 592 | inFile.stream().seekg(0); |
Yifan Hong | bfb3c1d | 2017-05-24 14:38:48 -0700 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 596 | void setOutputMatrix() override { mOutputMatrix = true; } |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 597 | |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 598 | bool setHalsOnly() override { |
Yifan Hong | a2635c4 | 2017-12-12 13:20:33 -0800 | [diff] [blame] | 599 | if (mSerializeFlags) return false; |
| 600 | mSerializeFlags |= SerializeFlag::HALS_ONLY; |
| 601 | return true; |
| 602 | } |
| 603 | |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 604 | bool setNoHals() override { |
Yifan Hong | a2635c4 | 2017-12-12 13:20:33 -0800 | [diff] [blame] | 605 | if (mSerializeFlags) return false; |
| 606 | mSerializeFlags |= SerializeFlag::NO_HALS; |
| 607 | return true; |
| 608 | } |
| 609 | |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 610 | private: |
Yifan Hong | aa219f5 | 2017-12-18 18:51:59 -0800 | [diff] [blame] | 611 | std::vector<NamedIstream> mInFiles; |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 612 | Ostream mOutRef; |
| 613 | Istream mCheckFile; |
Yifan Hong | 9aa6370 | 2017-05-16 16:37:50 -0700 | [diff] [blame] | 614 | bool mOutputMatrix = false; |
Yifan Hong | a2635c4 | 2017-12-12 13:20:33 -0800 | [diff] [blame] | 615 | SerializeFlags mSerializeFlags = SerializeFlag::EVERYTHING; |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 616 | std::map<Version, std::vector<NamedIstream>> mKernels; |
| 617 | std::map<std::string, std::string> mFakeEnv; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 618 | }; |
| 619 | |
Yifan Hong | 8302cea | 2017-12-18 20:17:05 -0800 | [diff] [blame^] | 620 | bool AssembleVintf::openOutFile(const std::string& path) { |
| 621 | return static_cast<std::ofstream&>(setOutputStream(std::make_unique<std::ofstream>(path))) |
| 622 | .is_open(); |
| 623 | } |
| 624 | |
| 625 | bool AssembleVintf::openInFile(const std::string& path) { |
| 626 | return static_cast<std::ifstream&>(addInputStream(path, std::make_unique<std::ifstream>(path))) |
| 627 | .is_open(); |
| 628 | } |
| 629 | |
| 630 | bool AssembleVintf::openCheckFile(const std::string& path) { |
| 631 | return static_cast<std::ifstream&>(setCheckInputStream(std::make_unique<std::ifstream>(path))) |
| 632 | .is_open(); |
| 633 | } |
| 634 | |
| 635 | bool AssembleVintf::addKernel(const std::string& kernelArg) { |
| 636 | auto tokens = details::tokenize(kernelArg); |
| 637 | if (tokens.size() <= 1) { |
| 638 | std::cerr << "Unrecognized --kernel option '" << kernelArg << "'" << std::endl; |
| 639 | return false; |
| 640 | } |
| 641 | Version kernelVer; |
| 642 | if (!parse(tokens.front(), &kernelVer)) { |
| 643 | std::cerr << "Unrecognized kernel version '" << tokens.front() << "'" << std::endl; |
| 644 | return false; |
| 645 | } |
| 646 | if (hasKernelVersion(kernelVer)) { |
| 647 | std::cerr << "Multiple --kernel for " << kernelVer << " is specified." << std::endl; |
| 648 | return false; |
| 649 | } |
| 650 | for (auto it = tokens.begin() + 1; it != tokens.end(); ++it) { |
| 651 | bool opened = |
| 652 | static_cast<std::ifstream&>( |
| 653 | addKernelConfigInputStream(kernelVer, *it, std::make_unique<std::ifstream>(*it))) |
| 654 | .is_open(); |
| 655 | if (!opened) { |
| 656 | std::cerr << "Cannot open file '" << *it << "'." << std::endl; |
| 657 | return false; |
| 658 | } |
| 659 | } |
| 660 | return true; |
| 661 | } |
| 662 | |
| 663 | std::unique_ptr<AssembleVintf> AssembleVintf::newInstance() { |
| 664 | return std::make_unique<AssembleVintfImpl>(); |
| 665 | } |
| 666 | |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 667 | } // namespace vintf |
| 668 | } // namespace android |