analyze_matrix.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 2018 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. #include <optional>
  17. #include <set>
  18. #include <android-base/logging.h>
  19. #include <gflags/gflags.h>
  20. #include <hidl-util/FqInstance.h>
  21. #include <vintf/FileSystem.h>
  22. #include <vintf/parse_string.h>
  23. #include <vintf/parse_xml.h>
  24. namespace android {
  25. namespace vintf {
  26. namespace {
  27. template <typename T>
  28. std::optional<T> readObject(const std::string& path, const XmlConverter<T>& converter) {
  29. std::string xml;
  30. std::string error;
  31. status_t err = details::FileSystemImpl().fetch(path, &xml, &error);
  32. if (err != OK) {
  33. LOG(ERROR) << "Cannot read '" << path << "': " << error;
  34. return std::nullopt;
  35. }
  36. auto ret = std::make_optional<T>();
  37. if (!converter(&ret.value(), xml, &error)) {
  38. LOG(ERROR) << "Cannot parse '" << path << "': " << error;
  39. return std::nullopt;
  40. }
  41. return ret;
  42. }
  43. std::optional<std::set<std::string>> getInterfaces(const CompatibilityMatrix& mat) {
  44. auto set = std::make_optional<std::set<std::string>>();
  45. mat.forEachInstance([&set](const auto& matrixInstance) {
  46. for (auto minorVer = matrixInstance.versionRange().minMinor;
  47. minorVer <= matrixInstance.versionRange().maxMinor; ++minorVer) {
  48. FqInstance fqInstance;
  49. if (!fqInstance.setTo(matrixInstance.package(), matrixInstance.versionRange().majorVer,
  50. minorVer, matrixInstance.interface())) {
  51. LOG(ERROR) << "Matrix not valid; '" << matrixInstance.package() << "@"
  52. << matrixInstance.versionRange().majorVer << "." << minorVer
  53. << "::" << matrixInstance.interface() << "' is not a valid FQName.";
  54. set = std::nullopt;
  55. return false; // break
  56. }
  57. set->insert(fqInstance.string());
  58. }
  59. return true; // continue
  60. });
  61. return set;
  62. }
  63. } // namespace
  64. } // namespace vintf
  65. } // namespace android
  66. DEFINE_string(input, "", "Input compatibility matrix file");
  67. static bool ValidateInput(const char* /* flagname */, const std::string& value) {
  68. return !value.empty();
  69. }
  70. DEFINE_validator(input, &ValidateInput);
  71. DEFINE_bool(level, false, "Write level (FCM version) of the compatibility matrix.");
  72. DEFINE_bool(interfaces, false, "Write strings like \"[email protected]::IFoo\".");
  73. int main(int argc, char** argv) {
  74. using namespace android::vintf;
  75. gflags::ParseCommandLineFlags(&argc, &argv, true /* remove flags */);
  76. auto mat = readObject(FLAGS_input, gCompatibilityMatrixConverter);
  77. if (!mat) {
  78. return 1;
  79. }
  80. bool written = false;
  81. if (FLAGS_level) {
  82. if (mat->level() == Level::UNSPECIFIED) {
  83. LOG(WARNING) << "FCM version is unspecified.";
  84. }
  85. std::cout << mat->level() << std::endl;
  86. written = true;
  87. }
  88. if (FLAGS_interfaces) {
  89. auto pvs = getInterfaces(*mat);
  90. if (!pvs) {
  91. return 1;
  92. }
  93. if (pvs->empty()) {
  94. LOG(WARNING) << "No package and versions are found.";
  95. }
  96. for (const auto& pv : *pvs) {
  97. std::cout << pv << std::endl;
  98. }
  99. written = true;
  100. }
  101. if (!written) {
  102. LOG(ERROR) << "No output format is set.";
  103. return 1;
  104. }
  105. return 0;
  106. }