blob: e19096ee471bf278a4a0916a991ebd4e7800bac8 [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
79 template <typename T>
80 bool getFlag(const std::string& key, T* value) const {
81 std::string envValue = getEnv(key);
82 if (envValue.empty()) {
83 std::cerr << "Warning: " << key << " is missing, defaulted to " << (*value) << "."
Yifan Hong488e16a2017-07-11 13:50:41 -070084 << std::endl;
85 return true;
Yifan Hong4d18bcc2017-04-07 21:47:16 +000086 }
87
88 if (!parse(envValue, value)) {
89 std::cerr << "Cannot parse " << envValue << "." << std::endl;
90 return false;
91 }
92 return true;
93 }
94
Yifan Hongeff04662017-12-18 16:27:24 -080095 /**
96 * Set *out to environment variable if *out is not a dummy value (i.e. default constructed).
97 */
98 template <typename T>
Yifan Hong8302cea2017-12-18 20:17:05 -080099 bool getFlagIfUnset(const std::string& envKey, T* out) const {
Yifan Hongeff04662017-12-18 16:27:24 -0800100 bool hasExistingValue = !(*out == T{});
101
102 bool hasEnvValue = false;
103 T envValue;
Yifan Hong8302cea2017-12-18 20:17:05 -0800104 std::string envStrValue = getEnv(envKey);
105 if (!envStrValue.empty()) {
106 if (!parse(envStrValue, &envValue)) {
107 std::cerr << "Cannot parse " << envValue << "." << std::endl;
Yifan Hongeff04662017-12-18 16:27:24 -0800108 return false;
109 }
110 hasEnvValue = true;
111 }
112
113 if (hasExistingValue) {
114 if (hasEnvValue) {
115 std::cerr << "Warning: cannot override existing value " << *out << " with "
116 << envKey << " (which is " << envValue << ")." << std::endl;
117 }
118 return false;
119 }
120 if (!hasEnvValue) {
121 std::cerr << "Warning: " << envKey << " is not specified. Default to " << T{} << "."
122 << std::endl;
123 return false;
124 }
125 *out = envValue;
126 return true;
127 }
128
Yifan Hong8302cea2017-12-18 20:17:05 -0800129 bool getBooleanFlag(const std::string& key) const { return getEnv(key) == std::string("true"); }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800130
Yifan Hong8302cea2017-12-18 20:17:05 -0800131 size_t getIntegerFlag(const std::string& key, size_t defaultValue = 0) const {
132 std::string envValue = getEnv(key);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800133 if (envValue.empty()) {
134 return defaultValue;
135 }
136 size_t value;
137 if (!base::ParseUint(envValue, &value)) {
138 std::cerr << "Error: " << key << " must be a number." << std::endl;
139 return defaultValue;
140 }
141 return value;
142 }
143
Yifan Hong4650ad82017-05-01 17:28:02 -0700144 static std::string read(std::basic_istream<char>& is) {
145 std::stringstream ss;
146 ss << is.rdbuf();
147 return ss.str();
148 }
149
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700150 static bool isCommonConfig(const std::string& path) {
151 return ::android::base::Basename(path) == gBaseConfig;
152 }
153
Yifan Hong079ec242017-08-25 18:53:38 -0700154 // nullptr on any error, otherwise the condition.
155 static Condition generateCondition(const std::string& path) {
156 std::string fname = ::android::base::Basename(path);
157 if (fname.size() <= gConfigPrefix.size() + gConfigSuffix.size() ||
158 !std::equal(gConfigPrefix.begin(), gConfigPrefix.end(), fname.begin()) ||
159 !std::equal(gConfigSuffix.rbegin(), gConfigSuffix.rend(), fname.rbegin())) {
160 return nullptr;
161 }
162
163 std::string sub = fname.substr(gConfigPrefix.size(),
164 fname.size() - gConfigPrefix.size() - gConfigSuffix.size());
165 if (sub.empty()) {
166 return nullptr; // should not happen
167 }
168 for (size_t i = 0; i < sub.size(); ++i) {
169 if (sub[i] == '-') {
170 sub[i] = '_';
171 continue;
172 }
173 if (isalnum(sub[i])) {
174 sub[i] = toupper(sub[i]);
175 continue;
176 }
177 std::cerr << "'" << fname << "' (in " << path
178 << ") is not a valid kernel config file name. Must match regex: "
179 << "android-base(-[0-9a-zA-Z-]+)?\\.cfg" << std::endl;
180 return nullptr;
181 }
182 sub.insert(0, "CONFIG_");
183 return std::make_unique<KernelConfig>(std::move(sub), Tristate::YES);
184 }
185
Yifan Hong8302cea2017-12-18 20:17:05 -0800186 static bool parseFileForKernelConfigs(std::basic_istream<char>& stream,
187 std::vector<KernelConfig>* out) {
Yifan Hong02e94002017-07-10 15:41:56 -0700188 KernelConfigParser parser(true /* processComments */, true /* relaxedFormat */);
Yifan Hong8302cea2017-12-18 20:17:05 -0800189 std::string content = read(stream);
Yifan Hong79efa8a2017-07-06 14:10:28 -0700190 status_t err = parser.process(content.c_str(), content.size());
191 if (err != OK) {
Yifan Hongae53a0e2017-07-07 15:19:06 -0700192 std::cerr << parser.error();
Yifan Hong79efa8a2017-07-06 14:10:28 -0700193 return false;
194 }
195 err = parser.finish();
196 if (err != OK) {
Yifan Hongae53a0e2017-07-07 15:19:06 -0700197 std::cerr << parser.error();
Yifan Hong79efa8a2017-07-06 14:10:28 -0700198 return false;
199 }
200
201 for (auto& configPair : parser.configs()) {
202 out->push_back({});
203 KernelConfig& config = out->back();
204 config.first = std::move(configPair.first);
205 if (!parseKernelConfigTypedValue(configPair.second, &config.second)) {
206 std::cerr << "Unknown value type for key = '" << config.first << "', value = '"
207 << configPair.second << "'\n";
208 return false;
209 }
210 }
211 return true;
212 }
213
Yifan Hong8302cea2017-12-18 20:17:05 -0800214 static bool parseFilesForKernelConfigs(std::vector<NamedIstream>* streams,
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700215 std::vector<ConditionedConfig>* out) {
216 out->clear();
217 ConditionedConfig commonConfig;
218 bool foundCommonConfig = false;
Steve Muckle0bef8682017-07-31 15:47:15 -0700219 bool ret = true;
Yifan Hong8302cea2017-12-18 20:17:05 -0800220
221 for (auto& namedStream : *streams) {
222 if (isCommonConfig(namedStream.name())) {
223 ret &= parseFileForKernelConfigs(namedStream.stream(), &commonConfig.second);
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700224 foundCommonConfig = true;
225 } else {
Yifan Hong8302cea2017-12-18 20:17:05 -0800226 Condition condition = generateCondition(namedStream.name());
Yifan Hong079ec242017-08-25 18:53:38 -0700227 ret &= (condition != nullptr);
228
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700229 std::vector<KernelConfig> kernelConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800230 if ((ret &= parseFileForKernelConfigs(namedStream.stream(), &kernelConfigs)))
Yifan Hong079ec242017-08-25 18:53:38 -0700231 out->emplace_back(std::move(condition), std::move(kernelConfigs));
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700232 }
Steve Muckle0bef8682017-07-31 15:47:15 -0700233 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700234
235 if (!foundCommonConfig) {
Yifan Hong8302cea2017-12-18 20:17:05 -0800236 std::cerr << "No android-base.cfg is found in these paths:" << std::endl;
237 for (auto& namedStream : *streams) {
238 std::cerr << " " << namedStream.name() << std::endl;
239 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700240 }
241 ret &= foundCommonConfig;
242 // first element is always common configs (no conditions).
243 out->insert(out->begin(), std::move(commonConfig));
Steve Muckle0bef8682017-07-31 15:47:15 -0700244 return ret;
245 }
246
Yifan Hong8302cea2017-12-18 20:17:05 -0800247 std::basic_ostream<char>& out() const { return mOutRef == nullptr ? std::cout : *mOutRef; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700248
Yifan Hongdbe9db32017-12-11 19:06:11 -0800249 template <typename S>
Yifan Hongffcaf992018-01-09 17:08:51 -0800250 using Schemas = std::vector<Named<S>>;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800251 using HalManifests = Schemas<HalManifest>;
252 using CompatibilityMatrices = Schemas<CompatibilityMatrix>;
253
254 bool assembleHalManifest(HalManifests* halManifests) {
Yifan Hong4650ad82017-05-01 17:28:02 -0700255 std::string error;
Yifan Hongffcaf992018-01-09 17:08:51 -0800256 HalManifest* halManifest = &halManifests->front().object;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800257 for (auto it = halManifests->begin() + 1; it != halManifests->end(); ++it) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800258 const std::string& path = it->name;
259 HalManifest& halToAdd = it->object;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800260
261 if (halToAdd.level() != Level::UNSPECIFIED) {
262 if (halManifest->level() == Level::UNSPECIFIED) {
263 halManifest->mLevel = halToAdd.level();
264 } else if (halManifest->level() != halToAdd.level()) {
265 std::cerr << "Inconsistent FCM Version in HAL manifests:" << std::endl
Yifan Hongffcaf992018-01-09 17:08:51 -0800266 << " File '" << halManifests->front().name << "' has level "
Yifan Hongdbe9db32017-12-11 19:06:11 -0800267 << halManifest->level() << std::endl
268 << " File '" << path << "' has level " << halToAdd.level()
269 << std::endl;
270 return false;
271 }
272 }
273
Yifan Hongea25dd42017-12-18 17:03:24 -0800274 if (!halManifest->addAllHals(&halToAdd, &error)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800275 std::cerr << "File \"" << path << "\" cannot be added: conflict on HAL \"" << error
276 << "\" with an existing HAL. See <hal> with the same name "
277 << "in previously parsed files or previously declared in this file."
278 << std::endl;
279 return false;
280 }
281 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700282
283 if (halManifest->mType == SchemaType::DEVICE) {
284 if (!getFlag("BOARD_SEPOLICY_VERS", &halManifest->device.mSepolicyVersion)) {
285 return false;
286 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800287 if (!setDeviceFcmVersion(halManifest)) {
288 return false;
289 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700290 }
291
Yifan Hongfeb454e2018-01-09 16:16:40 -0800292 if (halManifest->mType == SchemaType::FRAMEWORK) {
293 for (auto&& v : base::Split(getEnv("PROVIDED_VNDK_VERSIONS"), " ")) {
294 v = base::Trim(v);
295 if (!v.empty()) {
296 halManifest->framework.mVendorNdks.emplace_back(std::move(v));
297 }
298 }
299 }
300
Yifan Hong9aa63702017-05-16 16:37:50 -0700301 if (mOutputMatrix) {
302 CompatibilityMatrix generatedMatrix = halManifest->generateCompatibleMatrix();
303 if (!halManifest->checkCompatibility(generatedMatrix, &error)) {
304 std::cerr << "FATAL ERROR: cannot generate a compatible matrix: " << error
305 << std::endl;
306 }
307 out() << "<!-- \n"
308 " Autogenerated skeleton compatibility matrix. \n"
309 " Use with caution. Modify it to suit your needs.\n"
310 " All HALs are set to optional.\n"
311 " Many entries other than HALs are zero-filled and\n"
312 " require human attention. \n"
313 "-->\n"
Yifan Honga2635c42017-12-12 13:20:33 -0800314 << gCompatibilityMatrixConverter(generatedMatrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700315 } else {
Yifan Honga2635c42017-12-12 13:20:33 -0800316 out() << gHalManifestConverter(*halManifest, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700317 }
318 out().flush();
319
Yifan Hong8302cea2017-12-18 20:17:05 -0800320 if (mCheckFile != nullptr) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700321 CompatibilityMatrix checkMatrix;
Yifan Hong8302cea2017-12-18 20:17:05 -0800322 if (!gCompatibilityMatrixConverter(&checkMatrix, read(*mCheckFile))) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700323 std::cerr << "Cannot parse check file as a compatibility matrix: "
324 << gCompatibilityMatrixConverter.lastError() << std::endl;
325 return false;
326 }
327 if (!halManifest->checkCompatibility(checkMatrix, &error)) {
328 std::cerr << "Not compatible: " << error << std::endl;
329 return false;
330 }
331 }
332
333 return true;
334 }
335
Yifan Honge88e1672017-08-24 14:42:54 -0700336 bool assembleFrameworkCompatibilityMatrixKernels(CompatibilityMatrix* matrix) {
Yifan Hong8302cea2017-12-18 20:17:05 -0800337 for (auto& pair : mKernels) {
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700338 std::vector<ConditionedConfig> conditionedConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800339 if (!parseFilesForKernelConfigs(&pair.second, &conditionedConfigs)) {
Yifan Honge88e1672017-08-24 14:42:54 -0700340 return false;
341 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700342 for (ConditionedConfig& conditionedConfig : conditionedConfigs) {
Yifan Hong48602df2017-08-28 13:04:12 -0700343 MatrixKernel kernel(KernelVersion{pair.first.majorVer, pair.first.minorVer, 0u},
344 std::move(conditionedConfig.second));
Yifan Hong079ec242017-08-25 18:53:38 -0700345 if (conditionedConfig.first != nullptr)
346 kernel.mConditions.push_back(std::move(*conditionedConfig.first));
347 matrix->framework.mKernels.push_back(std::move(kernel));
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700348 }
Yifan Honge88e1672017-08-24 14:42:54 -0700349 }
350 return true;
351 }
352
Yifan Hongdbe9db32017-12-11 19:06:11 -0800353 bool setDeviceFcmVersion(HalManifest* manifest) {
354 size_t shippingApiLevel = getIntegerFlag("PRODUCT_SHIPPING_API_LEVEL");
Yifan Hong9aa63702017-05-16 16:37:50 -0700355
Yifan Hongdbe9db32017-12-11 19:06:11 -0800356 if (manifest->level() != Level::UNSPECIFIED) {
357 return true;
358 }
359 if (!getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
360 manifest->mLevel = Level::LEGACY;
361 return true;
362 }
363 // TODO(b/70628538): Do not infer from Shipping API level.
364 if (shippingApiLevel) {
365 std::cerr << "Warning: Shipping FCM Version is inferred from Shipping API level. "
366 << "Declare Shipping FCM Version in device manifest directly." << std::endl;
Yifan Hongafae1982018-01-11 18:15:24 -0800367 manifest->mLevel = details::convertFromApiLevel(shippingApiLevel);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800368 if (manifest->mLevel == Level::UNSPECIFIED) {
369 std::cerr << "Error: Shipping FCM Version cannot be inferred from Shipping API "
370 << "level " << shippingApiLevel << "."
371 << "Declare Shipping FCM Version in device manifest directly."
372 << std::endl;
373 return false;
374 }
375 return true;
376 }
377 // TODO(b/69638851): should be an error if Shipping API level is not defined.
378 // For now, just leave it empty; when framework compatibility matrix is built,
379 // lowest FCM Version is assumed.
380 std::cerr << "Warning: Shipping FCM Version cannot be inferred, because:" << std::endl
381 << " (1) It is not explicitly declared in device manifest;" << std::endl
382 << " (2) PRODUCT_ENFORCE_VINTF_MANIFEST is set to true;" << std::endl
383 << " (3) PRODUCT_SHIPPING_API_LEVEL is undefined." << std::endl
384 << "Assuming 'unspecified' Shipping FCM Version. " << std::endl
385 << "To remove this warning, define 'level' attribute in device manifest."
386 << std::endl;
387 return true;
388 }
389
390 Level getLowestFcmVersion(const CompatibilityMatrices& matrices) {
391 Level ret = Level::UNSPECIFIED;
392 for (const auto& e : matrices) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800393 if (ret == Level::UNSPECIFIED || ret > e.object.level()) {
394 ret = e.object.level();
Yifan Hongdbe9db32017-12-11 19:06:11 -0800395 }
396 }
397 return ret;
398 }
399
400 bool assembleCompatibilityMatrix(CompatibilityMatrices* matrices) {
401 std::string error;
402 CompatibilityMatrix* matrix = nullptr;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800403 std::unique_ptr<HalManifest> checkManifest;
Yifan Hongffcaf992018-01-09 17:08:51 -0800404 if (matrices->front().object.mType == SchemaType::DEVICE) {
405 matrix = &matrices->front().object;
Yifan Hongfeb454e2018-01-09 16:16:40 -0800406
407 auto vndkVersion = base::Trim(getEnv("REQUIRED_VNDK_VERSION"));
408 if (!vndkVersion.empty()) {
409 auto& valueInMatrix = matrix->device.mVendorNdk;
410 if (!valueInMatrix.version().empty() && valueInMatrix.version() != vndkVersion) {
411 std::cerr << "Hard-coded <vendor-ndk> version in device compatibility matrix ("
412 << matrices->front().name << "), '" << valueInMatrix.version()
413 << "', does not match value inferred "
414 << "from BOARD_VNDK_VERSION '" << vndkVersion << "'" << std::endl;
415 return false;
416 }
417 valueInMatrix = VendorNdk{std::move(vndkVersion)};
418 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800419 }
420
Yifan Hongffcaf992018-01-09 17:08:51 -0800421 if (matrices->front().object.mType == SchemaType::FRAMEWORK) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800422 Level deviceLevel = Level::UNSPECIFIED;
Yifan Hong8302cea2017-12-18 20:17:05 -0800423 if (mCheckFile != nullptr) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800424 checkManifest = std::make_unique<HalManifest>();
Yifan Hong8302cea2017-12-18 20:17:05 -0800425 if (!gHalManifestConverter(checkManifest.get(), read(*mCheckFile))) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800426 std::cerr << "Cannot parse check file as a HAL manifest: "
427 << gHalManifestConverter.lastError() << std::endl;
428 return false;
429 }
430 deviceLevel = checkManifest->level();
431 }
432
433 if (deviceLevel == Level::UNSPECIFIED) {
434 // For GSI build, legacy devices that do not have a HAL manifest,
435 // and devices in development, merge all compatibility matrices.
436 deviceLevel = getLowestFcmVersion(*matrices);
437 }
438
Yifan Hongddae77e2017-12-18 16:57:07 -0800439 if (deviceLevel == Level::UNSPECIFIED) {
440 // building empty.xml
Yifan Hongffcaf992018-01-09 17:08:51 -0800441 matrix = &matrices->front().object;
Yifan Hongddae77e2017-12-18 16:57:07 -0800442 } else {
443 matrix = CompatibilityMatrix::combine(deviceLevel, matrices, &error);
444 if (matrix == nullptr) {
445 std::cerr << error << std::endl;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800446 return false;
447 }
448 }
449
Yifan Honge88e1672017-08-24 14:42:54 -0700450 if (!assembleFrameworkCompatibilityMatrixKernels(matrix)) {
451 return false;
Yifan Hong79efa8a2017-07-06 14:10:28 -0700452 }
Yifan Honge88e1672017-08-24 14:42:54 -0700453
Yifan Hongf4135012017-12-18 17:27:19 -0800454 // set sepolicy.sepolicy-version to BOARD_SEPOLICY_VERS when none is specified.
455 std::vector<VersionRange>* sepolicyVrs =
456 &matrix->framework.mSepolicy.mSepolicyVersionRanges;
457 VersionRange sepolicyVr;
458 if (!sepolicyVrs->empty()) sepolicyVr = sepolicyVrs->front();
459 if (getFlagIfUnset("BOARD_SEPOLICY_VERS", &sepolicyVr)) {
460 *sepolicyVrs = {{sepolicyVr}};
Yifan Hong7f6c00c2017-07-06 19:50:29 +0000461 }
Yifan Hongf4135012017-12-18 17:27:19 -0800462
463 getFlagIfUnset("POLICYVERS", &matrix->framework.mSepolicy.mKernelSepolicyVersion);
464 getFlagIfUnset("FRAMEWORK_VBMETA_VERSION", &matrix->framework.mAvbMetaVersion);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800465
466 out() << "<!--" << std::endl;
467 out() << " Input:" << std::endl;
Yifan Hongddae77e2017-12-18 16:57:07 -0800468 for (const auto& e : *matrices) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800469 if (!e.name.empty()) {
Yifan Hong4f67f3b2018-01-11 17:05:47 -0800470 out() << " " << base::Basename(e.name) << std::endl;
Yifan Hongddae77e2017-12-18 16:57:07 -0800471 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800472 }
473 out() << "-->" << std::endl;
Yifan Hong9aa63702017-05-16 16:37:50 -0700474 }
Yifan Honga2635c42017-12-12 13:20:33 -0800475 out() << gCompatibilityMatrixConverter(*matrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700476 out().flush();
477
Yifan Hongdbe9db32017-12-11 19:06:11 -0800478 if (checkManifest != nullptr && getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST") &&
479 !checkManifest->checkCompatibility(*matrix, &error)) {
480 std::cerr << "Not compatible: " << error << std::endl;
481 return false;
Yifan Hong9aa63702017-05-16 16:37:50 -0700482 }
483
484 return true;
485 }
486
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700487 enum AssembleStatus { SUCCESS, FAIL_AND_EXIT, TRY_NEXT };
488 template <typename Schema, typename AssembleFunc>
489 AssembleStatus tryAssemble(const XmlConverter<Schema>& converter, const std::string& schemaName,
490 AssembleFunc assemble) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800491 Schemas<Schema> schemas;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700492 Schema schema;
Yifan Hongaa219f52017-12-18 18:51:59 -0800493 if (!converter(&schema, read(mInFiles.front().stream()))) {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700494 return TRY_NEXT;
495 }
496 auto firstType = schema.type();
Yifan Hongaa219f52017-12-18 18:51:59 -0800497 schemas.emplace_back(mInFiles.front().name(), std::move(schema));
Yifan Hongdbe9db32017-12-11 19:06:11 -0800498
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700499 for (auto it = mInFiles.begin() + 1; it != mInFiles.end(); ++it) {
500 Schema additionalSchema;
Yifan Hongaa219f52017-12-18 18:51:59 -0800501 const std::string& fileName = it->name();
502 if (!converter(&additionalSchema, read(it->stream()))) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800503 std::cerr << "File \"" << fileName << "\" is not a valid " << firstType << " "
504 << schemaName << " (but the first file is a valid " << firstType << " "
505 << schemaName << "). Error: " << converter.lastError() << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700506 return FAIL_AND_EXIT;
507 }
508 if (additionalSchema.type() != firstType) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800509 std::cerr << "File \"" << fileName << "\" is a " << additionalSchema.type() << " "
510 << schemaName << " (but a " << firstType << " " << schemaName
511 << " is expected)." << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700512 return FAIL_AND_EXIT;
513 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800514
515 schemas.emplace_back(fileName, std::move(additionalSchema));
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700516 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800517 return assemble(&schemas) ? SUCCESS : FAIL_AND_EXIT;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700518 }
519
Yifan Hong8302cea2017-12-18 20:17:05 -0800520 bool assemble() override {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700521 using std::placeholders::_1;
522 if (mInFiles.empty()) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700523 std::cerr << "Missing input file." << std::endl;
524 return false;
525 }
526
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700527 auto status = tryAssemble(gHalManifestConverter, "manifest",
Yifan Hong8302cea2017-12-18 20:17:05 -0800528 std::bind(&AssembleVintfImpl::assembleHalManifest, this, _1));
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700529 if (status == SUCCESS) return true;
530 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000531
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700532 resetInFiles();
Yifan Honga59d2562017-04-18 18:01:16 -0700533
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700534 status = tryAssemble(gCompatibilityMatrixConverter, "compatibility matrix",
Yifan Hong8302cea2017-12-18 20:17:05 -0800535 std::bind(&AssembleVintfImpl::assembleCompatibilityMatrix, this, _1));
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700536 if (status == SUCCESS) return true;
537 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000538
Yifan Hong959ee1b2017-04-28 14:37:56 -0700539 std::cerr << "Input file has unknown format." << std::endl
540 << "Error when attempting to convert to manifest: "
541 << gHalManifestConverter.lastError() << std::endl
542 << "Error when attempting to convert to compatibility matrix: "
543 << gCompatibilityMatrixConverter.lastError() << std::endl;
544 return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000545 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700546
Yifan Hong8302cea2017-12-18 20:17:05 -0800547 std::ostream& setOutputStream(Ostream&& out) override {
548 mOutRef = std::move(out);
549 return *mOutRef;
Yifan Hong9aa63702017-05-16 16:37:50 -0700550 }
551
Yifan Hong8302cea2017-12-18 20:17:05 -0800552 std::istream& addInputStream(const std::string& name, Istream&& in) override {
553 auto it = mInFiles.emplace(mInFiles.end(), name, std::move(in));
554 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700555 }
556
Yifan Hong8302cea2017-12-18 20:17:05 -0800557 std::istream& setCheckInputStream(Istream&& in) override {
558 mCheckFile = std::move(in);
559 return *mCheckFile;
560 }
561
562 bool hasKernelVersion(const Version& kernelVer) const override {
563 return mKernels.find(kernelVer) != mKernels.end();
564 }
565
566 std::istream& addKernelConfigInputStream(const Version& kernelVer, const std::string& name,
567 Istream&& in) override {
568 auto&& kernel = mKernels[kernelVer];
569 auto it = kernel.emplace(kernel.end(), name, std::move(in));
570 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700571 }
572
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700573 void resetInFiles() {
574 for (auto& inFile : mInFiles) {
Yifan Hongaa219f52017-12-18 18:51:59 -0800575 inFile.stream().clear();
576 inFile.stream().seekg(0);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700577 }
578 }
579
Yifan Hong8302cea2017-12-18 20:17:05 -0800580 void setOutputMatrix() override { mOutputMatrix = true; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700581
Yifan Hong8302cea2017-12-18 20:17:05 -0800582 bool setHalsOnly() override {
Yifan Honga2635c42017-12-12 13:20:33 -0800583 if (mSerializeFlags) return false;
584 mSerializeFlags |= SerializeFlag::HALS_ONLY;
585 return true;
586 }
587
Yifan Hong8302cea2017-12-18 20:17:05 -0800588 bool setNoHals() override {
Yifan Honga2635c42017-12-12 13:20:33 -0800589 if (mSerializeFlags) return false;
590 mSerializeFlags |= SerializeFlag::NO_HALS;
591 return true;
592 }
593
Yifan Hong9aa63702017-05-16 16:37:50 -0700594 private:
Yifan Hongaa219f52017-12-18 18:51:59 -0800595 std::vector<NamedIstream> mInFiles;
Yifan Hong8302cea2017-12-18 20:17:05 -0800596 Ostream mOutRef;
597 Istream mCheckFile;
Yifan Hong9aa63702017-05-16 16:37:50 -0700598 bool mOutputMatrix = false;
Yifan Honga2635c42017-12-12 13:20:33 -0800599 SerializeFlags mSerializeFlags = SerializeFlag::EVERYTHING;
Yifan Hong8302cea2017-12-18 20:17:05 -0800600 std::map<Version, std::vector<NamedIstream>> mKernels;
601 std::map<std::string, std::string> mFakeEnv;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000602};
603
Yifan Hong8302cea2017-12-18 20:17:05 -0800604bool AssembleVintf::openOutFile(const std::string& path) {
605 return static_cast<std::ofstream&>(setOutputStream(std::make_unique<std::ofstream>(path)))
606 .is_open();
607}
608
609bool AssembleVintf::openInFile(const std::string& path) {
610 return static_cast<std::ifstream&>(addInputStream(path, std::make_unique<std::ifstream>(path)))
611 .is_open();
612}
613
614bool AssembleVintf::openCheckFile(const std::string& path) {
615 return static_cast<std::ifstream&>(setCheckInputStream(std::make_unique<std::ifstream>(path)))
616 .is_open();
617}
618
619bool AssembleVintf::addKernel(const std::string& kernelArg) {
Yifan Hongb83e56b2018-01-09 14:36:30 -0800620 auto tokens = base::Split(kernelArg, ":");
Yifan Hong8302cea2017-12-18 20:17:05 -0800621 if (tokens.size() <= 1) {
622 std::cerr << "Unrecognized --kernel option '" << kernelArg << "'" << std::endl;
623 return false;
624 }
625 Version kernelVer;
626 if (!parse(tokens.front(), &kernelVer)) {
627 std::cerr << "Unrecognized kernel version '" << tokens.front() << "'" << std::endl;
628 return false;
629 }
630 if (hasKernelVersion(kernelVer)) {
631 std::cerr << "Multiple --kernel for " << kernelVer << " is specified." << std::endl;
632 return false;
633 }
634 for (auto it = tokens.begin() + 1; it != tokens.end(); ++it) {
635 bool opened =
636 static_cast<std::ifstream&>(
637 addKernelConfigInputStream(kernelVer, *it, std::make_unique<std::ifstream>(*it)))
638 .is_open();
639 if (!opened) {
640 std::cerr << "Cannot open file '" << *it << "'." << std::endl;
641 return false;
642 }
643 }
644 return true;
645}
646
647std::unique_ptr<AssembleVintf> AssembleVintf::newInstance() {
648 return std::make_unique<AssembleVintfImpl>();
649}
650
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000651} // namespace vintf
652} // namespace android