Copy cc_object output files to a name that matches the module

cc_object output files match the name of the module if there are any
postprocessing steps like partial linking or prefixing symbols.  If
there are no postprocessing steps the output file matches the name
of the source file with the extension changed to ".o".  Always copy
the object to an output file that matches the module name.

Bug: 242601708
Test: TestCcObjectOutputFile
Change-Id: I086bb0d14a3c02093515f55395aa7a11473f8040
diff --git a/cc/object.go b/cc/object.go
index 65a11e0..20eef30 100644
--- a/cc/object.go
+++ b/cc/object.go
@@ -256,13 +256,18 @@
 	builderFlags := flagsToBuilderFlags(flags)
 
 	if len(objs.objFiles) == 1 && String(object.Properties.Linker_script) == "" {
-		outputFile = objs.objFiles[0]
+		output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
+		outputFile = output
 
 		if String(object.Properties.Prefix_symbols) != "" {
-			output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
-			transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), outputFile,
+			transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), objs.objFiles[0],
 				builderFlags, output)
-			outputFile = output
+		} else {
+			ctx.Build(pctx, android.BuildParams{
+				Rule:   android.Cp,
+				Input:  objs.objFiles[0],
+				Output: output,
+			})
 		}
 	} else {
 		output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
diff --git a/cc/object_test.go b/cc/object_test.go
index 259a892..00f83d4 100644
--- a/cc/object_test.go
+++ b/cc/object_test.go
@@ -15,6 +15,7 @@
 package cc
 
 import (
+	"fmt"
 	"testing"
 
 	"android/soong/android"
@@ -107,3 +108,53 @@
 	expectedOutputFiles := []string{"outputbase/execroot/__main__/bazel_out.o"}
 	android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings())
 }
+
+func TestCcObjectOutputFile(t *testing.T) {
+	testcases := []struct {
+		name string
+		bp   string
+	}{
+		{
+			name: "normal",
+			bp: `
+				srcs: ["bar.c"],
+			`,
+		},
+		{
+			name: "keep symbols",
+			bp: `
+				srcs: ["bar.c"],
+				prefix_symbols: "foo_",
+			`,
+		},
+		{
+			name: "partial linking",
+			bp: `
+				srcs: ["bar.c", "baz.c"],
+			`,
+		},
+		{
+			name: "partial linking and prefix symbols",
+			bp: `
+				srcs: ["bar.c", "baz.c"],
+				prefix_symbols: "foo_",
+			`,
+		},
+	}
+
+	for _, testcase := range testcases {
+		bp := fmt.Sprintf(`
+			cc_object {
+				name: "foo",
+				%s
+			}
+		`, testcase.bp)
+		t.Run(testcase.name, func(t *testing.T) {
+			ctx := PrepareForIntegrationTestWithCc.RunTestWithBp(t, bp)
+			android.AssertPathRelativeToTopEquals(t, "expected output file foo.o",
+				"out/soong/.intermediates/foo/android_arm64_armv8-a/foo.o",
+				ctx.ModuleForTests("foo", "android_arm64_armv8-a").Output("foo.o").Output)
+		})
+	}
+
+}