blob: 0c75f505aa3091e76cf823d8c61a67b07af47fb6 [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 Hongeff04662017-12-18 16:27:24 -0800111 */
112 template <typename T>
Yifan Hongb36e85f2019-06-26 17:06:52 +0000113 void getFlagIfUnset(const std::string& envKey, T* out) const {
Yifan Hongeff04662017-12-18 16:27:24 -0800114 bool hasExistingValue = !(*out == T{});
115
116 bool hasEnvValue = false;
117 T envValue;
Yifan Hong8302cea2017-12-18 20:17:05 -0800118 std::string envStrValue = getEnv(envKey);
119 if (!envStrValue.empty()) {
120 if (!parse(envStrValue, &envValue)) {
Yifan Hongb36e85f2019-06-26 17:06:52 +0000121 std::cerr << "Cannot parse " << envValue << "." << std::endl;
122 return;
Yifan Hongeff04662017-12-18 16:27:24 -0800123 }
124 hasEnvValue = true;
125 }
126
127 if (hasExistingValue) {
Yifan Hongb36e85f2019-06-26 17:06:52 +0000128 if (hasEnvValue && (*out != envValue)) {
Bo Hu441fd2d2019-06-25 21:55:58 +0000129 std::cerr << "Warning: cannot override existing value " << *out << " with "
130 << envKey << " (which is " << envValue << ")." << std::endl;
Yifan Hongeff04662017-12-18 16:27:24 -0800131 }
Yifan Hongb36e85f2019-06-26 17:06:52 +0000132 return;
Yifan Hongeff04662017-12-18 16:27:24 -0800133 }
Yifan Hongb36e85f2019-06-26 17:06:52 +0000134 if (hasEnvValue) {
135 *out = envValue;
Yifan Hongeff04662017-12-18 16:27:24 -0800136 }
Yifan Hongeff04662017-12-18 16:27:24 -0800137 }
138
Yifan Hong8302cea2017-12-18 20:17:05 -0800139 bool getBooleanFlag(const std::string& key) const { return getEnv(key) == std::string("true"); }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800140
Yifan Hong8302cea2017-12-18 20:17:05 -0800141 size_t getIntegerFlag(const std::string& key, size_t defaultValue = 0) const {
142 std::string envValue = getEnv(key);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800143 if (envValue.empty()) {
144 return defaultValue;
145 }
146 size_t value;
147 if (!base::ParseUint(envValue, &value)) {
148 std::cerr << "Error: " << key << " must be a number." << std::endl;
149 return defaultValue;
150 }
151 return value;
152 }
153
Yifan Hong4650ad82017-05-01 17:28:02 -0700154 static std::string read(std::basic_istream<char>& is) {
155 std::stringstream ss;
156 ss << is.rdbuf();
157 return ss.str();
158 }
159
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700160 static bool isCommonConfig(const std::string& path) {
161 return ::android::base::Basename(path) == gBaseConfig;
162 }
163
Yifan Hong079ec242017-08-25 18:53:38 -0700164 // nullptr on any error, otherwise the condition.
165 static Condition generateCondition(const std::string& path) {
166 std::string fname = ::android::base::Basename(path);
167 if (fname.size() <= gConfigPrefix.size() + gConfigSuffix.size() ||
168 !std::equal(gConfigPrefix.begin(), gConfigPrefix.end(), fname.begin()) ||
169 !std::equal(gConfigSuffix.rbegin(), gConfigSuffix.rend(), fname.rbegin())) {
170 return nullptr;
171 }
172
173 std::string sub = fname.substr(gConfigPrefix.size(),
174 fname.size() - gConfigPrefix.size() - gConfigSuffix.size());
175 if (sub.empty()) {
176 return nullptr; // should not happen
177 }
178 for (size_t i = 0; i < sub.size(); ++i) {
179 if (sub[i] == '-') {
180 sub[i] = '_';
181 continue;
182 }
183 if (isalnum(sub[i])) {
184 sub[i] = toupper(sub[i]);
185 continue;
186 }
187 std::cerr << "'" << fname << "' (in " << path
188 << ") is not a valid kernel config file name. Must match regex: "
Steve Mucklead6aa912018-07-23 15:44:01 -0700189 << "android-base(-[0-9a-zA-Z-]+)?\\" << gConfigSuffix
190 << std::endl;
Yifan Hong079ec242017-08-25 18:53:38 -0700191 return nullptr;
192 }
193 sub.insert(0, "CONFIG_");
194 return std::make_unique<KernelConfig>(std::move(sub), Tristate::YES);
195 }
196
Yifan Hong8302cea2017-12-18 20:17:05 -0800197 static bool parseFileForKernelConfigs(std::basic_istream<char>& stream,
198 std::vector<KernelConfig>* out) {
Yifan Hong02e94002017-07-10 15:41:56 -0700199 KernelConfigParser parser(true /* processComments */, true /* relaxedFormat */);
Yifan Honga45f77d2018-12-03 16:42:52 -0800200 status_t err = parser.processAndFinish(read(stream));
Yifan Hong79efa8a2017-07-06 14:10:28 -0700201 if (err != OK) {
Yifan Hongae53a0e2017-07-07 15:19:06 -0700202 std::cerr << parser.error();
Yifan Hong79efa8a2017-07-06 14:10:28 -0700203 return false;
204 }
205
206 for (auto& configPair : parser.configs()) {
207 out->push_back({});
208 KernelConfig& config = out->back();
209 config.first = std::move(configPair.first);
210 if (!parseKernelConfigTypedValue(configPair.second, &config.second)) {
211 std::cerr << "Unknown value type for key = '" << config.first << "', value = '"
212 << configPair.second << "'\n";
213 return false;
214 }
215 }
216 return true;
217 }
218
Yifan Hong8302cea2017-12-18 20:17:05 -0800219 static bool parseFilesForKernelConfigs(std::vector<NamedIstream>* streams,
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700220 std::vector<ConditionedConfig>* out) {
221 out->clear();
222 ConditionedConfig commonConfig;
223 bool foundCommonConfig = false;
Steve Muckle0bef8682017-07-31 15:47:15 -0700224 bool ret = true;
Yifan Hong8302cea2017-12-18 20:17:05 -0800225
226 for (auto& namedStream : *streams) {
227 if (isCommonConfig(namedStream.name())) {
228 ret &= parseFileForKernelConfigs(namedStream.stream(), &commonConfig.second);
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700229 foundCommonConfig = true;
230 } else {
Yifan Hong8302cea2017-12-18 20:17:05 -0800231 Condition condition = generateCondition(namedStream.name());
Yifan Hong079ec242017-08-25 18:53:38 -0700232 ret &= (condition != nullptr);
233
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700234 std::vector<KernelConfig> kernelConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800235 if ((ret &= parseFileForKernelConfigs(namedStream.stream(), &kernelConfigs)))
Yifan Hong079ec242017-08-25 18:53:38 -0700236 out->emplace_back(std::move(condition), std::move(kernelConfigs));
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700237 }
Steve Muckle0bef8682017-07-31 15:47:15 -0700238 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700239
240 if (!foundCommonConfig) {
Steve Mucklead6aa912018-07-23 15:44:01 -0700241 std::cerr << "No " << gBaseConfig << " is found in these paths:" << std::endl;
Yifan Hong8302cea2017-12-18 20:17:05 -0800242 for (auto& namedStream : *streams) {
243 std::cerr << " " << namedStream.name() << std::endl;
244 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700245 }
246 ret &= foundCommonConfig;
247 // first element is always common configs (no conditions).
248 out->insert(out->begin(), std::move(commonConfig));
Steve Muckle0bef8682017-07-31 15:47:15 -0700249 return ret;
250 }
251
Yifan Hong8302cea2017-12-18 20:17:05 -0800252 std::basic_ostream<char>& out() const { return mOutRef == nullptr ? std::cout : *mOutRef; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700253
Yifan Hong1044fd12018-02-27 14:51:54 -0800254 // If -c is provided, check it.
255 bool checkDualFile(const HalManifest& manifest, const CompatibilityMatrix& matrix) {
256 if (getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
257 std::string error;
258 if (!manifest.checkCompatibility(matrix, &error)) {
259 std::cerr << "Not compatible: " << error << std::endl;
260 return false;
261 }
262 }
263
264 // Check HALs in device manifest that are not in framework matrix.
265 if (getBooleanFlag("VINTF_ENFORCE_NO_UNUSED_HALS")) {
266 auto unused = manifest.checkUnusedHals(matrix);
267 if (!unused.empty()) {
268 std::cerr << "Error: The following instances are in the device manifest but "
269 << "not specified in framework compatibility matrix: " << std::endl
270 << " " << android::base::Join(unused, "\n ") << std::endl
271 << "Suggested fix:" << std::endl
272 << "1. Check for any typos in device manifest or framework compatibility "
273 << "matrices with FCM version >= " << matrix.level() << "." << std::endl
274 << "2. Add them to any framework compatibility matrix with FCM "
275 << "version >= " << matrix.level() << " where applicable." << std::endl
Yifan Hong09efef32019-01-28 10:59:27 -0800276 << "3. Add them to DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE "
277 << "or DEVICE_PRODUCT_COMPATIBILITY_MATRIX_FILE." << std::endl;
Yifan Hong1044fd12018-02-27 14:51:54 -0800278
279 return false;
280 }
281 }
282 return true;
283 }
284
Yifan Hongdbe9db32017-12-11 19:06:11 -0800285 template <typename S>
Yifan Hongffcaf992018-01-09 17:08:51 -0800286 using Schemas = std::vector<Named<S>>;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800287 using HalManifests = Schemas<HalManifest>;
288 using CompatibilityMatrices = Schemas<CompatibilityMatrix>;
289
Steven Moreland5875afc2018-04-05 13:14:08 -0700290 template <typename M>
291 void outputInputs(const Schemas<M>& inputs) {
292 out() << "<!--" << std::endl;
293 out() << " Input:" << std::endl;
294 for (const auto& e : inputs) {
295 if (!e.name.empty()) {
296 out() << " " << base::Basename(e.name) << std::endl;
297 }
298 }
299 out() << "-->" << std::endl;
300 }
301
Yifan Honga45f77d2018-12-03 16:42:52 -0800302 // Parse --kernel arguments and write to output manifest.
303 bool setDeviceManifestKernel(HalManifest* manifest) {
304 if (mKernels.empty()) {
305 return true;
306 }
307 if (mKernels.size() > 1) {
308 std::cerr << "Warning: multiple --kernel is specified when building device manifest. "
309 << "Only the first one will be used." << std::endl;
310 }
311 auto& kernelArg = *mKernels.begin();
312 const auto& kernelVer = kernelArg.first;
313 auto& kernelConfigFiles = kernelArg.second;
314 // addKernel() guarantees that !kernelConfigFiles.empty().
315 if (kernelConfigFiles.size() > 1) {
316 std::cerr << "Warning: multiple config files are specified in --kernel when building "
317 << "device manfiest. Only the first one will be used." << std::endl;
318 }
319
320 KernelConfigParser parser(true /* processComments */, false /* relaxedFormat */);
321 status_t err = parser.processAndFinish(read(kernelConfigFiles[0].stream()));
322 if (err != OK) {
323 std::cerr << parser.error();
324 return false;
325 }
326 manifest->device.mKernel = std::make_optional<KernelInfo>();
327 manifest->device.mKernel->mVersion = kernelVer;
328 manifest->device.mKernel->mConfigs = parser.configs();
329 return true;
330 }
331
Yifan Hongdbe9db32017-12-11 19:06:11 -0800332 bool assembleHalManifest(HalManifests* halManifests) {
Yifan Hong4650ad82017-05-01 17:28:02 -0700333 std::string error;
Yifan Hongffcaf992018-01-09 17:08:51 -0800334 HalManifest* halManifest = &halManifests->front().object;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800335 for (auto it = halManifests->begin() + 1; it != halManifests->end(); ++it) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800336 const std::string& path = it->name;
Yifan Hong0a35ab12018-06-29 11:32:57 -0700337 HalManifest& manifestToAdd = it->object;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800338
Yifan Hong0a35ab12018-06-29 11:32:57 -0700339 if (manifestToAdd.level() != Level::UNSPECIFIED) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800340 if (halManifest->level() == Level::UNSPECIFIED) {
Yifan Hong0a35ab12018-06-29 11:32:57 -0700341 halManifest->mLevel = manifestToAdd.level();
342 } else if (halManifest->level() != manifestToAdd.level()) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800343 std::cerr << "Inconsistent FCM Version in HAL manifests:" << std::endl
Yifan Hongffcaf992018-01-09 17:08:51 -0800344 << " File '" << halManifests->front().name << "' has level "
Yifan Hongdbe9db32017-12-11 19:06:11 -0800345 << halManifest->level() << std::endl
Yifan Hong0a35ab12018-06-29 11:32:57 -0700346 << " File '" << path << "' has level " << manifestToAdd.level()
Yifan Hongdbe9db32017-12-11 19:06:11 -0800347 << std::endl;
348 return false;
349 }
350 }
351
Yifan Hong4c6962d2019-01-30 15:50:46 -0800352 if (!halManifest->addAll(&manifestToAdd, &error)) {
353 std::cerr << "File \"" << path << "\" cannot be added: " << error << std::endl;
Yifan Hong0a35ab12018-06-29 11:32:57 -0700354 return false;
355 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800356 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700357
358 if (halManifest->mType == SchemaType::DEVICE) {
Bo Hu441fd2d2019-06-25 21:55:58 +0000359 (void)getFlagIfUnset("BOARD_SEPOLICY_VERS", &halManifest->device.mSepolicyVersion);
Steven Morelandb1392772018-05-01 11:21:35 -0700360
Yifan Hongdbe9db32017-12-11 19:06:11 -0800361 if (!setDeviceFcmVersion(halManifest)) {
362 return false;
363 }
Yifan Honga45f77d2018-12-03 16:42:52 -0800364
365 if (!setDeviceManifestKernel(halManifest)) {
366 return false;
367 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700368 }
369
Yifan Hongfeb454e2018-01-09 16:16:40 -0800370 if (halManifest->mType == SchemaType::FRAMEWORK) {
Yifan Honga28729e2018-01-17 13:40:35 -0800371 for (auto&& v : getEnvList("PROVIDED_VNDK_VERSIONS")) {
372 halManifest->framework.mVendorNdks.emplace_back(std::move(v));
373 }
374
Yifan Hong8b8492b2018-01-22 14:08:40 -0800375 for (auto&& v : getEnvList("PLATFORM_SYSTEMSDK_VERSIONS")) {
Yifan Honga28729e2018-01-17 13:40:35 -0800376 halManifest->framework.mSystemSdk.mVersions.emplace(std::move(v));
Yifan Hongfeb454e2018-01-09 16:16:40 -0800377 }
378 }
379
Steven Moreland5875afc2018-04-05 13:14:08 -0700380 outputInputs(*halManifests);
381
Yifan Hong9aa63702017-05-16 16:37:50 -0700382 if (mOutputMatrix) {
383 CompatibilityMatrix generatedMatrix = halManifest->generateCompatibleMatrix();
384 if (!halManifest->checkCompatibility(generatedMatrix, &error)) {
385 std::cerr << "FATAL ERROR: cannot generate a compatible matrix: " << error
386 << std::endl;
387 }
388 out() << "<!-- \n"
389 " Autogenerated skeleton compatibility matrix. \n"
390 " Use with caution. Modify it to suit your needs.\n"
391 " All HALs are set to optional.\n"
392 " Many entries other than HALs are zero-filled and\n"
393 " require human attention. \n"
394 "-->\n"
Yifan Honga2635c42017-12-12 13:20:33 -0800395 << gCompatibilityMatrixConverter(generatedMatrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700396 } else {
Yifan Honga2635c42017-12-12 13:20:33 -0800397 out() << gHalManifestConverter(*halManifest, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700398 }
399 out().flush();
400
Yifan Hong8302cea2017-12-18 20:17:05 -0800401 if (mCheckFile != nullptr) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700402 CompatibilityMatrix checkMatrix;
Yifan Hong94757062018-02-09 16:36:31 -0800403 if (!gCompatibilityMatrixConverter(&checkMatrix, read(*mCheckFile), &error)) {
404 std::cerr << "Cannot parse check file as a compatibility matrix: " << error
405 << std::endl;
Yifan Hong9aa63702017-05-16 16:37:50 -0700406 return false;
407 }
Yifan Hong1044fd12018-02-27 14:51:54 -0800408 if (!checkDualFile(*halManifest, checkMatrix)) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700409 return false;
410 }
411 }
412
413 return true;
414 }
415
Yifan Honge7837b12018-10-11 10:38:57 -0700416 // Parse --kernel arguments and write to output matrix.
Yifan Honge88e1672017-08-24 14:42:54 -0700417 bool assembleFrameworkCompatibilityMatrixKernels(CompatibilityMatrix* matrix) {
Yifan Hong8302cea2017-12-18 20:17:05 -0800418 for (auto& pair : mKernels) {
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700419 std::vector<ConditionedConfig> conditionedConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800420 if (!parseFilesForKernelConfigs(&pair.second, &conditionedConfigs)) {
Yifan Honge88e1672017-08-24 14:42:54 -0700421 return false;
422 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700423 for (ConditionedConfig& conditionedConfig : conditionedConfigs) {
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800424 MatrixKernel kernel(KernelVersion{pair.first}, std::move(conditionedConfig.second));
Yifan Hong079ec242017-08-25 18:53:38 -0700425 if (conditionedConfig.first != nullptr)
426 kernel.mConditions.push_back(std::move(*conditionedConfig.first));
Yifan Honge7837b12018-10-11 10:38:57 -0700427 std::string error;
428 if (!matrix->addKernel(std::move(kernel), &error)) {
429 std::cerr << "Error:" << error << std::endl;
430 return false;
431 };
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700432 }
Yifan Honge88e1672017-08-24 14:42:54 -0700433 }
434 return true;
435 }
436
Yifan Hongdbe9db32017-12-11 19:06:11 -0800437 bool setDeviceFcmVersion(HalManifest* manifest) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700438 // Not needed for generating empty manifest for DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE.
Yifan Hong2eee1982018-03-29 10:55:30 -0700439 if (getBooleanFlag("VINTF_IGNORE_TARGET_FCM_VERSION")) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700440 return true;
441 }
442
Yifan Hongdbe9db32017-12-11 19:06:11 -0800443 size_t shippingApiLevel = getIntegerFlag("PRODUCT_SHIPPING_API_LEVEL");
Yifan Hong9aa63702017-05-16 16:37:50 -0700444
Yifan Hongdbe9db32017-12-11 19:06:11 -0800445 if (manifest->level() != Level::UNSPECIFIED) {
446 return true;
447 }
448 if (!getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
449 manifest->mLevel = Level::LEGACY;
450 return true;
451 }
452 // TODO(b/70628538): Do not infer from Shipping API level.
453 if (shippingApiLevel) {
454 std::cerr << "Warning: Shipping FCM Version is inferred from Shipping API level. "
455 << "Declare Shipping FCM Version in device manifest directly." << std::endl;
Yifan Hongafae1982018-01-11 18:15:24 -0800456 manifest->mLevel = details::convertFromApiLevel(shippingApiLevel);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800457 if (manifest->mLevel == Level::UNSPECIFIED) {
458 std::cerr << "Error: Shipping FCM Version cannot be inferred from Shipping API "
459 << "level " << shippingApiLevel << "."
460 << "Declare Shipping FCM Version in device manifest directly."
461 << std::endl;
462 return false;
463 }
464 return true;
465 }
466 // TODO(b/69638851): should be an error if Shipping API level is not defined.
467 // For now, just leave it empty; when framework compatibility matrix is built,
468 // lowest FCM Version is assumed.
469 std::cerr << "Warning: Shipping FCM Version cannot be inferred, because:" << std::endl
470 << " (1) It is not explicitly declared in device manifest;" << std::endl
471 << " (2) PRODUCT_ENFORCE_VINTF_MANIFEST is set to true;" << std::endl
472 << " (3) PRODUCT_SHIPPING_API_LEVEL is undefined." << std::endl
473 << "Assuming 'unspecified' Shipping FCM Version. " << std::endl
474 << "To remove this warning, define 'level' attribute in device manifest."
475 << std::endl;
476 return true;
477 }
478
479 Level getLowestFcmVersion(const CompatibilityMatrices& matrices) {
480 Level ret = Level::UNSPECIFIED;
481 for (const auto& e : matrices) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800482 if (ret == Level::UNSPECIFIED || ret > e.object.level()) {
483 ret = e.object.level();
Yifan Hongdbe9db32017-12-11 19:06:11 -0800484 }
485 }
486 return ret;
487 }
488
489 bool assembleCompatibilityMatrix(CompatibilityMatrices* matrices) {
490 std::string error;
491 CompatibilityMatrix* matrix = nullptr;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800492 std::unique_ptr<HalManifest> checkManifest;
Yifan Honge7837b12018-10-11 10:38:57 -0700493 std::unique_ptr<CompatibilityMatrix> builtMatrix;
494
495 if (mCheckFile != nullptr) {
496 checkManifest = std::make_unique<HalManifest>();
497 if (!gHalManifestConverter(checkManifest.get(), read(*mCheckFile), &error)) {
498 std::cerr << "Cannot parse check file as a HAL manifest: " << error << std::endl;
499 return false;
500 }
501 }
502
Yifan Hongffcaf992018-01-09 17:08:51 -0800503 if (matrices->front().object.mType == SchemaType::DEVICE) {
Yifan Hong5a51ea52019-04-22 14:54:57 -0700504 builtMatrix = CompatibilityMatrix::combineDeviceMatrices(matrices, &error);
505 matrix = builtMatrix.get();
506
507 if (matrix == nullptr) {
508 std::cerr << error << std::endl;
Yifan Honge7837b12018-10-11 10:38:57 -0700509 return false;
510 }
511
Yifan Hongfeb454e2018-01-09 16:16:40 -0800512 auto vndkVersion = base::Trim(getEnv("REQUIRED_VNDK_VERSION"));
513 if (!vndkVersion.empty()) {
514 auto& valueInMatrix = matrix->device.mVendorNdk;
515 if (!valueInMatrix.version().empty() && valueInMatrix.version() != vndkVersion) {
516 std::cerr << "Hard-coded <vendor-ndk> version in device compatibility matrix ("
517 << matrices->front().name << "), '" << valueInMatrix.version()
518 << "', does not match value inferred "
519 << "from BOARD_VNDK_VERSION '" << vndkVersion << "'" << std::endl;
520 return false;
521 }
522 valueInMatrix = VendorNdk{std::move(vndkVersion)};
523 }
Yifan Honga28729e2018-01-17 13:40:35 -0800524
525 for (auto&& v : getEnvList("BOARD_SYSTEMSDK_VERSIONS")) {
526 matrix->device.mSystemSdk.mVersions.emplace(std::move(v));
527 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800528 }
529
Yifan Hongffcaf992018-01-09 17:08:51 -0800530 if (matrices->front().object.mType == SchemaType::FRAMEWORK) {
Yifan Honge7837b12018-10-11 10:38:57 -0700531 Level deviceLevel =
532 checkManifest != nullptr ? checkManifest->level() : Level::UNSPECIFIED;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800533 if (deviceLevel == Level::UNSPECIFIED) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800534 deviceLevel = getLowestFcmVersion(*matrices);
Yifan Honge7837b12018-10-11 10:38:57 -0700535 if (checkManifest != nullptr && deviceLevel != Level::UNSPECIFIED) {
536 std::cerr << "Warning: No Target FCM Version for device. Assuming \""
537 << to_string(deviceLevel)
538 << "\" when building final framework compatibility matrix."
539 << std::endl;
540 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800541 }
Yifan Honge7837b12018-10-11 10:38:57 -0700542 builtMatrix = CompatibilityMatrix::combine(deviceLevel, matrices, &error);
543 matrix = builtMatrix.get();
Yifan Hongdbe9db32017-12-11 19:06:11 -0800544
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700545 if (matrix == nullptr) {
546 std::cerr << error << std::endl;
547 return false;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800548 }
549
Yifan Honge88e1672017-08-24 14:42:54 -0700550 if (!assembleFrameworkCompatibilityMatrixKernels(matrix)) {
551 return false;
Yifan Hong79efa8a2017-07-06 14:10:28 -0700552 }
Yifan Honge88e1672017-08-24 14:42:54 -0700553
Yifan Hong321ee222018-02-02 12:46:52 -0800554 // Add PLATFORM_SEPOLICY_* to sepolicy.sepolicy-version. Remove dupes.
555 std::set<Version> sepolicyVersions;
556 auto sepolicyVersionStrings = getEnvList("PLATFORM_SEPOLICY_COMPAT_VERSIONS");
557 auto currentSepolicyVersionString = getEnv("PLATFORM_SEPOLICY_VERSION");
558 if (!currentSepolicyVersionString.empty()) {
559 sepolicyVersionStrings.push_back(currentSepolicyVersionString);
560 }
561 for (auto&& s : sepolicyVersionStrings) {
562 Version v;
563 if (!parse(s, &v)) {
564 std::cerr << "Error: unknown sepolicy version '" << s << "' specified by "
565 << (s == currentSepolicyVersionString
566 ? "PLATFORM_SEPOLICY_VERSION"
567 : "PLATFORM_SEPOLICY_COMPAT_VERSIONS")
568 << ".";
569 return false;
570 }
571 sepolicyVersions.insert(v);
572 }
573 for (auto&& v : sepolicyVersions) {
574 matrix->framework.mSepolicy.mSepolicyVersionRanges.emplace_back(v.majorVer,
575 v.minorVer);
Yifan Hong7f6c00c2017-07-06 19:50:29 +0000576 }
Yifan Hongf4135012017-12-18 17:27:19 -0800577
Yifan Hongb36e85f2019-06-26 17:06:52 +0000578 getFlagIfUnset("POLICYVERS", &matrix->framework.mSepolicy.mKernelSepolicyVersion);
579 getFlagIfUnset("FRAMEWORK_VBMETA_VERSION", &matrix->framework.mAvbMetaVersion);
Yifan Hong4dec91d2018-08-13 12:37:47 -0700580 // Hard-override existing AVB version
581 getFlag("FRAMEWORK_VBMETA_VERSION_OVERRIDE", &matrix->framework.mAvbMetaVersion,
582 false /* log */);
Yifan Hong9aa63702017-05-16 16:37:50 -0700583 }
Steven Moreland5875afc2018-04-05 13:14:08 -0700584 outputInputs(*matrices);
Yifan Honga2635c42017-12-12 13:20:33 -0800585 out() << gCompatibilityMatrixConverter(*matrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700586 out().flush();
587
Yifan Hong1044fd12018-02-27 14:51:54 -0800588 if (checkManifest != nullptr && !checkDualFile(*checkManifest, *matrix)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800589 return false;
Yifan Hong9aa63702017-05-16 16:37:50 -0700590 }
591
592 return true;
593 }
594
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700595 enum AssembleStatus { SUCCESS, FAIL_AND_EXIT, TRY_NEXT };
596 template <typename Schema, typename AssembleFunc>
597 AssembleStatus tryAssemble(const XmlConverter<Schema>& converter, const std::string& schemaName,
Yifan Hong94757062018-02-09 16:36:31 -0800598 AssembleFunc assemble, std::string* error) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800599 Schemas<Schema> schemas;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700600 Schema schema;
Yifan Hong94757062018-02-09 16:36:31 -0800601 if (!converter(&schema, read(mInFiles.front().stream()), error)) {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700602 return TRY_NEXT;
603 }
604 auto firstType = schema.type();
Yifan Hongaa219f52017-12-18 18:51:59 -0800605 schemas.emplace_back(mInFiles.front().name(), std::move(schema));
Yifan Hongdbe9db32017-12-11 19:06:11 -0800606
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700607 for (auto it = mInFiles.begin() + 1; it != mInFiles.end(); ++it) {
608 Schema additionalSchema;
Yifan Hongaa219f52017-12-18 18:51:59 -0800609 const std::string& fileName = it->name();
Yifan Hong94757062018-02-09 16:36:31 -0800610 if (!converter(&additionalSchema, read(it->stream()), error)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800611 std::cerr << "File \"" << fileName << "\" is not a valid " << firstType << " "
612 << schemaName << " (but the first file is a valid " << firstType << " "
Yifan Hong94757062018-02-09 16:36:31 -0800613 << schemaName << "). Error: " << *error << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700614 return FAIL_AND_EXIT;
615 }
616 if (additionalSchema.type() != firstType) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800617 std::cerr << "File \"" << fileName << "\" is a " << additionalSchema.type() << " "
618 << schemaName << " (but a " << firstType << " " << schemaName
619 << " is expected)." << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700620 return FAIL_AND_EXIT;
621 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800622
623 schemas.emplace_back(fileName, std::move(additionalSchema));
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700624 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800625 return assemble(&schemas) ? SUCCESS : FAIL_AND_EXIT;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700626 }
627
Yifan Hong8302cea2017-12-18 20:17:05 -0800628 bool assemble() override {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700629 using std::placeholders::_1;
630 if (mInFiles.empty()) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700631 std::cerr << "Missing input file." << std::endl;
632 return false;
633 }
634
Yifan Hong94757062018-02-09 16:36:31 -0800635 std::string manifestError;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700636 auto status = tryAssemble(gHalManifestConverter, "manifest",
Yifan Hong94757062018-02-09 16:36:31 -0800637 std::bind(&AssembleVintfImpl::assembleHalManifest, this, _1),
638 &manifestError);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700639 if (status == SUCCESS) return true;
640 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000641
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700642 resetInFiles();
Yifan Honga59d2562017-04-18 18:01:16 -0700643
Yifan Hong94757062018-02-09 16:36:31 -0800644 std::string matrixError;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700645 status = tryAssemble(gCompatibilityMatrixConverter, "compatibility matrix",
Yifan Hong94757062018-02-09 16:36:31 -0800646 std::bind(&AssembleVintfImpl::assembleCompatibilityMatrix, this, _1),
647 &matrixError);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700648 if (status == SUCCESS) return true;
649 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000650
Yifan Hong959ee1b2017-04-28 14:37:56 -0700651 std::cerr << "Input file has unknown format." << std::endl
Yifan Hong94757062018-02-09 16:36:31 -0800652 << "Error when attempting to convert to manifest: " << manifestError << std::endl
653 << "Error when attempting to convert to compatibility matrix: " << matrixError
654 << std::endl;
Yifan Hong959ee1b2017-04-28 14:37:56 -0700655 return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000656 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700657
Yifan Hong8302cea2017-12-18 20:17:05 -0800658 std::ostream& setOutputStream(Ostream&& out) override {
659 mOutRef = std::move(out);
660 return *mOutRef;
Yifan Hong9aa63702017-05-16 16:37:50 -0700661 }
662
Yifan Hong8302cea2017-12-18 20:17:05 -0800663 std::istream& addInputStream(const std::string& name, Istream&& in) override {
664 auto it = mInFiles.emplace(mInFiles.end(), name, std::move(in));
665 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700666 }
667
Yifan Hong8302cea2017-12-18 20:17:05 -0800668 std::istream& setCheckInputStream(Istream&& in) override {
669 mCheckFile = std::move(in);
670 return *mCheckFile;
671 }
672
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800673 bool hasKernelVersion(const KernelVersion& kernelVer) const override {
Yifan Hong8302cea2017-12-18 20:17:05 -0800674 return mKernels.find(kernelVer) != mKernels.end();
675 }
676
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800677 std::istream& addKernelConfigInputStream(const KernelVersion& kernelVer,
678 const std::string& name, Istream&& in) override {
Yifan Hong8302cea2017-12-18 20:17:05 -0800679 auto&& kernel = mKernels[kernelVer];
680 auto it = kernel.emplace(kernel.end(), name, std::move(in));
681 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700682 }
683
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700684 void resetInFiles() {
685 for (auto& inFile : mInFiles) {
Yifan Hongaa219f52017-12-18 18:51:59 -0800686 inFile.stream().clear();
687 inFile.stream().seekg(0);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700688 }
689 }
690
Yifan Hong8302cea2017-12-18 20:17:05 -0800691 void setOutputMatrix() override { mOutputMatrix = true; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700692
Yifan Hong8302cea2017-12-18 20:17:05 -0800693 bool setHalsOnly() override {
Yifan Hong5c9ff702018-08-07 17:26:12 -0700694 if (mHasSetHalsOnlyFlag) {
695 std::cerr << "Error: Cannot set --hals-only with --no-hals." << std::endl;
696 return false;
697 }
698 // Just override it with HALS_ONLY because other flags that modify mSerializeFlags
699 // does not interfere with this (except --no-hals).
700 mSerializeFlags = SerializeFlags::HALS_ONLY;
701 mHasSetHalsOnlyFlag = true;
Yifan Honga2635c42017-12-12 13:20:33 -0800702 return true;
703 }
704
Yifan Hong8302cea2017-12-18 20:17:05 -0800705 bool setNoHals() override {
Yifan Hong5c9ff702018-08-07 17:26:12 -0700706 if (mHasSetHalsOnlyFlag) {
707 std::cerr << "Error: Cannot set --hals-only with --no-hals." << std::endl;
708 return false;
709 }
710 mSerializeFlags = mSerializeFlags.disableHals();
711 mHasSetHalsOnlyFlag = true;
Yifan Honga2635c42017-12-12 13:20:33 -0800712 return true;
713 }
714
Yifan Hong86678e42018-07-26 11:38:54 -0700715 bool setNoKernelRequirements() override {
Yifan Hong5c9ff702018-08-07 17:26:12 -0700716 mSerializeFlags = mSerializeFlags.disableKernelConfigs().disableKernelMinorRevision();
Yifan Hong86678e42018-07-26 11:38:54 -0700717 return true;
718 }
719
Yifan Hong9aa63702017-05-16 16:37:50 -0700720 private:
Yifan Hongaa219f52017-12-18 18:51:59 -0800721 std::vector<NamedIstream> mInFiles;
Yifan Hong8302cea2017-12-18 20:17:05 -0800722 Ostream mOutRef;
723 Istream mCheckFile;
Yifan Hong9aa63702017-05-16 16:37:50 -0700724 bool mOutputMatrix = false;
Yifan Hong5c9ff702018-08-07 17:26:12 -0700725 bool mHasSetHalsOnlyFlag = false;
Yifan Hongba588bc2018-08-08 14:19:41 -0700726 SerializeFlags::Type mSerializeFlags = SerializeFlags::EVERYTHING;
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800727 std::map<KernelVersion, std::vector<NamedIstream>> mKernels;
Yifan Hong8302cea2017-12-18 20:17:05 -0800728 std::map<std::string, std::string> mFakeEnv;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000729};
730
Yifan Hong8302cea2017-12-18 20:17:05 -0800731bool AssembleVintf::openOutFile(const std::string& path) {
732 return static_cast<std::ofstream&>(setOutputStream(std::make_unique<std::ofstream>(path)))
733 .is_open();
734}
735
736bool AssembleVintf::openInFile(const std::string& path) {
737 return static_cast<std::ifstream&>(addInputStream(path, std::make_unique<std::ifstream>(path)))
738 .is_open();
739}
740
741bool AssembleVintf::openCheckFile(const std::string& path) {
742 return static_cast<std::ifstream&>(setCheckInputStream(std::make_unique<std::ifstream>(path)))
743 .is_open();
744}
745
746bool AssembleVintf::addKernel(const std::string& kernelArg) {
Yifan Hongb83e56b2018-01-09 14:36:30 -0800747 auto tokens = base::Split(kernelArg, ":");
Yifan Hong8302cea2017-12-18 20:17:05 -0800748 if (tokens.size() <= 1) {
749 std::cerr << "Unrecognized --kernel option '" << kernelArg << "'" << std::endl;
750 return false;
751 }
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800752 KernelVersion kernelVer;
Yifan Hong8302cea2017-12-18 20:17:05 -0800753 if (!parse(tokens.front(), &kernelVer)) {
754 std::cerr << "Unrecognized kernel version '" << tokens.front() << "'" << std::endl;
755 return false;
756 }
757 if (hasKernelVersion(kernelVer)) {
758 std::cerr << "Multiple --kernel for " << kernelVer << " is specified." << std::endl;
759 return false;
760 }
761 for (auto it = tokens.begin() + 1; it != tokens.end(); ++it) {
762 bool opened =
763 static_cast<std::ifstream&>(
764 addKernelConfigInputStream(kernelVer, *it, std::make_unique<std::ifstream>(*it)))
765 .is_open();
766 if (!opened) {
767 std::cerr << "Cannot open file '" << *it << "'." << std::endl;
768 return false;
769 }
770 }
771 return true;
772}
773
774std::unique_ptr<AssembleVintf> AssembleVintf::newInstance() {
775 return std::make_unique<AssembleVintfImpl>();
776}
777
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000778} // namespace vintf
779} // namespace android