Move product_packages.txt to soong

Needed for soong-only builds.

Bug: 383892968
Test: Presubmits
Change-Id: Ibc214f489d97b2eb2ff29e454af3bf1ae025b0d2
diff --git a/android/Android.bp b/android/Android.bp
index 79969a8..4e2006d 100644
--- a/android/Android.bp
+++ b/android/Android.bp
@@ -92,6 +92,7 @@
         "prebuilt.go",
         "prebuilt_build_tool.go",
         "product_config.go",
+        "product_packages_file.go",
         "proto.go",
         "provider.go",
         "raw_files.go",
diff --git a/android/paths.go b/android/paths.go
index 94f00ad..9a7dcbc 100644
--- a/android/paths.go
+++ b/android/paths.go
@@ -1353,7 +1353,7 @@
 
 // PathForArbitraryOutput creates a path for the given components. Unlike PathForOutput,
 // the path is relative to the root of the output folder, not the out/soong folder.
-func PathForArbitraryOutput(ctx PathContext, pathComponents ...string) Path {
+func PathForArbitraryOutput(ctx PathContext, pathComponents ...string) WritablePath {
 	path, err := validatePath(pathComponents...)
 	if err != nil {
 		reportPathError(ctx, err)
diff --git a/android/product_packages_file.go b/android/product_packages_file.go
new file mode 100644
index 0000000..c7c18a2
--- /dev/null
+++ b/android/product_packages_file.go
@@ -0,0 +1,39 @@
+// Copyright 2024 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 android
+
+import (
+	"strings"
+)
+
+func init() {
+	RegisterParallelSingletonType("product_packages_file_singleton", productPackagesFileSingletonFactory)
+}
+
+func productPackagesFileSingletonFactory() Singleton {
+	return &productPackagesFileSingleton{}
+}
+
+type productPackagesFileSingleton struct{}
+
+func (s *productPackagesFileSingleton) GenerateBuildActions(ctx SingletonContext) {
+	// There's no HasDeviceName() function, but the device name and device product should always
+	// both be present or not.
+	if ctx.Config().HasDeviceProduct() {
+		productPackages := ctx.Config().productVariables.PartitionVarsForSoongMigrationOnlyDoNotUse.ProductPackages
+		output := PathForArbitraryOutput(ctx, "target", "product", ctx.Config().DeviceName(), "product_packages.txt")
+		WriteFileRule(ctx, output, strings.Join(productPackages, "\n"))
+	}
+}