pw_presubmit: Help prevent undefined GN build args

- Add the --fail-on-unused-args option to gn gen by default in
  presubmit.
- Import .gni files for backend build args into the toolchains
  that set them.
- Skip the init_cipd and init_virtualenv steps in local presubmits.
  Instead, use the current environment. This is much faster and avoids
  confusion about which environment is in use.

Fixes: 294
Change-Id: Ib86da7c3d4560b6c92cbacced725ee54c485e3d6
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/27580
Commit-Queue: Wyatt Hepler <hepler@google.com>
Reviewed-by: Michael Spang <spang@google.com>
diff --git a/pw_presubmit/py/pw_presubmit/build.py b/pw_presubmit/py/pw_presubmit/build.py
index 4a370e3..ea19ec6 100644
--- a/pw_presubmit/py/pw_presubmit/build.py
+++ b/pw_presubmit/py/pw_presubmit/build.py
@@ -66,16 +66,17 @@
            gn_output_dir: Path,
            *args: str,
            gn_check: bool = True,
+           gn_fail_on_unused: bool = True,
            **gn_arguments) -> None:
     """Runs gn gen in the specified directory with optional GN args."""
     args_option = (gn_args(**gn_arguments), ) if gn_arguments else ()
-    check_option = ['--check'] if gn_check else []
 
     call('gn',
          'gen',
          gn_output_dir,
          '--color=always',
-         *check_option,
+         *(['--check'] if gn_check else []),
+         *(['--fail-on-unused-args'] if gn_fail_on_unused else []),
          *args,
          *args_option,
          cwd=gn_source_dir)
diff --git a/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py b/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py
index 794967d..6df7ece 100755
--- a/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py
+++ b/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py
@@ -477,8 +477,6 @@
 
 QUICK = (
     commit_message_format,
-    init_cipd,
-    init_virtualenv,
     source_is_in_build_files,
     copyright_notice,
     format_code.presubmit_checks(),
diff --git a/pw_rpc/system_server/BUILD.gn b/pw_rpc/system_server/BUILD.gn
index c6fbe08..ef36c58 100644
--- a/pw_rpc/system_server/BUILD.gn
+++ b/pw_rpc/system_server/BUILD.gn
@@ -16,13 +16,11 @@
 import("$dir_pw_build/facade.gni")
 import("$dir_pw_build/target_types.gni")
 import("$dir_pw_docgen/docs.gni")
-declare_args() {
-  # Backend for the pw_rpc_system_server module.
-  pw_rpc_system_server_BACKEND = ""
-}
+import("backend.gni")
 
 config("public_includes") {
   include_dirs = [ "public" ]
+  visibility = [ ":*" ]
 }
 
 pw_facade("system_server") {
diff --git a/pw_rpc/system_server/backend.gni b/pw_rpc/system_server/backend.gni
new file mode 100644
index 0000000..980f428
--- /dev/null
+++ b/pw_rpc/system_server/backend.gni
@@ -0,0 +1,18 @@
+# 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.
+
+declare_args() {
+  # Backend for the pw_rpc_system_server module.
+  pw_rpc_system_server_BACKEND = ""
+}
diff --git a/pw_sync/BUILD.gn b/pw_sync/BUILD.gn
index 058c4f0..615c3db 100644
--- a/pw_sync/BUILD.gn
+++ b/pw_sync/BUILD.gn
@@ -18,20 +18,7 @@
 import("$dir_pw_build/target_types.gni")
 import("$dir_pw_docgen/docs.gni")
 import("$dir_pw_unit_test/test.gni")
-
-declare_args() {
-  # Backend for the pw_sync module's binary semaphore.
-  pw_sync_BINARY_SEMAPHORE_BACKEND = ""
-
-  # Backend for the pw_sync module's counting semaphore.
-  pw_sync_COUNTING_SEMAPHORE_BACKEND = ""
-
-  # Backend for the pw_sync module's mutex.
-  pw_sync_MUTEX_BACKEND = ""
-
-  # Backend for the pw_sync module's spin lock.
-  pw_sync_SPIN_LOCK_BACKEND = ""
-}
+import("backend.gni")
 
 config("public_include_path") {
   include_dirs = [ "public" ]
diff --git a/pw_sync/backend.gni b/pw_sync/backend.gni
new file mode 100644
index 0000000..9e43ae6
--- /dev/null
+++ b/pw_sync/backend.gni
@@ -0,0 +1,27 @@
+# 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.
+
+declare_args() {
+  # Backend for the pw_sync module's binary semaphore.
+  pw_sync_BINARY_SEMAPHORE_BACKEND = ""
+
+  # Backend for the pw_sync module's counting semaphore.
+  pw_sync_COUNTING_SEMAPHORE_BACKEND = ""
+
+  # Backend for the pw_sync module's mutex.
+  pw_sync_MUTEX_BACKEND = ""
+
+  # Backend for the pw_sync module's spin lock.
+  pw_sync_SPIN_LOCK_BACKEND = ""
+}
diff --git a/targets/host/target_toolchains.gni b/targets/host/target_toolchains.gni
index 9cf16d3..e3dd05d 100644
--- a/targets/host/target_toolchains.gni
+++ b/targets/host/target_toolchains.gni
@@ -14,7 +14,10 @@
 
 import("//build_overrides/pigweed.gni")
 
+import("$dir_pw_chrono/backend.gni")
 import("$dir_pw_protobuf_compiler/proto.gni")
+import("$dir_pw_rpc/system_server/backend.gni")
+import("$dir_pw_sync/backend.gni")
 import("$dir_pw_third_party/nanopb/nanopb.gni")
 import("$dir_pw_toolchain/host_clang/toolchains.gni")
 import("$dir_pw_toolchain/host_gcc/toolchains.gni")
diff --git a/targets/lm3s6965evb-qemu/target_toolchains.gni b/targets/lm3s6965evb-qemu/target_toolchains.gni
index 25d7151..be322ab 100644
--- a/targets/lm3s6965evb-qemu/target_toolchains.gni
+++ b/targets/lm3s6965evb-qemu/target_toolchains.gni
@@ -43,7 +43,7 @@
   # appear the divide-by-zero traps as expected when enabled, which prevents the
   # module from triggering a recoverable exception. Since pw_cpu_exception is
   # not fully set up on this target, disable it for now.
-  # pw_cpu_exception_BACKEND = dir_pw_cpu_exception_armv7m
+  # pw_cpu_exception_ENTRY_BACKEND = dir_pw_cpu_exception_armv7m
 
   pw_boot_armv7m_LINK_CONFIG_DEFINES = [
     "PW_BOOT_FLASH_BEGIN=0x00000200",
diff --git a/targets/stm32f429i-disc1/target_toolchains.gni b/targets/stm32f429i-disc1/target_toolchains.gni
index 6344f81..4125ead 100644
--- a/targets/stm32f429i-disc1/target_toolchains.gni
+++ b/targets/stm32f429i-disc1/target_toolchains.gni
@@ -14,6 +14,7 @@
 
 import("//build_overrides/pigweed.gni")
 
+import("$dir_pw_rpc/system_server/backend.gni")
 import("$dir_pw_toolchain/arm_gcc/toolchains.gni")
 
 declare_args() {