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