Support rust modules when packaging aconfig files in apex.

Also remove the code to package aconfig files for transitive
dependencies since those files should have already been included in the
direct dependencies.

Bug: 311173471
Test: Unit tests
Change-Id: I25637af9381f71a78b70a8de9894618188c9735d
diff --git a/aconfig/aconfig_declarations.go b/aconfig/aconfig_declarations.go
index 593ccc0..697dc22 100644
--- a/aconfig/aconfig_declarations.go
+++ b/aconfig/aconfig_declarations.go
@@ -211,6 +211,7 @@
 }
 
 func mergeAconfigFiles(ctx android.ModuleContext, inputs android.Paths) android.Paths {
+	inputs = android.LastUniquePaths(inputs)
 	if len(inputs) == 1 {
 		return android.Paths{inputs[0]}
 	}
diff --git a/apex/apex.go b/apex/apex.go
index a4dc7f1..38a166e 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -1970,6 +1970,7 @@
 				fi := apexFileForRustLibrary(ctx, ch)
 				fi.isJniLib = isJniLib
 				vctx.filesInfo = append(vctx.filesInfo, fi)
+				addAconfigFiles(vctx, ctx, child)
 				return true // track transitive dependencies
 			default:
 				ctx.PropertyErrorf(propertyName, "%q is not a cc_library or cc_library_shared module", depName)
@@ -1982,6 +1983,7 @@
 				return true // track transitive dependencies
 			case *rust.Module:
 				vctx.filesInfo = append(vctx.filesInfo, apexFileForRustExecutable(ctx, ch))
+				addAconfigFiles(vctx, ctx, child)
 				return true // track transitive dependencies
 			default:
 				ctx.PropertyErrorf("binaries",
@@ -2189,7 +2191,6 @@
 			}
 
 			vctx.filesInfo = append(vctx.filesInfo, af)
-			addAconfigFiles(vctx, ctx, child)
 			return true // track transitive dependencies
 		} else if rm, ok := child.(*rust.Module); ok {
 			af := apexFileForRustLibrary(ctx, rm)
diff --git a/apex/apex_test.go b/apex/apex_test.go
index 2ed053e..abf6b15 100644
--- a/apex/apex_test.go
+++ b/apex/apex_test.go
@@ -10897,9 +10897,7 @@
 			sdk_version: "none",
 			system_modules: "none",
 			static_libs: ["my_java_aconfig_library_foo"],
-			// TODO: remove //apex_available:platform
 			apex_available: [
-				"//apex_available:platform",
 				"myapex",
 			],
 		}
@@ -10910,9 +10908,7 @@
 			sdk_version: "none",
 			system_modules: "none",
 			static_libs: ["my_java_aconfig_library_bar"],
-			// TODO: remove //apex_available:platform
 			apex_available: [
-				"//apex_available:platform",
 				"myapex",
 			],
 		}
@@ -10927,9 +10923,7 @@
 		java_aconfig_library {
 			name: "my_java_aconfig_library_foo",
 			aconfig_declarations: "my_aconfig_declarations_foo",
-			// TODO: remove //apex_available:platform
 			apex_available: [
-				"//apex_available:platform",
 				"myapex",
 			],
 		}
@@ -10944,9 +10938,7 @@
 		java_aconfig_library {
 			name: "my_java_aconfig_library_bar",
 			aconfig_declarations: "my_aconfig_declarations_bar",
-			// TODO: remove //apex_available:platform
 			apex_available: [
-				"//apex_available:platform",
 				"myapex",
 			],
 		}
@@ -11001,9 +10993,7 @@
 			sdk_version: "none",
 			system_modules: "none",
 			static_libs: ["my_java_aconfig_library_foo"],
-			// TODO: remove //apex_available:platform
 			apex_available: [
-				"//apex_available:platform",
 				"myapex",
 			],
 		}
@@ -11011,10 +11001,11 @@
 		cc_library {
 			name: "my_cc_library_bar",
 			srcs: ["foo/bar/MyClass.cc"],
-			static_libs: ["my_cc_aconfig_library_bar"],
-			// TODO: remove //apex_available:platform
+			static_libs: [
+				"my_cc_aconfig_library_bar",
+				"my_cc_aconfig_library_baz",
+			],
 			apex_available: [
-				"//apex_available:platform",
 				"myapex",
 			],
 		}
@@ -11023,9 +11014,7 @@
 			name: "my_cc_binary_baz",
 			srcs: ["foo/bar/MyClass.cc"],
 			static_libs: ["my_cc_aconfig_library_baz"],
-			// TODO: remove //apex_available:platform
 			apex_available: [
-				"//apex_available:platform",
 				"myapex",
 			],
 		}
@@ -11040,9 +11029,7 @@
 		java_aconfig_library {
 			name: "my_java_aconfig_library_foo",
 			aconfig_declarations: "my_aconfig_declarations_foo",
-			// TODO: remove //apex_available:platform
 			apex_available: [
-				"//apex_available:platform",
 				"myapex",
 			],
 		}
@@ -11057,9 +11044,7 @@
 		cc_aconfig_library {
 			name: "my_cc_aconfig_library_bar",
 			aconfig_declarations: "my_aconfig_declarations_bar",
-			// TODO: remove //apex_available:platform
 			apex_available: [
-				"//apex_available:platform",
 				"myapex",
 			],
 		}
@@ -11074,9 +11059,7 @@
 		cc_aconfig_library {
 			name: "my_cc_aconfig_library_baz",
 			aconfig_declarations: "my_aconfig_declarations_baz",
-			// TODO: remove //apex_available:platform
 			apex_available: [
-				"//apex_available:platform",
 				"myapex",
 			],
 		}
@@ -11116,6 +11099,151 @@
 	ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
 }
 
+func TestAconfigFilesRustDeps(t *testing.T) {
+	ctx := testApex(t, apex_default_bp+`
+		apex {
+			name: "myapex",
+			manifest: ":myapex.manifest",
+			androidManifest: ":myapex.androidmanifest",
+			key: "myapex.key",
+			native_shared_libs: [
+				"libmy_rust_library",
+			],
+			binaries: [
+				"my_rust_binary",
+			],
+			rust_dyn_libs: [
+				"libmy_rust_dylib",
+			],
+			updatable: false,
+		}
+
+		rust_library {
+			name: "libflags_rust", // test mock
+			crate_name: "flags_rust",
+			srcs: ["lib.rs"],
+			apex_available: [
+				"myapex",
+			],
+		}
+
+		rust_library {
+			name: "liblazy_static", // test mock
+			crate_name: "lazy_static",
+			srcs: ["src/lib.rs"],
+			apex_available: [
+				"myapex",
+			],
+		}
+
+		rust_ffi_shared {
+			name: "libmy_rust_library",
+			srcs: ["src/lib.rs"],
+			rustlibs: ["libmy_rust_aconfig_library_foo"],
+			crate_name: "my_rust_library",
+			apex_available: [
+				"myapex",
+			],
+		}
+
+		rust_library_dylib {
+			name: "libmy_rust_dylib",
+			srcs: ["foo/bar/MyClass.rs"],
+			rustlibs: ["libmy_rust_aconfig_library_bar"],
+			crate_name: "my_rust_dylib",
+			apex_available: [
+				"myapex",
+			],
+		}
+
+		rust_binary {
+			name: "my_rust_binary",
+			srcs: ["foo/bar/MyClass.rs"],
+			rustlibs: [
+				"libmy_rust_aconfig_library_baz",
+				"libmy_rust_dylib",
+			],
+			apex_available: [
+				"myapex",
+			],
+		}
+
+		aconfig_declarations {
+			name: "my_aconfig_declarations_foo",
+			package: "com.example.package",
+			container: "myapex",
+			srcs: ["foo.aconfig"],
+		}
+
+		aconfig_declarations {
+			name: "my_aconfig_declarations_bar",
+			package: "com.example.package",
+			container: "myapex",
+			srcs: ["bar.aconfig"],
+		}
+
+		aconfig_declarations {
+			name: "my_aconfig_declarations_baz",
+			package: "com.example.package",
+			container: "myapex",
+			srcs: ["baz.aconfig"],
+		}
+
+		rust_aconfig_library {
+			name: "libmy_rust_aconfig_library_foo",
+			aconfig_declarations: "my_aconfig_declarations_foo",
+			crate_name: "my_rust_aconfig_library_foo",
+			apex_available: [
+				"myapex",
+			],
+		}
+
+		rust_aconfig_library {
+			name: "libmy_rust_aconfig_library_bar",
+			aconfig_declarations: "my_aconfig_declarations_bar",
+			crate_name: "my_rust_aconfig_library_bar",
+			apex_available: [
+				"myapex",
+			],
+		}
+
+		rust_aconfig_library {
+			name: "libmy_rust_aconfig_library_baz",
+			aconfig_declarations: "my_aconfig_declarations_baz",
+			crate_name: "my_rust_aconfig_library_baz",
+			apex_available: [
+				"myapex",
+			],
+		}
+	`)
+
+	mod := ctx.ModuleForTests("myapex", "android_common_myapex")
+	s := mod.Rule("apexRule").Args["copy_commands"]
+	copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
+	if len(copyCmds) != 23 {
+		t.Fatalf("Expected 23 commands, got %d in:\n%s", len(copyCmds), s)
+	}
+
+	ensureMatches(t, copyCmds[22], "^cp -f .*/aconfig_flags.pb .*/image.apex$")
+
+	combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
+	s = " " + combineAconfigRule.Args["cache_files"]
+	aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
+	if len(aconfigArgs) != 2 {
+		t.Fatalf("Expected 2 commands, got %d in:\n%s", len(aconfigArgs), s)
+	}
+	android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
+	android.EnsureListContainsSuffix(t, aconfigArgs, "my_rust_binary/android_arm64_armv8-a_apex10000/aconfig_merged.pb")
+
+	buildParams := combineAconfigRule.BuildParams
+	if len(buildParams.Inputs) != 2 {
+		t.Fatalf("Expected 3 input, got %d", len(buildParams.Inputs))
+	}
+	android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
+	android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_rust_binary/android_arm64_armv8-a_apex10000/aconfig_merged.pb")
+	ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
+}
+
 func TestAconfigFilesOnlyMatchCurrentApex(t *testing.T) {
 	ctx := testApex(t, apex_default_bp+`
 		apex {
@@ -11136,9 +11264,7 @@
 			sdk_version: "none",
 			system_modules: "none",
 			static_libs: ["my_java_aconfig_library_foo"],
-			// TODO: remove //apex_available:platform
 			apex_available: [
-				"//apex_available:platform",
 				"myapex",
 			],
 		}
@@ -11149,9 +11275,7 @@
 			sdk_version: "none",
 			system_modules: "none",
 			static_libs: ["other_java_aconfig_library_bar"],
-			// TODO: remove //apex_available:platform
 			apex_available: [
-				"//apex_available:platform",
 				"myapex",
 			],
 		}
@@ -11166,9 +11290,7 @@
 		java_aconfig_library {
 			name: "my_java_aconfig_library_foo",
 			aconfig_declarations: "my_aconfig_declarations_foo",
-			// TODO: remove //apex_available:platform
 			apex_available: [
-				"//apex_available:platform",
 				"myapex",
 			],
 		}
@@ -11183,9 +11305,7 @@
 		java_aconfig_library {
 			name: "other_java_aconfig_library_bar",
 			aconfig_declarations: "other_aconfig_declarations_bar",
-			// TODO: remove //apex_available:platform
 			apex_available: [
-				"//apex_available:platform",
 				"myapex",
 			],
 		}
@@ -11228,9 +11348,7 @@
 			sdk_version: "none",
 			system_modules: "none",
 			static_libs: ["my_java_aconfig_library_foo"],
-			// TODO: remove //apex_available:platform
 			apex_available: [
-				"//apex_available:platform",
 				"myapex",
 			],
 		}
@@ -11241,9 +11359,7 @@
 			sdk_version: "none",
 			system_modules: "none",
 			static_libs: ["my_java_aconfig_library_bar"],
-			// TODO: remove //apex_available:platform
 			apex_available: [
-				"//apex_available:platform",
 				"myapex",
 			],
 		}
@@ -11258,9 +11374,7 @@
 		java_aconfig_library {
 			name: "my_java_aconfig_library_foo",
 			aconfig_declarations: "my_aconfig_declarations_foo",
-			// TODO: remove //apex_available:platform
 			apex_available: [
-				"//apex_available:platform",
 				"myapex",
 			],
 		}
@@ -11268,9 +11382,7 @@
 		java_aconfig_library {
 			name: "my_java_aconfig_library_bar",
 			aconfig_declarations: "my_aconfig_declarations_foo",
-			// TODO: remove //apex_available:platform
 			apex_available: [
-				"//apex_available:platform",
 				"myapex",
 			],
 		}