Fix llvm-ar error caused by using lto and sanitizer together

LLVM-AR does not allow passing --plugin options more than once. The
--plugin ARFLAGS that lto want to add, may already exist if sanitizer is
also turned on.

Fixed this by adding a new bool Flags.ArGoldPlugin. Set this variable to
true whenever LLVM gold plugin is needed for ArFlags. In function
TransformObjToStaticLib(), add this option to arFlags using global value
${config.LLVMGoldPlugin} if the bool value is true.

Bug: http://b/73160350
Test: build the image with make and succeeded.

Change-Id: I62785829b0a4b663225926e4aed98defc1b6da2c
(cherry picked from commit 4917049f6efe2d08e67eb26ec77e21dcce5172c5)
diff --git a/cc/builder.go b/cc/builder.go
index 0646132..279c1da 100644
--- a/cc/builder.go
+++ b/cc/builder.go
@@ -256,6 +256,7 @@
 	systemIncludeFlags string
 
 	groupStaticLibs bool
+	arGoldPlugin    bool
 
 	stripKeepSymbols       bool
 	stripKeepMiniDebugInfo bool
@@ -512,6 +513,9 @@
 	if !ctx.Darwin() {
 		arFlags += " -format=gnu"
 	}
+	if flags.arGoldPlugin {
+		arFlags += " --plugin ${config.LLVMGoldPlugin}"
+	}
 	if flags.arFlags != "" {
 		arFlags += " " + flags.arFlags
 	}
diff --git a/cc/cc.go b/cc/cc.go
index 9cc7dfa..49fefe9 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -142,6 +142,7 @@
 	LdFlagsDeps android.Paths // Files depended on by linker flags
 
 	GroupStaticLibs bool
+	ArGoldPlugin    bool // Whether LLVM gold plugin option is passed to llvm-ar
 }
 
 type ObjectLinkerProperties struct {
diff --git a/cc/lto.go b/cc/lto.go
index 91b11b5..4757fc7 100644
--- a/cc/lto.go
+++ b/cc/lto.go
@@ -86,7 +86,7 @@
 			// https://github.com/android-ndk/ndk/issues/498.
 			flags.LdFlags = append(flags.LdFlags, "-Wl,-plugin-opt,-emulated-tls")
 		}
-		flags.ArFlags = append(flags.ArFlags, " --plugin ${config.LLVMGoldPlugin}")
+		flags.ArGoldPlugin = true
 	}
 	return flags
 }
diff --git a/cc/sanitize.go b/cc/sanitize.go
index ac6cb77..c47c319 100644
--- a/cc/sanitize.go
+++ b/cc/sanitize.go
@@ -38,7 +38,6 @@
 		"-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"}
 	cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
 		"-Wl,-plugin-opt,O1"}
-	cfiArflags         = []string{"--plugin ${config.ClangBin}/../lib64/LLVMgold.so"}
 	cfiExportsMapPath  = "build/soong/cc/config/cfi_exports.map"
 	cfiExportsMap      android.Path
 	cfiStaticLibsMutex sync.Mutex
@@ -407,7 +406,7 @@
 			// See b/72706604 or https://github.com/android-ndk/ndk/issues/498.
 			flags.LdFlags = append(flags.LdFlags, "-Wl,-plugin-opt,-emulated-tls")
 		}
-		flags.ArFlags = append(flags.ArFlags, cfiArflags...)
+		flags.ArGoldPlugin = true
 		if Bool(sanitize.Properties.Sanitize.Diag.Cfi) {
 			diagSanitizers = append(diagSanitizers, "cfi")
 		}
diff --git a/cc/util.go b/cc/util.go
index 5131b09..92a32bc 100644
--- a/cc/util.go
+++ b/cc/util.go
@@ -84,6 +84,7 @@
 		systemIncludeFlags: strings.Join(in.SystemIncludeFlags, " "),
 
 		groupStaticLibs: in.GroupStaticLibs,
+		arGoldPlugin:    in.ArGoldPlugin,
 	}
 }