Add apex_set module.

apex_set takes an .apks file that contains a set of prebuilt apexes with
different configurations. It uses extract_apks to select and install the
best matching one for the current target.

Bug: 153456259
Test: apex_test.go
Test: com.android.media.apks
Change-Id: I1da8bbcf1611b7c580a0cb225856cbd7029cc0a7
Merged-In: I1da8bbcf1611b7c580a0cb225856cbd7029cc0a7
diff --git a/cmd/extract_apks/main_test.go b/cmd/extract_apks/main_test.go
index 1d7726b..bc4d377 100644
--- a/cmd/extract_apks/main_test.go
+++ b/cmd/extract_apks/main_test.go
@@ -35,20 +35,20 @@
 	configs   []TestConfigDesc
 }
 
-var (
-	testCases = []TestDesc{
+func TestSelectApks_ApkSet(t *testing.T) {
+	testCases := []TestDesc{
 		{
 			protoText: `
 variant {
   targeting {
-    sdk_version_targeting { 
+    sdk_version_targeting {
       value { min { value: 29 } } } }
   apk_set {
-    module_metadata { 
+    module_metadata {
       name: "base" targeting {} delivery_type: INSTALL_TIME }
     apk_description {
       targeting {
-        screen_density_targeting { 
+        screen_density_targeting {
           value { density_alias: LDPI } }
         sdk_version_targeting {
           value { min { value: 21 } } } }
@@ -71,7 +71,10 @@
     apk_description {
       targeting {
         abi_targeting {
-	      value { alias: ARMEABI_V7A } }
+          value { alias: ARMEABI_V7A }
+          alternatives { alias: ARM64_V8A }
+          alternatives { alias: X86 }
+          alternatives { alias: X86_64 } }
         sdk_version_targeting {
           value { min { value: 21 } } } }
       path: "splits/base-armeabi_v7a.apk"
@@ -79,7 +82,10 @@
     apk_description {
       targeting {
         abi_targeting {
-          value { alias: ARM64_V8A } }
+          value { alias: ARM64_V8A }
+          alternatives { alias: ARMEABI_V7A }
+          alternatives { alias: X86 }
+          alternatives { alias: X86_64 } }
         sdk_version_targeting {
           value { min { value: 21 } } } }
       path: "splits/base-arm64_v8a.apk"
@@ -87,7 +93,10 @@
     apk_description {
       targeting {
         abi_targeting {
-          value { alias: X86 } }
+          value { alias: X86 }
+          alternatives { alias: ARMEABI_V7A }
+          alternatives { alias: ARM64_V8A }
+          alternatives { alias: X86_64 } }
         sdk_version_targeting {
           value { min { value: 21 } } } }
       path: "splits/base-x86.apk"
@@ -95,7 +104,10 @@
     apk_description {
       targeting {
         abi_targeting {
-          value { alias: X86_64 } }
+          value { alias: X86_64 }
+          alternatives { alias: ARMEABI_V7A }
+          alternatives { alias: ARM64_V8A }
+          alternatives { alias: X86 } }
         sdk_version_targeting {
           value { min { value: 21 } } } }
       path: "splits/base-x86_64.apk"
@@ -113,9 +125,9 @@
 						screenDpi: map[bp.ScreenDensity_DensityAlias]bool{
 							bp.ScreenDensity_DENSITY_UNSPECIFIED: true,
 						},
-						abis: map[bp.Abi_AbiAlias]bool{
-							bp.Abi_ARMEABI_V7A: true,
-							bp.Abi_ARM64_V8A:   true,
+						abis: map[bp.Abi_AbiAlias]int{
+							bp.Abi_ARMEABI_V7A: 0,
+							bp.Abi_ARM64_V8A:   1,
 						},
 					},
 					expected: SelectionResult{
@@ -125,7 +137,6 @@
 							"splits/base-mdpi.apk",
 							"splits/base-master.apk",
 							"splits/base-armeabi_v7a.apk",
-							"splits/base-arm64_v8a.apk",
 						},
 					},
 				},
@@ -136,7 +147,7 @@
 						screenDpi: map[bp.ScreenDensity_DensityAlias]bool{
 							bp.ScreenDensity_LDPI: true,
 						},
-						abis: map[bp.Abi_AbiAlias]bool{},
+						abis: map[bp.Abi_AbiAlias]int{},
 					},
 					expected: SelectionResult{
 						"base",
@@ -153,23 +164,44 @@
 						screenDpi: map[bp.ScreenDensity_DensityAlias]bool{
 							bp.ScreenDensity_LDPI: true,
 						},
-						abis: map[bp.Abi_AbiAlias]bool{},
+						abis: map[bp.Abi_AbiAlias]int{},
 					},
 					expected: SelectionResult{
 						"",
 						nil,
 					},
 				},
+				{
+					name: "four",
+					targetConfig: TargetConfig{
+						sdkVersion: 29,
+						screenDpi: map[bp.ScreenDensity_DensityAlias]bool{
+							bp.ScreenDensity_MDPI: true,
+						},
+						abis: map[bp.Abi_AbiAlias]int{
+							bp.Abi_ARM64_V8A:   0,
+							bp.Abi_ARMEABI_V7A: 1,
+						},
+					},
+					expected: SelectionResult{
+						"base",
+						[]string{
+							"splits/base-mdpi.apk",
+							"splits/base-master.apk",
+							"splits/base-arm64_v8a.apk",
+						},
+					},
+				},
 			},
 		},
 		{
 			protoText: `
 variant {
   targeting {
-    sdk_version_targeting { 
+    sdk_version_targeting {
       value { min { value: 10000 } } } }
   apk_set {
-    module_metadata { 
+    module_metadata {
       name: "base" targeting {} delivery_type: INSTALL_TIME }
     apk_description {
       targeting {
@@ -183,7 +215,7 @@
 					targetConfig: TargetConfig{
 						sdkVersion:       30,
 						screenDpi:        map[bp.ScreenDensity_DensityAlias]bool{},
-						abis:             map[bp.Abi_AbiAlias]bool{},
+						abis:             map[bp.Abi_AbiAlias]int{},
 						allowPrereleased: true,
 					},
 					expected: SelectionResult{
@@ -194,9 +226,160 @@
 			},
 		},
 	}
-)
+	for _, testCase := range testCases {
+		var toc bp.BuildApksResult
+		if err := proto.UnmarshalText(testCase.protoText, &toc); err != nil {
+			t.Fatal(err)
+		}
+		for _, config := range testCase.configs {
+			actual := selectApks(&toc, config.targetConfig)
+			if !reflect.DeepEqual(config.expected, actual) {
+				t.Errorf("%s: expected %v, got %v", config.name, config.expected, actual)
+			}
+		}
+	}
+}
 
-func TestSelectApks(t *testing.T) {
+func TestSelectApks_ApexSet(t *testing.T) {
+	testCases := []TestDesc{
+		{
+			protoText: `
+variant {
+  targeting {
+    sdk_version_targeting {
+      value { min { value: 29 } } } }
+  apk_set {
+    module_metadata {
+      name: "base" targeting {} delivery_type: INSTALL_TIME }
+    apk_description {
+      targeting {
+        multi_abi_targeting {
+          value { abi { alias: ARMEABI_V7A } }
+          alternatives { abi { alias: ARM64_V8A } }
+          alternatives { abi { alias: X86 } }
+          alternatives { abi { alias: X86_64 } } }
+        sdk_version_targeting {
+          value { min { value: 21 } } } }
+      path: "standalones/standalone-armeabi_v7a.apex"
+      apex_apk_metadata { } }
+    apk_description {
+      targeting {
+        multi_abi_targeting {
+          value { abi { alias: ARM64_V8A } }
+          alternatives { abi { alias: ARMEABI_V7A } }
+          alternatives { abi { alias: X86 } }
+          alternatives { abi { alias: X86_64 } } }
+        sdk_version_targeting {
+          value { min { value: 21 } } } }
+      path: "standalones/standalone-arm64_v8a.apex"
+      apex_apk_metadata { } }
+    apk_description {
+      targeting {
+        multi_abi_targeting {
+          value { abi { alias: X86 } }
+          alternatives { abi { alias: ARMEABI_V7A } }
+          alternatives { abi { alias: ARM64_V8A } }
+          alternatives { abi { alias: X86_64 } } }
+        sdk_version_targeting {
+          value { min { value: 21 } } } }
+      path: "standalones/standalone-x86.apex"
+      apex_apk_metadata { } }
+    apk_description {
+      targeting {
+        multi_abi_targeting {
+          value { abi { alias: X86_64 } }
+          alternatives { abi { alias: ARMEABI_V7A } }
+          alternatives { abi { alias: ARM64_V8A } }
+          alternatives { abi { alias: X86 } } }
+        sdk_version_targeting {
+          value { min { value: 21 } } } }
+      path: "standalones/standalone-x86_64.apex"
+      apex_apk_metadata { } } }
+}
+bundletool {
+  version: "0.10.3" }
+
+`,
+			configs: []TestConfigDesc{
+				{
+					name: "order matches priorities",
+					targetConfig: TargetConfig{
+						sdkVersion: 29,
+						screenDpi: map[bp.ScreenDensity_DensityAlias]bool{
+							bp.ScreenDensity_DENSITY_UNSPECIFIED: true,
+						},
+						abis: map[bp.Abi_AbiAlias]int{
+							bp.Abi_ARM64_V8A:   0,
+							bp.Abi_ARMEABI_V7A: 1,
+						},
+					},
+					expected: SelectionResult{
+						"base",
+						[]string{
+							"standalones/standalone-arm64_v8a.apex",
+						},
+					},
+				},
+				{
+					name: "order doesn't match priorities",
+					targetConfig: TargetConfig{
+						sdkVersion: 29,
+						screenDpi: map[bp.ScreenDensity_DensityAlias]bool{
+							bp.ScreenDensity_DENSITY_UNSPECIFIED: true,
+						},
+						abis: map[bp.Abi_AbiAlias]int{
+							bp.Abi_ARMEABI_V7A: 0,
+							bp.Abi_ARM64_V8A:   1,
+						},
+					},
+					expected: SelectionResult{
+						"base",
+						[]string{
+							"standalones/standalone-arm64_v8a.apex",
+						},
+					},
+				},
+				{
+					name: "single choice",
+					targetConfig: TargetConfig{
+						sdkVersion: 29,
+						screenDpi: map[bp.ScreenDensity_DensityAlias]bool{
+							bp.ScreenDensity_DENSITY_UNSPECIFIED: true,
+						},
+						abis: map[bp.Abi_AbiAlias]int{
+							bp.Abi_ARMEABI_V7A: 0,
+						},
+					},
+					expected: SelectionResult{
+						"base",
+						[]string{
+							"standalones/standalone-armeabi_v7a.apex",
+						},
+					},
+				},
+				{
+					name: "cross platform",
+					targetConfig: TargetConfig{
+						sdkVersion: 29,
+						screenDpi: map[bp.ScreenDensity_DensityAlias]bool{
+							bp.ScreenDensity_DENSITY_UNSPECIFIED: true,
+						},
+						abis: map[bp.Abi_AbiAlias]int{
+							bp.Abi_ARM64_V8A: 0,
+							bp.Abi_MIPS64:    1,
+							bp.Abi_X86:       2,
+						},
+					},
+					expected: SelectionResult{
+						"base",
+						[]string{
+							"standalones/standalone-x86.apex",
+						},
+					},
+				},
+			},
+		},
+	}
 	for _, testCase := range testCases {
 		var toc bp.BuildApksResult
 		if err := proto.UnmarshalText(testCase.protoText, &toc); err != nil {