qemu, qemu_v8: prefer gnome-terminal over xterm when available

gnome-terminal comes by default with Ubuntu and is much more advanced
than xterm, so use it when available.
While we're at it, move common definitions to common.mk.

Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
Reviewed-by: Joakim Bech <joakim.bech@linaro.org>
Reviewed by: Victor Chong <victor.chong@linaro.org>
diff --git a/common.mk b/common.mk
index fb8c5f8..4f43185 100644
--- a/common.mk
+++ b/common.mk
@@ -203,6 +203,42 @@
 QEMU_EXTRA_ARGS +=\
 	-netdev user,id=vmnic -device virtio-net-device,netdev=vmnic
 endif
+
+define run-help
+	@echo
+	@echo \* QEMU is now waiting to start the execution
+	@echo \* Start execution with either a \'c\' followed by \<enter\> in the QEMU console or
+	@echo \* attach a debugger and continue from there.
+	@echo \*
+	@echo \* To run OP-TEE tests, use the xtest command in the \'Normal World\' terminal
+	@echo \* Enter \'xtest -h\' for help.
+	@echo
+endef
+
+gnome-terminal := $(shell command -v gnome-terminal 2>/dev/null)
+xterm := $(shell command -v xterm 2>/dev/null)
+ifdef gnome-terminal
+# Note: the title option (-t) is ignored with gnome-terminal versions
+# >= 3.14 and < 3.20
+define launch-terminal
+	@nc -z  127.0.0.1 $(1) || \
+	$(gnome-terminal) -t "$(2)" -x $(SOC_TERM_PATH)/soc_term $(1) &
+endef
+else
+ifdef xterm
+define launch-terminal
+	@nc -z  127.0.0.1 $(1) || \
+	$(xterm) -title $(2) -e $(BASH) -c "$(SOC_TERM_PATH)/soc_term $(1)" &
+endef
+else
+check-terminal := @echo "Error: could not find gnome-terminal nor xterm" ; false
+endif
+endif
+
+define wait-for-ports
+	@while ! nc -z 127.0.0.1 $(1) || ! nc -z 127.0.0.1 $(2); do sleep 1; done
+endef
+
 ################################################################################
 # OP-TEE
 ################################################################################