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/pigweed.bzl b/pw_build/pigweed.bzl
index 3cc0de1..2b16982 100644
--- a/pw_build/pigweed.bzl
+++ b/pw_build/pigweed.bzl
@@ -12,6 +12,8 @@
# License for the specific language governing permissions and limitations under
# the License.
+load("@rules_cc//cc:defs.bzl", "cc_library")
+
"""Pigweed build environment for bazel."""
DEBUGGING = [
@@ -104,7 +106,7 @@
cc_kwargs, c_kwargs = _default_cc_and_c_kwargs(kwargs)
if c_kwargs:
- native.cc_library(**c_kwargs)
+ cc_library(**c_kwargs)
target(**cc_kwargs)
@@ -117,3 +119,15 @@
def pw_cc_test(**kwargs):
kwargs["deps"] = kwargs.get("deps", []) + ["//pw_unit_test:main"]
_add_cc_and_c_targets(native.cc_test, kwargs)
+
+def pw_cc_facade(**kwargs):
+ # Bazel facades should be source only cc_library's this is to simplify
+ # lazy header evaluation. Bazel headers are not 'precompiled' so the build
+ # system does not check to see if the build has the right dependant headers
+ # in the sandbox. If a source file is declared here and includes a header
+ # file the toolchain will compile as normal and complain about the missing
+ # backend headers.
+ if "srcs" in kwargs.keys():
+ fail("'srcs' attribute does not exist in pw_cc_facade, please use \
+ main implementing target.")
+ _add_cc_and_c_targets(native.cc_library, kwargs)
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",
+ )
+""",
+)