Add default -Wall -Werror or -Wall.

* When -Wno-error and -Werror are not used:
  add -Wall to the front of cflags
  if the project is in the WarningAllowedProjects,
  otherwise add -Wall -Werror.
* Add -Wall -Werror to ndk_library build targets.
* Collect names of modules with -Wno-error or without -Werror,
  and pass them to makefile variables:
      SOONG_MODULES_USING_WNO_ERROR
      SOONG_MODULES_ADDED_WERROR
      SOONG_MODULES_ADDED_WALL
* Generate ANDROID_WARNING_ALLOWED_PROJECTS for old makefiles.

Bug: 66996870
Test: normal build
Change-Id: I31385e12b80ca946c7395a5a184ef259b029aac6
diff --git a/cc/compiler.go b/cc/compiler.go
index ca68a00..fc2eeec 100644
--- a/cc/compiler.go
+++ b/cc/compiler.go
@@ -206,6 +206,21 @@
 	return deps
 }
 
+// Return true if the module is in the WarningAllowedProjects.
+func warningsAreAllowed(subdir string) bool {
+	subdir += "/"
+	for _, prefix := range config.WarningAllowedProjects {
+		if strings.HasPrefix(subdir, prefix) {
+			return true
+		}
+	}
+	return false
+}
+
+func addToModuleList(ctx ModuleContext, list string, module string) {
+	getWallWerrorMap(ctx.AConfig(), list).Store(module, true)
+}
+
 // Create a Flags struct that collects the compile flags from global values,
 // per-target values, module type values, and per-module Blueprints properties
 func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
@@ -464,6 +479,21 @@
 		flags = rsFlags(ctx, flags, &compiler.Properties)
 	}
 
+	if len(compiler.Properties.Srcs) > 0 {
+		module := ctx.ModuleDir() + "/Android.bp:" + ctx.ModuleName()
+		if inList("-Wno-error", flags.CFlags) || inList("-Wno-error", flags.CppFlags) {
+			addToModuleList(ctx, modulesUsingWnoError, module)
+		} else if !inList("-Werror", flags.CFlags) && !inList("-Werror", flags.CppFlags) {
+			if warningsAreAllowed(ctx.ModuleDir()) {
+				addToModuleList(ctx, modulesAddedWall, module)
+				flags.CFlags = append([]string{"-Wall"}, flags.CFlags...)
+			} else {
+				addToModuleList(ctx, modulesAddedWerror, module)
+				flags.CFlags = append([]string{"-Wall", "-Werror"}, flags.CFlags...)
+			}
+		}
+	}
+
 	return flags
 }