Added -Lmakefile-impl.

This generates a makefile for the code that is generated using
-Lc++-impl. (example, makes the shared library
android.hardware.tests.foo@1.0.impl)

make hidl-gen -j64
hidl-gen -o system/tools/hidl/fooimpl/ -Lc++-impl -randroid.hardware:hardware/interfaces android.hardware.tests.foo@1.0
hidl-gen -o system/tools/hidl/fooimpl/ -Lmakefile-impl -randroid.hardware:hardware/interfaces android.hardware.tests.foo@1.0
make android.hardware.tests.foo@1.0.impl -j64

b/31055568

Change-Id: I57fccf3da07d6b22b9ca4a9f1783294d1848ca7e
diff --git a/main.cpp b/main.cpp
index 6e5d3dc..9dae1c2 100644
--- a/main.cpp
+++ b/main.cpp
@@ -412,6 +412,79 @@
     return OutputHandler::PASS_PACKAGE;
 }
 
+static status_t generateMakefileImplForPackage(
+        const FQName &packageFQName,
+        const char *,
+        Coordinator *coordinator,
+        const std::string &outputDir) {
+
+    const std::string libraryName = makeLibraryName(packageFQName) + ".impl";
+
+    std::vector<FQName> packageInterfaces;
+
+    status_t err =
+        coordinator->appendPackageInterfacesToSet(packageFQName,
+                                                  &packageInterfaces);
+
+    if (err != OK) {
+        return err;
+    }
+
+    std::set<FQName> importedPackages;
+
+    for (const auto &fqName : packageInterfaces) {
+        AST *ast = coordinator->parse(fqName);
+
+        if (ast == NULL) {
+            fprintf(stderr,
+                    "ERROR: Could not parse %s. Aborting.\n",
+                    fqName.string().c_str());
+
+            return UNKNOWN_ERROR;
+        }
+
+        ast->getImportedPackages(&importedPackages);
+    }
+
+    std::string path = outputDir + "Android.mk";
+
+    CHECK(Coordinator::MakeParentHierarchy(path));
+    FILE *file = fopen(path.c_str(), "w");
+
+    if (file == NULL) {
+        return -errno;
+    }
+
+    Formatter out(file);
+
+    out << "LOCAL_PATH := $(call my-dir)\n\n"
+        << "include $(CLEAR_VARS)\n"
+        << "LOCAL_MODULE := " << libraryName << "\n"
+        << "LOCAL_MODULE_RELATIVE_PATH := hw\n"
+        << "LOCAL_SRC_FILES := \\\n";
+    out.indent();
+    for (const auto &fqName : packageInterfaces) {
+        if (fqName.name() == "types") {
+            continue;
+        }
+        out << fqName.getInterfaceBaseName() << ".cpp \\\n";
+    }
+    out.unindent();
+    out << "\n";
+    out << "LOCAL_SHARED_LIBRARIES := \\\n";
+    out.indent();
+    out << "libhidl \\\n"
+        << "libhwbinder \\\n"
+        << "libutils \\\n"
+        << makeLibraryName(packageFQName) << " \\\n";
+    out.unindent();
+    out << "\n";
+
+    out << "include $(BUILD_SHARED_LIBRARY)\n";
+
+    return OK;
+}
+
 OutputHandler::ValRes validateForSource(
         const FQName &fqName, const std::string &language) {
     if (fqName.package().empty()) {
@@ -539,6 +612,12 @@
      validateForMakefile,
      generateMakefileForPackage,
     },
+
+    {"makefile-impl",
+     true /* mNeedsOutputDir */,
+     validateForMakefile,
+     generateMakefileImplForPackage,
+    }
 };
 
 static void usage(const char *me) {