compiler_wrapper: remove WITH_TIDY support for android.

Also consolidates most of the android specific logic
into one place so it's clear which flags are used
and which ones are not.

Also detects wrapping of clang-tidy for future
special casing.

BUG=chromium:773875
TEST=unit tests, golden tests
TEST=android's test_compiler.py --with-tidy

Change-Id: Ic83340ba63ec3ea4c3c174069d3dc017ecbcdd72
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/1866603
Tested-by: Tobias Bosch <tbosch@google.com>
Reviewed-by: George Burgess <gbiv@chromium.org>
diff --git a/compiler_wrapper/android_config_test.go b/compiler_wrapper/android_config_test.go
index 4e2f1f5..104be6d 100644
--- a/compiler_wrapper/android_config_test.go
+++ b/compiler_wrapper/android_config_test.go
@@ -35,6 +35,7 @@
 	gomaPath := path.Join(ctx.tempDir, "gomacc")
 	ctx.writeFile(gomaPath, "")
 	defaultPath := filepath.Join(ctx.tempDir, "clang")
+	clangTidyPath := filepath.Join(ctx.tempDir, "clang-tidy")
 
 	deepPath := "a/b/c/d/e/f/g/clang"
 	linkedDeepPath := "symlinked/clang_other"
@@ -52,11 +53,21 @@
 				Cmds:       errorResults,
 			},
 			{
+				Env:        []string{"WITH_TIDY=1"},
+				WrapperCmd: newGoldenCmd(defaultPath, mainCc),
+				Cmds:       okResults,
+			},
+			{
 				WrapperCmd: newGoldenCmd(filepath.Join(ctx.tempDir, "clang++"), mainCc),
 				Cmds:       okResults,
 			},
 			{
-				WrapperCmd: newGoldenCmd(filepath.Join(ctx.tempDir, "clang-tidy"), mainCc),
+				WrapperCmd: newGoldenCmd(clangTidyPath, mainCc),
+				Cmds:       okResults,
+			},
+			{
+				Env:        []string{"WITH_TIDY=1"},
+				WrapperCmd: newGoldenCmd(clangTidyPath, mainCc),
 				Cmds:       okResults,
 			},
 			{
diff --git a/compiler_wrapper/bisect_flag_test.go b/compiler_wrapper/bisect_flag_test.go
index 8500eef..0bb6a82 100644
--- a/compiler_wrapper/bisect_flag_test.go
+++ b/compiler_wrapper/bisect_flag_test.go
@@ -85,7 +85,7 @@
 			"HOME=/somehome",
 		}
 		ctx.cfg.isAndroidWrapper = true
-		cmd := mustCallBisectDriver(ctx, callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, mainCc)))
+		cmd := mustCallBisectDriver(ctx, callCompiler(ctx, ctx.cfg, ctx.newCommand(clangAndroid, mainCc)))
 		if err := verifyArgOrder(cmd,
 			"someBisectStage", filepath.Join("/somehome", "ANDROID_BISECT")); err != nil {
 			t.Error(err)
diff --git a/compiler_wrapper/clang_flags.go b/compiler_wrapper/clang_flags.go
index ee041a2..8b76e96 100644
--- a/compiler_wrapper/clang_flags.go
+++ b/compiler_wrapper/clang_flags.go
@@ -12,15 +12,6 @@
 )
 
 func processClangFlags(builder *commandBuilder) error {
-	if builder.cfg.isAndroidWrapper {
-		// FIXME: This combination of using the directory of the symlink but the
-		// basename of the link target is strange but is the logic that old android
-		// wrapper uses. Change this to use directory and basename either from the
-		// absWrapperPath or from the builder.path, but don't mix anymore.
-		builder.path = filepath.Join(filepath.Dir(builder.path), filepath.Base(builder.absWrapperPath)+".real")
-		return nil
-	}
-
 	env := builder.env
 	clangDir, _ := env.getenv("CLANG")
 
diff --git a/compiler_wrapper/command.go b/compiler_wrapper/command.go
index 26f8556..6957859 100644
--- a/compiler_wrapper/command.go
+++ b/compiler_wrapper/command.go
@@ -129,6 +129,8 @@
 
 	var compilerType compilerType
 	switch {
+	case strings.HasPrefix(target.compiler, "clang-tidy"):
+		compilerType = clangTidyType
 	case strings.HasPrefix(target.compiler, "clang"):
 		compilerType = clangType
 	default:
@@ -172,6 +174,7 @@
 const (
 	gccType compilerType = iota
 	clangType
+	clangTidyType
 )
 
 type builderTarget struct {
diff --git a/compiler_wrapper/compile_with_fallback_test.go b/compiler_wrapper/compile_with_fallback_test.go
index 32d1915..4ea847f 100644
--- a/compiler_wrapper/compile_with_fallback_test.go
+++ b/compiler_wrapper/compile_with_fallback_test.go
@@ -169,7 +169,7 @@
 		}{
 			{"./clang", true},
 			{"./clang++", true},
-			{"./some_clang", false},
+			{"./clang-tidy", false},
 		}
 		ctx.env = append(ctx.env, "ANDROID_LLVM_FALLBACK_DISABLED_WARNINGS=-a -b")
 		extraArgs := []string{"-fno-color-diagnostics", "-a", "-b"}
diff --git a/compiler_wrapper/compiler_wrapper.go b/compiler_wrapper/compiler_wrapper.go
index b157687..17c62db 100644
--- a/compiler_wrapper/compiler_wrapper.go
+++ b/compiler_wrapper/compiler_wrapper.go
@@ -71,7 +71,27 @@
 	env = mainBuilder.env
 	var compilerCmd *command
 	clangSyntax := processClangSyntaxFlag(mainBuilder)
-	if mainBuilder.target.compilerType == clangType {
+	if cfg.isAndroidWrapper {
+		// FIXME: This combination of using the directory of the symlink but the
+		// basename of the link target is strange but is the logic that old android
+		// wrapper uses. Change this to use directory and basename either from the
+		// absWrapperPath or from the builder.path, but don't mix anymore.
+		mainBuilder.path = filepath.Join(filepath.Dir(mainBuilder.path), filepath.Base(mainBuilder.absWrapperPath)+".real")
+
+		switch mainBuilder.target.compilerType {
+		case clangType:
+			mainBuilder.addPreUserArgs(mainBuilder.cfg.clangFlags...)
+			mainBuilder.addPreUserArgs(mainBuilder.cfg.commonFlags...)
+			if _, err := processGomaCccFlags(mainBuilder); err != nil {
+				return 0, err
+			}
+			compilerCmd = mainBuilder.build()
+		case clangTidyType:
+			compilerCmd = mainBuilder.build()
+		default:
+			return 0, newErrorwithSourceLocf("unsupported compiler: %s", mainBuilder.target.compiler)
+		}
+	} else if mainBuilder.target.compilerType == clangType {
 		cSrcFile, useClangTidy := processClangTidyFlags(mainBuilder)
 		sysroot, err := prepareClangCommand(mainBuilder)
 		if err != nil {
@@ -146,7 +166,7 @@
 
 func prepareClangCommand(builder *commandBuilder) (sysroot string, err error) {
 	sysroot = ""
-	if !builder.cfg.isHostWrapper && !builder.cfg.isAndroidWrapper {
+	if !builder.cfg.isHostWrapper {
 		sysroot = processSysrootFlag(builder)
 	}
 	builder.addPreUserArgs(builder.cfg.clangFlags...)
@@ -189,15 +209,13 @@
 
 func calcCommonPreUserArgs(builder *commandBuilder) {
 	builder.addPreUserArgs(builder.cfg.commonFlags...)
-	if !builder.cfg.isHostWrapper && !builder.cfg.isAndroidWrapper {
+	if !builder.cfg.isHostWrapper {
 		processPieFlags(builder)
 		processThumbCodeFlags(builder)
 		processStackProtectorFlags(builder)
 		processX86Flags(builder)
 	}
-	if !builder.cfg.isAndroidWrapper {
-		processSanitizerFlags(builder)
-	}
+	processSanitizerFlags(builder)
 }
 
 func processGomaCCacheFlags(sysroot string, allowCCache bool, builder *commandBuilder) (err error) {
diff --git a/compiler_wrapper/testdata/android_golden/clang_path.json b/compiler_wrapper/testdata/android_golden/clang_path.json
index b784feb..5686b38 100644
--- a/compiler_wrapper/testdata/android_golden/clang_path.json
+++ b/compiler_wrapper/testdata/android_golden/clang_path.json
@@ -49,6 +49,30 @@
   },
   {
     "wd": "/tmp/stable",
+    "env": [
+      "WITH_TIDY=1"
+    ],
+    "wrapper": {
+      "cmd": {
+        "path": "/tmp/stable/clang",
+        "args": [
+          "main.cc"
+        ]
+      }
+    },
+    "cmds": [
+      {
+        "cmd": {
+          "path": "/tmp/stable/clang.real",
+          "args": [
+            "main.cc"
+          ]
+        }
+      }
+    ]
+  },
+  {
+    "wd": "/tmp/stable",
     "wrapper": {
       "cmd": {
         "path": "/tmp/stable/clang++",
@@ -91,6 +115,30 @@
   },
   {
     "wd": "/tmp/stable",
+    "env": [
+      "WITH_TIDY=1"
+    ],
+    "wrapper": {
+      "cmd": {
+        "path": "/tmp/stable/clang-tidy",
+        "args": [
+          "main.cc"
+        ]
+      }
+    },
+    "cmds": [
+      {
+        "cmd": {
+          "path": "/tmp/stable/clang-tidy.real",
+          "args": [
+            "main.cc"
+          ]
+        }
+      }
+    ]
+  },
+  {
+    "wd": "/tmp/stable",
     "wrapper": {
       "cmd": {
         "path": "a/b/c/d/e/f/g/clang",