Build: Add error when bad options are given

When either of the two required input paramters (CMAKE_TOOLCHAIN_FILE
and TFM_PLATFORM) are given, validate that they are sane values and
output a reasonable error message if they are not.

Change-Id: Ie3a93ffed331e5ed66483f0b22c7875db469700c
Signed-off-by: Raef Coles <raef.coles@arm.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e00790a..cd2d85f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -10,9 +10,22 @@
 project("Trusted Firmware M" VERSION 1.1.0 LANGUAGES C ASM)
 set(TFM_VERSION ${PROJECT_VERSION})
 
+if(NOT TFM_CMAKE_TOOLCHAIN_FILE_LOADED)
+    Message(FATAL_ERROR "Cmake toolchain file not set.")
+endif()
+
+if(NOT ${CMAKE_GENERATOR} STREQUAL "Unix Makefiles" AND
+   NOT ${CMAKE_GENERATOR} STREQUAL "Ninja")
+    Message(FATAL_ERROR "Unsupported generator ${CMAKE_GENERATOR}. Hint: Try -G\"Unix Makefiles\"")
+endif()
+
 # Some compiler flags depend on the CPU / platform config. This include should
 # be run before anything else so the compiler can be configured properly.
-include(platform/ext/target/${TFM_PLATFORM}/preload.cmake)
+if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/platform/ext/target/${TFM_PLATFORM}/preload.cmake)
+    Message(FATAL_ERROR "Unsupported TFM_PLATFORM ${TFM_PLATFORM}")
+else()
+    include(platform/ext/target/${TFM_PLATFORM}/preload.cmake)
+endif()
 
 # The default build type is release. If debug symbols are needed then
 # -DCMAKE_BUILD_TYPE=debug should be used (likewise with other build types)
@@ -20,13 +33,6 @@
     set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type: [Debug, Release, RelWithDebInfo, MinSizeRel]" FORCE)
 endif()
 
-if(
-    NOT ${CMAKE_GENERATOR} STREQUAL "Unix Makefiles" AND
-    NOT ${CMAKE_GENERATOR} STREQUAL "Ninja"
-)
-    Message(FATAL_ERROR "unsupported generator ${CMAKE_GENERATOR}. Hint: Try -G\"Unix Makefiles\"")
-endif()
-
 ############################ CONFIGURATION #####################################
 
 # First load defaults.
diff --git a/toolchain_ARMCLANG.cmake b/toolchain_ARMCLANG.cmake
index 032abb2..956b10f 100644
--- a/toolchain_ARMCLANG.cmake
+++ b/toolchain_ARMCLANG.cmake
@@ -7,6 +7,8 @@
 
 cmake_minimum_required(VERSION 3.15)
 
+set(TFM_CMAKE_TOOLCHAIN_FILE_LOADED YES)
+
 SET(CMAKE_SYSTEM_NAME Generic)
 # This setting is overridden in ${TFM_PLATFORM}/preload.cmake. It can be set to
 # any value here.
diff --git a/toolchain_GNUARM.cmake b/toolchain_GNUARM.cmake
index bb1728c..d1b4e1f 100644
--- a/toolchain_GNUARM.cmake
+++ b/toolchain_GNUARM.cmake
@@ -5,6 +5,8 @@
 #
 #-------------------------------------------------------------------------------
 
+set(TFM_CMAKE_TOOLCHAIN_FILE_LOADED YES)
+
 set(CMAKE_SYSTEM_NAME Generic)
 set(CMAKE_SYSTEM_PROCESSOR arm)
 
diff --git a/toolchain_IARARM.cmake b/toolchain_IARARM.cmake
index cbe8260..1d58f0f 100644
--- a/toolchain_IARARM.cmake
+++ b/toolchain_IARARM.cmake
@@ -8,6 +8,8 @@
 
 cmake_minimum_required(VERSION 3.14)
 
+set(TFM_CMAKE_TOOLCHAIN_FILE_LOADED YES)
+
 SET(CMAKE_SYSTEM_NAME Generic)
 # This setting is overridden in ${TFM_PLATFORM}/preload.cmake. It can be set to
 # any value here.