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