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