cc: use platform sdk version for vendor/product variants

This enables LLNDK symbols to work with INTRODUCED_IN and
builtin_available.

This should work well with TrunkStable as well.

Instead of passing 10000 in trunk_staging, we can pass e.g. 35 to vendor
modules. Then, vendor code can benefit from __builtin_available because
it's compiled as a runtime check (isOSVersionAtLeast) and handle the
version at runtime based on the actual platform version.

With this change, using new symbols (e.g. symbols introduced in
in-development version) can be detected even in the "trunk_stging"
configuration. Prevously, since 10000 is assumed in trunk_staging, new
symbols were okay in trunk_staging while required __builtin_available
guard in "next" configuration.

This also helps to build a vendor apex which can be built in a new
Android release while it's meant to be installed and run on an old
Android release by setting min_sdk_version.

Bug: 362658565
Test: m --no-skip-soong-tests
Change-Id: I06f46940f0b994b460c2c69171c708583391f836
diff --git a/cc/cc.go b/cc/cc.go
index 5dee32e..0ed6238 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -1567,12 +1567,11 @@
 	}
 
 	if ctx.ctx.Device() {
-		config := ctx.ctx.Config()
-		if ctx.inVendor() {
-			// If building for vendor with final API, then use the latest _stable_ API as "current".
-			if config.VendorApiLevelFrozen() && (ver == "" || ver == "current") {
-				ver = config.PlatformSdkVersion().String()
-			}
+		// When building for vendor/product, use the latest _stable_ API as "current".
+		// This is passed to clang/aidl compilers so that compiled/generated code works
+		// with the system.
+		if (ctx.inVendor() || ctx.inProduct()) && (ver == "" || ver == "current") {
+			ver = ctx.ctx.Config().PlatformSdkVersion().String()
 		}
 	}
 
diff --git a/cc/cc_test.go b/cc/cc_test.go
index e906706..f170d0a 100644
--- a/cc/cc_test.go
+++ b/cc/cc_test.go
@@ -3258,7 +3258,7 @@
 	testDepWithVariant("product")
 }
 
-func TestVendorSdkVersion(t *testing.T) {
+func TestVendorOrProductVariantUsesPlatformSdkVersionAsDefault(t *testing.T) {
 	t.Parallel()
 
 	bp := `
@@ -3266,31 +3266,29 @@
 			name: "libfoo",
 			srcs: ["libfoo.cc"],
 			vendor_available: true,
+			product_available: true,
 		}
 
 		cc_library {
 			name: "libbar",
 			srcs: ["libbar.cc"],
 			vendor_available: true,
+			product_available: true,
 			min_sdk_version: "29",
 		}
 	`
 
 	ctx := prepareForCcTest.RunTestWithBp(t, bp)
-	testSdkVersionFlag := func(module, version string) {
-		flags := ctx.ModuleForTests(module, "android_vendor_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
-		android.AssertStringDoesContain(t, "min sdk version", flags, "-target aarch64-linux-android"+version)
+	testSdkVersionFlag := func(module, variant, version string) {
+		flags := ctx.ModuleForTests(module, "android_"+variant+"_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
+		android.AssertStringDoesContain(t, "target SDK version", flags, "-target aarch64-linux-android"+version)
 	}
 
-	testSdkVersionFlag("libfoo", "10000")
-	testSdkVersionFlag("libbar", "29")
-
-	ctx = android.GroupFixturePreparers(
-		prepareForCcTest,
-		android.PrepareForTestWithBuildFlag("RELEASE_BOARD_API_LEVEL_FROZEN", "true"),
-	).RunTestWithBp(t, bp)
-	testSdkVersionFlag("libfoo", "30")
-	testSdkVersionFlag("libbar", "29")
+	testSdkVersionFlag("libfoo", "vendor", "30")
+	testSdkVersionFlag("libfoo", "product", "30")
+	// target SDK version can be set explicitly with min_sdk_version
+	testSdkVersionFlag("libbar", "vendor", "29")
+	testSdkVersionFlag("libbar", "product", "29")
 }
 
 func TestClangVerify(t *testing.T) {