Build: Convert buildsystem base to modern cmake

Alters root cmake file and configuration files, and adds cmake toolchain
files.

WARNING: This change will not build in isolation, it requires _all_
other cmake changes to successfully build. It is split out only for
clarity of changes.

Signed-off-by: Raef Coles <raef.coles@arm.com>
Change-Id: Iecc914859a5b1d497176ec82f0b6c22764c7b556
diff --git a/toolchain_GNUARM.cmake b/toolchain_GNUARM.cmake
new file mode 100644
index 0000000..bb1728c
--- /dev/null
+++ b/toolchain_GNUARM.cmake
@@ -0,0 +1,188 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2020, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+set(CMAKE_SYSTEM_NAME Generic)
+set(CMAKE_SYSTEM_PROCESSOR arm)
+
+set(CMAKE_C_COMPILER arm-none-eabi-gcc)
+set(CMAKE_ASM_COMPILER arm-none-eabi-gcc)
+
+# Tell CMake not to try to link executables during its checks
+set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
+
+# This variable name is a bit of a misnomer. The file it is set to is included
+# at a particular step in the compiler initialisation. It is used here to
+# configure the extensions for object files. Despite the name, it also works
+# with the Ninja generator.
+set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/cmake/set_extensions.cmake)
+
+# Cmake makes it _really_ hard to dynamically set the cmake_system_processor
+# (used for setting mcpu flags etc). Instead we load
+# platform/ext/target/${TFM_PLATFORM}/preload.cmake, which should run this macro
+# to reload the compiler autoconfig. Note that it can't be loaded in this file
+# as cmake does not allow the use of command-line defined variables in toolchain
+# files and the path is platform dependent.
+macro(_compiler_reload)
+    set(CMAKE_SYSTEM_PROCESSOR ${TFM_SYSTEM_PROCESSOR})
+    set(CMAKE_SYSTEM_ARCHITECTURE ${TFM_SYSTEM_ARCHITECTURE})
+
+    if (DEFINED TFM_SYSTEM_FP)
+        if(TFM_SYSTEM_FP)
+            # TODO Whether a system requires these extensions appears to depend
+            # on the system in question, with no real rule. Since adding +fp
+            # will cause compile failures on systems that already have fp
+            # enabled, this is commented out for now to avoid those failures. In
+            # future, better handling should be implemented.
+            # string(APPEND CMAKE_SYSTEM_PROCESSOR "+fp")
+        else()
+            string(APPEND CMAKE_SYSTEM_PROCESSOR "+nofp")
+        endif()
+    endif()
+
+    if (DEFINED TFM_SYSTEM_DSP)
+        if(TFM_SYSTEM_DSP)
+            # TODO Whether a system requires these extensions appears to depend
+            # on the system in question, with no real rule. Since adding +dsp
+            # will cause compile failures on systems that already have dsp
+            # enabled, this is commented out for now to avoid those failures. In
+            # future, better handling should be implemented.
+            # string(APPEND CMAKE_SYSTEM_PROCESSOR "+dsp")
+        else()
+            string(APPEND CMAKE_SYSTEM_PROCESSOR "+nodsp")
+        endif()
+    endif()
+
+    set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "")
+    set_property(DIRECTORY PROPERTY LINK_OPTIONS "")
+    add_compile_options(
+        --specs=nano.specs
+        -Wall
+        -Wno-format
+        -Wno-return-type
+        -Wno-unused-but-set-variable
+        -c
+        -fdata-sections
+        -ffunction-sections
+        -fno-builtin
+        -fshort-enums
+        -fshort-wchar
+        -funsigned-char
+        -mthumb
+        -nostdlib
+        -std=c99
+        $<$<NOT:$<BOOL:${TFM_SYSTEM_FP}>>:-msoft-float>
+    )
+    add_link_options(
+        --entry=Reset_Handler
+        --specs=nano.specs
+        LINKER:-check-sections
+        LINKER:-fatal-warnings
+        LINKER:--gc-sections
+        LINKER:--no-wchar-size-warning
+        LINKER:--print-memory-usage
+    )
+
+    unset(CMAKE_C_FLAGS_INIT)
+    unset(CMAKE_ASM_FLAGS_INIT)
+
+    set(CMAKE_C_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
+    set(CMAKE_ASM_FLAGS_INIT "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
+    set(CMAKE_C_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
+    set(CMAKE_ASM_LINK_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR}")
+
+    set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_INIT})
+    set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS_INIT})
+endmacro()
+
+set(LINKER_VENEER_OUTPUT_FLAG -Wl,--cmse-implib,--out-implib=)
+set(COMPILER_CMSE_FLAG -mcmse)
+
+macro(target_add_scatter_file target)
+    target_link_options(${target}
+        PRIVATE
+        -T $<TARGET_OBJECTS:${target}_scatter>
+    )
+
+    add_dependencies(${target}
+        ${target}_scatter
+    )
+
+    add_library(${target}_scatter OBJECT)
+    foreach(scatter_file ${ARGN})
+        target_sources(${target}_scatter
+            PRIVATE
+                ${scatter_file}
+        )
+        # Cmake cannot use generator expressions in the
+        # set_source_file_properties command, so instead we just parse the regex
+        # for the filename and set the property on all files, regardless of if
+        # the generator expression would evaluate to true or not.
+        string(REGEX REPLACE ".*>:(.*)>$" "\\1" SCATTER_FILE_PATH "${scatter_file}")
+        set_source_files_properties(${SCATTER_FILE_PATH}
+            PROPERTIES
+            LANGUAGE C
+        )
+    endforeach()
+
+    set_target_properties(${target}_scatter PROPERTIES
+        SUFFIX ".ld"
+    )
+
+    target_link_libraries(${target}_scatter
+        platform_region_defs
+        psa_interface
+        tfm_partition_defs
+    )
+
+    target_compile_options(${target}_scatter
+        PRIVATE
+            -E
+            -P
+            -xc
+    )
+endmacro()
+
+macro(add_convert_to_bin_target target)
+    get_target_property(bin_dir ${target} RUNTIME_OUTPUT_DIRECTORY)
+
+    add_custom_target(${target}_bin
+        SOURCES ${bin_dir}/${target}.bin
+    )
+    add_custom_command(OUTPUT ${bin_dir}/${target}.bin
+        DEPENDS ${target}
+        COMMAND ${CMAKE_OBJCOPY}
+            -O binary $<TARGET_FILE:${target}>
+            ${bin_dir}/${target}.bin
+    )
+
+    add_custom_target(${target}_elf
+        SOURCES ${bin_dir}/${target}.elf
+    )
+    add_custom_command(OUTPUT ${bin_dir}/${target}.elf
+        DEPENDS ${target}
+        COMMAND ${CMAKE_OBJCOPY}
+            -O elf32-littlearm $<TARGET_FILE:${target}>
+            ${bin_dir}/${target}.elf
+    )
+
+    add_custom_target(${target}_hex
+        SOURCES ${bin_dir}/${target}.hex
+    )
+    add_custom_command(OUTPUT ${bin_dir}/${target}.hex
+        DEPENDS ${target}
+        COMMAND ${CMAKE_OBJCOPY}
+            -O ihex $<TARGET_FILE:${target}>
+            ${bin_dir}/${target}.hex
+    )
+
+    add_custom_target(${target}_binaries
+        ALL
+        DEPENDS ${target}_bin
+        DEPENDS ${target}_elf
+        DEPENDS ${target}_hex
+    )
+endmacro()