GN build: Override built-in targets

- Override built-in targets, which gives more flexibility than
  set_defaults.
- Provide the default C++ in the toolchain, which can be overridden by
  explicitly specifying a config on a target.
- Remove the C++17 default flags from public configs, since it is no
  longer needed.

Change-Id: If211bada971c46cef83909bf15811a4f2c1eb7fd
diff --git a/BUILDCONFIG.gn b/BUILDCONFIG.gn
index df57e5e..6571ad0 100644
--- a/BUILDCONFIG.gn
+++ b/BUILDCONFIG.gn
@@ -1,4 +1,4 @@
-# Copyright 2019 The Pigweed Authors
+# Copyright 2020 The Pigweed Authors
 #
 # Licensed under the Apache License, Version 2.0 (the "License"); you may not
 # use this file except in compliance with the License. You may obtain a copy of
@@ -39,11 +39,6 @@
 # .gni files (as they depend on the dir_[module] variables).
 import("$dir_pigweed/modules.gni")
 
-# Import default GN configurations. This file provides default configurations
-# for upstream Pigweed development, and projects are free to provide their own
-# variation of this file. (depends on modules.gni)
-import("$dir_pigweed/gn_defaults.gni")
-
 # Import target configuration. This is what "completes" a Pigweed configuration.
 # This file should set a default toolchain, configure pw_executable, select
 # backends to build against, and provide target-specific build arguments.
@@ -54,3 +49,35 @@
 assert(pw_target_toolchain != "",
        "Build target must provide its own toolchain.")
 set_default_toolchain(pw_target_toolchain)
+
+# Override the built-in build targets to add default compilation options.
+# TODO(pwbug/72): Move this code to a .gni file for easier reuse.
+template("_pw_override_target_with_defaults") {
+  target(invoker._target_type, target_name) {
+    forward_variables_from(invoker, "*", [ "_target_type" ])
+
+    if (!defined(configs)) {
+      configs = []
+    }
+
+    # Add default configs to use for all binary build targets.
+    configs += [
+      "$dir_pw_build:reduced_size",
+      "$dir_pw_build:strict_warnings",
+    ]
+  }
+}
+
+foreach(_target_type,
+        [
+          "source_set",
+          "executable",
+          "shared_library",
+          "static_library",
+        ]) {
+  template(_target_type) {
+    _pw_override_target_with_defaults(target_name) {
+      forward_variables_from(invoker, "*")
+    }
+  }
+}
diff --git a/gn_defaults.gni b/gn_defaults.gni
deleted file mode 100644
index 5ff17c5..0000000
--- a/gn_defaults.gni
+++ /dev/null
@@ -1,40 +0,0 @@
-# Copyright 2020 The Pigweed Authors
-#
-# Licensed under the Apache License, Version 2.0 (the "License"); you may not
-# use this file except in compliance with the License. You may obtain a copy of
-# the License at
-#
-#     https://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-# License for the specific language governing permissions and limitations under
-# the License.
-
-# This file sets defaults for the different GN/Ninja target types in a way that
-# is suitable for upstream Pigweed development. Projects using Pigweed are
-# welcome to import a modified version of this in their BUILDCONFIG.gn, or may
-# choose not to import it altogether.
-
-# Default configs to use for all binary build targets.
-_default_common_binary_configs = [
-  "$dir_pw_build:reduced_size",
-  "$dir_pw_build:strict_warnings",
-]
-
-set_defaults("executable") {
-  configs = _default_common_binary_configs
-}
-
-set_defaults("static_library") {
-  configs = _default_common_binary_configs
-}
-
-set_defaults("shared_library") {
-  configs = _default_common_binary_configs
-}
-
-set_defaults("source_set") {
-  configs = _default_common_binary_configs
-}
diff --git a/pw_base64/BUILD.gn b/pw_base64/BUILD.gn
index 689e94c..e69421d 100644
--- a/pw_base64/BUILD.gn
+++ b/pw_base64/BUILD.gn
@@ -19,10 +19,7 @@
 }
 
 source_set("pw_base64") {
-  public_configs = [
-    "$dir_pw_build:pw_default_cpp",
-    ":default_config",
-  ]
+  public_configs = [ ":default_config" ]
   public = [
     "public/pw_base64/base64.h",
   ]
diff --git a/pw_bloat/BUILD.gn b/pw_bloat/BUILD.gn
index 67ca8f5..c98f611 100644
--- a/pw_bloat/BUILD.gn
+++ b/pw_bloat/BUILD.gn
@@ -23,10 +23,7 @@
 # Library which uses standard C/C++ functions such as memcpy to prevent them
 # from showing up within bloat diff reports.
 source_set("bloat_this_binary") {
-  public_configs = [
-    "$dir_pw_build:pw_default_cpp",
-    ":default_config",
-  ]
+  public_configs = [ ":default_config" ]
   public = [
     "public/pw_bloat/bloat_this_binary.h",
   ]
diff --git a/pw_build/BUILD.gn b/pw_build/BUILD.gn
index 935c47c..b5ad0be 100644
--- a/pw_build/BUILD.gn
+++ b/pw_build/BUILD.gn
@@ -47,11 +47,6 @@
   ]
 }
 
-# Default C++ version for Pigweed modules.
-config("pw_default_cpp") {
-  configs = [ ":cpp17" ]
-}
-
 pw_doc_group("docs") {
   sources = [
     "docs.rst",
diff --git a/pw_cpu_exception/BUILD.gn b/pw_cpu_exception/BUILD.gn
index 1e3aaaf..2e62376 100644
--- a/pw_cpu_exception/BUILD.gn
+++ b/pw_cpu_exception/BUILD.gn
@@ -20,10 +20,7 @@
 }
 
 source_set("facade") {
-  public_configs = [
-    "$dir_pw_build:pw_default_cpp",
-    ":default_config",
-  ]
+  public_configs = [ ":default_config" ]
   public_deps = [
     "$dir_pw_preprocessor",
     "$dir_pw_span",
diff --git a/pw_cpu_exception_armv7m/BUILD.gn b/pw_cpu_exception_armv7m/BUILD.gn
index 9c09d58..e7adce7 100644
--- a/pw_cpu_exception_armv7m/BUILD.gn
+++ b/pw_cpu_exception_armv7m/BUILD.gn
@@ -24,10 +24,7 @@
   }
 
   source_set("pw_cpu_exception_armv7m") {
-    public_configs = [
-      "$dir_pw_build:pw_default_cpp",
-      ":default_config",
-    ]
+    public_configs = [ ":default_config" ]
     deps = [
       "$dir_pw_cpu_exception:facade",
       "$dir_pw_dumb_io:default_putget_bytes",
diff --git a/pw_dumb_io/BUILD.gn b/pw_dumb_io/BUILD.gn
index 19426a3..50bc709 100644
--- a/pw_dumb_io/BUILD.gn
+++ b/pw_dumb_io/BUILD.gn
@@ -20,10 +20,7 @@
 }
 
 source_set("facade") {
-  public_configs = [
-    "$dir_pw_build:pw_default_cpp",
-    ":default_config",
-  ]
+  public_configs = [ ":default_config" ]
   public_deps = [
     "$dir_pw_span",
     "$dir_pw_status",
diff --git a/pw_dumb_io_baremetal_stm32f429/BUILD.gn b/pw_dumb_io_baremetal_stm32f429/BUILD.gn
index 6240129..7eaf6f1 100644
--- a/pw_dumb_io_baremetal_stm32f429/BUILD.gn
+++ b/pw_dumb_io_baremetal_stm32f429/BUILD.gn
@@ -30,7 +30,6 @@
   }
 
   source_set("pw_dumb_io_baremetal_stm32f429") {
-    public_configs = [ "$dir_pw_build:pw_default_cpp" ]
     public_deps = [
       ":linker_script",
     ]
diff --git a/pw_dumb_io_stdio/BUILD.gn b/pw_dumb_io_stdio/BUILD.gn
index c40c4a9..0cd7ca3 100644
--- a/pw_dumb_io_stdio/BUILD.gn
+++ b/pw_dumb_io_stdio/BUILD.gn
@@ -18,7 +18,6 @@
 # compatible with this backend.
 if (dir_pw_dumb_io_backend == dir_pw_dumb_io_stdio) {
   source_set("pw_dumb_io_stdio") {
-    public_configs = [ "$dir_pw_build:pw_default_cpp" ]
     deps = [
       "$dir_pw_dumb_io:default_putget_bytes",
       "$dir_pw_dumb_io:facade",
diff --git a/pw_preprocessor/BUILD.gn b/pw_preprocessor/BUILD.gn
index 6c197fb..3ef1b5b 100644
--- a/pw_preprocessor/BUILD.gn
+++ b/pw_preprocessor/BUILD.gn
@@ -20,10 +20,7 @@
 }
 
 source_set("pw_preprocessor") {
-  public_configs = [
-    "$dir_pw_build:pw_default_cpp",
-    ":default_config",
-  ]
+  public_configs = [ ":default_config" ]
   public = [
     "public/pw_preprocessor/boolean.h",
     "public/pw_preprocessor/compiler.h",
diff --git a/pw_protobuf/BUILD.gn b/pw_protobuf/BUILD.gn
index 01dfefa..ea82ecc 100644
--- a/pw_protobuf/BUILD.gn
+++ b/pw_protobuf/BUILD.gn
@@ -23,10 +23,7 @@
 }
 
 source_set("pw_protobuf") {
-  public_configs = [
-    "$dir_pw_build:pw_default_cpp",
-    ":default_config",
-  ]
+  public_configs = [ ":default_config" ]
   public_deps = [
     "$dir_pw_status",
     "$dir_pw_varint",
diff --git a/pw_span/BUILD.gn b/pw_span/BUILD.gn
index 52296d9..a3aace1 100644
--- a/pw_span/BUILD.gn
+++ b/pw_span/BUILD.gn
@@ -20,10 +20,7 @@
 }
 
 source_set("pw_span") {
-  public_configs = [
-    "$dir_pw_build:pw_default_cpp",
-    ":default_config",
-  ]
+  public_configs = [ ":default_config" ]
   public = [
     "public/pw_span/span.h",
   ]
diff --git a/pw_status/BUILD.gn b/pw_status/BUILD.gn
index 277ec33..5231111 100644
--- a/pw_status/BUILD.gn
+++ b/pw_status/BUILD.gn
@@ -20,10 +20,7 @@
 }
 
 source_set("pw_status") {
-  public_configs = [
-    "$dir_pw_build:pw_default_cpp",
-    ":default_config",
-  ]
+  public_configs = [ ":default_config" ]
   public = [
     "public/pw_status/status.h",
     "public/pw_status/status_with_size.h",
diff --git a/pw_string/BUILD.gn b/pw_string/BUILD.gn
index ac9666a..01bf37f 100644
--- a/pw_string/BUILD.gn
+++ b/pw_string/BUILD.gn
@@ -21,10 +21,7 @@
 }
 
 source_set("pw_string") {
-  public_configs = [
-    "$dir_pw_build:pw_default_cpp",
-    ":default_config",
-  ]
+  public_configs = [ ":default_config" ]
   public = [
     "public/pw_string/format.h",
     "public/pw_string/string_builder.h",
diff --git a/pw_tokenizer/BUILD.gn b/pw_tokenizer/BUILD.gn
index 897692f..2bf43fc 100644
--- a/pw_tokenizer/BUILD.gn
+++ b/pw_tokenizer/BUILD.gn
@@ -20,10 +20,7 @@
 }
 
 source_set("pw_tokenizer") {
-  public_configs = [
-    "$dir_pw_build:pw_default_cpp",
-    ":default_config",
-  ]
+  public_configs = [ ":default_config" ]
   public_deps = [
     "$dir_pw_preprocessor",
     "$dir_pw_span",
@@ -54,10 +51,7 @@
 }
 
 source_set("base64") {
-  public_configs = [
-    "$dir_pw_build:pw_default_cpp",
-    ":default_config",
-  ]
+  public_configs = [ ":default_config" ]
   public = [
     "public/pw_tokenizer/base64.h",
   ]
@@ -72,10 +66,7 @@
 }
 
 source_set("decoder") {
-  public_configs = [
-    "$dir_pw_build:pw_default_cpp",
-    ":default_config",
-  ]
+  public_configs = [ ":default_config" ]
   public_deps = [
     "$dir_pw_span",
   ]
@@ -233,10 +224,7 @@
 # the JNI headers must be available in the system or provided with the
 # pw_java_native_interface_include_dirs variable.
 shared_library("detokenizer_jni") {
-  public_configs = [
-    "$dir_pw_build:pw_default_cpp",
-    ":default_config",
-  ]
+  public_configs = [ ":default_config" ]
   include_dirs = pw_java_native_interface_include_dirs
   sources = [
     "java/dev/pigweed/tokenizer/detokenizer.cc",
diff --git a/pw_toolchain/arm_gcc.gni b/pw_toolchain/arm_gcc.gni
index 0a1631e..51bab63 100644
--- a/pw_toolchain/arm_gcc.gni
+++ b/pw_toolchain/arm_gcc.gni
@@ -44,6 +44,9 @@
 
   _toolchain_cflags = string_join(" ", _cflags_list)
 
+  # Specify the default C++ version, which targets can override with a config.
+  _toolchain_cflags_cc = "-std=c++17 -Wno-register"
+
   _toolchain_ldflags = ""
   if (defined(invoker.toolchain_ldflags)) {
     _toolchain_ldflags += string_join(" ", invoker.toolchain_ldflags)
@@ -109,6 +112,7 @@
                             [
                               _cc,
                               "-MMD -MF $depfile",  # Write out dependencies.
+                              _toolchain_cflags_cc,
                               _toolchain_cflags,
                               "{{defines}}",
                               "{{include_dirs}}",
diff --git a/pw_toolchain/host_clang.gni b/pw_toolchain/host_clang.gni
index fb2cca2..2dd9577 100644
--- a/pw_toolchain/host_clang.gni
+++ b/pw_toolchain/host_clang.gni
@@ -40,6 +40,9 @@
 
   _toolchain_cflags = string_join(" ", _cflags_list)
 
+  # Specify the default C++ version, which targets can override with a config.
+  _toolchain_cflags_cc = "-std=c++17 -Wno-register"
+
   # Toolchain LD flags
   _toolchain_ldflags = ""
   if (defined(invoker.toolchain_ldflags)) {
@@ -59,7 +62,7 @@
                             [
                               "$_cc",
                               "-MMD -MF $depfile",  # Write out dependencies.
-                              "$_toolchain_cflags",
+                              _toolchain_cflags,
                               "{{defines}}",
                               "{{include_dirs}}",
                               "{{asmflags}}",
@@ -82,7 +85,7 @@
                             [
                               "$_cc",
                               "-MMD -MF $depfile",  # Write out dependencies.
-                              "$_toolchain_cflags",
+                              _toolchain_cflags,
                               "{{defines}}",
                               "{{include_dirs}}",
                               "{{cflags}}",
@@ -103,7 +106,8 @@
                             [
                               "$_cxx",
                               "-MMD -MF $depfile",  # Write out dependencies.
-                              "$_toolchain_cflags",
+                              _toolchain_cflags_cc,
+                              _toolchain_cflags,
                               "{{defines}}",
                               "{{include_dirs}}",
                               "{{cflags}}",
@@ -136,8 +140,8 @@
       "$_cxx",
       "{{ldflags}}",
 
-      "$_toolchain_cflags",
-      "$_toolchain_ldflags",
+      _toolchain_cflags,
+      _toolchain_ldflags,
 
       "{{inputs}}",
       "{{libs}}",
diff --git a/pw_toolchain/host_gcc.gni b/pw_toolchain/host_gcc.gni
index 0ea8e22..211aa63 100644
--- a/pw_toolchain/host_gcc.gni
+++ b/pw_toolchain/host_gcc.gni
@@ -44,6 +44,9 @@
 
   _toolchain_cflags = string_join(" ", _cflags_list)
 
+  # Specify the default C++ version, which targets can override with a config.
+  _toolchain_cflags_cc = "-std=c++17 -Wno-register"
+
   _toolchain_ldflags = ""
   if (defined(invoker.toolchain_ldflags)) {
     foreach(flag, invoker.toolchain_ldflags) {
@@ -62,7 +65,7 @@
                             [
                               "$_cc",
                               "-MMD -MF $depfile",
-                              "$_toolchain_cflags",
+                              _toolchain_cflags,
                               "{{defines}}",
                               "{{include_dirs}}",
                               "{{asmflags}}",
@@ -85,7 +88,7 @@
                             [
                               "$_cc",
                               "-MMD -MF $depfile",
-                              "$_toolchain_cflags",
+                              _toolchain_cflags,
                               "{{defines}}",
                               "{{include_dirs}}",
                               "{{cflags}}",
@@ -106,7 +109,8 @@
                             [
                               "$_cxx",
                               "-MMD -MF $depfile",
-                              "$_toolchain_cflags",
+                              _toolchain_cflags_cc,
+                              _toolchain_cflags,
                               "{{defines}}",
                               "{{include_dirs}}",
                               "{{cflags}}",
@@ -139,8 +143,8 @@
       "$_cxx",
       "{{ldflags}}",
 
-      "$_toolchain_cflags",
-      "$_toolchain_ldflags",
+      _toolchain_cflags,
+      _toolchain_ldflags,
 
       "{{inputs}}",
       "{{libs}}",
diff --git a/pw_unit_test/BUILD.gn b/pw_unit_test/BUILD.gn
index 47f2ed3..c2dd15a 100644
--- a/pw_unit_test/BUILD.gn
+++ b/pw_unit_test/BUILD.gn
@@ -24,10 +24,7 @@
 
 # pw_unit_test core library.
 source_set("pw_unit_test") {
-  public_configs = [
-    "$dir_pw_build:pw_default_cpp",
-    ":default_config",
-  ]
+  public_configs = [ ":default_config" ]
   public_deps = [
     "$dir_pw_preprocessor",
     "$dir_pw_string",
diff --git a/pw_varint/BUILD.gn b/pw_varint/BUILD.gn
index cb46782..b794180 100644
--- a/pw_varint/BUILD.gn
+++ b/pw_varint/BUILD.gn
@@ -19,10 +19,7 @@
 }
 
 source_set("pw_varint") {
-  public_configs = [
-    "$dir_pw_build:pw_default_cpp",
-    ":default_config",
-  ]
+  public_configs = [ ":default_config" ]
   public_deps = [
     "$dir_pw_span",
   ]