Use ctx.ModuleBuild for darwin ar

Test: builds
Change-Id: If90975c8545158012bc6201acadd136363c21260
diff --git a/android/paths.go b/android/paths.go
index 75e5980..0d26dc0 100644
--- a/android/paths.go
+++ b/android/paths.go
@@ -721,3 +721,25 @@
 	}
 	return validateSafePath(ctx, paths...)
 }
+
+type testPath struct {
+	basePath
+}
+
+func (p testPath) String() string {
+	return p.path
+}
+
+func PathForTesting(paths ...string) Path {
+	p := validateSafePath(nil, paths...)
+	return testPath{basePath{path: p, rel: p}}
+}
+
+func PathsForTesting(strs []string) Paths {
+	p := make(Paths, len(strs))
+	for i, s := range strs {
+		p[i] = PathForTesting(s)
+	}
+
+	return p
+}
diff --git a/cc/builder.go b/cc/builder.go
index 7144508..8a0c7f4 100644
--- a/cc/builder.go
+++ b/cc/builder.go
@@ -468,7 +468,7 @@
 // very small command line length limit, so we have to split the ar into multiple
 // steps, each appending to the previous one.
 func transformDarwinObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
-	flags builderFlags, outputPath android.ModuleOutPath, deps android.Paths) {
+	flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
 
 	arFlags := "cqs"
 
@@ -493,7 +493,7 @@
 
 		ctx.ModuleBuild(pctx, android.ModuleBuildParams{
 			Rule:   darwinAppendAr,
-			Output: outputPath,
+			Output: outputFile,
 			Input:  dummy,
 			Args: map[string]string{
 				"arFlags": "d",
@@ -505,42 +505,33 @@
 	}
 
 	// ARG_MAX on darwin is 262144, use half that to be safe
-	objFilesLists, err := splitListForSize(objFiles.Strings(), 131072)
+	objFilesLists, err := splitListForSize(objFiles, 131072)
 	if err != nil {
 		ctx.ModuleErrorf("%s", err.Error())
 	}
 
-	outputFile := outputPath.String()
-
-	var in, out string
+	var in, out android.WritablePath
 	for i, l := range objFilesLists {
 		in = out
 		out = outputFile
 		if i != len(objFilesLists)-1 {
-			out += "." + strconv.Itoa(i)
+			out = android.PathForModuleOut(ctx, outputFile.Base()+strconv.Itoa(i))
 		}
 
-		if in == "" {
-			ctx.Build(pctx, blueprint.BuildParams{
-				Rule:      darwinAr,
-				Outputs:   []string{out},
-				Inputs:    l,
-				Implicits: deps.Strings(),
-				Args: map[string]string{
-					"arFlags": arFlags,
-				},
-			})
-		} else {
-			ctx.Build(pctx, blueprint.BuildParams{
-				Rule:    darwinAppendAr,
-				Outputs: []string{out},
-				Inputs:  l,
-				Args: map[string]string{
-					"arFlags": arFlags,
-					"inAr":    in,
-				},
-			})
+		build := android.ModuleBuildParams{
+			Rule:      darwinAr,
+			Output:    out,
+			Inputs:    l,
+			Implicits: deps,
+			Args: map[string]string{
+				"arFlags": arFlags,
+			},
 		}
+		if i != 0 {
+			build.Rule = darwinAppendAr
+			build.Args["inAr"] = in.String()
+		}
+		ctx.ModuleBuild(pctx, build)
 	}
 }
 
@@ -779,13 +770,13 @@
 	return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd)
 }
 
-func splitListForSize(list []string, limit int) (lists [][]string, err error) {
+func splitListForSize(list android.Paths, limit int) (lists []android.Paths, err error) {
 	var i int
 
 	start := 0
 	bytes := 0
 	for i = range list {
-		l := len(list[i])
+		l := len(list[i].String())
 		if l > limit {
 			return nil, fmt.Errorf("list element greater than size limit (%d)", limit)
 		}
diff --git a/cc/cc_test.go b/cc/cc_test.go
index ec7da90..6e779e7 100644
--- a/cc/cc_test.go
+++ b/cc/cc_test.go
@@ -1,6 +1,7 @@
 package cc
 
 import (
+	"android/soong/android"
 	"reflect"
 	"testing"
 )
@@ -142,13 +143,23 @@
 
 func TestSplitListForSize(t *testing.T) {
 	for _, testCase := range splitListForSizeTestCases {
-		out, _ := splitListForSize(testCase.in, testCase.size)
-		if !reflect.DeepEqual(out, testCase.out) {
+		out, _ := splitListForSize(android.PathsForTesting(testCase.in), testCase.size)
+
+		var outStrings [][]string
+
+		if len(out) > 0 {
+			outStrings = make([][]string, len(out))
+			for i, o := range out {
+				outStrings[i] = o.Strings()
+			}
+		}
+
+		if !reflect.DeepEqual(outStrings, testCase.out) {
 			t.Errorf("incorrect output:")
 			t.Errorf("     input: %#v", testCase.in)
 			t.Errorf("      size: %d", testCase.size)
 			t.Errorf("  expected: %#v", testCase.out)
-			t.Errorf("       got: %#v", out)
+			t.Errorf("       got: %#v", outStrings)
 		}
 	}
 }