Filter products during compile phase

Unfortunately there is no good way to deal with products in the link phase.
Products are like preprocessor defines in that they are processed early
and change the composition of the compiled unit.

Change-Id: I6d5e15ef60d29df8e83e059ba857c09333993779
diff --git a/tools/aapt2/compile/Compile.cpp b/tools/aapt2/compile/Compile.cpp
index 498bc9c..0bc5dce 100644
--- a/tools/aapt2/compile/Compile.cpp
+++ b/tools/aapt2/compile/Compile.cpp
@@ -102,6 +102,7 @@
 
 struct CompileOptions {
     std::string outputPath;
+    Maybe<std::u16string> product;
     bool verbose = false;
 };
 
@@ -121,8 +122,6 @@
 static bool compileTable(IAaptContext* context, const CompileOptions& options,
                          const ResourcePathData& pathData, const std::string& outputPath) {
     ResourceTable table;
-    table.createPackage(u"", 0x7f);
-
     {
         std::ifstream fin(pathData.source.path, std::ifstream::binary);
         if (!fin) {
@@ -134,7 +133,7 @@
         // Parse the values file from XML.
         XmlPullParser xmlParser(fin);
         ResourceParser resParser(context->getDiagnostics(), &table, pathData.source,
-                                 pathData.config);
+                                 pathData.config, ResourceParserOptions{ options.product });
         if (!resParser.parse(&xmlParser)) {
             return false;
         }
@@ -142,6 +141,12 @@
         fin.close();
     }
 
+    ResourceTablePackage* pkg = table.createPackage(context->getCompilationPackage());
+    if (!pkg->id) {
+        // If no package ID was set while parsing (public identifiers), auto assign an ID.
+        pkg->id = context->getPackageId();
+    }
+
     // Assign IDs to prepare the table for flattening.
     IdAssigner idAssigner;
     if (!idAssigner.consume(context, &table)) {
@@ -325,7 +330,7 @@
     }
 
     uint8_t getPackageId() override {
-       return 0x7f;
+       return 0x0;
     }
 
     ISymbolTable* getExternalSymbols() override {
@@ -340,13 +345,19 @@
 int compile(const std::vector<StringPiece>& args) {
     CompileOptions options;
 
+    Maybe<std::string> product;
     Flags flags = Flags()
             .requiredFlag("-o", "Output path", &options.outputPath)
+            .optionalFlag("--product", "Product type to compile", &product)
             .optionalSwitch("-v", "Enables verbose logging", &options.verbose);
     if (!flags.parse("aapt2 compile", args, &std::cerr)) {
         return 1;
     }
 
+    if (product) {
+        options.product = util::utf8ToUtf16(product.value());
+    }
+
     CompileContext context;
 
     std::vector<ResourcePathData> inputData;