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