pw_build: Add Pigweed specific Bazel platforms

This change adds a set of constraint_settings constraint_values and
platforms specific to pigweed. This is also tested on pw_boot_armv7m and
pw_sys_io_baremetal_stm32f429 packages.

Change-Id: I991aaff7a6208c6954555d4edd91142156a0db5c
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/33580
Reviewed-by: Wyatt Hepler <hepler@google.com>
Reviewed-by: Keir Mierle <keir@google.com>
Reviewed-by: Akira Baruah <akira.baruah@gmail.com>
Commit-Queue: Wyatt Hepler <hepler@google.com>
diff --git a/WORKSPACE b/WORKSPACE
index adcb010..573d3f2 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -69,7 +69,6 @@
     name = "bazel_embedded",
     commit = "89c05fa415218abd2e24fa7016cb7903317d606b",
     remote = "https://github.com/silvergasp/bazel-embedded.git",
-    shallow_since = "1614045367 +0800",
 )
 
 # Instantiate Pigweed configuration for embedded toolchain,
@@ -128,3 +127,5 @@
 )
 
 register_clang_toolchain()
+
+register_execution_platforms("//pw_build/platforms:all")
diff --git a/pw_boot_armv7m/BUILD b/pw_boot_armv7m/BUILD
index 0980c0a..52f7901 100644
--- a/pw_boot_armv7m/BUILD
+++ b/pw_boot_armv7m/BUILD
@@ -1,4 +1,4 @@
-# Copyright 2020 The Pigweed Authors
+# 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
@@ -11,15 +11,28 @@
 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 # License for the specific language governing permissions and limitations under
 # the License.
+load(
+    "//pw_build:pigweed.bzl",
+    "pw_cc_library",
+)
 
 package(default_visibility = ["//visibility:public"])
 
 licenses(["notice"])  # Apache License 2.0
 
-filegroup(
+pw_cc_library(
     name = "pw_boot_armv7m",
     srcs = [
         "core_init.c",
         "public/pw_boot_armv7m/boot.h",
     ],
+    includes = ["public"],
+    target_compatible_with = select({
+        "@platforms//cpu:armv7e-m": [],
+        "@platforms//cpu:armv7-m": [],
+        "//conditions:default": ["@platforms//:incompatible"],
+    }),
+    deps = [
+        "//pw_preprocessor",
+    ],
 )
diff --git a/pw_build/constraints/board/BUILD b/pw_build/constraints/board/BUILD
new file mode 100644
index 0000000..1bf3ea2
--- /dev/null
+++ b/pw_build/constraints/board/BUILD
@@ -0,0 +1,23 @@
+# 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.
+package(default_visibility = ["//visibility:public"])
+
+constraint_setting(
+    name = "board",
+)
+
+constraint_value(
+    name = "stm32f429i-disc1",
+    constraint_setting = ":board",
+)
diff --git a/pw_build/constraints/chipset/BUILD b/pw_build/constraints/chipset/BUILD
new file mode 100644
index 0000000..1c98052
--- /dev/null
+++ b/pw_build/constraints/chipset/BUILD
@@ -0,0 +1,28 @@
+# 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.
+package(default_visibility = ["//visibility:public"])
+
+constraint_setting(
+    name = "chipset",
+)
+
+constraint_value(
+    name = "stm32f429",
+    constraint_setting = ":chipset",
+)
+
+constraint_value(
+    name = "lm3s6965evb",
+    constraint_setting = ":chipset",
+)
diff --git a/pw_build/docs.rst b/pw_build/docs.rst
index 18aadb1..96e7a37 100644
--- a/pw_build/docs.rst
+++ b/pw_build/docs.rst
@@ -542,16 +542,92 @@
 The ARM Cortex-M Bazel toolchains are based around gcc-arm-non-eabi and are
 entirely hermetic. You can target Cortex-M, by using the platforms command line
 option. This set of toolchains is supported from hosts; Windows, Mac and Linux.
-The platforms that are currently supported are listed below.
+The platforms that are currently supported are listed below:
 
 .. code-block:: sh
 
-  bazel build //:your_target --platforms=@bazel_embedded//platforms:cortex_m0
-  bazel build //:your_target --platforms=@bazel_embedded//platforms:cortex_m1
-  bazel build //:your_target --platforms=@bazel_embedded//platforms:cortex_m3
-  bazel build //:your_target --platforms=@bazel_embedded//platforms:cortex_m4
-  bazel build //:your_target --platforms=@bazel_embedded//platforms:cortex_m7
+  bazel build //:your_target --platforms=@pigweed//pw_build/platforms:cortex_m0
+  bazel build //:your_target --platforms=@pigweed//pw_build/platforms:cortex_m1
+  bazel build //:your_target --platforms=@pigweed//pw_build/platforms:cortex_m3
+  bazel build //:your_target --platforms=@pigweed//pw_build/platforms:cortex_m4
+  bazel build //:your_target --platforms=@pigweed//pw_build/platforms:cortex_m7
   bazel build //:your_target \
-       --platforms=@bazel_embedded//platforms:cortex_m4_fpu
+    --platforms=@pigweed//pw_build/platforms:cortex_m4_fpu
   bazel build //:your_target \
-       --platforms=@bazel_embedded//platforms:cortex_m7_fpu
\ No newline at end of file
+    --platforms=@pigweed//pw_build/platforms:cortex_m7_fpu
+
+
+The above examples are cpu/fpu oriented platforms and can be used where
+applicable for your application. There some more specific platforms for the
+types of boards that are included as examples in Pigweed. It is strongly
+encouraged that you create your own set of platforms specific for your project,
+that implement the constraint_settings in this repository. e.g.
+
+New board constraint_value:
+
+.. code-block:: python
+
+  #your_repo/build_settings/constraints/board/BUILD
+  constraint_value(
+    name = "nucleo_l432kc",
+    constraint_setting = "@pigweed//pw_build/constraints/board",
+  )
+
+New chipset constraint_value:
+
+.. code-block:: python
+
+  # your_repo/build_settings/constraints/chipset/BUILD
+  constraint_value(
+    name = "stm32l432kc",
+    constraint_setting = "@pigweed//pw_build/constraints/chipset",
+  )
+
+New platforms for chipset and board:
+
+.. code-block:: python
+
+  #your_repo/build_settings/platforms/BUILD
+  # Works with all stm32l432kc
+  platforms(
+    name = "stm32l432kc",
+    parents = ["@pigweed//pw_build/platforms:cortex_m4"],
+    constraint_values =
+      ["@your_repo//build_settings/constraints/chipset:stm32l432kc"],
+  )
+
+  # Works with only the nucleo_l432kc
+  platforms(
+    name = "nucleo_l432kc",
+    parents = [":stm32l432kc"],
+    constraint_values =
+      ["@your_repo//build_settings/constraints/board:nucleo_l432kc"],
+  )
+
+In the above example you can build your code with the command line:
+
+.. code-block:: python
+
+  bazel build //:your_target_for_nucleo_l432kc \
+    --platforms=@your_repo//build_settings:nucleo_l432kc
+
+
+You can also specify that a specific target is only compatible with one
+platform:
+
+.. code-block:: python
+
+  cc_library(
+    name = "compatible_with_all_stm32l432kc",
+    srcs = ["tomato_src.c"],
+    target_compatible_with =
+      ["@your_repo//build_settings/constraints/chipset:stm32l432kc"],
+  )
+
+  cc_library(
+    name = "compatible_with_only_nucleo_l432kc",
+    srcs = ["bbq_src.c"],
+    target_compatible_with =
+      ["@your_repo//build_settings/constraints/board:nucleo_l432kc"],
+  )
+
diff --git a/pw_build/platforms/BUILD b/pw_build/platforms/BUILD
new file mode 100644
index 0000000..a19a462
--- /dev/null
+++ b/pw_build/platforms/BUILD
@@ -0,0 +1,70 @@
+# 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.
+package(default_visibility = ["//visibility:public"])
+
+# --- CPU's ---
+alias(
+    name = "cortex_m0",
+    actual = "@bazel_embedded//platforms:cortex_m0",
+)
+
+alias(
+    name = "cortex_m1",
+    actual = "@bazel_embedded//platforms:cortex_m1",
+)
+
+alias(
+    name = "cortex_m3",
+    actual = "@bazel_embedded//platforms:cortex_m3",
+)
+
+alias(
+    name = "cortex_m4",
+    actual = "@bazel_embedded//platforms:cortex_m4",
+)
+
+alias(
+    name = "cortex_m4_fpu",
+    actual = "@bazel_embedded//platforms:cortex_m4",
+)
+
+alias(
+    name = "cortex_m7",
+    actual = "@bazel_embedded//platforms:cortex_m7",
+)
+
+alias(
+    name = "cortex_m7_fpu",
+    actual = "@bazel_embedded//platforms:cortex_m7_fpu",
+)
+
+# --- Chipsets ---
+platform(
+    name = "stm32f429",
+    constraint_values = ["//pw_build/constraints/chipset:stm32f429"],
+    parents = [":cortex_m4"],
+)
+
+platform(
+    name = "lm3s6965evb",
+    constraint_values = ["//pw_build/constraints/chipset:lm3s6965evb"],
+    parents = [":cortex_m3"],
+)
+
+# --- Boards ---
+platform(
+    name = "stm32f429i-disc1",
+    constraint_values = ["//pw_build/constraints/board:stm32f429i-disc1"],
+    parents = [":stm32f429"],
+)
diff --git a/pw_sys_io_baremetal_stm32f429/BUILD b/pw_sys_io_baremetal_stm32f429/BUILD
index 48cbc0a..80b0280 100644
--- a/pw_sys_io_baremetal_stm32f429/BUILD
+++ b/pw_sys_io_baremetal_stm32f429/BUILD
@@ -1,4 +1,4 @@
-# Copyright 2019 The Pigweed Authors
+# 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
@@ -25,9 +25,12 @@
     name = "pw_sys_io_baremetal_stm32f429",
     srcs = ["sys_io_baremetal.cc"],
     hdrs = ["public/pw_sys_io_baremetal_stm32f429/init.h"],
+    target_compatible_with = [
+        "//pw_build/constraints/boards:stm32f429i-disc1",
+    ],
     deps = [
         "//pw_boot_armv7m",
         "//pw_preprocessor",
         "//pw_sys_io",
-    ]
+    ],
 )