Use WITH_TIDY_FLAGS env variable.

This variable is a space separated string of clang-tidy flags to be passed
to clang-tidy before any other system required extra flags.
Note that when this flag or local tidy_flags is defined,
the default -header-filter flag is suppressed.

Test: make with WITH_TIDY=1 WITH_TIDY_FLAGS="-extra-arg=-DABCD1=1 -extra-arg=-DABCD2=2"
Bug: 32668284
Change-Id: If7bd31c65404ef7fe6c3499d51f0f209a704efd9
diff --git a/cc/config/tidy.go b/cc/config/tidy.go
index 3a858cd..dd52a0e 100644
--- a/cc/config/tidy.go
+++ b/cc/config/tidy.go
@@ -80,6 +80,11 @@
 			"system/",
 		}, "|")
 	})
+
+	// Use WTIH_TIDY_FLAGS to pass extra global default clang-tidy flags.
+	pctx.VariableFunc("TidyWithTidyFlags", func(ctx android.PackageVarContext) string {
+		return ctx.Config().Getenv("WITH_TIDY_FLAGS")
+	})
 }
 
 type PathBasedTidyCheck struct {
diff --git a/cc/makevars.go b/cc/makevars.go
index 17edcdf..b7fb575 100644
--- a/cc/makevars.go
+++ b/cc/makevars.go
@@ -143,6 +143,7 @@
 	ctx.Strict("DEFAULT_GLOBAL_TIDY_CHECKS", "${config.TidyDefaultGlobalChecks}")
 	ctx.Strict("DEFAULT_LOCAL_TIDY_CHECKS", joinLocalTidyChecks(config.DefaultLocalTidyChecks))
 	ctx.Strict("DEFAULT_TIDY_HEADER_DIRS", "${config.TidyDefaultHeaderDirs}")
+	ctx.Strict("WITH_TIDY_FLAGS", "${config.TidyWithTidyFlags}")
 
 	ctx.Strict("AIDL_CPP", "${aidlCmd}")
 
diff --git a/cc/tidy.go b/cc/tidy.go
index 491cc22..0a6b413 100644
--- a/cc/tidy.go
+++ b/cc/tidy.go
@@ -69,9 +69,14 @@
 
 	flags.Tidy = true
 
+	// Add global WITH_TIDY_FLAGS and local tidy_flags.
+	withTidyFlags := ctx.Config().Getenv("WITH_TIDY_FLAGS")
+	if len(withTidyFlags) > 0 {
+		flags.TidyFlags = append(flags.TidyFlags, withTidyFlags)
+	}
 	esc := proptools.NinjaAndShellEscape
-
 	flags.TidyFlags = append(flags.TidyFlags, esc(tidy.Properties.Tidy_flags)...)
+	// If TidyFlags is empty, add default header filter.
 	if len(flags.TidyFlags) == 0 {
 		headerFilter := "-header-filter=\"(" + ctx.ModuleDir() + "|${config.TidyDefaultHeaderDirs})\""
 		flags.TidyFlags = append(flags.TidyFlags, headerFilter)