pw_toolchain: Add copy to GN dummy toolchain

Adds a .gni to share the stamp and copy tools between the dummy
toolchain and generated toolchains.

Change-Id: I04e044c293854b60ad8117b1a603aa1993891d92
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/24441
Commit-Queue: Armando Montanez <amontanez@google.com>
Reviewed-by: Keir Mierle <keir@google.com>
Reviewed-by: Wyatt Hepler <hepler@google.com>
diff --git a/pw_toolchain/dummy/BUILD.gn b/pw_toolchain/dummy/BUILD.gn
index 5d4cf74..aaf43d3 100644
--- a/pw_toolchain/dummy/BUILD.gn
+++ b/pw_toolchain/dummy/BUILD.gn
@@ -12,16 +12,19 @@
 # License for the specific language governing permissions and limitations under
 # the License.
 
+import("//build_overrides/pigweed.gni")
+
+import("$dir_pw_toolchain/universal_tools.gni")
+
 # A dummy toolchain which is set as the default for Pigweed. This is never used;
 # the top-level BUILD.gn enumerates the toolchains for each build.
 toolchain("dummy") {
   tool("stamp") {
-    if (host_os == "win") {
-      command = "cmd /c type nul > \"{{output}}\""
-    } else {
-      command = "touch {{output}}"
-    }
-    description = "stamp {{output}}"
+    forward_variables_from(pw_universal_stamp, "*")
+  }
+
+  tool("copy") {
+    forward_variables_from(pw_universal_copy, "*")
   }
 
   # If the user tries to build a target with the default toolchain, run a script
diff --git a/pw_toolchain/generate_toolchain.gni b/pw_toolchain/generate_toolchain.gni
index 7b19f2b..9a74cdf 100644
--- a/pw_toolchain/generate_toolchain.gni
+++ b/pw_toolchain/generate_toolchain.gni
@@ -14,6 +14,8 @@
 
 import("//build_overrides/pigweed.gni")
 
+import("$dir_pw_toolchain/universal_tools.gni")
+
 declare_args() {
   # Scope defining the current toolchain. Contains all of the arguments required
   # by the generate_toolchain template.
@@ -291,40 +293,17 @@
       }
 
       tool("stamp") {
-        if (host_os == "win") {
-          command = "cmd /c type nul > \"{{output}}\""
-        } else {
-          command = "touch {{output}}"
-        }
-        description = "stamp {{output}}"
+        # GN-ism: GN gets mad if you directly forward the contents of
+        # pw_universal_stamp.
+        _stamp = pw_universal_stamp
+        forward_variables_from(_stamp, "*")
       }
 
       tool("copy") {
-        if (host_os != "win") {
-          # Use a hard link if possible as this is faster. Also, Mac doesn't
-          # preserve timestamps properly with cp -af.
-          fallback_command = string_join(" ",
-                                         [
-                                           "rm -rf",
-                                           "{{output}}",
-                                           "&&",
-                                           "cp -af",
-                                           "{{source}}",
-                                           "{{output}}",
-                                         ])
-          command = string_join(" ",
-                                [
-                                  "ln -f",
-                                  "{{source}}",
-                                  "{{output}}",
-                                  "2>/dev/null",
-                                  "||",
-                                  "($fallback_command)",
-                                ])
-        } else {
-          command = "cp -af {{source}} {{output}}"
-        }
-        description = "cp {{source}} {{output}}"
+        # GN-ism: GN gets mad if you directly forward the contents of
+        # pw_universal_copy.
+        _copy = pw_universal_copy
+        forward_variables_from(_copy, "*")
       }
 
       # Build arguments to be overridden when compiling cross-toolchain:
diff --git a/pw_toolchain/universal_tools.gni b/pw_toolchain/universal_tools.gni
new file mode 100644
index 0000000..8fcc062
--- /dev/null
+++ b/pw_toolchain/universal_tools.gni
@@ -0,0 +1,50 @@
+# 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.
+
+pw_universal_copy = {
+  if (host_os == "win") {
+    command = "cp -af {{source}} {{output}}"
+  } else {
+    # Use a hard link if possible as this is faster. Also, Mac doesn't
+    # preserve timestamps properly with cp -af.
+    fallback_command = string_join(" ",
+                                   [
+                                     "rm -rf",
+                                     "{{output}}",
+                                     "&&",
+                                     "cp -af",
+                                     "{{source}}",
+                                     "{{output}}",
+                                   ])
+    command = string_join(" ",
+                          [
+                            "ln -f",
+                            "{{source}}",
+                            "{{output}}",
+                            "2>/dev/null",
+                            "||",
+                            "($fallback_command)",
+                          ])
+  }
+  description = "cp {{source}} {{output}}"
+}
+
+pw_universal_stamp = {
+  if (host_os == "win") {
+    command = "cmd /c type nul > \"{{output}}\""
+  } else {
+    command = "touch {{output}}"
+  }
+  description = "stamp {{output}}"
+}