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