hidl-gen: generate same-package Makefile dependencies correctly

If, within a package, say android.hardware.tests.baz@1.0, one interface
depends on another interface (whether implicitly via types.hal) or
explicitly via interface-level extension (e.g., IBaz extends IBase) or
by referring to a user-defined type, -Lmakefile was not generating the
inter-dependencies correctly.

b/31015466
b/31045569

Change-Id: I797a8079496ca1e1d154e2fa917781c64c7de633
Signed-off-by: Iliyan Malchev <malchev@google.com>
diff --git a/main.cpp b/main.cpp
index 6f0f248..e9c07b7 100644
--- a/main.cpp
+++ b/main.cpp
@@ -159,6 +159,31 @@
     out << "\n$(GEN): PRIVATE_HIDL := $(HIDL)";
     out << "\n$(GEN): PRIVATE_DEPS := $(LOCAL_PATH)/"
         << fqName.name() << ".hal";
+
+    {
+        AST *ast = coordinator->parse(fqName);
+        CHECK(ast != nullptr);
+        const std::set<FQName>& refs = ast->getImportedNames();
+        for (auto depFQName : refs) {
+            // If the package of depFQName is the same as this fqName's package,
+            // then add it explicitly as a .hal dependency within the same
+            // package.
+            if (fqName.package() == depFQName.package() &&
+                fqName.version() == depFQName.version()) {
+                    // PRIVATE_DEPS is not actually being used in the
+                    // auto-generated file, but is necessary if the build rule
+                    // ever needs to use the dependency information, since the
+                    // built-in Make variables are not supported in the Android
+                    // build system.
+                    out << "\n$(GEN): PRIVATE_DEPS += $(LOCAL_PATH)/"
+                        << depFQName.name() << ".hal";
+                    // This is the actual dependency.
+                    out << "\n$(GEN): $(LOCAL_PATH)/"
+                        << depFQName.name() << ".hal";
+            }
+        }
+    }
+
     out << "\n$(GEN): PRIVATE_OUTPUT_DIR := $(intermediates)"
         << "\n$(GEN): PRIVATE_CUSTOM_TOOL = \\";
     out.indent();