pw_build: Remove instancing of pools across toolchains

The build system uses pools for mutual exclusion. Currently multiple
instances of these pools exist which defeats their purpose.

For example, it can cause concurrent instances of "pip install" which is
not safe.

Bug: 390
No-Docs-Update-Reason: bug fix
Change-Id: I114d2428d747a6d5a68a024a0177343ee82cad76
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/48520
Commit-Queue: Michael Spang <spang@google.com>
Reviewed-by: Wyatt Hepler <hepler@google.com>
diff --git a/pw_build/BUILD.gn b/pw_build/BUILD.gn
index bb83617..02105cf 100644
--- a/pw_build/BUILD.gn
+++ b/pw_build/BUILD.gn
@@ -141,14 +141,6 @@
 group("empty") {
 }
 
-pool("pip_pool") {
-  depth = 1
-}
-
-pool("copy_from_cipd_pool") {
-  depth = 1
-}
-
 # Requirements for the pw_python_package lint targets.
 pw_python_requirements("python_lint") {
   requirements = [
@@ -164,8 +156,3 @@
     "python.rst",
   ]
 }
-
-# Pool to limit a single thread to download external Go packages at a time.
-pool("go_download_pool") {
-  depth = 1
-}
diff --git a/pw_build/copy_from_cipd.gni b/pw_build/copy_from_cipd.gni
index 19d332a..aff8afa 100644
--- a/pw_build/copy_from_cipd.gni
+++ b/pw_build/copy_from_cipd.gni
@@ -69,7 +69,7 @@
 
     # Parallel calls might both be invoking CIPD on the same directory which
     # might work but is redundant, so serialize calls.
-    pool = "$dir_pw_build:copy_from_cipd_pool"
+    pool = "$dir_pw_build/pool:copy_from_cipd($default_toolchain)"
 
     # TODO(pwbug/335): This should somehow track the actual .a for changes as
     # well.
diff --git a/pw_build/go.gni b/pw_build/go.gni
index eadb93b..d13af2e 100644
--- a/pw_build/go.gni
+++ b/pw_build/go.gni
@@ -140,7 +140,7 @@
     skip_empty_args = true
 
     # Limit download parallelization to 1.
-    pool = "$dir_pw_build:go_download_pool"
+    pool = "$dir_pw_build/pool:go_download($default_toolchain)"
   }
 
   # Run a "go build" command with the environment configured from metadata.
diff --git a/pw_build/pool/BUILD.gn b/pw_build/pool/BUILD.gn
new file mode 100644
index 0000000..1102744
--- /dev/null
+++ b/pw_build/pool/BUILD.gn
@@ -0,0 +1,30 @@
+# Copyright 2021 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.
+
+# Pools are used to limit concurrency; we don't want multiple instances.
+assert(current_toolchain == default_toolchain)
+
+# Pool to serialize python package installs.
+pool("pip") {
+  depth = 1
+}
+
+pool("copy_from_cipd") {
+  depth = 1
+}
+
+# Pool to limit a single thread to download external Go packages at a time.
+pool("go_download") {
+  depth = 1
+}
diff --git a/pw_build/python.gni b/pw_build/python.gni
index 217a7f8..fdd5b78 100644
--- a/pw_build/python.gni
+++ b/pw_build/python.gni
@@ -410,7 +410,7 @@
         stamp = true
 
         # Parallel pip installations don't work, so serialize pip invocations.
-        pool = "$dir_pw_build:pip_pool"
+        pool = "$dir_pw_build/pool:pip($default_toolchain)"
 
         foreach(dep, _python_deps) {
           # We need to add a suffix to the target name, but the label is
@@ -727,7 +727,7 @@
       ]
     }
 
-    pool = "$dir_pw_build:pip_pool"
+    pool = "$dir_pw_build/pool:pip($default_toolchain)"
     stamp = true
   }
 
diff --git a/pw_tokenizer/database.gni b/pw_tokenizer/database.gni
index 2508470..07d441f 100644
--- a/pw_tokenizer/database.gni
+++ b/pw_tokenizer/database.gni
@@ -91,15 +91,12 @@
     not_needed([ "_domain" ])
   }
 
-  # Restrict parallelism for updating this database file to one thread. This
-  # makes it safe to update it from multiple toolchains.
-  pool("$target_name._pool") {
-    depth = 1
-  }
-
   pw_python_action(target_name) {
     script = "$dir_pw_tokenizer/py/pw_tokenizer/database.py"
-    pool = ":$target_name._pool"
+
+    # Restrict parallelism for updating this database file to one thread. This
+    # makes it safe to update it from multiple toolchains.
+    pool = "$dir_pw_tokenizer/pool:database($default_toolchain)"
 
     inputs = _input_databases
 
diff --git a/pw_tokenizer/pool/BUILD.gn b/pw_tokenizer/pool/BUILD.gn
new file mode 100644
index 0000000..cdb5bc3
--- /dev/null
+++ b/pw_tokenizer/pool/BUILD.gn
@@ -0,0 +1,20 @@
+# Copyright 2021 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.
+
+# Pools are used to limit concurrency; we don't want multiple instances.
+assert(current_toolchain == default_toolchain)
+
+pool("database") {
+  depth = 1
+}