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