Fix stripping on Darwin

The strip tool is different on Darwin, use a separate darwinStrip rule
instead of calling strip.sh for host builds on Darwin.

Change-Id: I6d421cba0dcea04367d5bc638a03f64c81e2ead0
diff --git a/cc/builder.go b/cc/builder.go
index 10c8508..b97f29b 100644
--- a/cc/builder.go
+++ b/cc/builder.go
@@ -79,19 +79,26 @@
 
 	darwinAr = pctx.StaticRule("darwinAr",
 		blueprint.RuleParams{
-			Command:     "rm -f ${out} && $arCmd $arFlags $out $in",
-			CommandDeps: []string{"$arCmd"},
+			Command:     "rm -f ${out} && ${macArPath} $arFlags $out $in",
+			CommandDeps: []string{"${macArPath}"},
 			Description: "ar $out",
 		},
-		"arCmd", "arFlags")
+		"arFlags")
 
 	darwinAppendAr = pctx.StaticRule("darwinAppendAr",
 		blueprint.RuleParams{
-			Command:     "cp -f ${inAr} ${out}.tmp && $arCmd $arFlags ${out}.tmp $in && mv ${out}.tmp ${out}",
-			CommandDeps: []string{"$arCmd"},
+			Command:     "cp -f ${inAr} ${out}.tmp && ${macArPath} $arFlags ${out}.tmp $in && mv ${out}.tmp ${out}",
+			CommandDeps: []string{"${macArPath}"},
 			Description: "ar $out",
 		},
-		"arCmd", "arFlags", "inAr")
+		"arFlags", "inAr")
+
+	darwinStrip = pctx.StaticRule("darwinStrip",
+		blueprint.RuleParams{
+			Command:     "${macStripPath} -u -r -o $out $in",
+			CommandDeps: []string{"${macStripPath}"},
+			Description: "strip $out",
+		})
 
 	prefixSymbols = pctx.StaticRule("prefixSymbols",
 		blueprint.RuleParams{
@@ -252,7 +259,6 @@
 func TransformDarwinObjToStaticLib(ctx common.AndroidModuleContext, objFiles common.Paths,
 	flags builderFlags, outputPath common.ModuleOutPath) {
 
-	arCmd := "${macArPath}"
 	arFlags := "cqs"
 
 	// ARG_MAX on darwin is 262144, use half that to be safe
@@ -278,7 +284,6 @@
 				Inputs:  l,
 				Args: map[string]string{
 					"arFlags": arFlags,
-					"arCmd":   arCmd,
 				},
 			})
 		} else {
@@ -289,7 +294,6 @@
 				Implicits: []string{in},
 				Args: map[string]string{
 					"arFlags": arFlags,
-					"arCmd":   arCmd,
 					"inAr":    in,
 				},
 			})
@@ -439,6 +443,16 @@
 	})
 }
 
+func TransformDarwinStrip(ctx common.AndroidModuleContext, inputFile common.Path,
+	outputFile common.WritablePath) {
+
+	ctx.ModuleBuild(pctx, common.ModuleBuildParams{
+		Rule:   darwinStrip,
+		Output: outputFile,
+		Input:  inputFile,
+	})
+}
+
 func CopyGccLib(ctx common.AndroidModuleContext, libName string,
 	flags builderFlags, outputFile common.WritablePath) {
 
diff --git a/cc/cc.go b/cc/cc.go
index 052df2b..1a8ba3b 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -1979,10 +1979,14 @@
 
 func (stripper *stripper) strip(ctx ModuleContext, in, out common.ModuleOutPath,
 	flags builderFlags) {
-	flags.stripKeepSymbols = stripper.StripProperties.Strip.Keep_symbols
-	// TODO(ccross): don't add gnu debuglink for user builds
-	flags.stripAddGnuDebuglink = true
-	TransformStrip(ctx, in, out, flags)
+	if ctx.Darwin() {
+		TransformDarwinStrip(ctx, in, out)
+	} else {
+		flags.stripKeepSymbols = stripper.StripProperties.Strip.Keep_symbols
+		// TODO(ccross): don't add gnu debuglink for user builds
+		flags.stripAddGnuDebuglink = true
+		TransformStrip(ctx, in, out, flags)
+	}
 }
 
 func testPerSrcMutator(mctx common.AndroidBottomUpMutatorContext) {
diff --git a/cc/x86_darwin_host.go b/cc/x86_darwin_host.go
index 559a9a5..52add5c 100644
--- a/cc/x86_darwin_host.go
+++ b/cc/x86_darwin_host.go
@@ -105,6 +105,11 @@
 		return strings.TrimSpace(string(bytes)), err
 	})
 
+	pctx.VariableFunc("macStripPath", func(config interface{}) (string, error) {
+		bytes, err := exec.Command("xcrun", "--find", "strip").Output()
+		return strings.TrimSpace(string(bytes)), err
+	})
+
 	pctx.StaticVariable("darwinGccVersion", darwinGccVersion)
 	pctx.SourcePathVariable("darwinGccRoot",
 		"prebuilts/gcc/${HostPrebuiltTag}/host/i686-apple-darwin-${darwinGccVersion}")