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