Buildsystem restructuring

- Allow linker file to be overriden
- Disable Trustzone build for other targets than Corstone-300
- Make toolchain TARGET_CPU variable cached

Change-Id: I98a15e1080e4bf49e029578888b1e4ce362bbab7
diff --git a/applications/trustzone_inference/CMakeLists.txt b/applications/trustzone_inference/CMakeLists.txt
index f23c7f6..f1efa95 100644
--- a/applications/trustzone_inference/CMakeLists.txt
+++ b/applications/trustzone_inference/CMakeLists.txt
@@ -16,6 +16,10 @@
 # limitations under the License.
 #
 
+if (NOT ${ETHOSU_TARGET} STREQUAL "corstone-300")
+    return()
+endif()
+
 #############################################################################
 # Secure world
 #############################################################################
diff --git a/cmake/helpers.cmake b/cmake/helpers.cmake
index da954c0..3444e64 100644
--- a/cmake/helpers.cmake
+++ b/cmake/helpers.cmake
@@ -27,7 +27,8 @@
     cmake_parse_arguments(ARG "" "LINK_FILE;ENTRY" "" ${ARGN})
 
     # Store the link file in a property to be evaluated by the executable.
-    set_property(GLOBAL PROPERTY ETHOSU_TARGET_LINK_FILE ${CMAKE_CURRENT_SOURCE_DIR}/${ARG_LINK_FILE})
+    get_filename_component(LINK_FILE_PATH ${ARG_LINK_FILE} ABSOLUTE)
+    set_property(GLOBAL PROPERTY ETHOSU_TARGET_LINK_FILE ${LINK_FILE_PATH})
 
     if (ARG_ENTRY)
         target_link_options(${target} ${scope} --entry Reset_Handler)
@@ -42,17 +43,21 @@
 
     if (CMAKE_CXX_COMPILER_ID STREQUAL "ARMClang")
         set(LINK_FILE_EXT scatter)
-        set(LINK_FILE_IN ${LINK_FILE}.${LINK_FILE_EXT})
         set(LINK_FILE_OPTION "--scatter")
         # Note: the -mcpu flag is added to avoid warnings caused when using the default cpu/arch.
         set(COMPILER_PREPROCESSOR_OPTIONS -mcpu=${CMAKE_SYSTEM_PROCESSOR} --target=arm-arm-none-eabi -E -x c -P)
     elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
         set(LINK_FILE_EXT ld)
-        set(LINK_FILE_IN ${LINK_FILE}.${LINK_FILE_EXT})
         set(LINK_FILE_OPTION "-T")
         set(COMPILER_PREPROCESSOR_OPTIONS -E -x c -P)
     endif()
 
+    if (EXISTS ${LINK_FILE})
+        set(LINK_FILE_IN ${LINK_FILE})
+    else()
+        set(LINK_FILE_IN ${LINK_FILE}.${LINK_FILE_EXT})
+    endif()
+
     get_filename_component(LINK_FILE_OUT_BASE ${LINK_FILE} NAME)
     set(LINK_FILE_OUT ${CMAKE_CURRENT_BINARY_DIR}/${LINK_FILE_OUT_BASE}-${target}.${LINK_FILE_EXT})
 
@@ -140,10 +145,25 @@
 #############################################################################
 
 function(ethosu_add_binaries target)
+    set(SCRIPTS_DIR ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../scripts)
+
     add_custom_command(TARGET ${target} POST_BUILD
         COMMAND ${SCRIPTS_DIR}/generate_binaries.py --output ./fw $<TARGET_FILE:${target}>
         BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/fw/*
         WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
         COMMENT "Generate binaries for ${target}")
+endfunction()
 
+#############################################################################
+# Ethos-U NPU configuration
+#############################################################################
+
+function(ethosu_get_architecture config)
+    string(TOLOWER ${config} config)
+    if(${config} MATCHES "^ethos-(u[0-9]+|uz)-([0-9]+$)")
+        set(ETHOSU_ARCH ${CMAKE_MATCH_1} PARENT_SCOPE)
+        set(ETHOSU_NUM_MACS ${CMAKE_MATCH_2} PARENT_SCOPE)
+    else()
+        message(FATAL_ERROR "Unknown NPU config ${config}")
+    endif()
 endfunction()
diff --git a/cmake/toolchain/arm-none-eabi-gcc.cmake b/cmake/toolchain/arm-none-eabi-gcc.cmake
index 2c0ebd0..b0a27fd 100644
--- a/cmake/toolchain/arm-none-eabi-gcc.cmake
+++ b/cmake/toolchain/arm-none-eabi-gcc.cmake
@@ -16,9 +16,7 @@
 # limitations under the License.
 #
 
-if (NOT TARGET_CPU)
-    set(TARGET_CPU "cortex-m4")
-endif()
+set(TARGET_CPU "cortex-m4" CACHE STRING "Target CPU")
 
 set(CMAKE_SYSTEM_NAME Generic)
 set(CMAKE_C_COMPILER "arm-none-eabi-gcc")
diff --git a/cmake/toolchain/armclang.cmake b/cmake/toolchain/armclang.cmake
index bb29286..3fdc3a8 100644
--- a/cmake/toolchain/armclang.cmake
+++ b/cmake/toolchain/armclang.cmake
@@ -16,9 +16,7 @@
 # limitations under the License.
 #
 
-if (NOT TARGET_CPU)
-    set(TARGET_CPU "cortex-m4")
-endif()
+set(TARGET_CPU "cortex-m4" CACHE STRING "Target CPU")
 
 set(CMAKE_SYSTEM_NAME Generic)
 set(CMAKE_C_COMPILER "armclang")
diff --git a/targets/common/CMakeLists.txt b/targets/common/CMakeLists.txt
index f3382f3..3e222be 100644
--- a/targets/common/CMakeLists.txt
+++ b/targets/common/CMakeLists.txt
@@ -41,6 +41,8 @@
 # ethosu_target_common     # Common for all targets
 ###############################################################################
 
+option(CPU_CACHE_ENABLE "Enable CPU instruction- and data cache" OFF)
+
 # Common
 add_library(ethosu_target_common INTERFACE)
 target_include_directories(ethosu_target_common INTERFACE include)
@@ -49,6 +51,11 @@
 add_library(ethosu_target_link INTERFACE)
 target_link_libraries(ethosu_target_link INTERFACE ethosu_target_common)
 
+# Configuring caches
+if (CPU_CACHE_ENABLE)
+    target_compile_definitions(ethosu_target_link INTERFACE CPU_CACHE_ENABLE)
+endif()
+
 # Startup
 add_library(ethosu_target_startup INTERFACE)
 target_link_libraries(ethosu_target_startup INTERFACE ethosu_target_link)
diff --git a/targets/corstone-300/CMakeLists.txt b/targets/corstone-300/CMakeLists.txt
index 85fe944..7ae4d04 100644
--- a/targets/corstone-300/CMakeLists.txt
+++ b/targets/corstone-300/CMakeLists.txt
@@ -20,15 +20,13 @@
 # Default parameters
 #############################################################################
 
-set(TARGET_CPU "cortex-m55")
+set(TARGET_CPU "cortex-m55" CACHE INTERNAL "")
 
 if (NOT CMAKE_TOOLCHAIN_FILE)
     set(CMAKE_TOOLCHAIN_FILE "${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/toolchain/armclang.cmake")
 endif()
 
-set(ETHOSU_COMMAND_DEFAULT python3 ${CMAKE_CURRENT_SOURCE_DIR}/../../scripts/run_ctest.py -t corstone-300)
-
-option(CPU_CACHE_ENABLE "Enable CPU instruction- and data cache" OFF)
+set(ETHOSU_COMMAND_DEFAULT python3 ${CMAKE_CURRENT_SOURCE_DIR}/../../scripts/run_ctest.py -t corstone-300 CACHE INTERNAL "Default test command")
 
 #############################################################################
 # Project
@@ -44,8 +42,6 @@
 
 include(${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/helpers.cmake)
 
-set(SCRIPTS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../scripts)
-
 #############################################################################
 # Corstone-300
 #############################################################################
@@ -58,6 +54,7 @@
 
 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../common target)
 
+set(ETHOSU_TARGET_NPU_CONFIG "ethos-u55-128" CACHE STRING "NPU configuration")
 set(ETHOSU_TARGET_NPU_COUNT 1 CACHE INTERNAL "Number of NPUs")
 set(ETHOSU_TARGET_NPU_TA_COUNT 2 CACHE INTERNAL "Number of timing adapters per NPU")
 
@@ -65,14 +62,11 @@
     ETHOSU_NPU_TA_COUNT=${ETHOSU_TARGET_NPU_TA_COUNT}
     ETHOSU_NPU_COUNT=${ETHOSU_TARGET_NPU_COUNT})
 
-if (CPU_CACHE_ENABLE)
-    target_compile_definitions(ethosu_target_common INTERFACE
-        CPU_CACHE_ENABLE)
-endif()
-
 # Linker script
+set(LINK_FILE platform CACHE STRING "Link file")
+
 ethosu_target_link_options(ethosu_target_link INTERFACE
-    LINK_FILE platform
+    LINK_FILE ${LINK_FILE}
     ENTRY Reset_Handler)
 
 # Add drivers
@@ -91,4 +85,3 @@
 
 # Add all applications
 add_subdirectory(../../applications applications)
-