assemble_vintf checks compatibility matrices as well.

Test: make and ensure that vendor/compatibility-matrix.xml exist.
Bug: 37321309
Change-Id: I87ae89c3b562bd26162c8412a062a23acda29370
diff --git a/assemble_vintf.cpp b/assemble_vintf.cpp
index cfa9bf6..b2b0ed1 100644
--- a/assemble_vintf.cpp
+++ b/assemble_vintf.cpp
@@ -51,46 +51,55 @@
 
     static bool assemble(std::basic_istream<char>& inFile,
                          std::basic_ostream<char>& outFile,
-                         bool isMatrix) {
+                         bool outputMatrix) {
         std::stringstream ss;
         ss << inFile.rdbuf();
         std::string fileContent = ss.str();
 
         HalManifest halManifest;
-        if (!gHalManifestConverter(&halManifest, fileContent)) {
-            std::cerr << "Illformed HAL manifest: " << gHalManifestConverter.lastError()
-                    << std::endl;
-            return false;
-        }
-
-        if (halManifest.mType == SchemaType::DEVICE) {
-            if (!getFlag("BOARD_SEPOLICY_VERS", &halManifest.device.mSepolicyVersion)) {
-                return false;
+        if (gHalManifestConverter(&halManifest, fileContent)) {
+            if (halManifest.mType == SchemaType::DEVICE) {
+                if (!getFlag("BOARD_SEPOLICY_VERS", &halManifest.device.mSepolicyVersion)) {
+                    return false;
+                }
             }
-        }
 
-        if (isMatrix) {
-            CompatibilityMatrix mat = halManifest.generateCompatibleMatrix();
-            std::string error;
-            if (!halManifest.checkCompatibility(mat, &error)) {
-                std::cerr << "FATAL ERROR: cannot generate a compatible matrix: "
-                          << error << std::endl;
+            if (outputMatrix) {
+                CompatibilityMatrix mat = halManifest.generateCompatibleMatrix();
+                std::string error;
+                if (!halManifest.checkCompatibility(mat, &error)) {
+                    std::cerr << "FATAL ERROR: cannot generate a compatible matrix: "
+                              << error << std::endl;
+                }
+                outFile << "<!-- \n"
+                           "    Autogenerated skeleton compatibility matrix. \n"
+                           "    Use with caution. Modify it to suit your needs.\n"
+                           "    All HALs are set to optional.\n"
+                           "    Many entries other than HALs are zero-filled and\n"
+                           "    require human attention. \n"
+                           "-->\n"
+                        << gCompatibilityMatrixConverter(mat);
+            } else {
+                outFile << gHalManifestConverter(halManifest);
             }
-            outFile << "<!-- \n"
-                       "    Autogenerated skeleton compatibility matrix. \n"
-                       "    Use with caution. Modify it to suit your needs.\n"
-                       "    All HALs are set to optional.\n"
-                       "    Many entries other than HALs are zero-filled and\n"
-                       "    require human attention. \n"
-                       "-->\n"
-                    << gCompatibilityMatrixConverter(mat);
-        } else {
-            outFile << gHalManifestConverter(halManifest);
+            outFile.flush();
+            return true;
         }
 
-        outFile.flush();
+        CompatibilityMatrix matrix;
+        if (gCompatibilityMatrixConverter(&matrix, fileContent)) {
+            // TODO (b/37342627): get BOARD_VNDK_VERSION and put it here.
+            outFile << gCompatibilityMatrixConverter(matrix);
+            outFile.flush();
+            return true;
+        }
 
-        return true;
+        std::cerr << "Input file has unknown format." << std::endl
+                  << "Error when attempting to convert to manifest: "
+                  << gHalManifestConverter.lastError() << std::endl
+                  << "Error when attempting to convert to compatibility matrix: "
+                  << gCompatibilityMatrixConverter.lastError() << std::endl;
+        return false;
     }
 };
 
@@ -99,20 +108,24 @@
 
 void help() {
     std::cerr <<
+        "assemble_vintf: Checks if a given manifest / matrix file is valid.\n"
         "assemble_vintf -h\n"
         "               Display this help text.\n"
         "assemble_vintf -i <input file> [-o <output file>] [-m]\n"
-        "               Fill in build-time flags into the given manifest.\n"
+        "               Fill in build-time flags into the given file.\n"
+        "               Input file format is automatically detected.\n"
         "               If no designated output file, write to stdout.\n"
         "               If -m is set, a compatible compatibility matrix is\n"
-        "               generated instead.\n";
+        "               generated instead; for example, given a device manifest,\n"
+        "               a framework compatibility matrix is generated. This flag\n"
+        "               is ignored when input is a compatibility matrix itself.\n";
 }
 
 int main(int argc, char **argv) {
     std::ifstream inFile;
     std::ofstream outFile;
     std::ostream* outFileRef = &std::cout;
-    bool isMatrix = false;
+    bool outputMatrix = false;
     int res;
     while((res = getopt(argc, argv, "hi:o:m")) >= 0) {
         switch (res) {
@@ -134,7 +147,7 @@
             } break;
 
             case 'm': {
-                isMatrix = true;
+                outputMatrix = true;
             } break;
 
             case 'h':
@@ -151,7 +164,7 @@
         return 1;
     }
 
-    return ::android::vintf::AssembleVintf::assemble(inFile, *outFileRef, isMatrix)
+    return ::android::vintf::AssembleVintf::assemble(inFile, *outFileRef, outputMatrix)
             ? 0 : 1;
 }