blob: ea8c9b32dcfd4934da67e3042385c01b16874050 [file] [log] [blame]
Yifan Hong4d18bcc2017-04-07 21:47:16 +00001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdlib.h>
18#include <unistd.h>
19
20#include <fstream>
21#include <iostream>
Yifan Hong4d18bcc2017-04-07 21:47:16 +000022#include <sstream>
23#include <string>
Yifan Hong8302cea2017-12-18 20:17:05 -080024#include <unordered_map>
Yifan Hong4d18bcc2017-04-07 21:47:16 +000025
Yifan Hong9a8b1a72017-08-25 17:55:33 -070026#include <android-base/file.h>
Yifan Hongdbe9db32017-12-11 19:06:11 -080027#include <android-base/parseint.h>
Yifan Hongb83e56b2018-01-09 14:36:30 -080028#include <android-base/strings.h>
Yifan Hong9a8b1a72017-08-25 17:55:33 -070029
Yifan Hong8302cea2017-12-18 20:17:05 -080030#include <vintf/AssembleVintf.h>
Yifan Hong79efa8a2017-07-06 14:10:28 -070031#include <vintf/KernelConfigParser.h>
Yifan Hong4d18bcc2017-04-07 21:47:16 +000032#include <vintf/parse_string.h>
33#include <vintf/parse_xml.h>
Yifan Hong8302cea2017-12-18 20:17:05 -080034#include "utils.h"
Yifan Hong4d18bcc2017-04-07 21:47:16 +000035
Yifan Hong79efa8a2017-07-06 14:10:28 -070036#define BUFFER_SIZE sysconf(_SC_PAGESIZE)
37
Yifan Hong4d18bcc2017-04-07 21:47:16 +000038namespace android {
39namespace vintf {
40
Yifan Hong9a8b1a72017-08-25 17:55:33 -070041static const std::string gConfigPrefix = "android-base-";
Steve Mucklead6aa912018-07-23 15:44:01 -070042static const std::string gConfigSuffix = ".config";
43static const std::string gBaseConfig = "android-base.config";
Yifan Hong9a8b1a72017-08-25 17:55:33 -070044
Yifan Hongaa219f52017-12-18 18:51:59 -080045// An input stream with a name.
46// The input stream may be an actual file, or a stringstream for testing.
47// It takes ownership on the istream.
48class NamedIstream {
49 public:
50 NamedIstream(const std::string& name, std::unique_ptr<std::istream>&& stream)
51 : mName(name), mStream(std::move(stream)) {}
52 const std::string& name() const { return mName; }
53 std::istream& stream() { return *mStream; }
54
55 private:
56 std::string mName;
57 std::unique_ptr<std::istream> mStream;
58};
59
Yifan Hong4d18bcc2017-04-07 21:47:16 +000060/**
61 * Slurps the device manifest file and add build time flag to it.
62 */
Yifan Hong8302cea2017-12-18 20:17:05 -080063class AssembleVintfImpl : public AssembleVintf {
Yifan Hong9a8b1a72017-08-25 17:55:33 -070064 using Condition = std::unique_ptr<KernelConfig>;
65 using ConditionedConfig = std::pair<Condition, std::vector<KernelConfig> /* configs */>;
66
67 public:
Yifan Hong8302cea2017-12-18 20:17:05 -080068 void setFakeEnv(const std::string& key, const std::string& value) { mFakeEnv[key] = value; }
69
70 std::string getEnv(const std::string& key) const {
71 auto it = mFakeEnv.find(key);
72 if (it != mFakeEnv.end()) {
73 return it->second;
74 }
75 const char* envValue = getenv(key.c_str());
76 return envValue != nullptr ? std::string(envValue) : std::string();
77 }
78
Yifan Honga28729e2018-01-17 13:40:35 -080079 // Get environment variable and split with space.
80 std::vector<std::string> getEnvList(const std::string& key) const {
81 std::vector<std::string> ret;
82 for (auto&& v : base::Split(getEnv(key), " ")) {
83 v = base::Trim(v);
84 if (!v.empty()) {
85 ret.push_back(v);
86 }
87 }
88 return ret;
89 }
90
Yifan Hong8302cea2017-12-18 20:17:05 -080091 template <typename T>
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: "
Steve Mucklead6aa912018-07-23 15:44:01 -0700196 << "android-base(-[0-9a-zA-Z-]+)?\\" << gConfigSuffix
197 << std::endl;
Yifan Hong079ec242017-08-25 18:53:38 -0700198 return nullptr;
199 }
200 sub.insert(0, "CONFIG_");
201 return std::make_unique<KernelConfig>(std::move(sub), Tristate::YES);
202 }
203
Yifan Hong8302cea2017-12-18 20:17:05 -0800204 static bool parseFileForKernelConfigs(std::basic_istream<char>& stream,
205 std::vector<KernelConfig>* out) {
Yifan Hong02e94002017-07-10 15:41:56 -0700206 KernelConfigParser parser(true /* processComments */, true /* relaxedFormat */);
Yifan Hong8302cea2017-12-18 20:17:05 -0800207 std::string content = read(stream);
Yifan Hong79efa8a2017-07-06 14:10:28 -0700208 status_t err = parser.process(content.c_str(), content.size());
209 if (err != OK) {
Yifan Hongae53a0e2017-07-07 15:19:06 -0700210 std::cerr << parser.error();
Yifan Hong79efa8a2017-07-06 14:10:28 -0700211 return false;
212 }
213 err = parser.finish();
214 if (err != OK) {
Yifan Hongae53a0e2017-07-07 15:19:06 -0700215 std::cerr << parser.error();
Yifan Hong79efa8a2017-07-06 14:10:28 -0700216 return false;
217 }
218
219 for (auto& configPair : parser.configs()) {
220 out->push_back({});
221 KernelConfig& config = out->back();
222 config.first = std::move(configPair.first);
223 if (!parseKernelConfigTypedValue(configPair.second, &config.second)) {
224 std::cerr << "Unknown value type for key = '" << config.first << "', value = '"
225 << configPair.second << "'\n";
226 return false;
227 }
228 }
229 return true;
230 }
231
Yifan Hong8302cea2017-12-18 20:17:05 -0800232 static bool parseFilesForKernelConfigs(std::vector<NamedIstream>* streams,
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700233 std::vector<ConditionedConfig>* out) {
234 out->clear();
235 ConditionedConfig commonConfig;
236 bool foundCommonConfig = false;
Steve Muckle0bef8682017-07-31 15:47:15 -0700237 bool ret = true;
Yifan Hong8302cea2017-12-18 20:17:05 -0800238
239 for (auto& namedStream : *streams) {
240 if (isCommonConfig(namedStream.name())) {
241 ret &= parseFileForKernelConfigs(namedStream.stream(), &commonConfig.second);
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700242 foundCommonConfig = true;
243 } else {
Yifan Hong8302cea2017-12-18 20:17:05 -0800244 Condition condition = generateCondition(namedStream.name());
Yifan Hong079ec242017-08-25 18:53:38 -0700245 ret &= (condition != nullptr);
246
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700247 std::vector<KernelConfig> kernelConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800248 if ((ret &= parseFileForKernelConfigs(namedStream.stream(), &kernelConfigs)))
Yifan Hong079ec242017-08-25 18:53:38 -0700249 out->emplace_back(std::move(condition), std::move(kernelConfigs));
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700250 }
Steve Muckle0bef8682017-07-31 15:47:15 -0700251 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700252
253 if (!foundCommonConfig) {
Steve Mucklead6aa912018-07-23 15:44:01 -0700254 std::cerr << "No " << gBaseConfig << " is found in these paths:" << std::endl;
Yifan Hong8302cea2017-12-18 20:17:05 -0800255 for (auto& namedStream : *streams) {
256 std::cerr << " " << namedStream.name() << std::endl;
257 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700258 }
259 ret &= foundCommonConfig;
260 // first element is always common configs (no conditions).
261 out->insert(out->begin(), std::move(commonConfig));
Steve Muckle0bef8682017-07-31 15:47:15 -0700262 return ret;
263 }
264
Yifan Hong8302cea2017-12-18 20:17:05 -0800265 std::basic_ostream<char>& out() const { return mOutRef == nullptr ? std::cout : *mOutRef; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700266
Yifan Hong1044fd12018-02-27 14:51:54 -0800267 // If -c is provided, check it.
268 bool checkDualFile(const HalManifest& manifest, const CompatibilityMatrix& matrix) {
269 if (getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
270 std::string error;
271 if (!manifest.checkCompatibility(matrix, &error)) {
272 std::cerr << "Not compatible: " << error << std::endl;
273 return false;
274 }
275 }
276
277 // Check HALs in device manifest that are not in framework matrix.
278 if (getBooleanFlag("VINTF_ENFORCE_NO_UNUSED_HALS")) {
279 auto unused = manifest.checkUnusedHals(matrix);
280 if (!unused.empty()) {
281 std::cerr << "Error: The following instances are in the device manifest but "
282 << "not specified in framework compatibility matrix: " << std::endl
283 << " " << android::base::Join(unused, "\n ") << std::endl
284 << "Suggested fix:" << std::endl
285 << "1. Check for any typos in device manifest or framework compatibility "
286 << "matrices with FCM version >= " << matrix.level() << "." << std::endl
287 << "2. Add them to any framework compatibility matrix with FCM "
288 << "version >= " << matrix.level() << " where applicable." << std::endl
289 << "3. Add them to DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE."
290 << std::endl;
291
292 return false;
293 }
294 }
295 return true;
296 }
297
Yifan Hongdbe9db32017-12-11 19:06:11 -0800298 template <typename S>
Yifan Hongffcaf992018-01-09 17:08:51 -0800299 using Schemas = std::vector<Named<S>>;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800300 using HalManifests = Schemas<HalManifest>;
301 using CompatibilityMatrices = Schemas<CompatibilityMatrix>;
302
Steven Moreland5875afc2018-04-05 13:14:08 -0700303 template <typename M>
304 void outputInputs(const Schemas<M>& inputs) {
305 out() << "<!--" << std::endl;
306 out() << " Input:" << std::endl;
307 for (const auto& e : inputs) {
308 if (!e.name.empty()) {
309 out() << " " << base::Basename(e.name) << std::endl;
310 }
311 }
312 out() << "-->" << std::endl;
313 }
314
Yifan Hongdbe9db32017-12-11 19:06:11 -0800315 bool assembleHalManifest(HalManifests* halManifests) {
Yifan Hong4650ad82017-05-01 17:28:02 -0700316 std::string error;
Yifan Hongffcaf992018-01-09 17:08:51 -0800317 HalManifest* halManifest = &halManifests->front().object;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800318 for (auto it = halManifests->begin() + 1; it != halManifests->end(); ++it) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800319 const std::string& path = it->name;
Yifan Hong0a35ab12018-06-29 11:32:57 -0700320 HalManifest& manifestToAdd = it->object;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800321
Yifan Hong0a35ab12018-06-29 11:32:57 -0700322 if (manifestToAdd.level() != Level::UNSPECIFIED) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800323 if (halManifest->level() == Level::UNSPECIFIED) {
Yifan Hong0a35ab12018-06-29 11:32:57 -0700324 halManifest->mLevel = manifestToAdd.level();
325 } else if (halManifest->level() != manifestToAdd.level()) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800326 std::cerr << "Inconsistent FCM Version in HAL manifests:" << std::endl
Yifan Hongffcaf992018-01-09 17:08:51 -0800327 << " File '" << halManifests->front().name << "' has level "
Yifan Hongdbe9db32017-12-11 19:06:11 -0800328 << halManifest->level() << std::endl
Yifan Hong0a35ab12018-06-29 11:32:57 -0700329 << " File '" << path << "' has level " << manifestToAdd.level()
Yifan Hongdbe9db32017-12-11 19:06:11 -0800330 << std::endl;
331 return false;
332 }
333 }
334
Steven Morelandb1392772018-05-01 11:21:35 -0700335 // TODO(b/78943004): add everything
Yifan Hong0a35ab12018-06-29 11:32:57 -0700336 if (!halManifest->addAllHals(&manifestToAdd, &error)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800337 std::cerr << "File \"" << path << "\" cannot be added: conflict on HAL \"" << error
338 << "\" with an existing HAL. See <hal> with the same name "
339 << "in previously parsed files or previously declared in this file."
340 << std::endl;
341 return false;
342 }
Yifan Hong0a35ab12018-06-29 11:32:57 -0700343
344 // Check that manifestToAdd is empty.
345 if (!manifestToAdd.empty()) {
346 std::cerr << "File \"" << path << "\" contains extraneous entries and attributes. "
347 << "This is currently unsupported (b/78943004); it can only contain "
348 << "<hal>s and attribute \"type\". Only the first input "
349 << "file to assemble_vintf can contain other things. "
350 << "Remaining entries and attributes are:" << std::endl
351 << gHalManifestConverter(manifestToAdd);
352 return false;
353 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800354 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700355
356 if (halManifest->mType == SchemaType::DEVICE) {
Steven Morelandb1392772018-05-01 11:21:35 -0700357 (void)getFlagIfUnset("BOARD_SEPOLICY_VERS", &halManifest->device.mSepolicyVersion);
358
Yifan Hongdbe9db32017-12-11 19:06:11 -0800359 if (!setDeviceFcmVersion(halManifest)) {
360 return false;
361 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700362 }
363
Yifan Hongfeb454e2018-01-09 16:16:40 -0800364 if (halManifest->mType == SchemaType::FRAMEWORK) {
Yifan Honga28729e2018-01-17 13:40:35 -0800365 for (auto&& v : getEnvList("PROVIDED_VNDK_VERSIONS")) {
366 halManifest->framework.mVendorNdks.emplace_back(std::move(v));
367 }
368
Yifan Hong8b8492b2018-01-22 14:08:40 -0800369 for (auto&& v : getEnvList("PLATFORM_SYSTEMSDK_VERSIONS")) {
Yifan Honga28729e2018-01-17 13:40:35 -0800370 halManifest->framework.mSystemSdk.mVersions.emplace(std::move(v));
Yifan Hongfeb454e2018-01-09 16:16:40 -0800371 }
372 }
373
Steven Moreland5875afc2018-04-05 13:14:08 -0700374 outputInputs(*halManifests);
375
Yifan Hong9aa63702017-05-16 16:37:50 -0700376 if (mOutputMatrix) {
377 CompatibilityMatrix generatedMatrix = halManifest->generateCompatibleMatrix();
378 if (!halManifest->checkCompatibility(generatedMatrix, &error)) {
379 std::cerr << "FATAL ERROR: cannot generate a compatible matrix: " << error
380 << std::endl;
381 }
382 out() << "<!-- \n"
383 " Autogenerated skeleton compatibility matrix. \n"
384 " Use with caution. Modify it to suit your needs.\n"
385 " All HALs are set to optional.\n"
386 " Many entries other than HALs are zero-filled and\n"
387 " require human attention. \n"
388 "-->\n"
Yifan Honga2635c42017-12-12 13:20:33 -0800389 << gCompatibilityMatrixConverter(generatedMatrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700390 } else {
Yifan Honga2635c42017-12-12 13:20:33 -0800391 out() << gHalManifestConverter(*halManifest, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700392 }
393 out().flush();
394
Yifan Hong8302cea2017-12-18 20:17:05 -0800395 if (mCheckFile != nullptr) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700396 CompatibilityMatrix checkMatrix;
Yifan Hong94757062018-02-09 16:36:31 -0800397 if (!gCompatibilityMatrixConverter(&checkMatrix, read(*mCheckFile), &error)) {
398 std::cerr << "Cannot parse check file as a compatibility matrix: " << error
399 << std::endl;
Yifan Hong9aa63702017-05-16 16:37:50 -0700400 return false;
401 }
Yifan Hong1044fd12018-02-27 14:51:54 -0800402 if (!checkDualFile(*halManifest, checkMatrix)) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700403 return false;
404 }
405 }
406
407 return true;
408 }
409
Yifan Honge88e1672017-08-24 14:42:54 -0700410 bool assembleFrameworkCompatibilityMatrixKernels(CompatibilityMatrix* matrix) {
Yifan Hong8302cea2017-12-18 20:17:05 -0800411 for (auto& pair : mKernels) {
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700412 std::vector<ConditionedConfig> conditionedConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800413 if (!parseFilesForKernelConfigs(&pair.second, &conditionedConfigs)) {
Yifan Honge88e1672017-08-24 14:42:54 -0700414 return false;
415 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700416 for (ConditionedConfig& conditionedConfig : conditionedConfigs) {
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800417 MatrixKernel kernel(KernelVersion{pair.first}, std::move(conditionedConfig.second));
Yifan Hong079ec242017-08-25 18:53:38 -0700418 if (conditionedConfig.first != nullptr)
419 kernel.mConditions.push_back(std::move(*conditionedConfig.first));
420 matrix->framework.mKernels.push_back(std::move(kernel));
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700421 }
Yifan Honge88e1672017-08-24 14:42:54 -0700422 }
423 return true;
424 }
425
Yifan Hongdbe9db32017-12-11 19:06:11 -0800426 bool setDeviceFcmVersion(HalManifest* manifest) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700427 // Not needed for generating empty manifest for DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE.
Yifan Hong2eee1982018-03-29 10:55:30 -0700428 if (getBooleanFlag("VINTF_IGNORE_TARGET_FCM_VERSION")) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700429 return true;
430 }
431
Yifan Hongdbe9db32017-12-11 19:06:11 -0800432 size_t shippingApiLevel = getIntegerFlag("PRODUCT_SHIPPING_API_LEVEL");
Yifan Hong9aa63702017-05-16 16:37:50 -0700433
Yifan Hongdbe9db32017-12-11 19:06:11 -0800434 if (manifest->level() != Level::UNSPECIFIED) {
435 return true;
436 }
437 if (!getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
438 manifest->mLevel = Level::LEGACY;
439 return true;
440 }
441 // TODO(b/70628538): Do not infer from Shipping API level.
442 if (shippingApiLevel) {
443 std::cerr << "Warning: Shipping FCM Version is inferred from Shipping API level. "
444 << "Declare Shipping FCM Version in device manifest directly." << std::endl;
Yifan Hongafae1982018-01-11 18:15:24 -0800445 manifest->mLevel = details::convertFromApiLevel(shippingApiLevel);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800446 if (manifest->mLevel == Level::UNSPECIFIED) {
447 std::cerr << "Error: Shipping FCM Version cannot be inferred from Shipping API "
448 << "level " << shippingApiLevel << "."
449 << "Declare Shipping FCM Version in device manifest directly."
450 << std::endl;
451 return false;
452 }
453 return true;
454 }
455 // TODO(b/69638851): should be an error if Shipping API level is not defined.
456 // For now, just leave it empty; when framework compatibility matrix is built,
457 // lowest FCM Version is assumed.
458 std::cerr << "Warning: Shipping FCM Version cannot be inferred, because:" << std::endl
459 << " (1) It is not explicitly declared in device manifest;" << std::endl
460 << " (2) PRODUCT_ENFORCE_VINTF_MANIFEST is set to true;" << std::endl
461 << " (3) PRODUCT_SHIPPING_API_LEVEL is undefined." << std::endl
462 << "Assuming 'unspecified' Shipping FCM Version. " << std::endl
463 << "To remove this warning, define 'level' attribute in device manifest."
464 << std::endl;
465 return true;
466 }
467
468 Level getLowestFcmVersion(const CompatibilityMatrices& matrices) {
469 Level ret = Level::UNSPECIFIED;
470 for (const auto& e : matrices) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800471 if (ret == Level::UNSPECIFIED || ret > e.object.level()) {
472 ret = e.object.level();
Yifan Hongdbe9db32017-12-11 19:06:11 -0800473 }
474 }
475 return ret;
476 }
477
478 bool assembleCompatibilityMatrix(CompatibilityMatrices* matrices) {
479 std::string error;
480 CompatibilityMatrix* matrix = nullptr;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800481 std::unique_ptr<HalManifest> checkManifest;
Yifan Hongffcaf992018-01-09 17:08:51 -0800482 if (matrices->front().object.mType == SchemaType::DEVICE) {
483 matrix = &matrices->front().object;
Yifan Hongfeb454e2018-01-09 16:16:40 -0800484
485 auto vndkVersion = base::Trim(getEnv("REQUIRED_VNDK_VERSION"));
486 if (!vndkVersion.empty()) {
487 auto& valueInMatrix = matrix->device.mVendorNdk;
488 if (!valueInMatrix.version().empty() && valueInMatrix.version() != vndkVersion) {
489 std::cerr << "Hard-coded <vendor-ndk> version in device compatibility matrix ("
490 << matrices->front().name << "), '" << valueInMatrix.version()
491 << "', does not match value inferred "
492 << "from BOARD_VNDK_VERSION '" << vndkVersion << "'" << std::endl;
493 return false;
494 }
495 valueInMatrix = VendorNdk{std::move(vndkVersion)};
496 }
Yifan Honga28729e2018-01-17 13:40:35 -0800497
498 for (auto&& v : getEnvList("BOARD_SYSTEMSDK_VERSIONS")) {
499 matrix->device.mSystemSdk.mVersions.emplace(std::move(v));
500 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800501 }
502
Yifan Hongffcaf992018-01-09 17:08:51 -0800503 if (matrices->front().object.mType == SchemaType::FRAMEWORK) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800504 Level deviceLevel = Level::UNSPECIFIED;
Yifan Hong8302cea2017-12-18 20:17:05 -0800505 if (mCheckFile != nullptr) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800506 checkManifest = std::make_unique<HalManifest>();
Yifan Hong94757062018-02-09 16:36:31 -0800507 if (!gHalManifestConverter(checkManifest.get(), read(*mCheckFile), &error)) {
508 std::cerr << "Cannot parse check file as a HAL manifest: " << error
509 << std::endl;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800510 return false;
511 }
512 deviceLevel = checkManifest->level();
513 }
514
515 if (deviceLevel == Level::UNSPECIFIED) {
516 // For GSI build, legacy devices that do not have a HAL manifest,
517 // and devices in development, merge all compatibility matrices.
518 deviceLevel = getLowestFcmVersion(*matrices);
519 }
520
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700521 matrix = CompatibilityMatrix::combine(deviceLevel, matrices, &error);
522 if (matrix == nullptr) {
523 std::cerr << error << std::endl;
524 return false;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800525 }
526
Yifan Honge88e1672017-08-24 14:42:54 -0700527 if (!assembleFrameworkCompatibilityMatrixKernels(matrix)) {
528 return false;
Yifan Hong79efa8a2017-07-06 14:10:28 -0700529 }
Yifan Honge88e1672017-08-24 14:42:54 -0700530
Yifan Hong321ee222018-02-02 12:46:52 -0800531 // Add PLATFORM_SEPOLICY_* to sepolicy.sepolicy-version. Remove dupes.
532 std::set<Version> sepolicyVersions;
533 auto sepolicyVersionStrings = getEnvList("PLATFORM_SEPOLICY_COMPAT_VERSIONS");
534 auto currentSepolicyVersionString = getEnv("PLATFORM_SEPOLICY_VERSION");
535 if (!currentSepolicyVersionString.empty()) {
536 sepolicyVersionStrings.push_back(currentSepolicyVersionString);
537 }
538 for (auto&& s : sepolicyVersionStrings) {
539 Version v;
540 if (!parse(s, &v)) {
541 std::cerr << "Error: unknown sepolicy version '" << s << "' specified by "
542 << (s == currentSepolicyVersionString
543 ? "PLATFORM_SEPOLICY_VERSION"
544 : "PLATFORM_SEPOLICY_COMPAT_VERSIONS")
545 << ".";
546 return false;
547 }
548 sepolicyVersions.insert(v);
549 }
550 for (auto&& v : sepolicyVersions) {
551 matrix->framework.mSepolicy.mSepolicyVersionRanges.emplace_back(v.majorVer,
552 v.minorVer);
Yifan Hong7f6c00c2017-07-06 19:50:29 +0000553 }
Yifan Hongf4135012017-12-18 17:27:19 -0800554
Yifan Hongf7e8b1b2018-01-23 15:30:35 -0800555 getFlagIfUnset("POLICYVERS", &matrix->framework.mSepolicy.mKernelSepolicyVersion,
556 deviceLevel == Level::UNSPECIFIED /* log */);
557 getFlagIfUnset("FRAMEWORK_VBMETA_VERSION", &matrix->framework.mAvbMetaVersion,
558 deviceLevel == Level::UNSPECIFIED /* log */);
Yifan Hong9aa63702017-05-16 16:37:50 -0700559 }
Steven Moreland5875afc2018-04-05 13:14:08 -0700560 outputInputs(*matrices);
Yifan Honga2635c42017-12-12 13:20:33 -0800561 out() << gCompatibilityMatrixConverter(*matrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700562 out().flush();
563
Yifan Hong1044fd12018-02-27 14:51:54 -0800564 if (checkManifest != nullptr && !checkDualFile(*checkManifest, *matrix)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800565 return false;
Yifan Hong9aa63702017-05-16 16:37:50 -0700566 }
567
568 return true;
569 }
570
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700571 enum AssembleStatus { SUCCESS, FAIL_AND_EXIT, TRY_NEXT };
572 template <typename Schema, typename AssembleFunc>
573 AssembleStatus tryAssemble(const XmlConverter<Schema>& converter, const std::string& schemaName,
Yifan Hong94757062018-02-09 16:36:31 -0800574 AssembleFunc assemble, std::string* error) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800575 Schemas<Schema> schemas;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700576 Schema schema;
Yifan Hong94757062018-02-09 16:36:31 -0800577 if (!converter(&schema, read(mInFiles.front().stream()), error)) {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700578 return TRY_NEXT;
579 }
580 auto firstType = schema.type();
Yifan Hongaa219f52017-12-18 18:51:59 -0800581 schemas.emplace_back(mInFiles.front().name(), std::move(schema));
Yifan Hongdbe9db32017-12-11 19:06:11 -0800582
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700583 for (auto it = mInFiles.begin() + 1; it != mInFiles.end(); ++it) {
584 Schema additionalSchema;
Yifan Hongaa219f52017-12-18 18:51:59 -0800585 const std::string& fileName = it->name();
Yifan Hong94757062018-02-09 16:36:31 -0800586 if (!converter(&additionalSchema, read(it->stream()), error)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800587 std::cerr << "File \"" << fileName << "\" is not a valid " << firstType << " "
588 << schemaName << " (but the first file is a valid " << firstType << " "
Yifan Hong94757062018-02-09 16:36:31 -0800589 << schemaName << "). Error: " << *error << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700590 return FAIL_AND_EXIT;
591 }
592 if (additionalSchema.type() != firstType) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800593 std::cerr << "File \"" << fileName << "\" is a " << additionalSchema.type() << " "
594 << schemaName << " (but a " << firstType << " " << schemaName
595 << " is expected)." << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700596 return FAIL_AND_EXIT;
597 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800598
599 schemas.emplace_back(fileName, std::move(additionalSchema));
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700600 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800601 return assemble(&schemas) ? SUCCESS : FAIL_AND_EXIT;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700602 }
603
Yifan Hong8302cea2017-12-18 20:17:05 -0800604 bool assemble() override {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700605 using std::placeholders::_1;
606 if (mInFiles.empty()) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700607 std::cerr << "Missing input file." << std::endl;
608 return false;
609 }
610
Yifan Hong94757062018-02-09 16:36:31 -0800611 std::string manifestError;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700612 auto status = tryAssemble(gHalManifestConverter, "manifest",
Yifan Hong94757062018-02-09 16:36:31 -0800613 std::bind(&AssembleVintfImpl::assembleHalManifest, this, _1),
614 &manifestError);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700615 if (status == SUCCESS) return true;
616 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000617
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700618 resetInFiles();
Yifan Honga59d2562017-04-18 18:01:16 -0700619
Yifan Hong94757062018-02-09 16:36:31 -0800620 std::string matrixError;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700621 status = tryAssemble(gCompatibilityMatrixConverter, "compatibility matrix",
Yifan Hong94757062018-02-09 16:36:31 -0800622 std::bind(&AssembleVintfImpl::assembleCompatibilityMatrix, this, _1),
623 &matrixError);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700624 if (status == SUCCESS) return true;
625 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000626
Yifan Hong959ee1b2017-04-28 14:37:56 -0700627 std::cerr << "Input file has unknown format." << std::endl
Yifan Hong94757062018-02-09 16:36:31 -0800628 << "Error when attempting to convert to manifest: " << manifestError << std::endl
629 << "Error when attempting to convert to compatibility matrix: " << matrixError
630 << std::endl;
Yifan Hong959ee1b2017-04-28 14:37:56 -0700631 return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000632 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700633
Yifan Hong8302cea2017-12-18 20:17:05 -0800634 std::ostream& setOutputStream(Ostream&& out) override {
635 mOutRef = std::move(out);
636 return *mOutRef;
Yifan Hong9aa63702017-05-16 16:37:50 -0700637 }
638
Yifan Hong8302cea2017-12-18 20:17:05 -0800639 std::istream& addInputStream(const std::string& name, Istream&& in) override {
640 auto it = mInFiles.emplace(mInFiles.end(), name, std::move(in));
641 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700642 }
643
Yifan Hong8302cea2017-12-18 20:17:05 -0800644 std::istream& setCheckInputStream(Istream&& in) override {
645 mCheckFile = std::move(in);
646 return *mCheckFile;
647 }
648
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800649 bool hasKernelVersion(const KernelVersion& kernelVer) const override {
Yifan Hong8302cea2017-12-18 20:17:05 -0800650 return mKernels.find(kernelVer) != mKernels.end();
651 }
652
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800653 std::istream& addKernelConfigInputStream(const KernelVersion& kernelVer,
654 const std::string& name, Istream&& in) override {
Yifan Hong8302cea2017-12-18 20:17:05 -0800655 auto&& kernel = mKernels[kernelVer];
656 auto it = kernel.emplace(kernel.end(), name, std::move(in));
657 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700658 }
659
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700660 void resetInFiles() {
661 for (auto& inFile : mInFiles) {
Yifan Hongaa219f52017-12-18 18:51:59 -0800662 inFile.stream().clear();
663 inFile.stream().seekg(0);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700664 }
665 }
666
Yifan Hong8302cea2017-12-18 20:17:05 -0800667 void setOutputMatrix() override { mOutputMatrix = true; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700668
Yifan Hong8302cea2017-12-18 20:17:05 -0800669 bool setHalsOnly() override {
Yifan Honga2635c42017-12-12 13:20:33 -0800670 if (mSerializeFlags) return false;
671 mSerializeFlags |= SerializeFlag::HALS_ONLY;
672 return true;
673 }
674
Yifan Hong8302cea2017-12-18 20:17:05 -0800675 bool setNoHals() override {
Yifan Honga2635c42017-12-12 13:20:33 -0800676 if (mSerializeFlags) return false;
677 mSerializeFlags |= SerializeFlag::NO_HALS;
678 return true;
679 }
680
Yifan Hong9aa63702017-05-16 16:37:50 -0700681 private:
Yifan Hongaa219f52017-12-18 18:51:59 -0800682 std::vector<NamedIstream> mInFiles;
Yifan Hong8302cea2017-12-18 20:17:05 -0800683 Ostream mOutRef;
684 Istream mCheckFile;
Yifan Hong9aa63702017-05-16 16:37:50 -0700685 bool mOutputMatrix = false;
Yifan Honga2635c42017-12-12 13:20:33 -0800686 SerializeFlags mSerializeFlags = SerializeFlag::EVERYTHING;
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800687 std::map<KernelVersion, std::vector<NamedIstream>> mKernels;
Yifan Hong8302cea2017-12-18 20:17:05 -0800688 std::map<std::string, std::string> mFakeEnv;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000689};
690
Yifan Hong8302cea2017-12-18 20:17:05 -0800691bool AssembleVintf::openOutFile(const std::string& path) {
692 return static_cast<std::ofstream&>(setOutputStream(std::make_unique<std::ofstream>(path)))
693 .is_open();
694}
695
696bool AssembleVintf::openInFile(const std::string& path) {
697 return static_cast<std::ifstream&>(addInputStream(path, std::make_unique<std::ifstream>(path)))
698 .is_open();
699}
700
701bool AssembleVintf::openCheckFile(const std::string& path) {
702 return static_cast<std::ifstream&>(setCheckInputStream(std::make_unique<std::ifstream>(path)))
703 .is_open();
704}
705
706bool AssembleVintf::addKernel(const std::string& kernelArg) {
Yifan Hongb83e56b2018-01-09 14:36:30 -0800707 auto tokens = base::Split(kernelArg, ":");
Yifan Hong8302cea2017-12-18 20:17:05 -0800708 if (tokens.size() <= 1) {
709 std::cerr << "Unrecognized --kernel option '" << kernelArg << "'" << std::endl;
710 return false;
711 }
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800712 KernelVersion kernelVer;
Yifan Hong8302cea2017-12-18 20:17:05 -0800713 if (!parse(tokens.front(), &kernelVer)) {
714 std::cerr << "Unrecognized kernel version '" << tokens.front() << "'" << std::endl;
715 return false;
716 }
717 if (hasKernelVersion(kernelVer)) {
718 std::cerr << "Multiple --kernel for " << kernelVer << " is specified." << std::endl;
719 return false;
720 }
721 for (auto it = tokens.begin() + 1; it != tokens.end(); ++it) {
722 bool opened =
723 static_cast<std::ifstream&>(
724 addKernelConfigInputStream(kernelVer, *it, std::make_unique<std::ifstream>(*it)))
725 .is_open();
726 if (!opened) {
727 std::cerr << "Cannot open file '" << *it << "'." << std::endl;
728 return false;
729 }
730 }
731 return true;
732}
733
734std::unique_ptr<AssembleVintf> AssembleVintf::newInstance() {
735 return std::make_unique<AssembleVintfImpl>();
736}
737
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000738} // namespace vintf
739} // namespace android