blob: bf9d530e566a50b9a83bc188af5b7667529ca4c7 [file] [log] [blame]
Yifan Hong4d18bcc2017-04-07 21:47:16 +00001/*
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 Hong4d18bcc2017-04-07 21:47:16 +000022#include <sstream>
23#include <string>
Yifan Hong8302cea2017-12-18 20:17:05 -080024#include <unordered_map>
Yifan Hong4d18bcc2017-04-07 21:47:16 +000025
Yifan Hong9a8b1a72017-08-25 17:55:33 -070026#include <android-base/file.h>
Yifan Hongdbe9db32017-12-11 19:06:11 -080027#include <android-base/parseint.h>
Yifan Hongb83e56b2018-01-09 14:36:30 -080028#include <android-base/strings.h>
Yifan Hong9a8b1a72017-08-25 17:55:33 -070029
Yifan Hong8302cea2017-12-18 20:17:05 -080030#include <vintf/AssembleVintf.h>
Yifan Hong79efa8a2017-07-06 14:10:28 -070031#include <vintf/KernelConfigParser.h>
Yifan Hong4d18bcc2017-04-07 21:47:16 +000032#include <vintf/parse_string.h>
33#include <vintf/parse_xml.h>
Yifan Hong8302cea2017-12-18 20:17:05 -080034#include "utils.h"
Yifan Hong4d18bcc2017-04-07 21:47:16 +000035
Yifan Hong79efa8a2017-07-06 14:10:28 -070036#define BUFFER_SIZE sysconf(_SC_PAGESIZE)
37
Yifan Hong4d18bcc2017-04-07 21:47:16 +000038namespace android {
39namespace vintf {
40
Yifan Hong9a8b1a72017-08-25 17:55:33 -070041static const std::string gConfigPrefix = "android-base-";
Steve Mucklead6aa912018-07-23 15:44:01 -070042static const std::string gConfigSuffix = ".config";
43static const std::string gBaseConfig = "android-base.config";
Yifan Hong9a8b1a72017-08-25 17:55:33 -070044
Yifan Hongaa219f52017-12-18 18:51:59 -080045// 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.
48class NamedIstream {
49 public:
50 NamedIstream(const std::string& name, std::unique_ptr<std::istream>&& stream)
51 : mName(name), mStream(std::move(stream)) {}
52 const std::string& name() const { return mName; }
53 std::istream& stream() { return *mStream; }
54
55 private:
56 std::string mName;
57 std::unique_ptr<std::istream> mStream;
58};
59
Yifan Hong4d18bcc2017-04-07 21:47:16 +000060/**
61 * Slurps the device manifest file and add build time flag to it.
62 */
Yifan Hong8302cea2017-12-18 20:17:05 -080063class AssembleVintfImpl : public AssembleVintf {
Yifan Hong9a8b1a72017-08-25 17:55:33 -070064 using Condition = std::unique_ptr<KernelConfig>;
65 using ConditionedConfig = std::pair<Condition, std::vector<KernelConfig> /* configs */>;
66
67 public:
Yifan Hong8302cea2017-12-18 20:17:05 -080068 void setFakeEnv(const std::string& key, const std::string& value) { mFakeEnv[key] = value; }
69
70 std::string getEnv(const std::string& key) const {
71 auto it = mFakeEnv.find(key);
72 if (it != mFakeEnv.end()) {
73 return it->second;
74 }
75 const char* envValue = getenv(key.c_str());
76 return envValue != nullptr ? std::string(envValue) : std::string();
77 }
78
Yifan Honga28729e2018-01-17 13:40:35 -080079 // Get environment variable and split with space.
80 std::vector<std::string> getEnvList(const std::string& key) const {
81 std::vector<std::string> ret;
82 for (auto&& v : base::Split(getEnv(key), " ")) {
83 v = base::Trim(v);
84 if (!v.empty()) {
85 ret.push_back(v);
86 }
87 }
88 return ret;
89 }
90
Yifan Hong8302cea2017-12-18 20:17:05 -080091 template <typename T>
Yifan Hong4dec91d2018-08-13 12:37:47 -070092 bool getFlag(const std::string& key, T* value, bool log = true) const {
Yifan Hong8302cea2017-12-18 20:17:05 -080093 std::string envValue = getEnv(key);
94 if (envValue.empty()) {
Yifan Hong4dec91d2018-08-13 12:37:47 -070095 if (log) {
96 std::cerr << "Warning: " << key << " is missing, defaulted to " << (*value) << "."
97 << std::endl;
98 }
Yifan Hong488e16a2017-07-11 13:50:41 -070099 return true;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000100 }
101
102 if (!parse(envValue, value)) {
103 std::cerr << "Cannot parse " << envValue << "." << std::endl;
104 return false;
105 }
106 return true;
107 }
108
Yifan Hongeff04662017-12-18 16:27:24 -0800109 /**
Yifan Hong0f7f7282018-02-02 13:05:43 -0800110 * Set *out to environment variable only if *out is a dummy value (i.e. default constructed).
Yifan Hongce355902019-06-26 17:06:52 +0000111 * Return false if a fatal error has occurred:
112 * - The environment variable has an unknown format
113 * - The value of the environment variable does not match a predefined variable in the files
Yifan Hongeff04662017-12-18 16:27:24 -0800114 */
115 template <typename T>
Yifan Hongce355902019-06-26 17:06:52 +0000116 bool getFlagIfUnset(const std::string& envKey, T* out) const {
Yifan Hongeff04662017-12-18 16:27:24 -0800117 bool hasExistingValue = !(*out == T{});
118
119 bool hasEnvValue = false;
120 T envValue;
Yifan Hong8302cea2017-12-18 20:17:05 -0800121 std::string envStrValue = getEnv(envKey);
122 if (!envStrValue.empty()) {
123 if (!parse(envStrValue, &envValue)) {
Yifan Hongb36e85f2019-06-26 17:06:52 +0000124 std::cerr << "Cannot parse " << envValue << "." << std::endl;
Yifan Hongce355902019-06-26 17:06:52 +0000125 return false;
Yifan Hongeff04662017-12-18 16:27:24 -0800126 }
127 hasEnvValue = true;
128 }
129
130 if (hasExistingValue) {
Yifan Hongb36e85f2019-06-26 17:06:52 +0000131 if (hasEnvValue && (*out != envValue)) {
Yifan Hongce355902019-06-26 17:06:52 +0000132 std::cerr << "Cannot override existing value " << *out << " with " << envKey
133 << " (which is " << envValue << ")." << std::endl;
134 return false;
Yifan Hongeff04662017-12-18 16:27:24 -0800135 }
Yifan Hongce355902019-06-26 17:06:52 +0000136 return true;
Yifan Hongeff04662017-12-18 16:27:24 -0800137 }
Yifan Hongb36e85f2019-06-26 17:06:52 +0000138 if (hasEnvValue) {
139 *out = envValue;
Yifan Hongeff04662017-12-18 16:27:24 -0800140 }
Yifan Hongce355902019-06-26 17:06:52 +0000141 return true;
Yifan Hongeff04662017-12-18 16:27:24 -0800142 }
143
Yifan Hong8302cea2017-12-18 20:17:05 -0800144 bool getBooleanFlag(const std::string& key) const { return getEnv(key) == std::string("true"); }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800145
Yifan Hong8302cea2017-12-18 20:17:05 -0800146 size_t getIntegerFlag(const std::string& key, size_t defaultValue = 0) const {
147 std::string envValue = getEnv(key);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800148 if (envValue.empty()) {
149 return defaultValue;
150 }
151 size_t value;
152 if (!base::ParseUint(envValue, &value)) {
153 std::cerr << "Error: " << key << " must be a number." << std::endl;
154 return defaultValue;
155 }
156 return value;
157 }
158
Yifan Hong4650ad82017-05-01 17:28:02 -0700159 static std::string read(std::basic_istream<char>& is) {
160 std::stringstream ss;
161 ss << is.rdbuf();
162 return ss.str();
163 }
164
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700165 static bool isCommonConfig(const std::string& path) {
166 return ::android::base::Basename(path) == gBaseConfig;
167 }
168
Yifan Hong079ec242017-08-25 18:53:38 -0700169 // nullptr on any error, otherwise the condition.
170 static Condition generateCondition(const std::string& path) {
171 std::string fname = ::android::base::Basename(path);
172 if (fname.size() <= gConfigPrefix.size() + gConfigSuffix.size() ||
173 !std::equal(gConfigPrefix.begin(), gConfigPrefix.end(), fname.begin()) ||
174 !std::equal(gConfigSuffix.rbegin(), gConfigSuffix.rend(), fname.rbegin())) {
175 return nullptr;
176 }
177
178 std::string sub = fname.substr(gConfigPrefix.size(),
179 fname.size() - gConfigPrefix.size() - gConfigSuffix.size());
180 if (sub.empty()) {
181 return nullptr; // should not happen
182 }
183 for (size_t i = 0; i < sub.size(); ++i) {
184 if (sub[i] == '-') {
185 sub[i] = '_';
186 continue;
187 }
188 if (isalnum(sub[i])) {
189 sub[i] = toupper(sub[i]);
190 continue;
191 }
192 std::cerr << "'" << fname << "' (in " << path
193 << ") is not a valid kernel config file name. Must match regex: "
Steve Mucklead6aa912018-07-23 15:44:01 -0700194 << "android-base(-[0-9a-zA-Z-]+)?\\" << gConfigSuffix
195 << std::endl;
Yifan Hong079ec242017-08-25 18:53:38 -0700196 return nullptr;
197 }
198 sub.insert(0, "CONFIG_");
199 return std::make_unique<KernelConfig>(std::move(sub), Tristate::YES);
200 }
201
Yifan Hong8302cea2017-12-18 20:17:05 -0800202 static bool parseFileForKernelConfigs(std::basic_istream<char>& stream,
203 std::vector<KernelConfig>* out) {
Yifan Hong02e94002017-07-10 15:41:56 -0700204 KernelConfigParser parser(true /* processComments */, true /* relaxedFormat */);
Yifan Honga45f77d2018-12-03 16:42:52 -0800205 status_t err = parser.processAndFinish(read(stream));
Yifan Hong79efa8a2017-07-06 14:10:28 -0700206 if (err != OK) {
Yifan Hongae53a0e2017-07-07 15:19:06 -0700207 std::cerr << parser.error();
Yifan Hong79efa8a2017-07-06 14:10:28 -0700208 return false;
209 }
210
211 for (auto& configPair : parser.configs()) {
212 out->push_back({});
213 KernelConfig& config = out->back();
214 config.first = std::move(configPair.first);
215 if (!parseKernelConfigTypedValue(configPair.second, &config.second)) {
216 std::cerr << "Unknown value type for key = '" << config.first << "', value = '"
217 << configPair.second << "'\n";
218 return false;
219 }
220 }
221 return true;
222 }
223
Yifan Hong8302cea2017-12-18 20:17:05 -0800224 static bool parseFilesForKernelConfigs(std::vector<NamedIstream>* streams,
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700225 std::vector<ConditionedConfig>* out) {
226 out->clear();
227 ConditionedConfig commonConfig;
228 bool foundCommonConfig = false;
Steve Muckle0bef8682017-07-31 15:47:15 -0700229 bool ret = true;
Yifan Hong8302cea2017-12-18 20:17:05 -0800230
231 for (auto& namedStream : *streams) {
232 if (isCommonConfig(namedStream.name())) {
233 ret &= parseFileForKernelConfigs(namedStream.stream(), &commonConfig.second);
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700234 foundCommonConfig = true;
235 } else {
Yifan Hong8302cea2017-12-18 20:17:05 -0800236 Condition condition = generateCondition(namedStream.name());
Yifan Hong079ec242017-08-25 18:53:38 -0700237 ret &= (condition != nullptr);
238
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700239 std::vector<KernelConfig> kernelConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800240 if ((ret &= parseFileForKernelConfigs(namedStream.stream(), &kernelConfigs)))
Yifan Hong079ec242017-08-25 18:53:38 -0700241 out->emplace_back(std::move(condition), std::move(kernelConfigs));
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700242 }
Steve Muckle0bef8682017-07-31 15:47:15 -0700243 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700244
245 if (!foundCommonConfig) {
Steve Mucklead6aa912018-07-23 15:44:01 -0700246 std::cerr << "No " << gBaseConfig << " is found in these paths:" << std::endl;
Yifan Hong8302cea2017-12-18 20:17:05 -0800247 for (auto& namedStream : *streams) {
248 std::cerr << " " << namedStream.name() << std::endl;
249 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700250 }
251 ret &= foundCommonConfig;
252 // first element is always common configs (no conditions).
253 out->insert(out->begin(), std::move(commonConfig));
Steve Muckle0bef8682017-07-31 15:47:15 -0700254 return ret;
255 }
256
Yifan Hong8302cea2017-12-18 20:17:05 -0800257 std::basic_ostream<char>& out() const { return mOutRef == nullptr ? std::cout : *mOutRef; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700258
Yifan Hong1044fd12018-02-27 14:51:54 -0800259 // If -c is provided, check it.
260 bool checkDualFile(const HalManifest& manifest, const CompatibilityMatrix& matrix) {
261 if (getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
262 std::string error;
263 if (!manifest.checkCompatibility(matrix, &error)) {
264 std::cerr << "Not compatible: " << error << std::endl;
265 return false;
266 }
267 }
268
269 // Check HALs in device manifest that are not in framework matrix.
270 if (getBooleanFlag("VINTF_ENFORCE_NO_UNUSED_HALS")) {
271 auto unused = manifest.checkUnusedHals(matrix);
272 if (!unused.empty()) {
273 std::cerr << "Error: The following instances are in the device manifest but "
274 << "not specified in framework compatibility matrix: " << std::endl
275 << " " << android::base::Join(unused, "\n ") << std::endl
276 << "Suggested fix:" << std::endl
277 << "1. Check for any typos in device manifest or framework compatibility "
278 << "matrices with FCM version >= " << matrix.level() << "." << std::endl
279 << "2. Add them to any framework compatibility matrix with FCM "
280 << "version >= " << matrix.level() << " where applicable." << std::endl
Yifan Hong09efef32019-01-28 10:59:27 -0800281 << "3. Add them to DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE "
282 << "or DEVICE_PRODUCT_COMPATIBILITY_MATRIX_FILE." << std::endl;
Yifan Hong1044fd12018-02-27 14:51:54 -0800283
284 return false;
285 }
286 }
287 return true;
288 }
289
Yifan Hongdbe9db32017-12-11 19:06:11 -0800290 template <typename S>
Yifan Hongffcaf992018-01-09 17:08:51 -0800291 using Schemas = std::vector<Named<S>>;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800292 using HalManifests = Schemas<HalManifest>;
293 using CompatibilityMatrices = Schemas<CompatibilityMatrix>;
294
Steven Moreland5875afc2018-04-05 13:14:08 -0700295 template <typename M>
296 void outputInputs(const Schemas<M>& inputs) {
297 out() << "<!--" << std::endl;
298 out() << " Input:" << std::endl;
299 for (const auto& e : inputs) {
300 if (!e.name.empty()) {
301 out() << " " << base::Basename(e.name) << std::endl;
302 }
303 }
304 out() << "-->" << std::endl;
305 }
306
Yifan Honga45f77d2018-12-03 16:42:52 -0800307 // Parse --kernel arguments and write to output manifest.
308 bool setDeviceManifestKernel(HalManifest* manifest) {
309 if (mKernels.empty()) {
310 return true;
311 }
312 if (mKernels.size() > 1) {
313 std::cerr << "Warning: multiple --kernel is specified when building device manifest. "
314 << "Only the first one will be used." << std::endl;
315 }
316 auto& kernelArg = *mKernels.begin();
317 const auto& kernelVer = kernelArg.first;
318 auto& kernelConfigFiles = kernelArg.second;
319 // addKernel() guarantees that !kernelConfigFiles.empty().
320 if (kernelConfigFiles.size() > 1) {
321 std::cerr << "Warning: multiple config files are specified in --kernel when building "
322 << "device manfiest. Only the first one will be used." << std::endl;
323 }
324
325 KernelConfigParser parser(true /* processComments */, false /* relaxedFormat */);
326 status_t err = parser.processAndFinish(read(kernelConfigFiles[0].stream()));
327 if (err != OK) {
328 std::cerr << parser.error();
329 return false;
330 }
Yifan Hong08a48712019-12-11 14:56:00 -0800331
332 // Set version and configs in manifest.
333 auto kernel_info = std::make_optional<KernelInfo>();
334 kernel_info->mVersion = kernelVer;
335 kernel_info->mConfigs = parser.configs();
336 std::string error;
337 if (!manifest->mergeKernel(&kernel_info, &error)) {
338 std::cerr << error << "\n";
339 return false;
340 }
341
Yifan Honga45f77d2018-12-03 16:42:52 -0800342 return true;
343 }
344
Yifan Hong08a48712019-12-11 14:56:00 -0800345 void inferDeviceManifestKernelFcm(HalManifest* manifest) {
346 // No target FCM version.
347 if (manifest->level() == Level::UNSPECIFIED) return;
348 // target FCM version < R: leave value untouched.
349 if (manifest->level() < Level::R) return;
350 // No need to infer when <kernel> tag is missing.
351 if (!manifest->kernel().has_value()) return;
352 // Kernel FCM already set.
353 if (manifest->kernel()->level() != Level::UNSPECIFIED) return;
354
355 manifest->device.mKernel->mLevel = manifest->level();
356 }
357
Yifan Hongdbe9db32017-12-11 19:06:11 -0800358 bool assembleHalManifest(HalManifests* halManifests) {
Yifan Hong4650ad82017-05-01 17:28:02 -0700359 std::string error;
Yifan Hongffcaf992018-01-09 17:08:51 -0800360 HalManifest* halManifest = &halManifests->front().object;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800361 for (auto it = halManifests->begin() + 1; it != halManifests->end(); ++it) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800362 const std::string& path = it->name;
Yifan Hong0a35ab12018-06-29 11:32:57 -0700363 HalManifest& manifestToAdd = it->object;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800364
Yifan Hong0a35ab12018-06-29 11:32:57 -0700365 if (manifestToAdd.level() != Level::UNSPECIFIED) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800366 if (halManifest->level() == Level::UNSPECIFIED) {
Yifan Hong0a35ab12018-06-29 11:32:57 -0700367 halManifest->mLevel = manifestToAdd.level();
368 } else if (halManifest->level() != manifestToAdd.level()) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800369 std::cerr << "Inconsistent FCM Version in HAL manifests:" << std::endl
Yifan Hongffcaf992018-01-09 17:08:51 -0800370 << " File '" << halManifests->front().name << "' has level "
Yifan Hongdbe9db32017-12-11 19:06:11 -0800371 << halManifest->level() << std::endl
Yifan Hong0a35ab12018-06-29 11:32:57 -0700372 << " File '" << path << "' has level " << manifestToAdd.level()
Yifan Hongdbe9db32017-12-11 19:06:11 -0800373 << std::endl;
374 return false;
375 }
376 }
377
Yifan Hong4c6962d2019-01-30 15:50:46 -0800378 if (!halManifest->addAll(&manifestToAdd, &error)) {
379 std::cerr << "File \"" << path << "\" cannot be added: " << error << std::endl;
Yifan Hong0a35ab12018-06-29 11:32:57 -0700380 return false;
381 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800382 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700383
384 if (halManifest->mType == SchemaType::DEVICE) {
Yifan Hongce355902019-06-26 17:06:52 +0000385 if (!getFlagIfUnset("BOARD_SEPOLICY_VERS", &halManifest->device.mSepolicyVersion)) {
386 return false;
387 }
Steven Morelandb1392772018-05-01 11:21:35 -0700388
Yifan Hongdbe9db32017-12-11 19:06:11 -0800389 if (!setDeviceFcmVersion(halManifest)) {
390 return false;
391 }
Yifan Honga45f77d2018-12-03 16:42:52 -0800392
393 if (!setDeviceManifestKernel(halManifest)) {
394 return false;
395 }
Yifan Hong08a48712019-12-11 14:56:00 -0800396
397 inferDeviceManifestKernelFcm(halManifest);
Yifan Hong9aa63702017-05-16 16:37:50 -0700398 }
399
Yifan Hongfeb454e2018-01-09 16:16:40 -0800400 if (halManifest->mType == SchemaType::FRAMEWORK) {
Yifan Honga28729e2018-01-17 13:40:35 -0800401 for (auto&& v : getEnvList("PROVIDED_VNDK_VERSIONS")) {
402 halManifest->framework.mVendorNdks.emplace_back(std::move(v));
403 }
404
Yifan Hong8b8492b2018-01-22 14:08:40 -0800405 for (auto&& v : getEnvList("PLATFORM_SYSTEMSDK_VERSIONS")) {
Yifan Honga28729e2018-01-17 13:40:35 -0800406 halManifest->framework.mSystemSdk.mVersions.emplace(std::move(v));
Yifan Hongfeb454e2018-01-09 16:16:40 -0800407 }
408 }
409
Steven Moreland5875afc2018-04-05 13:14:08 -0700410 outputInputs(*halManifests);
411
Yifan Hong9aa63702017-05-16 16:37:50 -0700412 if (mOutputMatrix) {
413 CompatibilityMatrix generatedMatrix = halManifest->generateCompatibleMatrix();
414 if (!halManifest->checkCompatibility(generatedMatrix, &error)) {
415 std::cerr << "FATAL ERROR: cannot generate a compatible matrix: " << error
416 << std::endl;
417 }
418 out() << "<!-- \n"
419 " Autogenerated skeleton compatibility matrix. \n"
420 " Use with caution. Modify it to suit your needs.\n"
421 " All HALs are set to optional.\n"
422 " Many entries other than HALs are zero-filled and\n"
423 " require human attention. \n"
424 "-->\n"
Yifan Honga2635c42017-12-12 13:20:33 -0800425 << gCompatibilityMatrixConverter(generatedMatrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700426 } else {
Yifan Honga2635c42017-12-12 13:20:33 -0800427 out() << gHalManifestConverter(*halManifest, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700428 }
429 out().flush();
430
Yifan Hong8302cea2017-12-18 20:17:05 -0800431 if (mCheckFile != nullptr) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700432 CompatibilityMatrix checkMatrix;
Yifan Hong94757062018-02-09 16:36:31 -0800433 if (!gCompatibilityMatrixConverter(&checkMatrix, read(*mCheckFile), &error)) {
434 std::cerr << "Cannot parse check file as a compatibility matrix: " << error
435 << std::endl;
Yifan Hong9aa63702017-05-16 16:37:50 -0700436 return false;
437 }
Yifan Hong1044fd12018-02-27 14:51:54 -0800438 if (!checkDualFile(*halManifest, checkMatrix)) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700439 return false;
440 }
441 }
442
443 return true;
444 }
445
Yifan Honge7837b12018-10-11 10:38:57 -0700446 // Parse --kernel arguments and write to output matrix.
Yifan Honge88e1672017-08-24 14:42:54 -0700447 bool assembleFrameworkCompatibilityMatrixKernels(CompatibilityMatrix* matrix) {
Yifan Hong8302cea2017-12-18 20:17:05 -0800448 for (auto& pair : mKernels) {
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700449 std::vector<ConditionedConfig> conditionedConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800450 if (!parseFilesForKernelConfigs(&pair.second, &conditionedConfigs)) {
Yifan Honge88e1672017-08-24 14:42:54 -0700451 return false;
452 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700453 for (ConditionedConfig& conditionedConfig : conditionedConfigs) {
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800454 MatrixKernel kernel(KernelVersion{pair.first}, std::move(conditionedConfig.second));
Yifan Hong079ec242017-08-25 18:53:38 -0700455 if (conditionedConfig.first != nullptr)
456 kernel.mConditions.push_back(std::move(*conditionedConfig.first));
Yifan Honge7837b12018-10-11 10:38:57 -0700457 std::string error;
458 if (!matrix->addKernel(std::move(kernel), &error)) {
459 std::cerr << "Error:" << error << std::endl;
460 return false;
461 };
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700462 }
Yifan Honge88e1672017-08-24 14:42:54 -0700463 }
464 return true;
465 }
466
Yifan Hongdbe9db32017-12-11 19:06:11 -0800467 bool setDeviceFcmVersion(HalManifest* manifest) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700468 // Not needed for generating empty manifest for DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE.
Yifan Hong2eee1982018-03-29 10:55:30 -0700469 if (getBooleanFlag("VINTF_IGNORE_TARGET_FCM_VERSION")) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700470 return true;
471 }
472
Yifan Hongdbe9db32017-12-11 19:06:11 -0800473 size_t shippingApiLevel = getIntegerFlag("PRODUCT_SHIPPING_API_LEVEL");
Yifan Hong9aa63702017-05-16 16:37:50 -0700474
Yifan Hongdbe9db32017-12-11 19:06:11 -0800475 if (manifest->level() != Level::UNSPECIFIED) {
476 return true;
477 }
478 if (!getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
479 manifest->mLevel = Level::LEGACY;
480 return true;
481 }
482 // TODO(b/70628538): Do not infer from Shipping API level.
483 if (shippingApiLevel) {
484 std::cerr << "Warning: Shipping FCM Version is inferred from Shipping API level. "
485 << "Declare Shipping FCM Version in device manifest directly." << std::endl;
Yifan Hongafae1982018-01-11 18:15:24 -0800486 manifest->mLevel = details::convertFromApiLevel(shippingApiLevel);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800487 if (manifest->mLevel == Level::UNSPECIFIED) {
488 std::cerr << "Error: Shipping FCM Version cannot be inferred from Shipping API "
489 << "level " << shippingApiLevel << "."
490 << "Declare Shipping FCM Version in device manifest directly."
491 << std::endl;
492 return false;
493 }
494 return true;
495 }
496 // TODO(b/69638851): should be an error if Shipping API level is not defined.
497 // For now, just leave it empty; when framework compatibility matrix is built,
498 // lowest FCM Version is assumed.
499 std::cerr << "Warning: Shipping FCM Version cannot be inferred, because:" << std::endl
500 << " (1) It is not explicitly declared in device manifest;" << std::endl
501 << " (2) PRODUCT_ENFORCE_VINTF_MANIFEST is set to true;" << std::endl
502 << " (3) PRODUCT_SHIPPING_API_LEVEL is undefined." << std::endl
503 << "Assuming 'unspecified' Shipping FCM Version. " << std::endl
504 << "To remove this warning, define 'level' attribute in device manifest."
505 << std::endl;
506 return true;
507 }
508
509 Level getLowestFcmVersion(const CompatibilityMatrices& matrices) {
510 Level ret = Level::UNSPECIFIED;
511 for (const auto& e : matrices) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800512 if (ret == Level::UNSPECIFIED || ret > e.object.level()) {
513 ret = e.object.level();
Yifan Hongdbe9db32017-12-11 19:06:11 -0800514 }
515 }
516 return ret;
517 }
518
519 bool assembleCompatibilityMatrix(CompatibilityMatrices* matrices) {
520 std::string error;
521 CompatibilityMatrix* matrix = nullptr;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800522 std::unique_ptr<HalManifest> checkManifest;
Yifan Honge7837b12018-10-11 10:38:57 -0700523 std::unique_ptr<CompatibilityMatrix> builtMatrix;
524
525 if (mCheckFile != nullptr) {
526 checkManifest = std::make_unique<HalManifest>();
527 if (!gHalManifestConverter(checkManifest.get(), read(*mCheckFile), &error)) {
528 std::cerr << "Cannot parse check file as a HAL manifest: " << error << std::endl;
529 return false;
530 }
531 }
532
Yifan Hongffcaf992018-01-09 17:08:51 -0800533 if (matrices->front().object.mType == SchemaType::DEVICE) {
Yifan Hong5a51ea52019-04-22 14:54:57 -0700534 builtMatrix = CompatibilityMatrix::combineDeviceMatrices(matrices, &error);
535 matrix = builtMatrix.get();
536
537 if (matrix == nullptr) {
538 std::cerr << error << std::endl;
Yifan Honge7837b12018-10-11 10:38:57 -0700539 return false;
540 }
541
Yifan Hongfeb454e2018-01-09 16:16:40 -0800542 auto vndkVersion = base::Trim(getEnv("REQUIRED_VNDK_VERSION"));
543 if (!vndkVersion.empty()) {
544 auto& valueInMatrix = matrix->device.mVendorNdk;
545 if (!valueInMatrix.version().empty() && valueInMatrix.version() != vndkVersion) {
546 std::cerr << "Hard-coded <vendor-ndk> version in device compatibility matrix ("
547 << matrices->front().name << "), '" << valueInMatrix.version()
548 << "', does not match value inferred "
549 << "from BOARD_VNDK_VERSION '" << vndkVersion << "'" << std::endl;
550 return false;
551 }
552 valueInMatrix = VendorNdk{std::move(vndkVersion)};
553 }
Yifan Honga28729e2018-01-17 13:40:35 -0800554
555 for (auto&& v : getEnvList("BOARD_SYSTEMSDK_VERSIONS")) {
556 matrix->device.mSystemSdk.mVersions.emplace(std::move(v));
557 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800558 }
559
Yifan Hongffcaf992018-01-09 17:08:51 -0800560 if (matrices->front().object.mType == SchemaType::FRAMEWORK) {
Yifan Honge7837b12018-10-11 10:38:57 -0700561 Level deviceLevel =
562 checkManifest != nullptr ? checkManifest->level() : Level::UNSPECIFIED;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800563 if (deviceLevel == Level::UNSPECIFIED) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800564 deviceLevel = getLowestFcmVersion(*matrices);
Yifan Honge7837b12018-10-11 10:38:57 -0700565 if (checkManifest != nullptr && deviceLevel != Level::UNSPECIFIED) {
566 std::cerr << "Warning: No Target FCM Version for device. Assuming \""
567 << to_string(deviceLevel)
568 << "\" when building final framework compatibility matrix."
569 << std::endl;
570 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800571 }
Yifan Honge7837b12018-10-11 10:38:57 -0700572 builtMatrix = CompatibilityMatrix::combine(deviceLevel, matrices, &error);
573 matrix = builtMatrix.get();
Yifan Hongdbe9db32017-12-11 19:06:11 -0800574
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700575 if (matrix == nullptr) {
576 std::cerr << error << std::endl;
577 return false;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800578 }
579
Yifan Honge88e1672017-08-24 14:42:54 -0700580 if (!assembleFrameworkCompatibilityMatrixKernels(matrix)) {
581 return false;
Yifan Hong79efa8a2017-07-06 14:10:28 -0700582 }
Yifan Honge88e1672017-08-24 14:42:54 -0700583
Yifan Hong321ee222018-02-02 12:46:52 -0800584 // Add PLATFORM_SEPOLICY_* to sepolicy.sepolicy-version. Remove dupes.
585 std::set<Version> sepolicyVersions;
586 auto sepolicyVersionStrings = getEnvList("PLATFORM_SEPOLICY_COMPAT_VERSIONS");
587 auto currentSepolicyVersionString = getEnv("PLATFORM_SEPOLICY_VERSION");
588 if (!currentSepolicyVersionString.empty()) {
589 sepolicyVersionStrings.push_back(currentSepolicyVersionString);
590 }
591 for (auto&& s : sepolicyVersionStrings) {
592 Version v;
593 if (!parse(s, &v)) {
594 std::cerr << "Error: unknown sepolicy version '" << s << "' specified by "
595 << (s == currentSepolicyVersionString
596 ? "PLATFORM_SEPOLICY_VERSION"
597 : "PLATFORM_SEPOLICY_COMPAT_VERSIONS")
598 << ".";
599 return false;
600 }
601 sepolicyVersions.insert(v);
602 }
603 for (auto&& v : sepolicyVersions) {
604 matrix->framework.mSepolicy.mSepolicyVersionRanges.emplace_back(v.majorVer,
605 v.minorVer);
Yifan Hong7f6c00c2017-07-06 19:50:29 +0000606 }
Yifan Hongf4135012017-12-18 17:27:19 -0800607
Yifan Hongce355902019-06-26 17:06:52 +0000608 if (!getFlagIfUnset("POLICYVERS",
609 &matrix->framework.mSepolicy.mKernelSepolicyVersion)) {
610 return false;
611 }
612 if (!getFlagIfUnset("FRAMEWORK_VBMETA_VERSION", &matrix->framework.mAvbMetaVersion)) {
613 return false;
614 }
Yifan Hong4dec91d2018-08-13 12:37:47 -0700615 // Hard-override existing AVB version
616 getFlag("FRAMEWORK_VBMETA_VERSION_OVERRIDE", &matrix->framework.mAvbMetaVersion,
617 false /* log */);
Yifan Hong9aa63702017-05-16 16:37:50 -0700618 }
Steven Moreland5875afc2018-04-05 13:14:08 -0700619 outputInputs(*matrices);
Yifan Honga2635c42017-12-12 13:20:33 -0800620 out() << gCompatibilityMatrixConverter(*matrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700621 out().flush();
622
Yifan Hong1044fd12018-02-27 14:51:54 -0800623 if (checkManifest != nullptr && !checkDualFile(*checkManifest, *matrix)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800624 return false;
Yifan Hong9aa63702017-05-16 16:37:50 -0700625 }
626
627 return true;
628 }
629
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700630 enum AssembleStatus { SUCCESS, FAIL_AND_EXIT, TRY_NEXT };
631 template <typename Schema, typename AssembleFunc>
632 AssembleStatus tryAssemble(const XmlConverter<Schema>& converter, const std::string& schemaName,
Yifan Hong94757062018-02-09 16:36:31 -0800633 AssembleFunc assemble, std::string* error) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800634 Schemas<Schema> schemas;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700635 Schema schema;
Yifan Hong94757062018-02-09 16:36:31 -0800636 if (!converter(&schema, read(mInFiles.front().stream()), error)) {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700637 return TRY_NEXT;
638 }
639 auto firstType = schema.type();
Yifan Hongaa219f52017-12-18 18:51:59 -0800640 schemas.emplace_back(mInFiles.front().name(), std::move(schema));
Yifan Hongdbe9db32017-12-11 19:06:11 -0800641
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700642 for (auto it = mInFiles.begin() + 1; it != mInFiles.end(); ++it) {
643 Schema additionalSchema;
Yifan Hongaa219f52017-12-18 18:51:59 -0800644 const std::string& fileName = it->name();
Yifan Hong94757062018-02-09 16:36:31 -0800645 if (!converter(&additionalSchema, read(it->stream()), error)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800646 std::cerr << "File \"" << fileName << "\" is not a valid " << firstType << " "
647 << schemaName << " (but the first file is a valid " << firstType << " "
Yifan Hong94757062018-02-09 16:36:31 -0800648 << schemaName << "). Error: " << *error << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700649 return FAIL_AND_EXIT;
650 }
651 if (additionalSchema.type() != firstType) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800652 std::cerr << "File \"" << fileName << "\" is a " << additionalSchema.type() << " "
653 << schemaName << " (but a " << firstType << " " << schemaName
654 << " is expected)." << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700655 return FAIL_AND_EXIT;
656 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800657
658 schemas.emplace_back(fileName, std::move(additionalSchema));
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700659 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800660 return assemble(&schemas) ? SUCCESS : FAIL_AND_EXIT;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700661 }
662
Yifan Hong8302cea2017-12-18 20:17:05 -0800663 bool assemble() override {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700664 using std::placeholders::_1;
665 if (mInFiles.empty()) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700666 std::cerr << "Missing input file." << std::endl;
667 return false;
668 }
669
Yifan Hong94757062018-02-09 16:36:31 -0800670 std::string manifestError;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700671 auto status = tryAssemble(gHalManifestConverter, "manifest",
Yifan Hong94757062018-02-09 16:36:31 -0800672 std::bind(&AssembleVintfImpl::assembleHalManifest, this, _1),
673 &manifestError);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700674 if (status == SUCCESS) return true;
675 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000676
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700677 resetInFiles();
Yifan Honga59d2562017-04-18 18:01:16 -0700678
Yifan Hong94757062018-02-09 16:36:31 -0800679 std::string matrixError;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700680 status = tryAssemble(gCompatibilityMatrixConverter, "compatibility matrix",
Yifan Hong94757062018-02-09 16:36:31 -0800681 std::bind(&AssembleVintfImpl::assembleCompatibilityMatrix, this, _1),
682 &matrixError);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700683 if (status == SUCCESS) return true;
684 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000685
Yifan Hong959ee1b2017-04-28 14:37:56 -0700686 std::cerr << "Input file has unknown format." << std::endl
Yifan Hong94757062018-02-09 16:36:31 -0800687 << "Error when attempting to convert to manifest: " << manifestError << std::endl
688 << "Error when attempting to convert to compatibility matrix: " << matrixError
689 << std::endl;
Yifan Hong959ee1b2017-04-28 14:37:56 -0700690 return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000691 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700692
Yifan Hong8302cea2017-12-18 20:17:05 -0800693 std::ostream& setOutputStream(Ostream&& out) override {
694 mOutRef = std::move(out);
695 return *mOutRef;
Yifan Hong9aa63702017-05-16 16:37:50 -0700696 }
697
Yifan Hong8302cea2017-12-18 20:17:05 -0800698 std::istream& addInputStream(const std::string& name, Istream&& in) override {
699 auto it = mInFiles.emplace(mInFiles.end(), name, std::move(in));
700 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700701 }
702
Yifan Hong8302cea2017-12-18 20:17:05 -0800703 std::istream& setCheckInputStream(Istream&& in) override {
704 mCheckFile = std::move(in);
705 return *mCheckFile;
706 }
707
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800708 bool hasKernelVersion(const KernelVersion& kernelVer) const override {
Yifan Hong8302cea2017-12-18 20:17:05 -0800709 return mKernels.find(kernelVer) != mKernels.end();
710 }
711
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800712 std::istream& addKernelConfigInputStream(const KernelVersion& kernelVer,
713 const std::string& name, Istream&& in) override {
Yifan Hong8302cea2017-12-18 20:17:05 -0800714 auto&& kernel = mKernels[kernelVer];
715 auto it = kernel.emplace(kernel.end(), name, std::move(in));
716 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700717 }
718
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700719 void resetInFiles() {
720 for (auto& inFile : mInFiles) {
Yifan Hongaa219f52017-12-18 18:51:59 -0800721 inFile.stream().clear();
722 inFile.stream().seekg(0);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700723 }
724 }
725
Yifan Hong8302cea2017-12-18 20:17:05 -0800726 void setOutputMatrix() override { mOutputMatrix = true; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700727
Yifan Hong8302cea2017-12-18 20:17:05 -0800728 bool setHalsOnly() override {
Yifan Hong5c9ff702018-08-07 17:26:12 -0700729 if (mHasSetHalsOnlyFlag) {
730 std::cerr << "Error: Cannot set --hals-only with --no-hals." << std::endl;
731 return false;
732 }
733 // Just override it with HALS_ONLY because other flags that modify mSerializeFlags
734 // does not interfere with this (except --no-hals).
735 mSerializeFlags = SerializeFlags::HALS_ONLY;
736 mHasSetHalsOnlyFlag = true;
Yifan Honga2635c42017-12-12 13:20:33 -0800737 return true;
738 }
739
Yifan Hong8302cea2017-12-18 20:17:05 -0800740 bool setNoHals() override {
Yifan Hong5c9ff702018-08-07 17:26:12 -0700741 if (mHasSetHalsOnlyFlag) {
742 std::cerr << "Error: Cannot set --hals-only with --no-hals." << std::endl;
743 return false;
744 }
745 mSerializeFlags = mSerializeFlags.disableHals();
746 mHasSetHalsOnlyFlag = true;
Yifan Honga2635c42017-12-12 13:20:33 -0800747 return true;
748 }
749
Yifan Hong86678e42018-07-26 11:38:54 -0700750 bool setNoKernelRequirements() override {
Yifan Hong5c9ff702018-08-07 17:26:12 -0700751 mSerializeFlags = mSerializeFlags.disableKernelConfigs().disableKernelMinorRevision();
Yifan Hong86678e42018-07-26 11:38:54 -0700752 return true;
753 }
754
Yifan Hong9aa63702017-05-16 16:37:50 -0700755 private:
Yifan Hongaa219f52017-12-18 18:51:59 -0800756 std::vector<NamedIstream> mInFiles;
Yifan Hong8302cea2017-12-18 20:17:05 -0800757 Ostream mOutRef;
758 Istream mCheckFile;
Yifan Hong9aa63702017-05-16 16:37:50 -0700759 bool mOutputMatrix = false;
Yifan Hong5c9ff702018-08-07 17:26:12 -0700760 bool mHasSetHalsOnlyFlag = false;
Yifan Hongba588bc2018-08-08 14:19:41 -0700761 SerializeFlags::Type mSerializeFlags = SerializeFlags::EVERYTHING;
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800762 std::map<KernelVersion, std::vector<NamedIstream>> mKernels;
Yifan Hong8302cea2017-12-18 20:17:05 -0800763 std::map<std::string, std::string> mFakeEnv;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000764};
765
Yifan Hong8302cea2017-12-18 20:17:05 -0800766bool AssembleVintf::openOutFile(const std::string& path) {
767 return static_cast<std::ofstream&>(setOutputStream(std::make_unique<std::ofstream>(path)))
768 .is_open();
769}
770
771bool AssembleVintf::openInFile(const std::string& path) {
772 return static_cast<std::ifstream&>(addInputStream(path, std::make_unique<std::ifstream>(path)))
773 .is_open();
774}
775
776bool AssembleVintf::openCheckFile(const std::string& path) {
777 return static_cast<std::ifstream&>(setCheckInputStream(std::make_unique<std::ifstream>(path)))
778 .is_open();
779}
780
781bool AssembleVintf::addKernel(const std::string& kernelArg) {
Yifan Hongb83e56b2018-01-09 14:36:30 -0800782 auto tokens = base::Split(kernelArg, ":");
Yifan Hong8302cea2017-12-18 20:17:05 -0800783 if (tokens.size() <= 1) {
784 std::cerr << "Unrecognized --kernel option '" << kernelArg << "'" << std::endl;
785 return false;
786 }
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800787 KernelVersion kernelVer;
Yifan Hong8302cea2017-12-18 20:17:05 -0800788 if (!parse(tokens.front(), &kernelVer)) {
789 std::cerr << "Unrecognized kernel version '" << tokens.front() << "'" << std::endl;
790 return false;
791 }
792 if (hasKernelVersion(kernelVer)) {
793 std::cerr << "Multiple --kernel for " << kernelVer << " is specified." << std::endl;
794 return false;
795 }
796 for (auto it = tokens.begin() + 1; it != tokens.end(); ++it) {
797 bool opened =
798 static_cast<std::ifstream&>(
799 addKernelConfigInputStream(kernelVer, *it, std::make_unique<std::ifstream>(*it)))
800 .is_open();
801 if (!opened) {
802 std::cerr << "Cannot open file '" << *it << "'." << std::endl;
803 return false;
804 }
805 }
806 return true;
807}
808
809std::unique_ptr<AssembleVintf> AssembleVintf::newInstance() {
810 return std::make_unique<AssembleVintfImpl>();
811}
812
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000813} // namespace vintf
814} // namespace android