Add CMake variable to pass arguments to ctest

This is needed in order to for example turn tarmac tracing on for the
FVP running the tests.

Change-Id: I6606134046c2e228d3fe06db41792e7403cd6ba8
diff --git a/cmake/helpers.cmake b/cmake/helpers.cmake
index c8a4987..c12f427 100644
--- a/cmake/helpers.cmake
+++ b/cmake/helpers.cmake
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2020-2021 Arm Limited. All rights reserved.
+# Copyright (c) 2020-2022 Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: Apache-2.0
 #
@@ -111,6 +111,8 @@
 # Test
 #############################################################################
 
+set(ETHOSU_CTEST_TARMAC_TRACE OFF CACHE BOOL "Enable to generate tarmac trace files when running ctest")
+
 function(ethosu_add_test target)
     if(NOT BUILD_TESTING)
         return()
diff --git a/scripts/run_ctest.py b/scripts/run_ctest.py
index 1faf0e2..93a5427 100755
--- a/scripts/run_ctest.py
+++ b/scripts/run_ctest.py
@@ -21,6 +21,8 @@
 import argparse
 import subprocess
 import sys
+import os
+from pathlib import Path
 
 def __print_arguments(args):
     if isinstance(args, list):
@@ -89,6 +91,25 @@
             '-C', 'mps3_board.uart0.unbuffered_output=1',
             '-C', 'mps3_board.uart0.shutdown_on_eot=1']
 
+    if args.tarmac:
+        try:
+            pvlib_home = Path(os.getenv('PVLIB_HOME'))
+        except:
+            raise Exception("Environment variable PVLIB_HOME not found. Needed to produce tarmac trace.")
+
+        if sys.platform == 'linux':
+            tarmac_trace_plugin = pvlib_home / Path('plugins/Linux64_GCC-7.3/TarmacTrace.so')
+        else:
+            raise Exception("tarmac trace: This feature is not currently supported on" + sys.platform)
+
+        if tarmac_trace_plugin.exists():
+            print("Tarmac trace will be created");
+            basename = [ e for e in args.args if e.endswith('.elf')][0][:-4]
+            cmd += ['--plugin', str(tarmac_trace_plugin)]
+            cmd += ['-C', f'TRACE.TarmacTrace.trace-file={basename}.trace']
+        else:
+            raise Exception("tarmac trace: Can't find TarmacTrace plugin in " + pvlib_home)
+
     cmd += args.args
 
     return run_fvp(cmd)
@@ -129,6 +150,8 @@
     parser.add_argument('-t', '--target', choices=['corstone-300', 'corstone-polaris'], required=True, help='FVP target.')
     parser.add_argument('-a', '--arch', choices=['ethos-u55', 'ethos-u65'], help='NPU architecture.')
     parser.add_argument('-m', '--macs', type=int, choices=[32, 64, 128, 256, 512], default=128, help='NPU number of MACs.')
+    parser.add_argument('-c', '--tarmac', dest='tarmac', action='store_true', help='Collect tarmac traces when running FVP.')
+
     parser.add_argument('args', nargs='+', help='Arguments.')
     args = parser.parse_args()
 
diff --git a/targets/corstone-300/CMakeLists.txt b/targets/corstone-300/CMakeLists.txt
index 3a6f291..a409a97 100644
--- a/targets/corstone-300/CMakeLists.txt
+++ b/targets/corstone-300/CMakeLists.txt
@@ -64,6 +64,7 @@
         -t corstone-300
         -a ethos-${ETHOSU_ARCH}
         -m ${ETHOSU_NUM_MACS}
+        $<$<BOOL:ETHOSU_CTEST_TARMAC_TRACE>:--tarmac>
     CACHE INTERNAL "Default test command")
 
 # Enable trustzone support in core_software