workspace: Adds support for Bazel facades/backends

This makes use of Bazel's configuration feature 'label_flag' to create
a set of configurations that have overridable defaults from the command
line.

No-Docs-Update-Reason: Docs further up the stack in 41561.
Change-Id: Id4064c8f11bc6ec468aad18ef4d02fa472fdc590
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/41560
Reviewed-by: Keir Mierle <keir@google.com>
Reviewed-by: Ewout van Bekkum <ewout@google.com>
Reviewed-by: Akira Baruah <akirabaruah@google.com>
Commit-Queue: Keir Mierle <keir@google.com>
diff --git a/pw_build/target_config.bzl b/pw_build/target_config.bzl
new file mode 100644
index 0000000..ebd6e74
--- /dev/null
+++ b/pw_build/target_config.bzl
@@ -0,0 +1,59 @@
+# 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.
+
+def _pigweed_config_impl(repository_ctx):
+    if repository_ctx.attr.build_file_content and \
+       repository_ctx.attr.build_file:
+        fail("Attributes 'build_file_content' and 'build_file' cannot both be \
+        defined at the same time.")
+    if not repository_ctx.attr.build_file_content and \
+       not repository_ctx.attr.build_file:
+        fail("Either 'build_file_content' or 'build_file' must be defined.")
+
+    if repository_ctx.name != "pigweed_config":
+        fail("This repository should be name 'pigweed_config'")
+
+    if repository_ctx.attr.build_file_content:
+        repository_ctx.file("BUILD", repository_ctx.attr.build_file_content)
+
+    if repository_ctx.attr.build_file:
+        repository_ctx.template("BUILD", repository_ctx.attr.build_file, {})
+
+pigweed_config = repository_rule(
+    _pigweed_config_impl,
+    attrs = {
+        "build_file_content": attr.string(
+            doc = "The build file content to configure Pigweed.",
+            mandatory = False,
+            default = "",
+        ),
+        "build_file": attr.label(
+            doc = "The label for the Pigweed config build file to use.",
+            mandatory = False,
+            default = "@pigweed//targets/host:host_config.BUILD",
+        ),
+    },
+    doc = """
+Configure Pigweeds backend implementations.
+
+Example:
+    # WORKSPACE
+    pigweed_config(
+        # This must use the exact name specified here otherwise this
+        # remote repository will not function as expected.
+        name = "pigweed_config",
+        build_file = "//path/to/config.BUILD",
+    )
+""",
+)