Soong generates kernel_version_for_uffd_gc.txt

In the context of support "--soong-only", kernel_version_for_uffd_gc.txt
needs to be generated by Soong.

Soong will write the BOARD_KERNEL_VERSION to the txt file
in soong-only mode. It will panic if BOARD_KERNEL_VERSION
is not set.

Test: CI
Bug: 383564057
Change-Id: Idb264eaa91424ed9bab5be4988c7eb96334153f5
diff --git a/android/variable.go b/android/variable.go
index 3cf66ea..a60cad5 100644
--- a/android/variable.go
+++ b/android/variable.go
@@ -532,7 +532,8 @@
 	OdmPropFiles       []string `json:",omitempty"`
 	VendorPropFiles    []string `json:",omitempty"`
 
-	EnableUffdGc *string `json:",omitempty"`
+	EnableUffdGc       *string `json:",omitempty"`
+	BoardKernelVersion *string `json:",omitempty"`
 
 	BoardAvbEnable                         *bool    `json:",omitempty"`
 	BoardAvbSystemAddHashtreeFooterArgs    []string `json:",omitempty"`
diff --git a/dexpreopt/config.go b/dexpreopt/config.go
index e3804e5..ee23b51 100644
--- a/dexpreopt/config.go
+++ b/dexpreopt/config.go
@@ -718,6 +718,16 @@
 	} else if global.EnableUffdGc == "default" {
 		// Generated by `build/make/core/Makefile`.
 		kernelVersionFile := android.PathForOutput(ctx, "dexpreopt/kernel_version_for_uffd_gc.txt")
+		if !ctx.Config().KatiEnabled() {
+			// In soong-only mode, we need to generate the kernel_version_for_uffd_gc.txt with kernel version
+			kernelVersion := android.String(ctx.Config().ProductVariables().BoardKernelVersion)
+			if kernelVersion == "" {
+				// https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=5382;drc=66783fca85911af9da48d9b4f35a61b3873023e9
+				panic("BOARD_KERNEL_VERSION is not set. Build team will convert more stuff from Make to Soong to support this scenario.")
+			}
+			android.WriteFileRule(ctx, kernelVersionFile, kernelVersion)
+		}
+
 		// Determine the UFFD GC flag by the kernel version file.
 		rule := android.NewRuleBuilder(pctx, ctx)
 		rule.Command().
diff --git a/dexpreopt/dexpreopt_test.go b/dexpreopt/dexpreopt_test.go
index 7b0f51f..1f188d5 100644
--- a/dexpreopt/dexpreopt_test.go
+++ b/dexpreopt/dexpreopt_test.go
@@ -15,9 +15,10 @@
 package dexpreopt
 
 import (
-	"android/soong/android"
 	"fmt"
 	"testing"
+
+	"android/soong/android"
 )
 
 func testSystemModuleConfig(ctx android.PathContext, name string) *ModuleConfig {
@@ -403,6 +404,7 @@
 	preparers := android.GroupFixturePreparers(
 		PrepareForTestWithFakeDex2oatd,
 		PrepareForTestWithDexpreoptConfig,
+		android.FixtureModifyConfig(android.SetKatiEnabledForTests),
 		FixtureSetEnableUffdGc("default"),
 	)