blob: d2fbb479d78f8ad9cd1fcdc7d1dccbd7fbec8d65 [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-";
42static const std::string gConfigSuffix = ".cfg";
43static const std::string gBaseConfig = "android-base.cfg";
44
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>
92 bool getFlag(const std::string& key, T* value) const {
93 std::string envValue = getEnv(key);
94 if (envValue.empty()) {
95 std::cerr << "Warning: " << key << " is missing, defaulted to " << (*value) << "."
Yifan Hong488e16a2017-07-11 13:50:41 -070096 << std::endl;
97 return true;
Yifan Hong4d18bcc2017-04-07 21:47:16 +000098 }
99
100 if (!parse(envValue, value)) {
101 std::cerr << "Cannot parse " << envValue << "." << std::endl;
102 return false;
103 }
104 return true;
105 }
106
Yifan Hongeff04662017-12-18 16:27:24 -0800107 /**
108 * Set *out to environment variable if *out is not a dummy value (i.e. default constructed).
109 */
110 template <typename T>
Yifan Hongf7e8b1b2018-01-23 15:30:35 -0800111 bool getFlagIfUnset(const std::string& envKey, T* out, bool log = true) const {
Yifan Hongeff04662017-12-18 16:27:24 -0800112 bool hasExistingValue = !(*out == T{});
113
114 bool hasEnvValue = false;
115 T envValue;
Yifan Hong8302cea2017-12-18 20:17:05 -0800116 std::string envStrValue = getEnv(envKey);
117 if (!envStrValue.empty()) {
118 if (!parse(envStrValue, &envValue)) {
Yifan Hongf7e8b1b2018-01-23 15:30:35 -0800119 if (log) {
120 std::cerr << "Cannot parse " << envValue << "." << std::endl;
121 }
Yifan Hongeff04662017-12-18 16:27:24 -0800122 return false;
123 }
124 hasEnvValue = true;
125 }
126
127 if (hasExistingValue) {
Yifan Hongf7e8b1b2018-01-23 15:30:35 -0800128 if (hasEnvValue && log) {
Yifan Hongeff04662017-12-18 16:27:24 -0800129 std::cerr << "Warning: cannot override existing value " << *out << " with "
130 << envKey << " (which is " << envValue << ")." << std::endl;
131 }
132 return false;
133 }
134 if (!hasEnvValue) {
Yifan Hongf7e8b1b2018-01-23 15:30:35 -0800135 if (log) {
136 std::cerr << "Warning: " << envKey << " is not specified. Default to " << T{} << "."
137 << std::endl;
138 }
Yifan Hongeff04662017-12-18 16:27:24 -0800139 return false;
140 }
141 *out = envValue;
142 return true;
143 }
144
Yifan Hong8302cea2017-12-18 20:17:05 -0800145 bool getBooleanFlag(const std::string& key) const { return getEnv(key) == std::string("true"); }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800146
Yifan Hong8302cea2017-12-18 20:17:05 -0800147 size_t getIntegerFlag(const std::string& key, size_t defaultValue = 0) const {
148 std::string envValue = getEnv(key);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800149 if (envValue.empty()) {
150 return defaultValue;
151 }
152 size_t value;
153 if (!base::ParseUint(envValue, &value)) {
154 std::cerr << "Error: " << key << " must be a number." << std::endl;
155 return defaultValue;
156 }
157 return value;
158 }
159
Yifan Hong4650ad82017-05-01 17:28:02 -0700160 static std::string read(std::basic_istream<char>& is) {
161 std::stringstream ss;
162 ss << is.rdbuf();
163 return ss.str();
164 }
165
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700166 static bool isCommonConfig(const std::string& path) {
167 return ::android::base::Basename(path) == gBaseConfig;
168 }
169
Yifan Hong079ec242017-08-25 18:53:38 -0700170 // nullptr on any error, otherwise the condition.
171 static Condition generateCondition(const std::string& path) {
172 std::string fname = ::android::base::Basename(path);
173 if (fname.size() <= gConfigPrefix.size() + gConfigSuffix.size() ||
174 !std::equal(gConfigPrefix.begin(), gConfigPrefix.end(), fname.begin()) ||
175 !std::equal(gConfigSuffix.rbegin(), gConfigSuffix.rend(), fname.rbegin())) {
176 return nullptr;
177 }
178
179 std::string sub = fname.substr(gConfigPrefix.size(),
180 fname.size() - gConfigPrefix.size() - gConfigSuffix.size());
181 if (sub.empty()) {
182 return nullptr; // should not happen
183 }
184 for (size_t i = 0; i < sub.size(); ++i) {
185 if (sub[i] == '-') {
186 sub[i] = '_';
187 continue;
188 }
189 if (isalnum(sub[i])) {
190 sub[i] = toupper(sub[i]);
191 continue;
192 }
193 std::cerr << "'" << fname << "' (in " << path
194 << ") is not a valid kernel config file name. Must match regex: "
195 << "android-base(-[0-9a-zA-Z-]+)?\\.cfg" << std::endl;
196 return nullptr;
197 }
198 sub.insert(0, "CONFIG_");
199 return std::make_unique<KernelConfig>(std::move(sub), Tristate::YES);
200 }
201
Yifan 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 Hong8302cea2017-12-18 20:17:05 -0800205 std::string content = read(stream);
Yifan Hong79efa8a2017-07-06 14:10:28 -0700206 status_t err = parser.process(content.c_str(), content.size());
207 if (err != OK) {
Yifan Hongae53a0e2017-07-07 15:19:06 -0700208 std::cerr << parser.error();
Yifan Hong79efa8a2017-07-06 14:10:28 -0700209 return false;
210 }
211 err = parser.finish();
212 if (err != OK) {
Yifan Hongae53a0e2017-07-07 15:19:06 -0700213 std::cerr << parser.error();
Yifan Hong79efa8a2017-07-06 14:10:28 -0700214 return false;
215 }
216
217 for (auto& configPair : parser.configs()) {
218 out->push_back({});
219 KernelConfig& config = out->back();
220 config.first = std::move(configPair.first);
221 if (!parseKernelConfigTypedValue(configPair.second, &config.second)) {
222 std::cerr << "Unknown value type for key = '" << config.first << "', value = '"
223 << configPair.second << "'\n";
224 return false;
225 }
226 }
227 return true;
228 }
229
Yifan Hong8302cea2017-12-18 20:17:05 -0800230 static bool parseFilesForKernelConfigs(std::vector<NamedIstream>* streams,
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700231 std::vector<ConditionedConfig>* out) {
232 out->clear();
233 ConditionedConfig commonConfig;
234 bool foundCommonConfig = false;
Steve Muckle0bef8682017-07-31 15:47:15 -0700235 bool ret = true;
Yifan Hong8302cea2017-12-18 20:17:05 -0800236
237 for (auto& namedStream : *streams) {
238 if (isCommonConfig(namedStream.name())) {
239 ret &= parseFileForKernelConfigs(namedStream.stream(), &commonConfig.second);
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700240 foundCommonConfig = true;
241 } else {
Yifan Hong8302cea2017-12-18 20:17:05 -0800242 Condition condition = generateCondition(namedStream.name());
Yifan Hong079ec242017-08-25 18:53:38 -0700243 ret &= (condition != nullptr);
244
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700245 std::vector<KernelConfig> kernelConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800246 if ((ret &= parseFileForKernelConfigs(namedStream.stream(), &kernelConfigs)))
Yifan Hong079ec242017-08-25 18:53:38 -0700247 out->emplace_back(std::move(condition), std::move(kernelConfigs));
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700248 }
Steve Muckle0bef8682017-07-31 15:47:15 -0700249 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700250
251 if (!foundCommonConfig) {
Yifan Hong8302cea2017-12-18 20:17:05 -0800252 std::cerr << "No android-base.cfg is found in these paths:" << std::endl;
253 for (auto& namedStream : *streams) {
254 std::cerr << " " << namedStream.name() << std::endl;
255 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700256 }
257 ret &= foundCommonConfig;
258 // first element is always common configs (no conditions).
259 out->insert(out->begin(), std::move(commonConfig));
Steve Muckle0bef8682017-07-31 15:47:15 -0700260 return ret;
261 }
262
Yifan Hong8302cea2017-12-18 20:17:05 -0800263 std::basic_ostream<char>& out() const { return mOutRef == nullptr ? std::cout : *mOutRef; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700264
Yifan Hongdbe9db32017-12-11 19:06:11 -0800265 template <typename S>
Yifan Hongffcaf992018-01-09 17:08:51 -0800266 using Schemas = std::vector<Named<S>>;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800267 using HalManifests = Schemas<HalManifest>;
268 using CompatibilityMatrices = Schemas<CompatibilityMatrix>;
269
270 bool assembleHalManifest(HalManifests* halManifests) {
Yifan Hong4650ad82017-05-01 17:28:02 -0700271 std::string error;
Yifan Hongffcaf992018-01-09 17:08:51 -0800272 HalManifest* halManifest = &halManifests->front().object;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800273 for (auto it = halManifests->begin() + 1; it != halManifests->end(); ++it) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800274 const std::string& path = it->name;
275 HalManifest& halToAdd = it->object;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800276
277 if (halToAdd.level() != Level::UNSPECIFIED) {
278 if (halManifest->level() == Level::UNSPECIFIED) {
279 halManifest->mLevel = halToAdd.level();
280 } else if (halManifest->level() != halToAdd.level()) {
281 std::cerr << "Inconsistent FCM Version in HAL manifests:" << std::endl
Yifan Hongffcaf992018-01-09 17:08:51 -0800282 << " File '" << halManifests->front().name << "' has level "
Yifan Hongdbe9db32017-12-11 19:06:11 -0800283 << halManifest->level() << std::endl
284 << " File '" << path << "' has level " << halToAdd.level()
285 << std::endl;
286 return false;
287 }
288 }
289
Yifan Hongea25dd42017-12-18 17:03:24 -0800290 if (!halManifest->addAllHals(&halToAdd, &error)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800291 std::cerr << "File \"" << path << "\" cannot be added: conflict on HAL \"" << error
292 << "\" with an existing HAL. See <hal> with the same name "
293 << "in previously parsed files or previously declared in this file."
294 << std::endl;
295 return false;
296 }
297 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700298
299 if (halManifest->mType == SchemaType::DEVICE) {
300 if (!getFlag("BOARD_SEPOLICY_VERS", &halManifest->device.mSepolicyVersion)) {
301 return false;
302 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800303 if (!setDeviceFcmVersion(halManifest)) {
304 return false;
305 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700306 }
307
Yifan Hongfeb454e2018-01-09 16:16:40 -0800308 if (halManifest->mType == SchemaType::FRAMEWORK) {
Yifan Honga28729e2018-01-17 13:40:35 -0800309 for (auto&& v : getEnvList("PROVIDED_VNDK_VERSIONS")) {
310 halManifest->framework.mVendorNdks.emplace_back(std::move(v));
311 }
312
Yifan Hong8b8492b2018-01-22 14:08:40 -0800313 for (auto&& v : getEnvList("PLATFORM_SYSTEMSDK_VERSIONS")) {
Yifan Honga28729e2018-01-17 13:40:35 -0800314 halManifest->framework.mSystemSdk.mVersions.emplace(std::move(v));
Yifan Hongfeb454e2018-01-09 16:16:40 -0800315 }
316 }
317
Yifan Hong9aa63702017-05-16 16:37:50 -0700318 if (mOutputMatrix) {
319 CompatibilityMatrix generatedMatrix = halManifest->generateCompatibleMatrix();
320 if (!halManifest->checkCompatibility(generatedMatrix, &error)) {
321 std::cerr << "FATAL ERROR: cannot generate a compatible matrix: " << error
322 << std::endl;
323 }
324 out() << "<!-- \n"
325 " Autogenerated skeleton compatibility matrix. \n"
326 " Use with caution. Modify it to suit your needs.\n"
327 " All HALs are set to optional.\n"
328 " Many entries other than HALs are zero-filled and\n"
329 " require human attention. \n"
330 "-->\n"
Yifan Honga2635c42017-12-12 13:20:33 -0800331 << gCompatibilityMatrixConverter(generatedMatrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700332 } else {
Yifan Honga2635c42017-12-12 13:20:33 -0800333 out() << gHalManifestConverter(*halManifest, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700334 }
335 out().flush();
336
Yifan Hong8302cea2017-12-18 20:17:05 -0800337 if (mCheckFile != nullptr) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700338 CompatibilityMatrix checkMatrix;
Yifan Hong8302cea2017-12-18 20:17:05 -0800339 if (!gCompatibilityMatrixConverter(&checkMatrix, read(*mCheckFile))) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700340 std::cerr << "Cannot parse check file as a compatibility matrix: "
341 << gCompatibilityMatrixConverter.lastError() << std::endl;
342 return false;
343 }
344 if (!halManifest->checkCompatibility(checkMatrix, &error)) {
345 std::cerr << "Not compatible: " << error << std::endl;
346 return false;
347 }
348 }
349
350 return true;
351 }
352
Yifan Honge88e1672017-08-24 14:42:54 -0700353 bool assembleFrameworkCompatibilityMatrixKernels(CompatibilityMatrix* matrix) {
Yifan Hong8302cea2017-12-18 20:17:05 -0800354 for (auto& pair : mKernels) {
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700355 std::vector<ConditionedConfig> conditionedConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800356 if (!parseFilesForKernelConfigs(&pair.second, &conditionedConfigs)) {
Yifan Honge88e1672017-08-24 14:42:54 -0700357 return false;
358 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700359 for (ConditionedConfig& conditionedConfig : conditionedConfigs) {
Yifan Hong48602df2017-08-28 13:04:12 -0700360 MatrixKernel kernel(KernelVersion{pair.first.majorVer, pair.first.minorVer, 0u},
361 std::move(conditionedConfig.second));
Yifan Hong079ec242017-08-25 18:53:38 -0700362 if (conditionedConfig.first != nullptr)
363 kernel.mConditions.push_back(std::move(*conditionedConfig.first));
364 matrix->framework.mKernels.push_back(std::move(kernel));
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700365 }
Yifan Honge88e1672017-08-24 14:42:54 -0700366 }
367 return true;
368 }
369
Yifan Hongdbe9db32017-12-11 19:06:11 -0800370 bool setDeviceFcmVersion(HalManifest* manifest) {
371 size_t shippingApiLevel = getIntegerFlag("PRODUCT_SHIPPING_API_LEVEL");
Yifan Hong9aa63702017-05-16 16:37:50 -0700372
Yifan Hongdbe9db32017-12-11 19:06:11 -0800373 if (manifest->level() != Level::UNSPECIFIED) {
374 return true;
375 }
376 if (!getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
377 manifest->mLevel = Level::LEGACY;
378 return true;
379 }
380 // TODO(b/70628538): Do not infer from Shipping API level.
381 if (shippingApiLevel) {
382 std::cerr << "Warning: Shipping FCM Version is inferred from Shipping API level. "
383 << "Declare Shipping FCM Version in device manifest directly." << std::endl;
Yifan Hongafae1982018-01-11 18:15:24 -0800384 manifest->mLevel = details::convertFromApiLevel(shippingApiLevel);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800385 if (manifest->mLevel == Level::UNSPECIFIED) {
386 std::cerr << "Error: Shipping FCM Version cannot be inferred from Shipping API "
387 << "level " << shippingApiLevel << "."
388 << "Declare Shipping FCM Version in device manifest directly."
389 << std::endl;
390 return false;
391 }
392 return true;
393 }
394 // TODO(b/69638851): should be an error if Shipping API level is not defined.
395 // For now, just leave it empty; when framework compatibility matrix is built,
396 // lowest FCM Version is assumed.
397 std::cerr << "Warning: Shipping FCM Version cannot be inferred, because:" << std::endl
398 << " (1) It is not explicitly declared in device manifest;" << std::endl
399 << " (2) PRODUCT_ENFORCE_VINTF_MANIFEST is set to true;" << std::endl
400 << " (3) PRODUCT_SHIPPING_API_LEVEL is undefined." << std::endl
401 << "Assuming 'unspecified' Shipping FCM Version. " << std::endl
402 << "To remove this warning, define 'level' attribute in device manifest."
403 << std::endl;
404 return true;
405 }
406
407 Level getLowestFcmVersion(const CompatibilityMatrices& matrices) {
408 Level ret = Level::UNSPECIFIED;
409 for (const auto& e : matrices) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800410 if (ret == Level::UNSPECIFIED || ret > e.object.level()) {
411 ret = e.object.level();
Yifan Hongdbe9db32017-12-11 19:06:11 -0800412 }
413 }
414 return ret;
415 }
416
417 bool assembleCompatibilityMatrix(CompatibilityMatrices* matrices) {
418 std::string error;
419 CompatibilityMatrix* matrix = nullptr;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800420 std::unique_ptr<HalManifest> checkManifest;
Yifan Hongffcaf992018-01-09 17:08:51 -0800421 if (matrices->front().object.mType == SchemaType::DEVICE) {
422 matrix = &matrices->front().object;
Yifan Hongfeb454e2018-01-09 16:16:40 -0800423
424 auto vndkVersion = base::Trim(getEnv("REQUIRED_VNDK_VERSION"));
425 if (!vndkVersion.empty()) {
426 auto& valueInMatrix = matrix->device.mVendorNdk;
427 if (!valueInMatrix.version().empty() && valueInMatrix.version() != vndkVersion) {
428 std::cerr << "Hard-coded <vendor-ndk> version in device compatibility matrix ("
429 << matrices->front().name << "), '" << valueInMatrix.version()
430 << "', does not match value inferred "
431 << "from BOARD_VNDK_VERSION '" << vndkVersion << "'" << std::endl;
432 return false;
433 }
434 valueInMatrix = VendorNdk{std::move(vndkVersion)};
435 }
Yifan Honga28729e2018-01-17 13:40:35 -0800436
437 for (auto&& v : getEnvList("BOARD_SYSTEMSDK_VERSIONS")) {
438 matrix->device.mSystemSdk.mVersions.emplace(std::move(v));
439 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800440 }
441
Yifan Hongffcaf992018-01-09 17:08:51 -0800442 if (matrices->front().object.mType == SchemaType::FRAMEWORK) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800443 Level deviceLevel = Level::UNSPECIFIED;
Yifan Hong8302cea2017-12-18 20:17:05 -0800444 if (mCheckFile != nullptr) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800445 checkManifest = std::make_unique<HalManifest>();
Yifan Hong8302cea2017-12-18 20:17:05 -0800446 if (!gHalManifestConverter(checkManifest.get(), read(*mCheckFile))) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800447 std::cerr << "Cannot parse check file as a HAL manifest: "
448 << gHalManifestConverter.lastError() << std::endl;
449 return false;
450 }
451 deviceLevel = checkManifest->level();
452 }
453
454 if (deviceLevel == Level::UNSPECIFIED) {
455 // For GSI build, legacy devices that do not have a HAL manifest,
456 // and devices in development, merge all compatibility matrices.
457 deviceLevel = getLowestFcmVersion(*matrices);
458 }
459
Yifan Hongddae77e2017-12-18 16:57:07 -0800460 if (deviceLevel == Level::UNSPECIFIED) {
461 // building empty.xml
Yifan Hongffcaf992018-01-09 17:08:51 -0800462 matrix = &matrices->front().object;
Yifan Hongddae77e2017-12-18 16:57:07 -0800463 } else {
464 matrix = CompatibilityMatrix::combine(deviceLevel, matrices, &error);
465 if (matrix == nullptr) {
466 std::cerr << error << std::endl;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800467 return false;
468 }
469 }
470
Yifan Honge88e1672017-08-24 14:42:54 -0700471 if (!assembleFrameworkCompatibilityMatrixKernels(matrix)) {
472 return false;
Yifan Hong79efa8a2017-07-06 14:10:28 -0700473 }
Yifan Honge88e1672017-08-24 14:42:54 -0700474
Yifan Hongf4135012017-12-18 17:27:19 -0800475 // set sepolicy.sepolicy-version to BOARD_SEPOLICY_VERS when none is specified.
476 std::vector<VersionRange>* sepolicyVrs =
477 &matrix->framework.mSepolicy.mSepolicyVersionRanges;
478 VersionRange sepolicyVr;
479 if (!sepolicyVrs->empty()) sepolicyVr = sepolicyVrs->front();
Yifan Hongf7e8b1b2018-01-23 15:30:35 -0800480 if (getFlagIfUnset("BOARD_SEPOLICY_VERS", &sepolicyVr,
481 deviceLevel == Level::UNSPECIFIED /* log */)) {
Yifan Hongf4135012017-12-18 17:27:19 -0800482 *sepolicyVrs = {{sepolicyVr}};
Yifan Hong7f6c00c2017-07-06 19:50:29 +0000483 }
Yifan Hongf4135012017-12-18 17:27:19 -0800484
Yifan Hongf7e8b1b2018-01-23 15:30:35 -0800485 getFlagIfUnset("POLICYVERS", &matrix->framework.mSepolicy.mKernelSepolicyVersion,
486 deviceLevel == Level::UNSPECIFIED /* log */);
487 getFlagIfUnset("FRAMEWORK_VBMETA_VERSION", &matrix->framework.mAvbMetaVersion,
488 deviceLevel == Level::UNSPECIFIED /* log */);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800489
490 out() << "<!--" << std::endl;
491 out() << " Input:" << std::endl;
Yifan Hongddae77e2017-12-18 16:57:07 -0800492 for (const auto& e : *matrices) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800493 if (!e.name.empty()) {
Yifan Hong4f67f3b2018-01-11 17:05:47 -0800494 out() << " " << base::Basename(e.name) << std::endl;
Yifan Hongddae77e2017-12-18 16:57:07 -0800495 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800496 }
497 out() << "-->" << std::endl;
Yifan Hong9aa63702017-05-16 16:37:50 -0700498 }
Yifan Honga2635c42017-12-12 13:20:33 -0800499 out() << gCompatibilityMatrixConverter(*matrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700500 out().flush();
501
Yifan Hongdbe9db32017-12-11 19:06:11 -0800502 if (checkManifest != nullptr && getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST") &&
503 !checkManifest->checkCompatibility(*matrix, &error)) {
504 std::cerr << "Not compatible: " << error << std::endl;
505 return false;
Yifan Hong9aa63702017-05-16 16:37:50 -0700506 }
507
508 return true;
509 }
510
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700511 enum AssembleStatus { SUCCESS, FAIL_AND_EXIT, TRY_NEXT };
512 template <typename Schema, typename AssembleFunc>
513 AssembleStatus tryAssemble(const XmlConverter<Schema>& converter, const std::string& schemaName,
514 AssembleFunc assemble) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800515 Schemas<Schema> schemas;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700516 Schema schema;
Yifan Hongaa219f52017-12-18 18:51:59 -0800517 if (!converter(&schema, read(mInFiles.front().stream()))) {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700518 return TRY_NEXT;
519 }
520 auto firstType = schema.type();
Yifan Hongaa219f52017-12-18 18:51:59 -0800521 schemas.emplace_back(mInFiles.front().name(), std::move(schema));
Yifan Hongdbe9db32017-12-11 19:06:11 -0800522
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700523 for (auto it = mInFiles.begin() + 1; it != mInFiles.end(); ++it) {
524 Schema additionalSchema;
Yifan Hongaa219f52017-12-18 18:51:59 -0800525 const std::string& fileName = it->name();
526 if (!converter(&additionalSchema, read(it->stream()))) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800527 std::cerr << "File \"" << fileName << "\" is not a valid " << firstType << " "
528 << schemaName << " (but the first file is a valid " << firstType << " "
529 << schemaName << "). Error: " << converter.lastError() << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700530 return FAIL_AND_EXIT;
531 }
532 if (additionalSchema.type() != firstType) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800533 std::cerr << "File \"" << fileName << "\" is a " << additionalSchema.type() << " "
534 << schemaName << " (but a " << firstType << " " << schemaName
535 << " is expected)." << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700536 return FAIL_AND_EXIT;
537 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800538
539 schemas.emplace_back(fileName, std::move(additionalSchema));
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700540 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800541 return assemble(&schemas) ? SUCCESS : FAIL_AND_EXIT;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700542 }
543
Yifan Hong8302cea2017-12-18 20:17:05 -0800544 bool assemble() override {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700545 using std::placeholders::_1;
546 if (mInFiles.empty()) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700547 std::cerr << "Missing input file." << std::endl;
548 return false;
549 }
550
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700551 auto status = tryAssemble(gHalManifestConverter, "manifest",
Yifan Hong8302cea2017-12-18 20:17:05 -0800552 std::bind(&AssembleVintfImpl::assembleHalManifest, this, _1));
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700553 if (status == SUCCESS) return true;
554 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000555
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700556 resetInFiles();
Yifan Honga59d2562017-04-18 18:01:16 -0700557
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700558 status = tryAssemble(gCompatibilityMatrixConverter, "compatibility matrix",
Yifan Hong8302cea2017-12-18 20:17:05 -0800559 std::bind(&AssembleVintfImpl::assembleCompatibilityMatrix, this, _1));
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700560 if (status == SUCCESS) return true;
561 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000562
Yifan Hong959ee1b2017-04-28 14:37:56 -0700563 std::cerr << "Input file has unknown format." << std::endl
564 << "Error when attempting to convert to manifest: "
565 << gHalManifestConverter.lastError() << std::endl
566 << "Error when attempting to convert to compatibility matrix: "
567 << gCompatibilityMatrixConverter.lastError() << std::endl;
568 return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000569 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700570
Yifan Hong8302cea2017-12-18 20:17:05 -0800571 std::ostream& setOutputStream(Ostream&& out) override {
572 mOutRef = std::move(out);
573 return *mOutRef;
Yifan Hong9aa63702017-05-16 16:37:50 -0700574 }
575
Yifan Hong8302cea2017-12-18 20:17:05 -0800576 std::istream& addInputStream(const std::string& name, Istream&& in) override {
577 auto it = mInFiles.emplace(mInFiles.end(), name, std::move(in));
578 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700579 }
580
Yifan Hong8302cea2017-12-18 20:17:05 -0800581 std::istream& setCheckInputStream(Istream&& in) override {
582 mCheckFile = std::move(in);
583 return *mCheckFile;
584 }
585
586 bool hasKernelVersion(const Version& kernelVer) const override {
587 return mKernels.find(kernelVer) != mKernels.end();
588 }
589
590 std::istream& addKernelConfigInputStream(const Version& kernelVer, const std::string& name,
591 Istream&& in) override {
592 auto&& kernel = mKernels[kernelVer];
593 auto it = kernel.emplace(kernel.end(), name, std::move(in));
594 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700595 }
596
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700597 void resetInFiles() {
598 for (auto& inFile : mInFiles) {
Yifan Hongaa219f52017-12-18 18:51:59 -0800599 inFile.stream().clear();
600 inFile.stream().seekg(0);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700601 }
602 }
603
Yifan Hong8302cea2017-12-18 20:17:05 -0800604 void setOutputMatrix() override { mOutputMatrix = true; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700605
Yifan Hong8302cea2017-12-18 20:17:05 -0800606 bool setHalsOnly() override {
Yifan Honga2635c42017-12-12 13:20:33 -0800607 if (mSerializeFlags) return false;
608 mSerializeFlags |= SerializeFlag::HALS_ONLY;
609 return true;
610 }
611
Yifan Hong8302cea2017-12-18 20:17:05 -0800612 bool setNoHals() override {
Yifan Honga2635c42017-12-12 13:20:33 -0800613 if (mSerializeFlags) return false;
614 mSerializeFlags |= SerializeFlag::NO_HALS;
615 return true;
616 }
617
Yifan Hong9aa63702017-05-16 16:37:50 -0700618 private:
Yifan Hongaa219f52017-12-18 18:51:59 -0800619 std::vector<NamedIstream> mInFiles;
Yifan Hong8302cea2017-12-18 20:17:05 -0800620 Ostream mOutRef;
621 Istream mCheckFile;
Yifan Hong9aa63702017-05-16 16:37:50 -0700622 bool mOutputMatrix = false;
Yifan Honga2635c42017-12-12 13:20:33 -0800623 SerializeFlags mSerializeFlags = SerializeFlag::EVERYTHING;
Yifan Hong8302cea2017-12-18 20:17:05 -0800624 std::map<Version, std::vector<NamedIstream>> mKernels;
625 std::map<std::string, std::string> mFakeEnv;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000626};
627
Yifan Hong8302cea2017-12-18 20:17:05 -0800628bool AssembleVintf::openOutFile(const std::string& path) {
629 return static_cast<std::ofstream&>(setOutputStream(std::make_unique<std::ofstream>(path)))
630 .is_open();
631}
632
633bool AssembleVintf::openInFile(const std::string& path) {
634 return static_cast<std::ifstream&>(addInputStream(path, std::make_unique<std::ifstream>(path)))
635 .is_open();
636}
637
638bool AssembleVintf::openCheckFile(const std::string& path) {
639 return static_cast<std::ifstream&>(setCheckInputStream(std::make_unique<std::ifstream>(path)))
640 .is_open();
641}
642
643bool AssembleVintf::addKernel(const std::string& kernelArg) {
Yifan Hongb83e56b2018-01-09 14:36:30 -0800644 auto tokens = base::Split(kernelArg, ":");
Yifan Hong8302cea2017-12-18 20:17:05 -0800645 if (tokens.size() <= 1) {
646 std::cerr << "Unrecognized --kernel option '" << kernelArg << "'" << std::endl;
647 return false;
648 }
649 Version kernelVer;
650 if (!parse(tokens.front(), &kernelVer)) {
651 std::cerr << "Unrecognized kernel version '" << tokens.front() << "'" << std::endl;
652 return false;
653 }
654 if (hasKernelVersion(kernelVer)) {
655 std::cerr << "Multiple --kernel for " << kernelVer << " is specified." << std::endl;
656 return false;
657 }
658 for (auto it = tokens.begin() + 1; it != tokens.end(); ++it) {
659 bool opened =
660 static_cast<std::ifstream&>(
661 addKernelConfigInputStream(kernelVer, *it, std::make_unique<std::ifstream>(*it)))
662 .is_open();
663 if (!opened) {
664 std::cerr << "Cannot open file '" << *it << "'." << std::endl;
665 return false;
666 }
667 }
668 return true;
669}
670
671std::unique_ptr<AssembleVintf> AssembleVintf::newInstance() {
672 return std::make_unique<AssembleVintfImpl>();
673}
674
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000675} // namespace vintf
676} // namespace android