blob: bcefc7313f7be47937509c7a9b51526f15ee37b1 [file] [log] [blame]
Yifan Hong4d18bcc2017-04-07 21:47:16 +00001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdlib.h>
18#include <unistd.h>
19
20#include <fstream>
21#include <iostream>
Yifan Hong4d18bcc2017-04-07 21:47:16 +000022#include <sstream>
23#include <string>
Yifan Hong8302cea2017-12-18 20:17:05 -080024#include <unordered_map>
Yifan Hong4d18bcc2017-04-07 21:47:16 +000025
Yifan Hong9a8b1a72017-08-25 17:55:33 -070026#include <android-base/file.h>
Yifan Hongdbe9db32017-12-11 19:06:11 -080027#include <android-base/parseint.h>
Yifan Hongb83e56b2018-01-09 14:36:30 -080028#include <android-base/strings.h>
Yifan Hong9a8b1a72017-08-25 17:55:33 -070029
Yifan Hong8302cea2017-12-18 20:17:05 -080030#include <vintf/AssembleVintf.h>
Yifan Hong79efa8a2017-07-06 14:10:28 -070031#include <vintf/KernelConfigParser.h>
Yifan Hong4d18bcc2017-04-07 21:47:16 +000032#include <vintf/parse_string.h>
33#include <vintf/parse_xml.h>
Yifan Hong8302cea2017-12-18 20:17:05 -080034#include "utils.h"
Yifan Hong4d18bcc2017-04-07 21:47:16 +000035
Yifan Hong79efa8a2017-07-06 14:10:28 -070036#define BUFFER_SIZE sysconf(_SC_PAGESIZE)
37
Yifan Hong4d18bcc2017-04-07 21:47:16 +000038namespace android {
39namespace vintf {
40
Yifan Hong9a8b1a72017-08-25 17:55:33 -070041static const std::string gConfigPrefix = "android-base-";
Steve Mucklead6aa912018-07-23 15:44:01 -070042static const std::string gConfigSuffix = ".config";
43static const std::string gBaseConfig = "android-base.config";
Yifan Hong9a8b1a72017-08-25 17:55:33 -070044
Yifan Hongaa219f52017-12-18 18:51:59 -080045// An input stream with a name.
46// The input stream may be an actual file, or a stringstream for testing.
47// It takes ownership on the istream.
48class NamedIstream {
49 public:
50 NamedIstream(const std::string& name, std::unique_ptr<std::istream>&& stream)
51 : mName(name), mStream(std::move(stream)) {}
52 const std::string& name() const { return mName; }
53 std::istream& stream() { return *mStream; }
54
55 private:
56 std::string mName;
57 std::unique_ptr<std::istream> mStream;
58};
59
Yifan Hong4d18bcc2017-04-07 21:47:16 +000060/**
61 * Slurps the device manifest file and add build time flag to it.
62 */
Yifan Hong8302cea2017-12-18 20:17:05 -080063class AssembleVintfImpl : public AssembleVintf {
Yifan Hong9a8b1a72017-08-25 17:55:33 -070064 using Condition = std::unique_ptr<KernelConfig>;
65 using ConditionedConfig = std::pair<Condition, std::vector<KernelConfig> /* configs */>;
66
67 public:
Yifan Hong8302cea2017-12-18 20:17:05 -080068 void setFakeEnv(const std::string& key, const std::string& value) { mFakeEnv[key] = value; }
69
70 std::string getEnv(const std::string& key) const {
71 auto it = mFakeEnv.find(key);
72 if (it != mFakeEnv.end()) {
73 return it->second;
74 }
75 const char* envValue = getenv(key.c_str());
76 return envValue != nullptr ? std::string(envValue) : std::string();
77 }
78
Yifan Honga28729e2018-01-17 13:40:35 -080079 // Get environment variable and split with space.
80 std::vector<std::string> getEnvList(const std::string& key) const {
81 std::vector<std::string> ret;
82 for (auto&& v : base::Split(getEnv(key), " ")) {
83 v = base::Trim(v);
84 if (!v.empty()) {
85 ret.push_back(v);
86 }
87 }
88 return ret;
89 }
90
Yifan Hong8302cea2017-12-18 20:17:05 -080091 template <typename T>
Yifan Hong4dec91d2018-08-13 12:37:47 -070092 bool getFlag(const std::string& key, T* value, bool log = true) const {
Yifan Hong8302cea2017-12-18 20:17:05 -080093 std::string envValue = getEnv(key);
94 if (envValue.empty()) {
Yifan Hong4dec91d2018-08-13 12:37:47 -070095 if (log) {
96 std::cerr << "Warning: " << key << " is missing, defaulted to " << (*value) << "."
97 << std::endl;
98 }
Yifan Hong488e16a2017-07-11 13:50:41 -070099 return true;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000100 }
101
102 if (!parse(envValue, value)) {
103 std::cerr << "Cannot parse " << envValue << "." << std::endl;
104 return false;
105 }
106 return true;
107 }
108
Yifan Hongeff04662017-12-18 16:27:24 -0800109 /**
Yifan Hong0f7f7282018-02-02 13:05:43 -0800110 * Set *out to environment variable only if *out is a dummy value (i.e. default constructed).
111 * Return true if *out is set to environment variable, otherwise false.
Yifan Hongeff04662017-12-18 16:27:24 -0800112 */
113 template <typename T>
Yifan Hongf7e8b1b2018-01-23 15:30:35 -0800114 bool getFlagIfUnset(const std::string& envKey, T* out, bool log = true) const {
Yifan Hongeff04662017-12-18 16:27:24 -0800115 bool hasExistingValue = !(*out == T{});
116
117 bool hasEnvValue = false;
118 T envValue;
Yifan Hong8302cea2017-12-18 20:17:05 -0800119 std::string envStrValue = getEnv(envKey);
120 if (!envStrValue.empty()) {
121 if (!parse(envStrValue, &envValue)) {
Yifan Hongf7e8b1b2018-01-23 15:30:35 -0800122 if (log) {
123 std::cerr << "Cannot parse " << envValue << "." << std::endl;
124 }
Yifan Hongeff04662017-12-18 16:27:24 -0800125 return false;
126 }
127 hasEnvValue = true;
128 }
129
130 if (hasExistingValue) {
Yifan Hongf7e8b1b2018-01-23 15:30:35 -0800131 if (hasEnvValue && log) {
Yifan Hongeff04662017-12-18 16:27:24 -0800132 std::cerr << "Warning: cannot override existing value " << *out << " with "
133 << envKey << " (which is " << envValue << ")." << std::endl;
134 }
135 return false;
136 }
137 if (!hasEnvValue) {
Yifan Hongf7e8b1b2018-01-23 15:30:35 -0800138 if (log) {
139 std::cerr << "Warning: " << envKey << " is not specified. Default to " << T{} << "."
140 << std::endl;
141 }
Yifan Hongeff04662017-12-18 16:27:24 -0800142 return false;
143 }
144 *out = envValue;
145 return true;
146 }
147
Yifan Hong8302cea2017-12-18 20:17:05 -0800148 bool getBooleanFlag(const std::string& key) const { return getEnv(key) == std::string("true"); }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800149
Yifan Hong8302cea2017-12-18 20:17:05 -0800150 size_t getIntegerFlag(const std::string& key, size_t defaultValue = 0) const {
151 std::string envValue = getEnv(key);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800152 if (envValue.empty()) {
153 return defaultValue;
154 }
155 size_t value;
156 if (!base::ParseUint(envValue, &value)) {
157 std::cerr << "Error: " << key << " must be a number." << std::endl;
158 return defaultValue;
159 }
160 return value;
161 }
162
Yifan Hong4650ad82017-05-01 17:28:02 -0700163 static std::string read(std::basic_istream<char>& is) {
164 std::stringstream ss;
165 ss << is.rdbuf();
166 return ss.str();
167 }
168
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700169 static bool isCommonConfig(const std::string& path) {
170 return ::android::base::Basename(path) == gBaseConfig;
171 }
172
Yifan Hong079ec242017-08-25 18:53:38 -0700173 // nullptr on any error, otherwise the condition.
174 static Condition generateCondition(const std::string& path) {
175 std::string fname = ::android::base::Basename(path);
176 if (fname.size() <= gConfigPrefix.size() + gConfigSuffix.size() ||
177 !std::equal(gConfigPrefix.begin(), gConfigPrefix.end(), fname.begin()) ||
178 !std::equal(gConfigSuffix.rbegin(), gConfigSuffix.rend(), fname.rbegin())) {
179 return nullptr;
180 }
181
182 std::string sub = fname.substr(gConfigPrefix.size(),
183 fname.size() - gConfigPrefix.size() - gConfigSuffix.size());
184 if (sub.empty()) {
185 return nullptr; // should not happen
186 }
187 for (size_t i = 0; i < sub.size(); ++i) {
188 if (sub[i] == '-') {
189 sub[i] = '_';
190 continue;
191 }
192 if (isalnum(sub[i])) {
193 sub[i] = toupper(sub[i]);
194 continue;
195 }
196 std::cerr << "'" << fname << "' (in " << path
197 << ") is not a valid kernel config file name. Must match regex: "
Steve Mucklead6aa912018-07-23 15:44:01 -0700198 << "android-base(-[0-9a-zA-Z-]+)?\\" << gConfigSuffix
199 << std::endl;
Yifan Hong079ec242017-08-25 18:53:38 -0700200 return nullptr;
201 }
202 sub.insert(0, "CONFIG_");
203 return std::make_unique<KernelConfig>(std::move(sub), Tristate::YES);
204 }
205
Yifan Hong8302cea2017-12-18 20:17:05 -0800206 static bool parseFileForKernelConfigs(std::basic_istream<char>& stream,
207 std::vector<KernelConfig>* out) {
Yifan Hong02e94002017-07-10 15:41:56 -0700208 KernelConfigParser parser(true /* processComments */, true /* relaxedFormat */);
Yifan Honga45f77d2018-12-03 16:42:52 -0800209 status_t err = parser.processAndFinish(read(stream));
Yifan Hong79efa8a2017-07-06 14:10:28 -0700210 if (err != OK) {
Yifan Hongae53a0e2017-07-07 15:19:06 -0700211 std::cerr << parser.error();
Yifan Hong79efa8a2017-07-06 14:10:28 -0700212 return false;
213 }
214
215 for (auto& configPair : parser.configs()) {
216 out->push_back({});
217 KernelConfig& config = out->back();
218 config.first = std::move(configPair.first);
219 if (!parseKernelConfigTypedValue(configPair.second, &config.second)) {
220 std::cerr << "Unknown value type for key = '" << config.first << "', value = '"
221 << configPair.second << "'\n";
222 return false;
223 }
224 }
225 return true;
226 }
227
Yifan Hong8302cea2017-12-18 20:17:05 -0800228 static bool parseFilesForKernelConfigs(std::vector<NamedIstream>* streams,
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700229 std::vector<ConditionedConfig>* out) {
230 out->clear();
231 ConditionedConfig commonConfig;
232 bool foundCommonConfig = false;
Steve Muckle0bef8682017-07-31 15:47:15 -0700233 bool ret = true;
Yifan Hong8302cea2017-12-18 20:17:05 -0800234
235 for (auto& namedStream : *streams) {
236 if (isCommonConfig(namedStream.name())) {
237 ret &= parseFileForKernelConfigs(namedStream.stream(), &commonConfig.second);
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700238 foundCommonConfig = true;
239 } else {
Yifan Hong8302cea2017-12-18 20:17:05 -0800240 Condition condition = generateCondition(namedStream.name());
Yifan Hong079ec242017-08-25 18:53:38 -0700241 ret &= (condition != nullptr);
242
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700243 std::vector<KernelConfig> kernelConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800244 if ((ret &= parseFileForKernelConfigs(namedStream.stream(), &kernelConfigs)))
Yifan Hong079ec242017-08-25 18:53:38 -0700245 out->emplace_back(std::move(condition), std::move(kernelConfigs));
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700246 }
Steve Muckle0bef8682017-07-31 15:47:15 -0700247 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700248
249 if (!foundCommonConfig) {
Steve Mucklead6aa912018-07-23 15:44:01 -0700250 std::cerr << "No " << gBaseConfig << " is found in these paths:" << std::endl;
Yifan Hong8302cea2017-12-18 20:17:05 -0800251 for (auto& namedStream : *streams) {
252 std::cerr << " " << namedStream.name() << std::endl;
253 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700254 }
255 ret &= foundCommonConfig;
256 // first element is always common configs (no conditions).
257 out->insert(out->begin(), std::move(commonConfig));
Steve Muckle0bef8682017-07-31 15:47:15 -0700258 return ret;
259 }
260
Yifan Hong8302cea2017-12-18 20:17:05 -0800261 std::basic_ostream<char>& out() const { return mOutRef == nullptr ? std::cout : *mOutRef; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700262
Yifan Hong1044fd12018-02-27 14:51:54 -0800263 // If -c is provided, check it.
264 bool checkDualFile(const HalManifest& manifest, const CompatibilityMatrix& matrix) {
265 if (getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
266 std::string error;
267 if (!manifest.checkCompatibility(matrix, &error)) {
268 std::cerr << "Not compatible: " << error << std::endl;
269 return false;
270 }
271 }
272
273 // Check HALs in device manifest that are not in framework matrix.
274 if (getBooleanFlag("VINTF_ENFORCE_NO_UNUSED_HALS")) {
275 auto unused = manifest.checkUnusedHals(matrix);
276 if (!unused.empty()) {
277 std::cerr << "Error: The following instances are in the device manifest but "
278 << "not specified in framework compatibility matrix: " << std::endl
279 << " " << android::base::Join(unused, "\n ") << std::endl
280 << "Suggested fix:" << std::endl
281 << "1. Check for any typos in device manifest or framework compatibility "
282 << "matrices with FCM version >= " << matrix.level() << "." << std::endl
283 << "2. Add them to any framework compatibility matrix with FCM "
284 << "version >= " << matrix.level() << " where applicable." << std::endl
Yifan Hong09efef32019-01-28 10:59:27 -0800285 << "3. Add them to DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE "
286 << "or DEVICE_PRODUCT_COMPATIBILITY_MATRIX_FILE." << std::endl;
Yifan Hong1044fd12018-02-27 14:51:54 -0800287
288 return false;
289 }
290 }
291 return true;
292 }
293
Yifan Hongdbe9db32017-12-11 19:06:11 -0800294 template <typename S>
Yifan Hongffcaf992018-01-09 17:08:51 -0800295 using Schemas = std::vector<Named<S>>;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800296 using HalManifests = Schemas<HalManifest>;
297 using CompatibilityMatrices = Schemas<CompatibilityMatrix>;
298
Steven Moreland5875afc2018-04-05 13:14:08 -0700299 template <typename M>
300 void outputInputs(const Schemas<M>& inputs) {
301 out() << "<!--" << std::endl;
302 out() << " Input:" << std::endl;
303 for (const auto& e : inputs) {
304 if (!e.name.empty()) {
305 out() << " " << base::Basename(e.name) << std::endl;
306 }
307 }
308 out() << "-->" << std::endl;
309 }
310
Yifan Honga45f77d2018-12-03 16:42:52 -0800311 // Parse --kernel arguments and write to output manifest.
312 bool setDeviceManifestKernel(HalManifest* manifest) {
313 if (mKernels.empty()) {
314 return true;
315 }
316 if (mKernels.size() > 1) {
317 std::cerr << "Warning: multiple --kernel is specified when building device manifest. "
318 << "Only the first one will be used." << std::endl;
319 }
320 auto& kernelArg = *mKernels.begin();
321 const auto& kernelVer = kernelArg.first;
322 auto& kernelConfigFiles = kernelArg.second;
323 // addKernel() guarantees that !kernelConfigFiles.empty().
324 if (kernelConfigFiles.size() > 1) {
325 std::cerr << "Warning: multiple config files are specified in --kernel when building "
326 << "device manfiest. Only the first one will be used." << std::endl;
327 }
328
329 KernelConfigParser parser(true /* processComments */, false /* relaxedFormat */);
330 status_t err = parser.processAndFinish(read(kernelConfigFiles[0].stream()));
331 if (err != OK) {
332 std::cerr << parser.error();
333 return false;
334 }
335 manifest->device.mKernel = std::make_optional<KernelInfo>();
336 manifest->device.mKernel->mVersion = kernelVer;
337 manifest->device.mKernel->mConfigs = parser.configs();
338 return true;
339 }
340
Yifan Hongdbe9db32017-12-11 19:06:11 -0800341 bool assembleHalManifest(HalManifests* halManifests) {
Yifan Hong4650ad82017-05-01 17:28:02 -0700342 std::string error;
Yifan Hongffcaf992018-01-09 17:08:51 -0800343 HalManifest* halManifest = &halManifests->front().object;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800344 for (auto it = halManifests->begin() + 1; it != halManifests->end(); ++it) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800345 const std::string& path = it->name;
Yifan Hong0a35ab12018-06-29 11:32:57 -0700346 HalManifest& manifestToAdd = it->object;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800347
Yifan Hong0a35ab12018-06-29 11:32:57 -0700348 if (manifestToAdd.level() != Level::UNSPECIFIED) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800349 if (halManifest->level() == Level::UNSPECIFIED) {
Yifan Hong0a35ab12018-06-29 11:32:57 -0700350 halManifest->mLevel = manifestToAdd.level();
351 } else if (halManifest->level() != manifestToAdd.level()) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800352 std::cerr << "Inconsistent FCM Version in HAL manifests:" << std::endl
Yifan Hongffcaf992018-01-09 17:08:51 -0800353 << " File '" << halManifests->front().name << "' has level "
Yifan Hongdbe9db32017-12-11 19:06:11 -0800354 << halManifest->level() << std::endl
Yifan Hong0a35ab12018-06-29 11:32:57 -0700355 << " File '" << path << "' has level " << manifestToAdd.level()
Yifan Hongdbe9db32017-12-11 19:06:11 -0800356 << std::endl;
357 return false;
358 }
359 }
360
Steven Morelandb1392772018-05-01 11:21:35 -0700361 // TODO(b/78943004): add everything
Yifan Hong0a35ab12018-06-29 11:32:57 -0700362 if (!halManifest->addAllHals(&manifestToAdd, &error)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800363 std::cerr << "File \"" << path << "\" cannot be added: conflict on HAL \"" << error
364 << "\" with an existing HAL. See <hal> with the same name "
365 << "in previously parsed files or previously declared in this file."
366 << std::endl;
367 return false;
368 }
Yifan Hong0a35ab12018-06-29 11:32:57 -0700369
370 // Check that manifestToAdd is empty.
371 if (!manifestToAdd.empty()) {
Yifan Hong304cdab2018-08-02 16:03:59 -0700372 std::cerr
373 << "File \"" << path << "\" contains extraneous entries and attributes. "
374 << "This is currently unsupported (b/78943004); it can only contain "
375 << "<hal>s and attribute \"type\" and \"version\". Only the first input "
376 << "file to assemble_vintf can contain other things. "
377 << "Remaining entries and attributes are:" << std::endl
378 << gHalManifestConverter(
379 manifestToAdd,
380 SerializeFlags::EVERYTHING.disableMetaVersion().disableSchemaType());
Yifan Hong0a35ab12018-06-29 11:32:57 -0700381 return false;
382 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800383 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700384
385 if (halManifest->mType == SchemaType::DEVICE) {
Steven Morelandb1392772018-05-01 11:21:35 -0700386 (void)getFlagIfUnset("BOARD_SEPOLICY_VERS", &halManifest->device.mSepolicyVersion);
387
Yifan Hongdbe9db32017-12-11 19:06:11 -0800388 if (!setDeviceFcmVersion(halManifest)) {
389 return false;
390 }
Yifan Honga45f77d2018-12-03 16:42:52 -0800391
392 if (!setDeviceManifestKernel(halManifest)) {
393 return false;
394 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700395 }
396
Yifan Hongfeb454e2018-01-09 16:16:40 -0800397 if (halManifest->mType == SchemaType::FRAMEWORK) {
Yifan Honga28729e2018-01-17 13:40:35 -0800398 for (auto&& v : getEnvList("PROVIDED_VNDK_VERSIONS")) {
399 halManifest->framework.mVendorNdks.emplace_back(std::move(v));
400 }
401
Yifan Hong8b8492b2018-01-22 14:08:40 -0800402 for (auto&& v : getEnvList("PLATFORM_SYSTEMSDK_VERSIONS")) {
Yifan Honga28729e2018-01-17 13:40:35 -0800403 halManifest->framework.mSystemSdk.mVersions.emplace(std::move(v));
Yifan Hongfeb454e2018-01-09 16:16:40 -0800404 }
405 }
406
Steven Moreland5875afc2018-04-05 13:14:08 -0700407 outputInputs(*halManifests);
408
Yifan Hong9aa63702017-05-16 16:37:50 -0700409 if (mOutputMatrix) {
410 CompatibilityMatrix generatedMatrix = halManifest->generateCompatibleMatrix();
411 if (!halManifest->checkCompatibility(generatedMatrix, &error)) {
412 std::cerr << "FATAL ERROR: cannot generate a compatible matrix: " << error
413 << std::endl;
414 }
415 out() << "<!-- \n"
416 " Autogenerated skeleton compatibility matrix. \n"
417 " Use with caution. Modify it to suit your needs.\n"
418 " All HALs are set to optional.\n"
419 " Many entries other than HALs are zero-filled and\n"
420 " require human attention. \n"
421 "-->\n"
Yifan Honga2635c42017-12-12 13:20:33 -0800422 << gCompatibilityMatrixConverter(generatedMatrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700423 } else {
Yifan Honga2635c42017-12-12 13:20:33 -0800424 out() << gHalManifestConverter(*halManifest, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700425 }
426 out().flush();
427
Yifan Hong8302cea2017-12-18 20:17:05 -0800428 if (mCheckFile != nullptr) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700429 CompatibilityMatrix checkMatrix;
Yifan Hong94757062018-02-09 16:36:31 -0800430 if (!gCompatibilityMatrixConverter(&checkMatrix, read(*mCheckFile), &error)) {
431 std::cerr << "Cannot parse check file as a compatibility matrix: " << error
432 << std::endl;
Yifan Hong9aa63702017-05-16 16:37:50 -0700433 return false;
434 }
Yifan Hong1044fd12018-02-27 14:51:54 -0800435 if (!checkDualFile(*halManifest, checkMatrix)) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700436 return false;
437 }
438 }
439
440 return true;
441 }
442
Yifan Honge7837b12018-10-11 10:38:57 -0700443 // Parse --kernel arguments and write to output matrix.
Yifan Honge88e1672017-08-24 14:42:54 -0700444 bool assembleFrameworkCompatibilityMatrixKernels(CompatibilityMatrix* matrix) {
Yifan Hong8302cea2017-12-18 20:17:05 -0800445 for (auto& pair : mKernels) {
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700446 std::vector<ConditionedConfig> conditionedConfigs;
Yifan Hong8302cea2017-12-18 20:17:05 -0800447 if (!parseFilesForKernelConfigs(&pair.second, &conditionedConfigs)) {
Yifan Honge88e1672017-08-24 14:42:54 -0700448 return false;
449 }
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700450 for (ConditionedConfig& conditionedConfig : conditionedConfigs) {
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800451 MatrixKernel kernel(KernelVersion{pair.first}, std::move(conditionedConfig.second));
Yifan Hong079ec242017-08-25 18:53:38 -0700452 if (conditionedConfig.first != nullptr)
453 kernel.mConditions.push_back(std::move(*conditionedConfig.first));
Yifan Honge7837b12018-10-11 10:38:57 -0700454 std::string error;
455 if (!matrix->addKernel(std::move(kernel), &error)) {
456 std::cerr << "Error:" << error << std::endl;
457 return false;
458 };
Yifan Hong9a8b1a72017-08-25 17:55:33 -0700459 }
Yifan Honge88e1672017-08-24 14:42:54 -0700460 }
461 return true;
462 }
463
Yifan Hongdbe9db32017-12-11 19:06:11 -0800464 bool setDeviceFcmVersion(HalManifest* manifest) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700465 // Not needed for generating empty manifest for DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE.
Yifan Hong2eee1982018-03-29 10:55:30 -0700466 if (getBooleanFlag("VINTF_IGNORE_TARGET_FCM_VERSION")) {
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700467 return true;
468 }
469
Yifan Hongdbe9db32017-12-11 19:06:11 -0800470 size_t shippingApiLevel = getIntegerFlag("PRODUCT_SHIPPING_API_LEVEL");
Yifan Hong9aa63702017-05-16 16:37:50 -0700471
Yifan Hongdbe9db32017-12-11 19:06:11 -0800472 if (manifest->level() != Level::UNSPECIFIED) {
473 return true;
474 }
475 if (!getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
476 manifest->mLevel = Level::LEGACY;
477 return true;
478 }
479 // TODO(b/70628538): Do not infer from Shipping API level.
480 if (shippingApiLevel) {
481 std::cerr << "Warning: Shipping FCM Version is inferred from Shipping API level. "
482 << "Declare Shipping FCM Version in device manifest directly." << std::endl;
Yifan Hongafae1982018-01-11 18:15:24 -0800483 manifest->mLevel = details::convertFromApiLevel(shippingApiLevel);
Yifan Hongdbe9db32017-12-11 19:06:11 -0800484 if (manifest->mLevel == Level::UNSPECIFIED) {
485 std::cerr << "Error: Shipping FCM Version cannot be inferred from Shipping API "
486 << "level " << shippingApiLevel << "."
487 << "Declare Shipping FCM Version in device manifest directly."
488 << std::endl;
489 return false;
490 }
491 return true;
492 }
493 // TODO(b/69638851): should be an error if Shipping API level is not defined.
494 // For now, just leave it empty; when framework compatibility matrix is built,
495 // lowest FCM Version is assumed.
496 std::cerr << "Warning: Shipping FCM Version cannot be inferred, because:" << std::endl
497 << " (1) It is not explicitly declared in device manifest;" << std::endl
498 << " (2) PRODUCT_ENFORCE_VINTF_MANIFEST is set to true;" << std::endl
499 << " (3) PRODUCT_SHIPPING_API_LEVEL is undefined." << std::endl
500 << "Assuming 'unspecified' Shipping FCM Version. " << std::endl
501 << "To remove this warning, define 'level' attribute in device manifest."
502 << std::endl;
503 return true;
504 }
505
506 Level getLowestFcmVersion(const CompatibilityMatrices& matrices) {
507 Level ret = Level::UNSPECIFIED;
508 for (const auto& e : matrices) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800509 if (ret == Level::UNSPECIFIED || ret > e.object.level()) {
510 ret = e.object.level();
Yifan Hongdbe9db32017-12-11 19:06:11 -0800511 }
512 }
513 return ret;
514 }
515
516 bool assembleCompatibilityMatrix(CompatibilityMatrices* matrices) {
517 std::string error;
518 CompatibilityMatrix* matrix = nullptr;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800519 std::unique_ptr<HalManifest> checkManifest;
Yifan Honge7837b12018-10-11 10:38:57 -0700520 std::unique_ptr<CompatibilityMatrix> builtMatrix;
521
522 if (mCheckFile != nullptr) {
523 checkManifest = std::make_unique<HalManifest>();
524 if (!gHalManifestConverter(checkManifest.get(), read(*mCheckFile), &error)) {
525 std::cerr << "Cannot parse check file as a HAL manifest: " << error << std::endl;
526 return false;
527 }
528 }
529
Yifan Hongffcaf992018-01-09 17:08:51 -0800530 if (matrices->front().object.mType == SchemaType::DEVICE) {
Yifan Honge7837b12018-10-11 10:38:57 -0700531 if (matrices->size() > 1) {
532 std::cerr
533 << "assemble_vintf does not support merging device compatibilility matrices."
534 << std::endl;
535 return false;
536 }
537
Yifan Hongffcaf992018-01-09 17:08:51 -0800538 matrix = &matrices->front().object;
Yifan Hongfeb454e2018-01-09 16:16:40 -0800539
540 auto vndkVersion = base::Trim(getEnv("REQUIRED_VNDK_VERSION"));
541 if (!vndkVersion.empty()) {
542 auto& valueInMatrix = matrix->device.mVendorNdk;
543 if (!valueInMatrix.version().empty() && valueInMatrix.version() != vndkVersion) {
544 std::cerr << "Hard-coded <vendor-ndk> version in device compatibility matrix ("
545 << matrices->front().name << "), '" << valueInMatrix.version()
546 << "', does not match value inferred "
547 << "from BOARD_VNDK_VERSION '" << vndkVersion << "'" << std::endl;
548 return false;
549 }
550 valueInMatrix = VendorNdk{std::move(vndkVersion)};
551 }
Yifan Honga28729e2018-01-17 13:40:35 -0800552
553 for (auto&& v : getEnvList("BOARD_SYSTEMSDK_VERSIONS")) {
554 matrix->device.mSystemSdk.mVersions.emplace(std::move(v));
555 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800556 }
557
Yifan Hongffcaf992018-01-09 17:08:51 -0800558 if (matrices->front().object.mType == SchemaType::FRAMEWORK) {
Yifan Honge7837b12018-10-11 10:38:57 -0700559 Level deviceLevel =
560 checkManifest != nullptr ? checkManifest->level() : Level::UNSPECIFIED;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800561 if (deviceLevel == Level::UNSPECIFIED) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800562 deviceLevel = getLowestFcmVersion(*matrices);
Yifan Honge7837b12018-10-11 10:38:57 -0700563 if (checkManifest != nullptr && deviceLevel != Level::UNSPECIFIED) {
564 std::cerr << "Warning: No Target FCM Version for device. Assuming \""
565 << to_string(deviceLevel)
566 << "\" when building final framework compatibility matrix."
567 << std::endl;
568 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800569 }
Yifan Honge7837b12018-10-11 10:38:57 -0700570 builtMatrix = CompatibilityMatrix::combine(deviceLevel, matrices, &error);
571 matrix = builtMatrix.get();
Yifan Hongdbe9db32017-12-11 19:06:11 -0800572
Yifan Hong1e6e34c2018-03-22 16:38:09 -0700573 if (matrix == nullptr) {
574 std::cerr << error << std::endl;
575 return false;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800576 }
577
Yifan Honge88e1672017-08-24 14:42:54 -0700578 if (!assembleFrameworkCompatibilityMatrixKernels(matrix)) {
579 return false;
Yifan Hong79efa8a2017-07-06 14:10:28 -0700580 }
Yifan Honge88e1672017-08-24 14:42:54 -0700581
Yifan Hong321ee222018-02-02 12:46:52 -0800582 // Add PLATFORM_SEPOLICY_* to sepolicy.sepolicy-version. Remove dupes.
583 std::set<Version> sepolicyVersions;
584 auto sepolicyVersionStrings = getEnvList("PLATFORM_SEPOLICY_COMPAT_VERSIONS");
585 auto currentSepolicyVersionString = getEnv("PLATFORM_SEPOLICY_VERSION");
586 if (!currentSepolicyVersionString.empty()) {
587 sepolicyVersionStrings.push_back(currentSepolicyVersionString);
588 }
589 for (auto&& s : sepolicyVersionStrings) {
590 Version v;
591 if (!parse(s, &v)) {
592 std::cerr << "Error: unknown sepolicy version '" << s << "' specified by "
593 << (s == currentSepolicyVersionString
594 ? "PLATFORM_SEPOLICY_VERSION"
595 : "PLATFORM_SEPOLICY_COMPAT_VERSIONS")
596 << ".";
597 return false;
598 }
599 sepolicyVersions.insert(v);
600 }
601 for (auto&& v : sepolicyVersions) {
602 matrix->framework.mSepolicy.mSepolicyVersionRanges.emplace_back(v.majorVer,
603 v.minorVer);
Yifan Hong7f6c00c2017-07-06 19:50:29 +0000604 }
Yifan Hongf4135012017-12-18 17:27:19 -0800605
Yifan Hongf7e8b1b2018-01-23 15:30:35 -0800606 getFlagIfUnset("POLICYVERS", &matrix->framework.mSepolicy.mKernelSepolicyVersion,
Yifan Honge7837b12018-10-11 10:38:57 -0700607 false /* log */);
Yifan Hongf7e8b1b2018-01-23 15:30:35 -0800608 getFlagIfUnset("FRAMEWORK_VBMETA_VERSION", &matrix->framework.mAvbMetaVersion,
Yifan Honge7837b12018-10-11 10:38:57 -0700609 false /* log */);
Yifan Hong4dec91d2018-08-13 12:37:47 -0700610 // Hard-override existing AVB version
611 getFlag("FRAMEWORK_VBMETA_VERSION_OVERRIDE", &matrix->framework.mAvbMetaVersion,
612 false /* log */);
Yifan Hong9aa63702017-05-16 16:37:50 -0700613 }
Steven Moreland5875afc2018-04-05 13:14:08 -0700614 outputInputs(*matrices);
Yifan Honga2635c42017-12-12 13:20:33 -0800615 out() << gCompatibilityMatrixConverter(*matrix, mSerializeFlags);
Yifan Hong9aa63702017-05-16 16:37:50 -0700616 out().flush();
617
Yifan Hong1044fd12018-02-27 14:51:54 -0800618 if (checkManifest != nullptr && !checkDualFile(*checkManifest, *matrix)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800619 return false;
Yifan Hong9aa63702017-05-16 16:37:50 -0700620 }
621
622 return true;
623 }
624
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700625 enum AssembleStatus { SUCCESS, FAIL_AND_EXIT, TRY_NEXT };
626 template <typename Schema, typename AssembleFunc>
627 AssembleStatus tryAssemble(const XmlConverter<Schema>& converter, const std::string& schemaName,
Yifan Hong94757062018-02-09 16:36:31 -0800628 AssembleFunc assemble, std::string* error) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800629 Schemas<Schema> schemas;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700630 Schema schema;
Yifan Hong94757062018-02-09 16:36:31 -0800631 if (!converter(&schema, read(mInFiles.front().stream()), error)) {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700632 return TRY_NEXT;
633 }
634 auto firstType = schema.type();
Yifan Hongaa219f52017-12-18 18:51:59 -0800635 schemas.emplace_back(mInFiles.front().name(), std::move(schema));
Yifan Hongdbe9db32017-12-11 19:06:11 -0800636
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700637 for (auto it = mInFiles.begin() + 1; it != mInFiles.end(); ++it) {
638 Schema additionalSchema;
Yifan Hongaa219f52017-12-18 18:51:59 -0800639 const std::string& fileName = it->name();
Yifan Hong94757062018-02-09 16:36:31 -0800640 if (!converter(&additionalSchema, read(it->stream()), error)) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800641 std::cerr << "File \"" << fileName << "\" is not a valid " << firstType << " "
642 << schemaName << " (but the first file is a valid " << firstType << " "
Yifan Hong94757062018-02-09 16:36:31 -0800643 << schemaName << "). Error: " << *error << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700644 return FAIL_AND_EXIT;
645 }
646 if (additionalSchema.type() != firstType) {
Yifan Hongdbe9db32017-12-11 19:06:11 -0800647 std::cerr << "File \"" << fileName << "\" is a " << additionalSchema.type() << " "
648 << schemaName << " (but a " << firstType << " " << schemaName
649 << " is expected)." << std::endl;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700650 return FAIL_AND_EXIT;
651 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800652
653 schemas.emplace_back(fileName, std::move(additionalSchema));
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700654 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800655 return assemble(&schemas) ? SUCCESS : FAIL_AND_EXIT;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700656 }
657
Yifan Hong8302cea2017-12-18 20:17:05 -0800658 bool assemble() override {
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700659 using std::placeholders::_1;
660 if (mInFiles.empty()) {
Yifan Hong9aa63702017-05-16 16:37:50 -0700661 std::cerr << "Missing input file." << std::endl;
662 return false;
663 }
664
Yifan Hong94757062018-02-09 16:36:31 -0800665 std::string manifestError;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700666 auto status = tryAssemble(gHalManifestConverter, "manifest",
Yifan Hong94757062018-02-09 16:36:31 -0800667 std::bind(&AssembleVintfImpl::assembleHalManifest, this, _1),
668 &manifestError);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700669 if (status == SUCCESS) return true;
670 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000671
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700672 resetInFiles();
Yifan Honga59d2562017-04-18 18:01:16 -0700673
Yifan Hong94757062018-02-09 16:36:31 -0800674 std::string matrixError;
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700675 status = tryAssemble(gCompatibilityMatrixConverter, "compatibility matrix",
Yifan Hong94757062018-02-09 16:36:31 -0800676 std::bind(&AssembleVintfImpl::assembleCompatibilityMatrix, this, _1),
677 &matrixError);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700678 if (status == SUCCESS) return true;
679 if (status == FAIL_AND_EXIT) return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000680
Yifan Hong959ee1b2017-04-28 14:37:56 -0700681 std::cerr << "Input file has unknown format." << std::endl
Yifan Hong94757062018-02-09 16:36:31 -0800682 << "Error when attempting to convert to manifest: " << manifestError << std::endl
683 << "Error when attempting to convert to compatibility matrix: " << matrixError
684 << std::endl;
Yifan Hong959ee1b2017-04-28 14:37:56 -0700685 return false;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000686 }
Yifan Hong9aa63702017-05-16 16:37:50 -0700687
Yifan Hong8302cea2017-12-18 20:17:05 -0800688 std::ostream& setOutputStream(Ostream&& out) override {
689 mOutRef = std::move(out);
690 return *mOutRef;
Yifan Hong9aa63702017-05-16 16:37:50 -0700691 }
692
Yifan Hong8302cea2017-12-18 20:17:05 -0800693 std::istream& addInputStream(const std::string& name, Istream&& in) override {
694 auto it = mInFiles.emplace(mInFiles.end(), name, std::move(in));
695 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700696 }
697
Yifan Hong8302cea2017-12-18 20:17:05 -0800698 std::istream& setCheckInputStream(Istream&& in) override {
699 mCheckFile = std::move(in);
700 return *mCheckFile;
701 }
702
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800703 bool hasKernelVersion(const KernelVersion& kernelVer) const override {
Yifan Hong8302cea2017-12-18 20:17:05 -0800704 return mKernels.find(kernelVer) != mKernels.end();
705 }
706
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800707 std::istream& addKernelConfigInputStream(const KernelVersion& kernelVer,
708 const std::string& name, Istream&& in) override {
Yifan Hong8302cea2017-12-18 20:17:05 -0800709 auto&& kernel = mKernels[kernelVer];
710 auto it = kernel.emplace(kernel.end(), name, std::move(in));
711 return it->stream();
Yifan Hong9aa63702017-05-16 16:37:50 -0700712 }
713
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700714 void resetInFiles() {
715 for (auto& inFile : mInFiles) {
Yifan Hongaa219f52017-12-18 18:51:59 -0800716 inFile.stream().clear();
717 inFile.stream().seekg(0);
Yifan Hongbfb3c1d2017-05-24 14:38:48 -0700718 }
719 }
720
Yifan Hong8302cea2017-12-18 20:17:05 -0800721 void setOutputMatrix() override { mOutputMatrix = true; }
Yifan Hong9aa63702017-05-16 16:37:50 -0700722
Yifan Hong8302cea2017-12-18 20:17:05 -0800723 bool setHalsOnly() override {
Yifan Hong5c9ff702018-08-07 17:26:12 -0700724 if (mHasSetHalsOnlyFlag) {
725 std::cerr << "Error: Cannot set --hals-only with --no-hals." << std::endl;
726 return false;
727 }
728 // Just override it with HALS_ONLY because other flags that modify mSerializeFlags
729 // does not interfere with this (except --no-hals).
730 mSerializeFlags = SerializeFlags::HALS_ONLY;
731 mHasSetHalsOnlyFlag = true;
Yifan Honga2635c42017-12-12 13:20:33 -0800732 return true;
733 }
734
Yifan Hong8302cea2017-12-18 20:17:05 -0800735 bool setNoHals() override {
Yifan Hong5c9ff702018-08-07 17:26:12 -0700736 if (mHasSetHalsOnlyFlag) {
737 std::cerr << "Error: Cannot set --hals-only with --no-hals." << std::endl;
738 return false;
739 }
740 mSerializeFlags = mSerializeFlags.disableHals();
741 mHasSetHalsOnlyFlag = true;
Yifan Honga2635c42017-12-12 13:20:33 -0800742 return true;
743 }
744
Yifan Hong86678e42018-07-26 11:38:54 -0700745 bool setNoKernelRequirements() override {
Yifan Hong5c9ff702018-08-07 17:26:12 -0700746 mSerializeFlags = mSerializeFlags.disableKernelConfigs().disableKernelMinorRevision();
Yifan Hong86678e42018-07-26 11:38:54 -0700747 return true;
748 }
749
Yifan Hong9aa63702017-05-16 16:37:50 -0700750 private:
Yifan Hongaa219f52017-12-18 18:51:59 -0800751 std::vector<NamedIstream> mInFiles;
Yifan Hong8302cea2017-12-18 20:17:05 -0800752 Ostream mOutRef;
753 Istream mCheckFile;
Yifan Hong9aa63702017-05-16 16:37:50 -0700754 bool mOutputMatrix = false;
Yifan Hong5c9ff702018-08-07 17:26:12 -0700755 bool mHasSetHalsOnlyFlag = false;
Yifan Hongba588bc2018-08-08 14:19:41 -0700756 SerializeFlags::Type mSerializeFlags = SerializeFlags::EVERYTHING;
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800757 std::map<KernelVersion, std::vector<NamedIstream>> mKernels;
Yifan Hong8302cea2017-12-18 20:17:05 -0800758 std::map<std::string, std::string> mFakeEnv;
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000759};
760
Yifan Hong8302cea2017-12-18 20:17:05 -0800761bool AssembleVintf::openOutFile(const std::string& path) {
762 return static_cast<std::ofstream&>(setOutputStream(std::make_unique<std::ofstream>(path)))
763 .is_open();
764}
765
766bool AssembleVintf::openInFile(const std::string& path) {
767 return static_cast<std::ifstream&>(addInputStream(path, std::make_unique<std::ifstream>(path)))
768 .is_open();
769}
770
771bool AssembleVintf::openCheckFile(const std::string& path) {
772 return static_cast<std::ifstream&>(setCheckInputStream(std::make_unique<std::ifstream>(path)))
773 .is_open();
774}
775
776bool AssembleVintf::addKernel(const std::string& kernelArg) {
Yifan Hongb83e56b2018-01-09 14:36:30 -0800777 auto tokens = base::Split(kernelArg, ":");
Yifan Hong8302cea2017-12-18 20:17:05 -0800778 if (tokens.size() <= 1) {
779 std::cerr << "Unrecognized --kernel option '" << kernelArg << "'" << std::endl;
780 return false;
781 }
Yifan Hongfb9e8b62018-01-23 15:47:23 -0800782 KernelVersion kernelVer;
Yifan Hong8302cea2017-12-18 20:17:05 -0800783 if (!parse(tokens.front(), &kernelVer)) {
784 std::cerr << "Unrecognized kernel version '" << tokens.front() << "'" << std::endl;
785 return false;
786 }
787 if (hasKernelVersion(kernelVer)) {
788 std::cerr << "Multiple --kernel for " << kernelVer << " is specified." << std::endl;
789 return false;
790 }
791 for (auto it = tokens.begin() + 1; it != tokens.end(); ++it) {
792 bool opened =
793 static_cast<std::ifstream&>(
794 addKernelConfigInputStream(kernelVer, *it, std::make_unique<std::ifstream>(*it)))
795 .is_open();
796 if (!opened) {
797 std::cerr << "Cannot open file '" << *it << "'." << std::endl;
798 return false;
799 }
800 }
801 return true;
802}
803
804std::unique_ptr<AssembleVintf> AssembleVintf::newInstance() {
805 return std::make_unique<AssembleVintfImpl>();
806}
807
Yifan Hong4d18bcc2017-04-07 21:47:16 +0000808} // namespace vintf
809} // namespace android