blob: 7f86b940e2aa93c25444e6254851308f44f0d9b0 [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 /**
Yifan Hong0f7f7282018-02-02 13:05:43 -0800108 * Set *out to environment variable only if *out is a dummy value (i.e. default constructed).
109 * Return true if *out is set to environment variable, otherwise false.
Yifan Hongeff04662017-12-18 16:27:24 -0800110 */
111 template <typename T>
Yifan Hongf7e8b1b2018-01-23 15:30:35 -0800112 bool getFlagIfUnset(const std::string& envKey, T* out, bool log = true) const {
Yifan Hongeff04662017-12-18 16:27:24 -0800113 bool hasExistingValue = !(*out == T{});
114
115 bool hasEnvValue = false;
116 T envValue;
Yifan Hong8302cea2017-12-18 20:17:05 -0800117 std::string envStrValue = getEnv(envKey);
118 if (!envStrValue.empty()) {
119 if (!parse(envStrValue, &envValue)) {
Yifan Hongf7e8b1b2018-01-23 15:30:35 -0800120 if (log) {
121 std::cerr << "Cannot parse " << envValue << "." << std::endl;
122 }
Yifan Hongeff04662017-12-18 16:27:24 -0800123 return false;
124 }
125 hasEnvValue = true;
126 }
127
128 if (hasExistingValue) {
Yifan Hongf7e8b1b2018-01-23 15:30:35 -0800129 if (hasEnvValue && log) {
Yifan Hongeff04662017-12-18 16:27:24 -0800130 std::cerr << "Warning: cannot override existing value " << *out << " with "
131 << envKey << " (which is " << envValue << ")." << std::endl;
132 }
133 return false;
134 }
135 if (!hasEnvValue) {
Yifan Hongf7e8b1b2018-01-23 15:30:35 -0800136 if (log) {
137 std::cerr << "Warning: " << envKey << " is not specified. Default to " << T{} << "."
138 << std::endl;
139 }
Yifan Hongeff04662017-12-18 16:27:24 -0800140 return false;
141 }
142 *out = envValue;
143 return true;
144 }
145
Yifan Hong8302cea2017-12-18 20:17:05 -0800146 bool getBooleanFlag(const std::string& key) const { return getEnv(key) == std::string("true"); }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800147
Yifan Hong8302cea2017-12-18 20:17:05 -0800148 size_t getIntegerFlag(const std::string& key, size_t defaultValue = 0) const {
149 std::string envValue = getEnv(key);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800150 if (envValue.empty()) {
151 return defaultValue;
152 }
153 size_t value;
154 if (!base::ParseUint(envValue, &value)) {
155 std::cerr << "Error: " << key << " must be a number." << std::endl;
156 return defaultValue;
157 }
158 return value;
159 }
160
Yifan Hong4650ad82017-05-01 17:28:02 -0700161 static std::string read(std::basic_istream<char>& is) {
162 std::stringstream ss;
163 ss << is.rdbuf();
164 return ss.str();
165 }
166
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700167 static bool isCommonConfig(const std::string& path) {
168 return ::android::base::Basename(path) == gBaseConfig;
169 }
170
Yifan Hong079ec242017-08-25 18:53:38 -0700171 // nullptr on any error, otherwise the condition.
172 static Condition generateCondition(const std::string& path) {
173 std::string fname = ::android::base::Basename(path);
174 if (fname.size() <= gConfigPrefix.size() + gConfigSuffix.size() ||
175 !std::equal(gConfigPrefix.begin(), gConfigPrefix.end(), fname.begin()) ||
176 !std::equal(gConfigSuffix.rbegin(), gConfigSuffix.rend(), fname.rbegin())) {
177 return nullptr;
178 }
179
180 std::string sub = fname.substr(gConfigPrefix.size(),
181 fname.size() - gConfigPrefix.size() - gConfigSuffix.size());
182 if (sub.empty()) {
183 return nullptr; // should not happen
184 }
185 for (size_t i = 0; i < sub.size(); ++i) {
186 if (sub[i] == '-') {
187 sub[i] = '_';
188 continue;
189 }
190 if (isalnum(sub[i])) {
191 sub[i] = toupper(sub[i]);
192 continue;
193 }
194 std::cerr << "'" << fname << "' (in " << path
195 << ") is not a valid kernel config file name. Must match regex: "
196 << "android-base(-[0-9a-zA-Z-]+)?\\.cfg" << std::endl;
197 return nullptr;
198 }
199 sub.insert(0, "CONFIG_");
200 return std::make_unique<KernelConfig>(std::move(sub), Tristate::YES);
201 }
202
Yifan Hong8302cea2017-12-18 20:17:05 -0800203 static bool parseFileForKernelConfigs(std::basic_istream<char>& stream,
204 std::vector<KernelConfig>* out) {
Yifan Hong02e94002017-07-10 15:41:56 -0700205 KernelConfigParser parser(true /* processComments */, true /* relaxedFormat */);
Yifan Hong8302cea2017-12-18 20:17:05 -0800206 std::string content = read(stream);
Yifan Hong79efa8a2017-07-06 14:10:28 -0700207 status_t err = parser.process(content.c_str(), content.size());
208 if (err != OK) {
Yifan Hongae53a0e2017-07-07 15:19:06 -0700209 std::cerr << parser.error();
Yifan Hong79efa8a2017-07-06 14:10:28 -0700210 return false;
211 }
212 err = parser.finish();
213 if (err != OK) {
Yifan Hongae53a0e2017-07-07 15:19:06 -0700214 std::cerr << parser.error();
Yifan Hong79efa8a2017-07-06 14:10:28 -0700215 return false;
216 }
217
218 for (auto& configPair : parser.configs()) {
219 out->push_back({});
220 KernelConfig& config = out->back();
221 config.first = std::move(configPair.first);
222 if (!parseKernelConfigTypedValue(configPair.second, &config.second)) {
223 std::cerr << "Unknown value type for key = '" << config.first << "', value = '"
224 << configPair.second << "'\n";
225 return false;
226 }
227 }
228 return true;
229 }
230
Yifan Hong8302cea2017-12-18 20:17:05 -0800231 static bool parseFilesForKernelConfigs(std::vector<NamedIstream>* streams,
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700232 std::vector<ConditionedConfig>* out) {
233 out->clear();
234 ConditionedConfig commonConfig;
235 bool foundCommonConfig = false;
Steve Muckle0bef8682017-07-31 15:47:15 -0700236 bool ret = true;
Yifan Hong8302cea2017-12-18 20:17:05 -0800237
238 for (auto& namedStream : *streams) {
239 if (isCommonConfig(namedStream.name())) {
240 ret &= parseFileForKernelConfigs(namedStream.stream(), &commonConfig.second);
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700241 foundCommonConfig = true;
242 } else {
Yifan Hong8302cea2017-12-18 20:17:05 -0800243 Condition condition = generateCondition(namedStream.name());
Yifan Hong079ec242017-08-25 18:53:38 -0700244 ret &= (condition != nullptr);
245
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700246 std::vector<KernelConfig> kernelConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800247 if ((ret &= parseFileForKernelConfigs(namedStream.stream(), &kernelConfigs)))
Yifan Hong079ec242017-08-25 18:53:38 -0700248 out->emplace_back(std::move(condition), std::move(kernelConfigs));
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700249 }
Steve Muckle0bef8682017-07-31 15:47:15 -0700250 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700251
252 if (!foundCommonConfig) {
Yifan Hong8302cea2017-12-18 20:17:05 -0800253 std::cerr << "No android-base.cfg is found in these paths:" << std::endl;
254 for (auto& namedStream : *streams) {
255 std::cerr << " " << namedStream.name() << std::endl;
256 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700257 }
258 ret &= foundCommonConfig;
259 // first element is always common configs (no conditions).
260 out->insert(out->begin(), std::move(commonConfig));
Steve Muckle0bef8682017-07-31 15:47:15 -0700261 return ret;
262 }
263
Yifan Hong8302cea2017-12-18 20:17:05 -0800264 std::basic_ostream<char>& out() const { return mOutRef == nullptr ? std::cout : *mOutRef; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700265
Yifan Hongdbe9db32017-12-11 19:06:11 -0800266 template <typename S>
Yifan Hongffcaf992018-01-09 17:08:51 -0800267 using Schemas = std::vector<Named<S>>;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800268 using HalManifests = Schemas<HalManifest>;
269 using CompatibilityMatrices = Schemas<CompatibilityMatrix>;
270
271 bool assembleHalManifest(HalManifests* halManifests) {
Yifan Hong4650ad82017-05-01 17:28:02 -0700272 std::string error;
Yifan Hongffcaf992018-01-09 17:08:51 -0800273 HalManifest* halManifest = &halManifests->front().object;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800274 for (auto it = halManifests->begin() + 1; it != halManifests->end(); ++it) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800275 const std::string& path = it->name;
276 HalManifest& halToAdd = it->object;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800277
278 if (halToAdd.level() != Level::UNSPECIFIED) {
279 if (halManifest->level() == Level::UNSPECIFIED) {
280 halManifest->mLevel = halToAdd.level();
281 } else if (halManifest->level() != halToAdd.level()) {
282 std::cerr << "Inconsistent FCM Version in HAL manifests:" << std::endl
Yifan Hongffcaf992018-01-09 17:08:51 -0800283 << " File '" << halManifests->front().name << "' has level "
Yifan Hongdbe9db32017-12-11 19:06:11 -0800284 << halManifest->level() << std::endl
285 << " File '" << path << "' has level " << halToAdd.level()
286 << std::endl;
287 return false;
288 }
289 }
290
Yifan Hongea25dd42017-12-18 17:03:24 -0800291 if (!halManifest->addAllHals(&halToAdd, &error)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800292 std::cerr << "File \"" << path << "\" cannot be added: conflict on HAL \"" << error
293 << "\" with an existing HAL. See <hal> with the same name "
294 << "in previously parsed files or previously declared in this file."
295 << std::endl;
296 return false;
297 }
298 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700299
300 if (halManifest->mType == SchemaType::DEVICE) {
301 if (!getFlag("BOARD_SEPOLICY_VERS", &halManifest->device.mSepolicyVersion)) {
302 return false;
303 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800304 if (!setDeviceFcmVersion(halManifest)) {
305 return false;
306 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700307 }
308
Yifan Hongfeb454e2018-01-09 16:16:40 -0800309 if (halManifest->mType == SchemaType::FRAMEWORK) {
Yifan Honga28729e2018-01-17 13:40:35 -0800310 for (auto&& v : getEnvList("PROVIDED_VNDK_VERSIONS")) {
311 halManifest->framework.mVendorNdks.emplace_back(std::move(v));
312 }
313
Yifan Hong8b8492b2018-01-22 14:08:40 -0800314 for (auto&& v : getEnvList("PLATFORM_SYSTEMSDK_VERSIONS")) {
Yifan Honga28729e2018-01-17 13:40:35 -0800315 halManifest->framework.mSystemSdk.mVersions.emplace(std::move(v));
Yifan Hongfeb454e2018-01-09 16:16:40 -0800316 }
317 }
318
Yifan Hong9aa63702017-05-16 16:37:50 -0700319 if (mOutputMatrix) {
320 CompatibilityMatrix generatedMatrix = halManifest->generateCompatibleMatrix();
321 if (!halManifest->checkCompatibility(generatedMatrix, &error)) {
322 std::cerr << "FATAL ERROR: cannot generate a compatible matrix: " << error
323 << std::endl;
324 }
325 out() << "<!-- \n"
326 " Autogenerated skeleton compatibility matrix. \n"
327 " Use with caution. Modify it to suit your needs.\n"
328 " All HALs are set to optional.\n"
329 " Many entries other than HALs are zero-filled and\n"
330 " require human attention. \n"
331 "-->\n"
Yifan Honga2635c42017-12-12 13:20:33 -0800332 << gCompatibilityMatrixConverter(generatedMatrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700333 } else {
Yifan Honga2635c42017-12-12 13:20:33 -0800334 out() << gHalManifestConverter(*halManifest, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700335 }
336 out().flush();
337
Yifan Hong8302cea2017-12-18 20:17:05 -0800338 if (mCheckFile != nullptr) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700339 CompatibilityMatrix checkMatrix;
Yifan Hong94757062018-02-09 16:36:31 -0800340 if (!gCompatibilityMatrixConverter(&checkMatrix, read(*mCheckFile), &error)) {
341 std::cerr << "Cannot parse check file as a compatibility matrix: " << error
342 << std::endl;
Yifan Hong9aa63702017-05-16 16:37:50 -0700343 return false;
344 }
345 if (!halManifest->checkCompatibility(checkMatrix, &error)) {
346 std::cerr << "Not compatible: " << error << std::endl;
347 return false;
348 }
349 }
350
351 return true;
352 }
353
Yifan Honge88e1672017-08-24 14:42:54 -0700354 bool assembleFrameworkCompatibilityMatrixKernels(CompatibilityMatrix* matrix) {
Yifan Hong8302cea2017-12-18 20:17:05 -0800355 for (auto& pair : mKernels) {
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700356 std::vector<ConditionedConfig> conditionedConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800357 if (!parseFilesForKernelConfigs(&pair.second, &conditionedConfigs)) {
Yifan Honge88e1672017-08-24 14:42:54 -0700358 return false;
359 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700360 for (ConditionedConfig& conditionedConfig : conditionedConfigs) {
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800361 MatrixKernel kernel(KernelVersion{pair.first}, 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) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700371 // Not needed for generating empty manifest for DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE.
372 if (getBooleanFlag("IGNORE_TARGET_FCM_VERSION")) {
373 return true;
374 }
375
Yifan Hongdbe9db32017-12-11 19:06:11 -0800376 size_t shippingApiLevel = getIntegerFlag("PRODUCT_SHIPPING_API_LEVEL");
Yifan Hong9aa63702017-05-16 16:37:50 -0700377
Yifan Hongdbe9db32017-12-11 19:06:11 -0800378 if (manifest->level() != Level::UNSPECIFIED) {
379 return true;
380 }
381 if (!getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
382 manifest->mLevel = Level::LEGACY;
383 return true;
384 }
385 // TODO(b/70628538): Do not infer from Shipping API level.
386 if (shippingApiLevel) {
387 std::cerr << "Warning: Shipping FCM Version is inferred from Shipping API level. "
388 << "Declare Shipping FCM Version in device manifest directly." << std::endl;
Yifan Hongafae1982018-01-11 18:15:24 -0800389 manifest->mLevel = details::convertFromApiLevel(shippingApiLevel);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800390 if (manifest->mLevel == Level::UNSPECIFIED) {
391 std::cerr << "Error: Shipping FCM Version cannot be inferred from Shipping API "
392 << "level " << shippingApiLevel << "."
393 << "Declare Shipping FCM Version in device manifest directly."
394 << std::endl;
395 return false;
396 }
397 return true;
398 }
399 // TODO(b/69638851): should be an error if Shipping API level is not defined.
400 // For now, just leave it empty; when framework compatibility matrix is built,
401 // lowest FCM Version is assumed.
402 std::cerr << "Warning: Shipping FCM Version cannot be inferred, because:" << std::endl
403 << " (1) It is not explicitly declared in device manifest;" << std::endl
404 << " (2) PRODUCT_ENFORCE_VINTF_MANIFEST is set to true;" << std::endl
405 << " (3) PRODUCT_SHIPPING_API_LEVEL is undefined." << std::endl
406 << "Assuming 'unspecified' Shipping FCM Version. " << std::endl
407 << "To remove this warning, define 'level' attribute in device manifest."
408 << std::endl;
409 return true;
410 }
411
412 Level getLowestFcmVersion(const CompatibilityMatrices& matrices) {
413 Level ret = Level::UNSPECIFIED;
414 for (const auto& e : matrices) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800415 if (ret == Level::UNSPECIFIED || ret > e.object.level()) {
416 ret = e.object.level();
Yifan Hongdbe9db32017-12-11 19:06:11 -0800417 }
418 }
419 return ret;
420 }
421
422 bool assembleCompatibilityMatrix(CompatibilityMatrices* matrices) {
423 std::string error;
424 CompatibilityMatrix* matrix = nullptr;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800425 std::unique_ptr<HalManifest> checkManifest;
Yifan Hongffcaf992018-01-09 17:08:51 -0800426 if (matrices->front().object.mType == SchemaType::DEVICE) {
427 matrix = &matrices->front().object;
Yifan Hongfeb454e2018-01-09 16:16:40 -0800428
429 auto vndkVersion = base::Trim(getEnv("REQUIRED_VNDK_VERSION"));
430 if (!vndkVersion.empty()) {
431 auto& valueInMatrix = matrix->device.mVendorNdk;
432 if (!valueInMatrix.version().empty() && valueInMatrix.version() != vndkVersion) {
433 std::cerr << "Hard-coded <vendor-ndk> version in device compatibility matrix ("
434 << matrices->front().name << "), '" << valueInMatrix.version()
435 << "', does not match value inferred "
436 << "from BOARD_VNDK_VERSION '" << vndkVersion << "'" << std::endl;
437 return false;
438 }
439 valueInMatrix = VendorNdk{std::move(vndkVersion)};
440 }
Yifan Honga28729e2018-01-17 13:40:35 -0800441
442 for (auto&& v : getEnvList("BOARD_SYSTEMSDK_VERSIONS")) {
443 matrix->device.mSystemSdk.mVersions.emplace(std::move(v));
444 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800445 }
446
Yifan Hongffcaf992018-01-09 17:08:51 -0800447 if (matrices->front().object.mType == SchemaType::FRAMEWORK) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800448 Level deviceLevel = Level::UNSPECIFIED;
Yifan Hong8302cea2017-12-18 20:17:05 -0800449 if (mCheckFile != nullptr) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800450 checkManifest = std::make_unique<HalManifest>();
Yifan Hong94757062018-02-09 16:36:31 -0800451 if (!gHalManifestConverter(checkManifest.get(), read(*mCheckFile), &error)) {
452 std::cerr << "Cannot parse check file as a HAL manifest: " << error
453 << std::endl;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800454 return false;
455 }
456 deviceLevel = checkManifest->level();
457 }
458
459 if (deviceLevel == Level::UNSPECIFIED) {
460 // For GSI build, legacy devices that do not have a HAL manifest,
461 // and devices in development, merge all compatibility matrices.
462 deviceLevel = getLowestFcmVersion(*matrices);
463 }
464
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700465 matrix = CompatibilityMatrix::combine(deviceLevel, matrices, &error);
466 if (matrix == nullptr) {
467 std::cerr << error << std::endl;
468 return false;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800469 }
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 Hong321ee222018-02-02 12:46:52 -0800475 // Add PLATFORM_SEPOLICY_* to sepolicy.sepolicy-version. Remove dupes.
476 std::set<Version> sepolicyVersions;
477 auto sepolicyVersionStrings = getEnvList("PLATFORM_SEPOLICY_COMPAT_VERSIONS");
478 auto currentSepolicyVersionString = getEnv("PLATFORM_SEPOLICY_VERSION");
479 if (!currentSepolicyVersionString.empty()) {
480 sepolicyVersionStrings.push_back(currentSepolicyVersionString);
481 }
482 for (auto&& s : sepolicyVersionStrings) {
483 Version v;
484 if (!parse(s, &v)) {
485 std::cerr << "Error: unknown sepolicy version '" << s << "' specified by "
486 << (s == currentSepolicyVersionString
487 ? "PLATFORM_SEPOLICY_VERSION"
488 : "PLATFORM_SEPOLICY_COMPAT_VERSIONS")
489 << ".";
490 return false;
491 }
492 sepolicyVersions.insert(v);
493 }
494 for (auto&& v : sepolicyVersions) {
495 matrix->framework.mSepolicy.mSepolicyVersionRanges.emplace_back(v.majorVer,
496 v.minorVer);
Yifan Hong7f6c00c2017-07-06 19:50:29 +0000497 }
Yifan Hongf4135012017-12-18 17:27:19 -0800498
Yifan Hongf7e8b1b2018-01-23 15:30:35 -0800499 getFlagIfUnset("POLICYVERS", &matrix->framework.mSepolicy.mKernelSepolicyVersion,
500 deviceLevel == Level::UNSPECIFIED /* log */);
501 getFlagIfUnset("FRAMEWORK_VBMETA_VERSION", &matrix->framework.mAvbMetaVersion,
502 deviceLevel == Level::UNSPECIFIED /* log */);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800503
504 out() << "<!--" << std::endl;
505 out() << " Input:" << std::endl;
Yifan Hongddae77e2017-12-18 16:57:07 -0800506 for (const auto& e : *matrices) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800507 if (!e.name.empty()) {
Yifan Hong4f67f3b2018-01-11 17:05:47 -0800508 out() << " " << base::Basename(e.name) << std::endl;
Yifan Hongddae77e2017-12-18 16:57:07 -0800509 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800510 }
511 out() << "-->" << std::endl;
Yifan Hong9aa63702017-05-16 16:37:50 -0700512 }
Yifan Honga2635c42017-12-12 13:20:33 -0800513 out() << gCompatibilityMatrixConverter(*matrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700514 out().flush();
515
Yifan Hongdbe9db32017-12-11 19:06:11 -0800516 if (checkManifest != nullptr && getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST") &&
517 !checkManifest->checkCompatibility(*matrix, &error)) {
518 std::cerr << "Not compatible: " << error << std::endl;
519 return false;
Yifan Hong9aa63702017-05-16 16:37:50 -0700520 }
521
522 return true;
523 }
524
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700525 enum AssembleStatus { SUCCESS, FAIL_AND_EXIT, TRY_NEXT };
526 template <typename Schema, typename AssembleFunc>
527 AssembleStatus tryAssemble(const XmlConverter<Schema>& converter, const std::string& schemaName,
Yifan Hong94757062018-02-09 16:36:31 -0800528 AssembleFunc assemble, std::string* error) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800529 Schemas<Schema> schemas;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700530 Schema schema;
Yifan Hong94757062018-02-09 16:36:31 -0800531 if (!converter(&schema, read(mInFiles.front().stream()), error)) {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700532 return TRY_NEXT;
533 }
534 auto firstType = schema.type();
Yifan Hongaa219f52017-12-18 18:51:59 -0800535 schemas.emplace_back(mInFiles.front().name(), std::move(schema));
Yifan Hongdbe9db32017-12-11 19:06:11 -0800536
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700537 for (auto it = mInFiles.begin() + 1; it != mInFiles.end(); ++it) {
538 Schema additionalSchema;
Yifan Hongaa219f52017-12-18 18:51:59 -0800539 const std::string& fileName = it->name();
Yifan Hong94757062018-02-09 16:36:31 -0800540 if (!converter(&additionalSchema, read(it->stream()), error)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800541 std::cerr << "File \"" << fileName << "\" is not a valid " << firstType << " "
542 << schemaName << " (but the first file is a valid " << firstType << " "
Yifan Hong94757062018-02-09 16:36:31 -0800543 << schemaName << "). Error: " << *error << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700544 return FAIL_AND_EXIT;
545 }
546 if (additionalSchema.type() != firstType) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800547 std::cerr << "File \"" << fileName << "\" is a " << additionalSchema.type() << " "
548 << schemaName << " (but a " << firstType << " " << schemaName
549 << " is expected)." << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700550 return FAIL_AND_EXIT;
551 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800552
553 schemas.emplace_back(fileName, std::move(additionalSchema));
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700554 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800555 return assemble(&schemas) ? SUCCESS : FAIL_AND_EXIT;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700556 }
557
Yifan Hong8302cea2017-12-18 20:17:05 -0800558 bool assemble() override {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700559 using std::placeholders::_1;
560 if (mInFiles.empty()) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700561 std::cerr << "Missing input file." << std::endl;
562 return false;
563 }
564
Yifan Hong94757062018-02-09 16:36:31 -0800565 std::string manifestError;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700566 auto status = tryAssemble(gHalManifestConverter, "manifest",
Yifan Hong94757062018-02-09 16:36:31 -0800567 std::bind(&AssembleVintfImpl::assembleHalManifest, this, _1),
568 &manifestError);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700569 if (status == SUCCESS) return true;
570 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000571
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700572 resetInFiles();
Yifan Honga59d2562017-04-18 18:01:16 -0700573
Yifan Hong94757062018-02-09 16:36:31 -0800574 std::string matrixError;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700575 status = tryAssemble(gCompatibilityMatrixConverter, "compatibility matrix",
Yifan Hong94757062018-02-09 16:36:31 -0800576 std::bind(&AssembleVintfImpl::assembleCompatibilityMatrix, this, _1),
577 &matrixError);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700578 if (status == SUCCESS) return true;
579 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000580
Yifan Hong959ee1b2017-04-28 14:37:56 -0700581 std::cerr << "Input file has unknown format." << std::endl
Yifan Hong94757062018-02-09 16:36:31 -0800582 << "Error when attempting to convert to manifest: " << manifestError << std::endl
583 << "Error when attempting to convert to compatibility matrix: " << matrixError
584 << std::endl;
Yifan Hong959ee1b2017-04-28 14:37:56 -0700585 return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000586 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700587
Yifan Hong8302cea2017-12-18 20:17:05 -0800588 std::ostream& setOutputStream(Ostream&& out) override {
589 mOutRef = std::move(out);
590 return *mOutRef;
Yifan Hong9aa63702017-05-16 16:37:50 -0700591 }
592
Yifan Hong8302cea2017-12-18 20:17:05 -0800593 std::istream& addInputStream(const std::string& name, Istream&& in) override {
594 auto it = mInFiles.emplace(mInFiles.end(), name, std::move(in));
595 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700596 }
597
Yifan Hong8302cea2017-12-18 20:17:05 -0800598 std::istream& setCheckInputStream(Istream&& in) override {
599 mCheckFile = std::move(in);
600 return *mCheckFile;
601 }
602
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800603 bool hasKernelVersion(const KernelVersion& kernelVer) const override {
Yifan Hong8302cea2017-12-18 20:17:05 -0800604 return mKernels.find(kernelVer) != mKernels.end();
605 }
606
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800607 std::istream& addKernelConfigInputStream(const KernelVersion& kernelVer,
608 const std::string& name, Istream&& in) override {
Yifan Hong8302cea2017-12-18 20:17:05 -0800609 auto&& kernel = mKernels[kernelVer];
610 auto it = kernel.emplace(kernel.end(), name, std::move(in));
611 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700612 }
613
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700614 void resetInFiles() {
615 for (auto& inFile : mInFiles) {
Yifan Hongaa219f52017-12-18 18:51:59 -0800616 inFile.stream().clear();
617 inFile.stream().seekg(0);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700618 }
619 }
620
Yifan Hong8302cea2017-12-18 20:17:05 -0800621 void setOutputMatrix() override { mOutputMatrix = true; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700622
Yifan Hong8302cea2017-12-18 20:17:05 -0800623 bool setHalsOnly() override {
Yifan Honga2635c42017-12-12 13:20:33 -0800624 if (mSerializeFlags) return false;
625 mSerializeFlags |= SerializeFlag::HALS_ONLY;
626 return true;
627 }
628
Yifan Hong8302cea2017-12-18 20:17:05 -0800629 bool setNoHals() override {
Yifan Honga2635c42017-12-12 13:20:33 -0800630 if (mSerializeFlags) return false;
631 mSerializeFlags |= SerializeFlag::NO_HALS;
632 return true;
633 }
634
Yifan Hong9aa63702017-05-16 16:37:50 -0700635 private:
Yifan Hongaa219f52017-12-18 18:51:59 -0800636 std::vector<NamedIstream> mInFiles;
Yifan Hong8302cea2017-12-18 20:17:05 -0800637 Ostream mOutRef;
638 Istream mCheckFile;
Yifan Hong9aa63702017-05-16 16:37:50 -0700639 bool mOutputMatrix = false;
Yifan Honga2635c42017-12-12 13:20:33 -0800640 SerializeFlags mSerializeFlags = SerializeFlag::EVERYTHING;
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800641 std::map<KernelVersion, std::vector<NamedIstream>> mKernels;
Yifan Hong8302cea2017-12-18 20:17:05 -0800642 std::map<std::string, std::string> mFakeEnv;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000643};
644
Yifan Hong8302cea2017-12-18 20:17:05 -0800645bool AssembleVintf::openOutFile(const std::string& path) {
646 return static_cast<std::ofstream&>(setOutputStream(std::make_unique<std::ofstream>(path)))
647 .is_open();
648}
649
650bool AssembleVintf::openInFile(const std::string& path) {
651 return static_cast<std::ifstream&>(addInputStream(path, std::make_unique<std::ifstream>(path)))
652 .is_open();
653}
654
655bool AssembleVintf::openCheckFile(const std::string& path) {
656 return static_cast<std::ifstream&>(setCheckInputStream(std::make_unique<std::ifstream>(path)))
657 .is_open();
658}
659
660bool AssembleVintf::addKernel(const std::string& kernelArg) {
Yifan Hongb83e56b2018-01-09 14:36:30 -0800661 auto tokens = base::Split(kernelArg, ":");
Yifan Hong8302cea2017-12-18 20:17:05 -0800662 if (tokens.size() <= 1) {
663 std::cerr << "Unrecognized --kernel option '" << kernelArg << "'" << std::endl;
664 return false;
665 }
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800666 KernelVersion kernelVer;
Yifan Hong8302cea2017-12-18 20:17:05 -0800667 if (!parse(tokens.front(), &kernelVer)) {
668 std::cerr << "Unrecognized kernel version '" << tokens.front() << "'" << std::endl;
669 return false;
670 }
671 if (hasKernelVersion(kernelVer)) {
672 std::cerr << "Multiple --kernel for " << kernelVer << " is specified." << std::endl;
673 return false;
674 }
675 for (auto it = tokens.begin() + 1; it != tokens.end(); ++it) {
676 bool opened =
677 static_cast<std::ifstream&>(
678 addKernelConfigInputStream(kernelVer, *it, std::make_unique<std::ifstream>(*it)))
679 .is_open();
680 if (!opened) {
681 std::cerr << "Cannot open file '" << *it << "'." << std::endl;
682 return false;
683 }
684 }
685 return true;
686}
687
688std::unique_ptr<AssembleVintf> AssembleVintf::newInstance() {
689 return std::make_unique<AssembleVintfImpl>();
690}
691
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000692} // namespace vintf
693} // namespace android