Merge "Sandbox camera-its" into main
diff --git a/aconfig/aconfig_declarations.go b/aconfig/aconfig_declarations.go
index 697dc22..c4fc31a 100644
--- a/aconfig/aconfig_declarations.go
+++ b/aconfig/aconfig_declarations.go
@@ -230,3 +230,12 @@
 
 	return android.Paths{output}
 }
+
+func SetAconfigFileMkEntries(m *android.ModuleBase, entries *android.AndroidMkEntries, aconfigFiles map[string]android.Paths) {
+	if m.InstallInVendor() {
+		entries.SetPaths("LOCAL_ACONFIG_FILES", aconfigFiles["vendor"])
+	} else {
+		// TODO(b/311155208): The container here should be system.
+		entries.SetPaths("LOCAL_ACONFIG_FILES", aconfigFiles[""])
+	}
+}
diff --git a/aconfig/codegen/cc_aconfig_library_test.go b/aconfig/codegen/cc_aconfig_library_test.go
index 0c8a969..3de4626 100644
--- a/aconfig/codegen/cc_aconfig_library_test.go
+++ b/aconfig/codegen/cc_aconfig_library_test.go
@@ -104,3 +104,61 @@
 			}
 		`, bpMode))
 }
+
+func TestAndroidMkCcLibrary(t *testing.T) {
+	bp := `
+		aconfig_declarations {
+			name: "my_aconfig_declarations_foo",
+			package: "com.example.package",
+			srcs: ["foo.aconfig"],
+			container: "vendor",
+		}
+
+		cc_aconfig_library {
+			name: "my_cc_aconfig_library_foo",
+			aconfig_declarations: "my_aconfig_declarations_foo",
+			vendor_available: true,
+		}
+
+		aconfig_declarations {
+			name: "my_aconfig_declarations_bar",
+			package: "com.example.package",
+			srcs: ["bar.aconfig"],
+		}
+
+		cc_aconfig_library {
+			name: "my_cc_aconfig_library_bar",
+			aconfig_declarations: "my_aconfig_declarations_bar",
+			vendor_available: true,
+		}
+
+		cc_library {
+			name: "my_cc_library",
+			srcs: [
+				"src/foo.cc",
+			],
+			static_libs: [
+				"my_cc_aconfig_library_foo",
+				"my_cc_aconfig_library_bar",
+			],
+			vendor: true,
+		}
+
+		cc_library {
+			name: "server_configurable_flags",
+			srcs: ["server_configurable_flags.cc"],
+		}
+	`
+	result := android.GroupFixturePreparers(
+		PrepareForTestWithAconfigBuildComponents,
+		cc.PrepareForTestWithCcDefaultModules).
+		ExtendWithErrorHandler(android.FixtureExpectsNoErrors).RunTestWithBp(t, bp)
+
+	module := result.ModuleForTests("my_cc_library", "android_arm64_armv8-a_shared").Module()
+
+	entry := android.AndroidMkEntriesForTest(t, result.TestContext, module)[0]
+
+	makeVar := entry.EntryMap["LOCAL_ACONFIG_FILES"]
+	android.AssertIntEquals(t, "len(LOCAL_ACONFIG_FILES)", 1, len(makeVar))
+	android.EnsureListContainsSuffix(t, makeVar, "my_aconfig_declarations_foo/intermediate.pb")
+}
diff --git a/android/arch.go b/android/arch.go
index 7436660..c39db02 100644
--- a/android/arch.go
+++ b/android/arch.go
@@ -446,8 +446,10 @@
 		}
 	}
 
+	createCommonOSVariant := base.commonProperties.CreateCommonOSVariant
+
 	// If there are no supported OSes then disable the module.
-	if len(moduleOSList) == 0 {
+	if len(moduleOSList) == 0 && !createCommonOSVariant {
 		base.Disable()
 		return
 	}
@@ -458,7 +460,6 @@
 		osNames[i] = os.String()
 	}
 
-	createCommonOSVariant := base.commonProperties.CreateCommonOSVariant
 	if createCommonOSVariant {
 		// A CommonOS variant was requested so add it to the list of OS variants to
 		// create. It needs to be added to the end because it needs to depend on the
diff --git a/cc/androidmk.go b/cc/androidmk.go
index 8cae634..54c7f97 100644
--- a/cc/androidmk.go
+++ b/cc/androidmk.go
@@ -15,6 +15,7 @@
 package cc
 
 import (
+	"android/soong/aconfig"
 	"github.com/google/blueprint/proptools"
 
 	"fmt"
@@ -133,8 +134,7 @@
 					entries.SetString("SOONG_SDK_VARIANT_MODULES",
 						"$(SOONG_SDK_VARIANT_MODULES) $(patsubst %.sdk,%,$(LOCAL_MODULE))")
 				}
-				// TODO(b/311155208): The container here should be system.
-				entries.SetPaths("LOCAL_ACONFIG_FILES", c.mergedAconfigFiles[""])
+				aconfig.SetAconfigFileMkEntries(c.AndroidModuleBase(), entries, c.mergedAconfigFiles)
 			},
 		},
 		ExtraFooters: []android.AndroidMkExtraFootersFunc{
diff --git a/cc/compiler.go b/cc/compiler.go
index bb7885b..c9de1b0 100644
--- a/cc/compiler.go
+++ b/cc/compiler.go
@@ -479,7 +479,7 @@
 			target += strconv.Itoa(android.FutureApiLevelInt)
 		} else {
 			apiLevel := nativeApiLevelOrPanic(ctx, version)
-			target += apiLevel.String()
+			target += strconv.Itoa(apiLevel.FinalOrFutureInt())
 		}
 	}
 
diff --git a/java/androidmk.go b/java/androidmk.go
index a3f94cd..809f9b5 100644
--- a/java/androidmk.go
+++ b/java/androidmk.go
@@ -19,6 +19,7 @@
 	"io"
 	"strings"
 
+	"android/soong/aconfig"
 	"android/soong/android"
 
 	"github.com/google/blueprint/proptools"
@@ -128,9 +129,7 @@
 					if library.dexpreopter.configPath != nil {
 						entries.SetPath("LOCAL_SOONG_DEXPREOPT_CONFIG", library.dexpreopter.configPath)
 					}
-					// TODO(b/311155208): The container here should be system.
-
-					entries.SetPaths("LOCAL_ACONFIG_FILES", library.mergedAconfigFiles[""])
+					aconfig.SetAconfigFileMkEntries(&library.ModuleBase, entries, library.mergedAconfigFiles)
 				},
 			},
 		})
@@ -307,8 +306,7 @@
 					if len(binary.dexpreopter.builtInstalled) > 0 {
 						entries.SetString("LOCAL_SOONG_BUILT_INSTALLED", binary.dexpreopter.builtInstalled)
 					}
-					// TODO(b/311155208): The container here should be system.
-					entries.SetPaths("LOCAL_ACONFIG_FILES", binary.mergedAconfigFiles[""])
+					aconfig.SetAconfigFileMkEntries(&binary.ModuleBase, entries, binary.mergedAconfigFiles)
 				},
 			},
 			ExtraFooters: []android.AndroidMkExtraFootersFunc{
@@ -461,8 +459,7 @@
 				entries.SetOptionalPaths("LOCAL_SOONG_LINT_REPORTS", app.linter.reports)
 
 				if app.Name() != "framework-res" {
-					// TODO(b/311155208): The container here should be system.
-					entries.SetPaths("LOCAL_ACONFIG_FILES", app.mergedAconfigFiles[""])
+					aconfig.SetAconfigFileMkEntries(&app.ModuleBase, entries, app.mergedAconfigFiles)
 				}
 			},
 		},
@@ -540,8 +537,7 @@
 		entries.SetPath("LOCAL_FULL_MANIFEST_FILE", a.mergedManifestFile)
 		entries.SetPath("LOCAL_SOONG_EXPORT_PROGUARD_FLAGS", a.combinedExportedProguardFlagsFile)
 		entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", true)
-		// TODO(b/311155208): The container here should be system.
-		entries.SetPaths("LOCAL_ACONFIG_FILES", a.mergedAconfigFiles[""])
+		aconfig.SetAconfigFileMkEntries(&a.ModuleBase, entries, a.mergedAconfigFiles)
 	})
 
 	return entriesList
diff --git a/java/app.go b/java/app.go
index d5c4eba..ee82a32 100755
--- a/java/app.go
+++ b/java/app.go
@@ -139,12 +139,6 @@
 	// PRODUCT_CHARACTERISTICS.
 	Generate_product_characteristics_rro *bool
 
-	// A list of files or dependencies to make available to the build sandbox. This is
-	// useful if source files are symlinks, the targets of the symlinks must be listed here.
-	// Note that currently not all actions implemented by android_apps are sandboxed, so you
-	// may only see this being necessary in lint builds.
-	Compile_data []string
-
 	ProductCharacteristicsRROPackageName        *string `blueprint:"mutated"`
 	ProductCharacteristicsRROManifestModuleName *string `blueprint:"mutated"`
 }
@@ -824,7 +818,6 @@
 	a.linter.mergedManifest = a.aapt.mergedManifestFile
 	a.linter.manifest = a.aapt.manifestPath
 	a.linter.resources = a.aapt.resourceFiles
-	a.linter.compile_data = android.PathsForModuleSrc(ctx, a.appProperties.Compile_data)
 	a.linter.buildModuleReportZip = ctx.Config().UnbundledBuildApps()
 
 	dexJarFile, packageResources := a.dexBuildActions(ctx)
diff --git a/java/base.go b/java/base.go
index cdb58a2..7cd2820 100644
--- a/java/base.go
+++ b/java/base.go
@@ -195,6 +195,12 @@
 
 	// If true, then only the headers are built and not the implementation jar.
 	Headers_only *bool
+
+	// A list of files or dependencies to make available to the build sandbox. This is
+	// useful if source files are symlinks, the targets of the symlinks must be listed here.
+	// Note that currently not all actions implemented by android_apps are sandboxed, so you
+	// may only see this being necessary in lint builds.
+	Compile_data []string `android:"path"`
 }
 
 // Properties that are specific to device modules. Host module factories should not add these when
@@ -1677,6 +1683,7 @@
 		j.linter.compileSdkKind = j.SdkVersion(ctx).Kind
 		j.linter.javaLanguageLevel = flags.javaVersion.String()
 		j.linter.kotlinLanguageLevel = "1.3"
+		j.linter.compile_data = android.PathsForModuleSrc(ctx, j.properties.Compile_data)
 		if !apexInfo.IsForPlatform() && ctx.Config().UnbundledBuildApps() {
 			j.linter.buildModuleReportZip = true
 		}
diff --git a/rust/androidmk.go b/rust/androidmk.go
index 733ffc5..c355a56 100644
--- a/rust/androidmk.go
+++ b/rust/androidmk.go
@@ -17,6 +17,7 @@
 import (
 	"path/filepath"
 
+	"android/soong/aconfig"
 	"android/soong/android"
 )
 
@@ -66,8 +67,7 @@
 				if mod.UseVndk() {
 					entries.SetBool("LOCAL_USE_VNDK", true)
 				}
-				// TODO(b/311155208): The container here should be system.
-				entries.SetPaths("LOCAL_ACONFIG_FILES", mod.mergedAconfigFiles[""])
+				aconfig.SetAconfigFileMkEntries(mod.AndroidModuleBase(), entries, mod.mergedAconfigFiles)
 			},
 		},
 	}
diff --git a/sdk/Android.bp b/sdk/Android.bp
index f42b478..f436320 100644
--- a/sdk/Android.bp
+++ b/sdk/Android.bp
@@ -18,6 +18,7 @@
         "bp.go",
         "build_release.go",
         "exports.go",
+        "genrule.go",
         "member_trait.go",
         "member_type.go",
         "sdk.go",
@@ -30,6 +31,7 @@
         "cc_sdk_test.go",
         "compat_config_sdk_test.go",
         "exports_test.go",
+        "genrule_test.go",
         "java_sdk_test.go",
         "license_sdk_test.go",
         "member_trait_test.go",
diff --git a/sdk/genrule.go b/sdk/genrule.go
new file mode 100644
index 0000000..347ab05
--- /dev/null
+++ b/sdk/genrule.go
@@ -0,0 +1,44 @@
+// Copyright 2023 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//	http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package sdk
+
+import (
+	"android/soong/android"
+	"android/soong/genrule"
+)
+
+func init() {
+	registerGenRuleBuildComponents(android.InitRegistrationContext)
+}
+
+func registerGenRuleBuildComponents(ctx android.RegistrationContext) {
+	ctx.RegisterModuleType("sdk_genrule", SdkGenruleFactory)
+}
+
+// sdk_genrule_host is a genrule that can depend on sdk and sdk_snapshot module types
+//
+// What this means is that it's a genrule with only the "common_os" variant.
+// sdk modules have 3 variants: host, android, and common_os. The common_os one depends
+// on the host/device ones and packages their result into a final snapshot zip.
+// Genrules probably want access to this snapshot zip when they depend on an sdk module,
+// which means they want to depend on the common_os variant and not the host/android
+// variants.
+func SdkGenruleFactory() android.Module {
+	module := genrule.NewGenRule()
+
+	android.InitCommonOSAndroidMultiTargetsArchModule(module, android.NeitherHostNorDeviceSupported, android.MultilibCommon)
+	android.InitDefaultableModule(module)
+
+	return module
+}
diff --git a/sdk/genrule_test.go b/sdk/genrule_test.go
new file mode 100644
index 0000000..6e52a3d
--- /dev/null
+++ b/sdk/genrule_test.go
@@ -0,0 +1,52 @@
+// Copyright 2018 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package sdk
+
+import (
+	"testing"
+
+	"android/soong/android"
+	"android/soong/genrule"
+	"android/soong/java"
+)
+
+func TestSdkGenrule(t *testing.T) {
+	// Test that an sdk_genrule can depend on an sdk, and that a genrule can depend on an sdk_genrule
+	bp := `
+				sdk {
+					name: "my_sdk",
+				}
+				sdk_genrule {
+					name: "my_sdk_genrule",
+					tool_files: ["tool"],
+					cmd: "$(location tool) $(in) $(out)",
+					srcs: [":my_sdk"],
+					out: ["out"],
+				}
+				genrule {
+					name: "my_regular_genrule",
+					srcs: [":my_sdk_genrule"],
+					out: ["out"],
+					cmd: "cp $(in) $(out)",
+				}
+			`
+	android.GroupFixturePreparers(
+		// if java components aren't registered, the sdk module doesn't create a snapshot for some reason.
+		java.PrepareForTestWithJavaBuildComponents,
+		genrule.PrepareForTestWithGenRuleBuildComponents,
+		PrepareForTestWithSdkBuildComponents,
+		android.FixtureRegisterWithContext(registerGenRuleBuildComponents),
+	).RunTestWithBp(t, bp)
+}
diff --git a/sdk/sdk.go b/sdk/sdk.go
index 4d4a2a2..fd16ab6 100644
--- a/sdk/sdk.go
+++ b/sdk/sdk.go
@@ -222,6 +222,18 @@
 	}}
 }
 
+func (s *sdk) OutputFiles(tag string) (android.Paths, error) {
+	switch tag {
+	case "":
+		if s.snapshotFile.Valid() {
+			return []android.Path{s.snapshotFile.Path()}, nil
+		}
+		return nil, fmt.Errorf("snapshot file not defined. This is most likely because this isn't the common_os variant of this module")
+	default:
+		return nil, fmt.Errorf("unknown tag %q", tag)
+	}
+}
+
 // gatherTraits gathers the traits from the dynamically generated trait specific properties.
 //
 // Returns a map from member name to the set of required traits.
diff --git a/tradefed/suite_harness/tradefed_binary.go b/tradefed/suite_harness/tradefed_binary.go
index 1ce94bc..96fb354 100644
--- a/tradefed/suite_harness/tradefed_binary.go
+++ b/tradefed/suite_harness/tradefed_binary.go
@@ -35,6 +35,7 @@
 	Short_name                    string
 	Full_name                     string
 	Version                       string
+	Suite_arch                    string
 	Prepend_platform_version_name bool
 }
 
@@ -67,6 +68,7 @@
 				Name:       &genName,
 				Short_name: tfb.Short_name,
 				Full_name:  tfb.Full_name,
+				Suite_arch: tfb.Suite_arch,
 				Version:    version,
 			})
 
@@ -95,6 +97,7 @@
 	Short_name string
 	Full_name  string
 	Version    string
+	Suite_arch string
 }
 
 type tradefedBinaryGen struct {
@@ -127,13 +130,19 @@
 func (tfg *tradefedBinaryGen) GenerateAndroidBuildActions(ctx android.ModuleContext) {
 	buildNumberFile := ctx.Config().BuildNumberFile(ctx)
 	outputFile := android.PathForModuleOut(ctx, "test-suite-info.properties")
+
+	arch := strings.ReplaceAll(tfg.properties.Suite_arch, " ", "")
+	if arch == "" {
+		arch = ctx.Config().DevicePrimaryArchType().String()
+	}
+
 	ctx.Build(pctx, android.BuildParams{
 		Rule:      tradefedBinaryGenRule,
 		Output:    outputFile,
 		OrderOnly: android.Paths{buildNumberFile},
 		Args: map[string]string{
 			"buildNumberFile": buildNumberFile.String(),
-			"arch":            ctx.Config().DevicePrimaryArchType().String(),
+			"arch":            arch,
 			"name":            tfg.properties.Short_name,
 			"fullname":        tfg.properties.Full_name,
 			"version":         tfg.properties.Version,