sencha: refactor cheriot-testbench and add cheriot-examples

- refactor cheriot-testbench support into cheriot-tests.mk
- add cheriot-examples.mk that builds the cheriot-rtos examples;
  there are no targets (yet) to run the firmware images but one
  can use the sim_cheriot_renode shell function

Bypass-Presubmit-Reason: no sencha CI tests

Change-Id: Idea8d5b2b4b2be1a0926ebb4447b06eb0d6259ad
diff --git a/platforms/sencha/cheriot-examples.mk b/platforms/sencha/cheriot-examples.mk
new file mode 100644
index 0000000..4317b16
--- /dev/null
+++ b/platforms/sencha/cheriot-examples.mk
@@ -0,0 +1,72 @@
+# Copyright 2024 Googl4 LLC
+#
+# 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.
+
+# Support for building examples from cheriot-rtos
+
+EXAMPLES_DIR  := $(OUT)/cheriot/examples
+
+# XXX too fragile
+# Common logic for building an example
+__cheriot-example:
+	${MAKE} \
+		CHERIOT_BOARD=sail \
+		CHERIOT_OUT_DIR="${OUT}/cheriot/examples/${EXAMPLE}" \
+		CHERIOT_FIRMWARE_SRC_DIR=${CHERIOT_SRC_DIR}/examples/*.${EXAMPLE} \
+		CHERIOT_FIRMWARE_RELEASE=$(EXAMPLES_DIR)/${EXAMPLE}/release/cheriot/cheriot/release/$(EXAMPLE) \
+		$(EXAMPLES_DIR)/${EXAMPLE}/release/cheriot/cheriot/release/$(EXAMPLE)
+
+cheriot-example-hello-world:
+	${MAKE} EXAMPLE=hello_world __cheriot-example
+cheriot-example-hello-compartment:
+	${MAKE} EXAMPLE=hello_compartment __cheriot-example
+cheriot-example-hello-safe-compartment:
+	${MAKE} EXAMPLE=hello_safe_compartment __cheriot-example
+cheriot-example-temporal_safety:
+	${MAKE} EXAMPLE=temporal_safety __cheriot-example
+cheriot-example-sealing:
+	${MAKE} EXAMPLE=sealing __cheriot-example
+cheriot-example-producer-consumer:
+	${MAKE} EXAMPLE=producer-consumer __cheriot-example
+cheriot-example-error_handling:
+	${MAKE} EXAMPLE=error_handling __cheriot-example
+cheriot-example-memory_safety:
+	${MAKE} EXAMPLE=memory_safety __cheriot-example
+cheriot-example-javascript:
+	${MAKE} EXAMPLE=javascript __cheriot-example
+
+## Builds firmware for all cheriot-rtos examples
+cheriot-examples: cheriot-example-hello-world \
+	cheriot-example-hello-compartment \
+	cheriot-example-hello-safe-compartment \
+	cheriot-example-temporal_safety \
+	cheriot-example-sealing \
+	cheriot-example-producer-consumer \
+	cheriot-example-error_handling \
+	cheriot-example-memory_safety \
+	cheriot-example-javascript
+
+## Cleans all build artifacts for cheriot-rtos examples
+cheriot-examples-clean:
+	rm -rf ${EXAMPLES_DIR}
+
+.PHONY:: cheriot-examples
+.PHONY:: cheriot-example-hello-world
+.PHONY:: cheriot-example-hello-compartment
+.PHONY:: cheriot-example-hello-safe-compartment
+.PHONY:: cheriot-example-temporal_safety
+.PHONY:: cheriot-example-sealing
+.PHONY:: cheriot-example-producer-consumer
+.PHONY:: cheriot-example-error_handling
+.PHONY:: cheriot-example-memory_safety
+.PHONY:: cheriot-example-javascript
diff --git a/platforms/sencha/cheriot-tests.mk b/platforms/sencha/cheriot-tests.mk
new file mode 100644
index 0000000..309e1a7
--- /dev/null
+++ b/platforms/sencha/cheriot-tests.mk
@@ -0,0 +1,37 @@
+# Copyright 2024 Googl4 LLC
+#
+# 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.
+
+# Support for building cheriot-rtos functional tests
+
+CHERIOT_TEST_DIR := $(OUT)/cheriot/test-suite
+CHERIOT_TEST := $(CHERIOT_TEST_DIR)/release/cheriot/cheriot/release/test-suite
+
+## Runs the cheriot-rtos testbench in renode
+cheriot-testbench: renode cheriot_sim cheriot-test
+	$(RENODE_CMD) -e "i @${CHERIOT_RESC_RELEASE}; start"
+
+## Generates the cheriot-rtos test-suite firmware
+cheriot-test:
+	${MAKE} \
+		CHERIOT_BOARD=sail \
+		CHERIOT_OUT_DIR="${OUT}/cheriot/test-suite" \
+		CHERIOT_FIRMWARE_SRC_DIR=${CHERIOT_SRC_DIR}/tests \
+		CHERIOT_FIRMWARE_RELEASE=$(CHERIOT_TEST) \
+		$(CHERIOT_TEST)
+
+## Cleans all build artifacts for the cheriot-rtos test suite
+cheriot-tests-clean:
+	rm -rf ${CHERIOT_TEST_DIR}
+
+.PHONY:: cheriot-test cheriot-test-clean
diff --git a/platforms/sencha/cheriot.mk b/platforms/sencha/cheriot.mk
index 150fd32..8481826 100644
--- a/platforms/sencha/cheriot.mk
+++ b/platforms/sencha/cheriot.mk
@@ -37,8 +37,6 @@
 #CHERIOT_FIRMWARE_DEBUG   := $(CHERIOT_OUT_DEBUG)/cheriot/cheriot/debug/${CHERIOT_FIRMWARE}-firmware
 CHERIOT_FIRMWARE_RELEASE := $(CHERIOT_OUT_RELEASE)/cheriot/cheriot/release/${CHERIOT_FIRMWARE}-firmware
 
-CHERIOT_TEST := $(CHERIOT_OUT_RELEASE)/cheriot/cheriot/release/test-suite
-
 # TODO: add CHERIOT_FIRMWARE_SRC_DIR (not currently defined in time to use here)
 CHERIOT_SOURCES := $(shell find $(CHERIOT_SRC_DIR) \(\
 	-name \*.rs -or \
@@ -91,7 +89,7 @@
 
 ## Cleans all CHERIoT build artifacts for the current platform
 # TODO(sleffler): cheriot-clean-headers once they are per-platform
-cheriot-clean:
+cheriot-clean: cheriot-tests-clean cheriot-examples-clean
 	rm -rf $(CHERIOT_OUT_DIR)
 
 $(CHERIOT_OUT_DEBUG):
@@ -137,20 +135,14 @@
 ## Generates CHERIoT build artifacts setup for release
 cheriot-bundle-release: $(CHERIOT_FIRMWARE_RELEASE)
 
-## Generates CHERIoT provided test-suite binary
-cheriot-test:
-	${MAKE} \
-		CHERIOT_BOARD=sail \
-		CHERIOT_FIRMWARE_SRC_DIR=${CHERIOT_SRC_DIR}/tests \
-		CHERIOT_FIRMWARE_RELEASE=$(CHERIOT_TEST) \
-		${CHERIOT_TEST}
-
 ## Generates both debug & release CHERIoT build artifacts
 cheriot: cheriot-bundle-debug cheriot-bundle-release
 
+include $(ROOTDIR)/build/platforms/sencha/cheriot-examples.mk
+include $(ROOTDIR)/build/platforms/sencha/cheriot-tests.mk
+
 .PHONY:: cheriot cheriot_check cheriot-clean
 .PHONY:: cheriot-bundle-debug cheriot-bundle-release
-.PHONY:: cheriot-test
 .PHONY:: cheriot-builtins-debug cheriot-builtins-release
 .PHONY:: cheriot-gen-headers cheriot-clean-headers
 .PHONY:: cheriot-build-debug-prepare cheriot-build-release-prepare
diff --git a/platforms/sencha/sim.mk b/platforms/sencha/sim.mk
index 018e442..539ad53 100644
--- a/platforms/sencha/sim.mk
+++ b/platforms/sencha/sim.mk
@@ -62,12 +62,6 @@
 
 SENCHA_REPL=sim/config/platforms/sencha.repl
 
-## Run the testbench in standalone cheriot
-cheriot-testbench: renode cheriot_sim $(CHERIOT_TEST)
-	$(RENODE_CMD) -e "\
-    i @${CHERIOT_RESC_RELEASE}; \
-        start"
-
 ## Launches an end-to-end build of the system and starts Renode
 #
 # This top-level target triggers the `matcha_tock_release`, `cheriot`, `renode`,