sencha: add sencha-testbench targets

Add targets that run the cheriot-rtos test suite on a sencha platform
(cheriot-testbennch runs the test suite on a sail platform). Note this
currently completes but the allocator test(s) fail because sencha's heap
is too small.

While here:
- s/cheriot-tests-clean/cheriot-test-clean/
- use mkdir -p when creating tmp directories to avoid forced ordering

Change-Id: I36a77e088031e4600b74bd4696277dfdc0fc101b
diff --git a/platforms/sencha/cheriot-tests.mk b/platforms/sencha/cheriot-tests.mk
index 416e6b0..f1139f3 100644
--- a/platforms/sencha/cheriot-tests.mk
+++ b/platforms/sencha/cheriot-tests.mk
@@ -45,7 +45,7 @@
 		$(CHERIOT_TEST)
 
 ## Cleans all build artifacts for the cheriot-rtos test suite
-cheriot-tests-clean:
+cheriot-test-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 87595e0..355e6ea 100644
--- a/platforms/sencha/cheriot.mk
+++ b/platforms/sencha/cheriot.mk
@@ -89,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-tests-clean cheriot-examples-clean
+cheriot-clean: cheriot-test-clean cheriot-examples-clean
 	rm -rf $(CHERIOT_OUT_DIR)
 
 $(CHERIOT_OUT_DIR):
diff --git a/platforms/sencha/sim.mk b/platforms/sencha/sim.mk
index c8dd05c..8c4d398 100644
--- a/platforms/sencha/sim.mk
+++ b/platforms/sencha/sim.mk
@@ -28,9 +28,9 @@
 	@rm -rf $(OUT)/renode_configs
 
 $(TMP_DEBUG):
-	mkdir $(TMP_DEBUG)
+	mkdir -p $(TMP_DEBUG)
 $(TMP_RELEASE):
-	mkdir $(TMP_RELEASE)
+	mkdir -p $(TMP_RELEASE)
 
 # XXX symlink cheriot fw to "kernel" to satisfy elfloader
 $(EXT_FLASH_DEBUG): $(MATCHA_BUNDLE_DEBUG) $(CHERIOT_FIRMWARE_DEBUG) | $(TMP_DEBUG)
@@ -121,6 +121,64 @@
     \$$cheriot_elf = @$(TMP_DEBUG)/kernel; \
     $(PORT_PRESTART_CMDS) i @${SENCHA_RESC_DEBUG}; start"
 
+
+# Support for building cheriot-rtos functional tests for sencha
+
+SENCHA_TEST_DIR := $(OUT)/cheriot/sencha/test-suite
+SENCHA_TEST := $(SENCHA_TEST_DIR)/release/cheriot/cheriot/release/test-suite
+
+TEST_FLASH_RELEASE=$(CHERIOT_OUT_RELEASE)/test_flash.tar
+
+# XXX symlink cheriot fw to "kernel" to satisfy elfloader
+$(TEST_FLASH_RELEASE): $(MATCHA_BUNDLE_RELEASE) $(SENCHA_TEST) | $(TMP_RELEASE)
+	cp -f $(MATCHA_BUNDLE_RELEASE) $(TMP_RELEASE)/matcha-tock-bundle
+	${C_PREFIX}strip $(TMP_RELEASE)/matcha-tock-bundle
+	${C_PREFIX}objcopy -O binary -g $(TMP_RELEASE)/matcha-tock-bundle $(TMP_RELEASE)/matcha-tock-bundle.bin
+	ln -sf $(SENCHA_TEST) $(TMP_RELEASE)/kernel
+	tar -C $(TMP_RELEASE) -cvhf $@ matcha-tock-bundle.bin kernel
+test_flash_release: sencha-test $(TEST_FLASH_RELEASE)
+
+## Runs the cheriot-rtos testbench in renode on sencha
+sencha-testbench: renode cheriot_sim kelvin_sim multihart_boot_rom test_flash_release
+	$(RENODE_CMD) -e "\
+    \$$repl_file = @${SENCHA_REPL}; \
+    \$$tar = @$(TEST_FLASH_RELEASE); \
+    \$$sc_bin =@$(TMP_RELEASE)/matcha-tock-bundle.bin; \
+    \$$cheriot_elf = @$(TMP_RELEASE)/kernel; \
+    $(PORT_PRESTART_CMDS) i @${SENCHA_RESC_RELEASE}; \
+        $(RENODE_PRESTART_CMDS) start"
+
+## Version of the `sencha-testbench` target that also enables the simulator command
+## line interface on port 4567. To access the simulator use something like
+## telnet localhost 4567. Note renode will block until the cli is connected.
+sencha-testbench+cli: renode cheriot_sim kelvin_sim multihart_boot_rom test_flash_release
+	$(RENODE_CMD) -e "\
+    \$$repl_file = @${SENCHA_REPL}; \
+    \$$tar = @$(TEST_FLASH_RELEASE); \
+    \$$sc_bin =@$(TMP_RELEASE)/matcha-tock-bundle.bin; \
+    \$$cheriot_elf = @$(TMP_RELEASE)/kernel; \
+    \$$cli_port = 4567; \
+    \$$wait_for_cli = true; \
+    $(PORT_PRESTART_CMDS) i @${SENCHA_RESC_RELEASE}; \
+        $(RENODE_PRESTART_CMDS) start"
+
+## Generates the cheriot-rtos test-suite firmware
+sencha-test:
+	${MAKE} \
+		CHERIOT_BOARD=sencha \
+		CHERIOT_OUT_DIR=${SENCHA_TEST_DIR} \
+		CHERIOT_FIRMWARE_SRC_DIR=${CHERIOT_SRC_DIR}/tests \
+		CHERIOT_FIRMWARE_RELEASE=$(SENCHA_TEST) \
+		$(SENCHA_TEST)
+
+## Cleans all build artifacts for the cheriot-rtos test suite built for sencha
+sencha-test-clean:
+	rm -rf ${SENCHA_TEST_DIR}
+
+.PHONY:: sencha-testbench sencha-testbench+cli
+.PHONY:: test_flash_release
+.PHONY:: sencha-test sencha-test-clean
+
 $(CHERIOT_SIM_OUT_DIR):
 	mkdir -p "$(CHERIOT_SIM_OUT_DIR)"