Check unused HALs in check_vintf

The check is lost when deleting BUILT_ASSEMBLED_VENDOR_MANIFEST
(see I9791abc44). Re-add the check.

The check is enforced when product matrix or device system matrix is
detected, similar to the condition before I9791abc44.

Also, delete unused check in assemble_vintf when
VINTF_ENFORCE_UNUSED_HALS is set.

Test: builds
Bug: 121287858

Change-Id: Ifc5435e2ef1b4936a488827fe824126bdc50deae
Merged-In: Ifc5435e2ef1b4936a488827fe824126bdc50deae
diff --git a/check_vintf.cpp b/check_vintf.cpp
index d4dc3bc..17e32df 100644
--- a/check_vintf.cpp
+++ b/check_vintf.cpp
@@ -24,6 +24,7 @@
 #include <android-base/file.h>
 #include <android-base/logging.h>
 #include <android-base/parseint.h>
+#include <android-base/result.h>
 #include <android-base/strings.h>
 #include <utils/Errors.h>
 #include <vintf/KernelConfigParser.h>
@@ -345,8 +346,8 @@
     return EX_USAGE;
 }
 
-int checkAllFiles(const Dirmap& dirmap, const Properties& props,
-                  std::shared_ptr<StaticRuntimeInfo> runtimeInfo, std::string* error) {
+android::base::Result<void> checkAllFiles(const Dirmap& dirmap, const Properties& props,
+                                          std::shared_ptr<StaticRuntimeInfo> runtimeInfo) {
     auto hostPropertyFetcher = std::make_unique<PresetPropertyFetcher>();
     hostPropertyFetcher->setProperties(props);
 
@@ -359,7 +360,25 @@
             .setPropertyFetcher(std::move(hostPropertyFetcher))
             .setRuntimeInfoFactory(std::make_unique<StaticRuntimeInfoFactory>(runtimeInfo))
             .build();
-    return vintfObject->checkCompatibility(error, flags);
+
+    std::string error;
+    int compatibleResult = vintfObject->checkCompatibility(&error, flags);
+    if (compatibleResult == INCOMPATIBLE) {
+        return android::base::Error() << error;
+    }
+    if (compatibleResult != COMPATIBLE) {
+        return android::base::Error(-compatibleResult) << error;
+    }
+
+    auto hasFcmExt = vintfObject->hasFrameworkCompatibilityMatrixExtensions();
+    if (!hasFcmExt.has_value()) {
+        return hasFcmExt.error();
+    }
+    if (*hasFcmExt) {
+        return vintfObject->checkUnusedHals();
+    }
+    LOG(INFO) << "Skip checking unused HALs.";
+    return {};
 }
 
 int checkDirmaps(const Dirmap& dirmap) {
@@ -469,23 +488,22 @@
         }
     }
 
-    std::string error;
     if (dirmap.empty()) {
         LOG(ERROR) << "Missing --rootdir or --dirmap option.";
         return usage(argv[0]);
     }
 
-    int compat = checkAllFiles(dirmap, properties, runtimeInfo, &error);
+    auto compat = checkAllFiles(dirmap, properties, runtimeInfo);
 
-    if (compat == COMPATIBLE) {
+    if (compat.ok()) {
         std::cout << "COMPATIBLE" << std::endl;
         return EX_OK;
     }
-    if (compat == INCOMPATIBLE) {
-        LOG(ERROR) << "files are incompatible: " << error;
+    if (compat.error().code() == 0) {
+        LOG(ERROR) << "files are incompatible: " << compat.error();
         std::cout << "INCOMPATIBLE" << std::endl;
         return EX_DATAERR;
     }
-    LOG(ERROR) << strerror(-compat) << ": " << error;
+    LOG(ERROR) << strerror(compat.error().code()) << ": " << compat.error();
     return EX_SOFTWARE;
 }