Separate general defaults from Skia defaults

In the build there are some defaults which actually apply to every
use of a built-in target type, but there are some (particularaly
warnings) which apply only to targets controlled by Skia. Currently
these unwanted defaults are magically known to exist and removed
wherever they are not wanted. Instead, create 'skia_' prefixed target
templates and apply these defaults to those instead.

Change-Id: I3a2afb53c7205a2e2748d1cfad46319f2e93d3b3
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/385516
Commit-Queue: Ben Wagner <bungeman@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index 33c7b9b..4ea3a2f 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -111,16 +111,20 @@
 
 # Use for CPU-specific Skia code that needs particular compiler flags.
 template("opts") {
-  visibility = [ ":*" ]
   if (invoker.enabled) {
-    source_set(target_name) {
+    skia_source_set(target_name) {
+      visibility = [ ":*" ]
       check_includes = false
+      configs = skia_library_configs
       forward_variables_from(invoker, "*")
-      configs += skia_library_configs
+      if (defined(invoker.configs)) {
+        configs += invoker.configs
+      }
     }
   } else {
     # If not enabled, a phony empty target that swallows all otherwise unused variables.
-    source_set(target_name) {
+    skia_source_set(target_name) {
+      visibility = [ ":*" ]
       check_includes = false
       forward_variables_from(invoker,
                              "*",
@@ -239,7 +243,6 @@
 
 # Any feature of Skia that requires third-party code should be optional and use this template.
 template("optional") {
-  visibility = [ ":*" ]
   if (invoker.enabled) {
     config(target_name + "_public") {
       if (defined(invoker.public_defines)) {
@@ -252,34 +255,44 @@
         include_dirs = invoker.public_include_dirs
       }
     }
-    source_set(target_name) {
+    skia_source_set(target_name) {
+      visibility = [ ":*" ]
       check_includes = false
+      configs = skia_library_configs
+
+      # "*" clobbers the current scope; append to existing configs
       forward_variables_from(invoker,
                              "*",
                              [
+                               "configs",
                                "public_defines",
                                "sources_for_tests",
                                "sources_when_disabled",
-                               "configs_to_remove",
                              ])
-      all_dependent_configs = [ ":" + target_name + "_public" ]
-      configs += skia_library_configs
-      if (defined(invoker.configs_to_remove)) {
-        configs -= invoker.configs_to_remove
+      if (defined(invoker.configs)) {
+        configs += invoker.configs
       }
+      all_dependent_configs = [ ":" + target_name + "_public" ]
     }
     if (defined(invoker.sources_for_tests) && skia_enable_tools) {
-      source_set(target_name + "_tests") {
+      skia_source_set(target_name + "_tests") {
+        visibility = [ ":*" ]
         check_includes = false
+        configs = skia_library_configs
+
+        # "*" clobbers the current scope; append to existing configs
         forward_variables_from(invoker,
                                "*",
                                [
+                                 "configs",
                                  "public_defines",
                                  "sources",
                                  "sources_for_tests",
                                  "sources_when_disabled",
-                                 "configs_to_remove",
                                ])
+        if (defined(invoker.configs)) {
+          configs += invoker.configs
+        }
         testonly = true
         sources = invoker.sources_for_tests
         if (!defined(deps)) {
@@ -287,17 +300,18 @@
         }
         deps += [ ":test" ]
         all_dependent_configs = [ ":" + target_name + "_public" ]
-        configs += skia_library_configs
-        if (defined(invoker.configs_to_remove)) {
-          configs -= invoker.configs_to_remove
-        }
       }
     }
   } else {
-    source_set(target_name) {
+    skia_source_set(target_name) {
+      visibility = [ ":*" ]
+      configs = skia_library_configs
+
+      # "*" clobbers the current scope; append to existing configs
       forward_variables_from(invoker,
                              "*",
                              [
+                               "configs",
                                "public",
                                "public_defines",
                                "public_deps",
@@ -307,15 +321,17 @@
                                "sources",
                                "sources_for_tests",
                                "sources_when_disabled",
-                               "configs_to_remove",
                              ])
+      if (defined(invoker.configs)) {
+        configs += invoker.configs
+      }
       if (defined(invoker.sources_when_disabled)) {
         sources = invoker.sources_when_disabled
       }
-      configs += skia_library_configs
     }
     if (defined(invoker.sources_for_tests)) {
-      source_set(target_name + "_tests") {
+      skia_source_set(target_name + "_tests") {
+        visibility = [ ":*" ]
       }
     }
   }
@@ -545,7 +561,7 @@
 }
 
 if (skia_lex) {
-  executable("sksllex") {
+  skia_executable("sksllex") {
     sources = [
       "src/sksl/lex/Main.cpp",
       "src/sksl/lex/NFA.cpp",
@@ -586,7 +602,7 @@
 
 # `Compile Processors` and `Compile SkSL Tests` both rely on skslc.
 if (skia_compile_processors || skia_compile_sksl_tests) {
-  executable("skslc") {
+  skia_executable("skslc") {
     defines = [
       "SKSL_STANDALONE",
       "SK_DISABLE_TRACING",
@@ -1118,13 +1134,13 @@
 
   # SkRawCodec catches any exceptions thrown by dng_sdk, insulating the rest of
   # Skia.
-  configs_to_remove = [ "//gn:no_exceptions" ]
+  configs = [ "gn/portable:add_exceptions" ]
 
   sources = [ "src/codec/SkRawCodec.cpp" ]
 }
 
 import("third_party/skcms/skcms.gni")
-source_set("skcms") {
+skia_source_set("skcms") {
   cflags = []
   if (!is_win || is_clang) {
     cflags += [
@@ -1219,9 +1235,9 @@
   }
 }
 
-component("skia") {
+skia_component("skia") {
   public_configs = [ ":skia_public" ]
-  configs += skia_library_configs
+  configs = skia_library_configs
 
   public_deps = [
     ":fontmgr_FontConfigInterface",
@@ -1527,8 +1543,8 @@
   ]
 
   # Used by gn_to_bp.py to list our public include dirs.
-  source_set("public") {
-    configs += [ ":skia_public" ]
+  skia_source_set("public") {
+    configs = [ ":skia_public" ]
     include_dirs = skia_public_includes
   }
 
@@ -1548,7 +1564,7 @@
   }
 
   if (target_cpu == "x64") {
-    executable("fiddle") {
+    skia_executable("fiddle") {
       check_includes = false
       libs = []
       sources = [
@@ -1584,13 +1600,15 @@
     }
   }
 
-  source_set("public_headers_warnings_check") {
+  skia_source_set("public_headers_warnings_check") {
     sources = [ "tools/public_headers_warnings_check.cpp" ]
-    configs -= [ "//gn:warnings_except_public_headers" ]
-    configs += [
+    configs = [
       ":our_vulkan_headers",
       ":cpp14",
     ]
+    if (defined(skia_header_target_default_configs)) {
+      configs += skia_header_target_default_configs
+    }
     deps = [
       ":skia",
       ":skia.h",
@@ -1609,7 +1627,7 @@
         defines = invoker.public_defines
       }
     }
-    source_set(target_name) {
+    skia_source_set(target_name) {
       forward_variables_from(invoker, "*", [])
       check_includes = false
       public_configs = [
@@ -1648,24 +1666,32 @@
     } else {
       # !is_ios
 
-      output_dir = root_build_dir
       if (defined(invoker.is_shared_library) && invoker.is_shared_library) {
-        shared_library("lib" + target_name) {
+        skia_shared_library("lib" + target_name) {
+          output_dir = root_build_dir
           forward_variables_from(invoker, "*", [ "is_shared_library" ])
+          if (!defined(configs)) {
+            configs = []
+          }
           configs += [ ":skia_private" ]
           testonly = true
         }
       } else {
         _executable = target_name
-        executable(_executable) {
+        skia_executable(_executable) {
           check_includes = false
+          output_dir = root_build_dir
           forward_variables_from(invoker, "*", [ "is_shared_library" ])
+          if (!defined(configs)) {
+            configs = []
+          }
           configs += [ ":skia_private" ]
           testonly = true
         }
       }
       if (is_android && skia_android_serial != "" && defined(_executable)) {
         action("push_" + target_name) {
+          output_dir = root_build_dir
           script = "gn/push_to_android.py"
           deps = [ ":" + _executable ]
           _stamp = "$target_gen_dir/$_executable.pushed_$skia_android_serial"
@@ -2118,7 +2144,7 @@
     }
 
     # optional separate library to dlopen when running CanvasStateTests.
-    shared_library("canvas_state_lib") {
+    skia_shared_library("canvas_state_lib") {
       sources = [
         "tests/CanvasStateHelpers.cpp",
         "tests/CanvasStateHelpers.h",
@@ -2359,12 +2385,9 @@
   }
 
   if (!is_win) {
-    source_set("skqp_lib") {
+    source_set("skqp_lib") {  # Not a skia_source_set
       check_includes = false
       testonly = true
-      if (!is_official_build) {
-        configs -= [ "//gn:warnings" ]
-      }
       public_configs = [ ":skia_private" ]
       defines =
           [ "SK_SKQP_GLOBAL_ERROR_TOLERANCE=$skia_skqp_global_error_tolerance" ]
@@ -2403,11 +2426,8 @@
     }
   }
   if (is_android) {
-    shared_library("libskqp_app") {
+    shared_library("libskqp_app") {  # Not a skia_shared_library
       configs += [ ":skia_private" ]
-      if (!is_official_build) {
-        configs -= [ "//gn:warnings" ]
-      }
       testonly = true
       sources = [ "tools/skqp/src/jni_skqp.cpp" ]
       deps = [
@@ -2803,7 +2823,7 @@
     }
   }
 
-  executable("cpu_modules") {
+  skia_executable("cpu_modules") {
     sources = [ "tools/cpu_modules.cpp" ]
     deps = [
       ":skia",
@@ -2818,7 +2838,7 @@
     }
   }
 
-  executable("image_diff_metric") {
+  skia_executable("image_diff_metric") {
     sources = [ "tools/image_diff_metric.cpp" ]
     deps = [ ":skia" ]
   }
@@ -2833,10 +2853,13 @@
 
   if (skia_build_fuzzers) {
     template("libfuzzer_app") {
-      output_dir = root_build_dir
-      executable(target_name) {
+      skia_executable(target_name) {
+        output_dir = root_build_dir
         check_includes = false
         forward_variables_from(invoker, "*", [ "is_shared_library" ])
+        if (!defined(configs)) {
+          configs = []
+        }
         configs += [ ":skia_private" ]
         sources += [
           "fuzz/Fuzz.cpp",
@@ -3218,7 +3241,7 @@
   }
 }
 
-executable("skia_c_api_example") {
+skia_executable("skia_c_api_example") {
   sources = [ "experimental/c-api-example/skia-c-example.c" ]
   include_dirs = [ "." ]
   deps = [ ":skia" ]