T398: Source cleanup for tool chain integration

This is a code cleanup to improve portability.

Specific issues addressed:
- Added type casts to (void *) here and there
- Changed non-standard \e escapes to \033
- Added cmake function to handle preinclude
- Changed a few Image$$ references to make use of the REGION_DECLARE
  macro
- Reordered code slightly to avoid the need for a "void *rangeptr"
  variable
- Changed compile time check typedef "err_msg" to avoid declaring
  zero sized array, which is not standards compliant. It will now
  either be -1 (error) or 1 (ok), not -1 and 0
- Reordered the *nfsptr_t typedef to make the cmse_nonsecure_call
  standards compliant
- Added null tests to both secure and non_secure suites to avoid
  defining zero length array. Also use this to find end of list
- Only define __stdout for ARMCLANG builds and conditionalize ns printf
  output for ARMCLANG/GCC/IAR
- Cleaned up some enum type mismatches
- Changed non standard EINVAL error return to -1. The value was only
  checked against 0 anyway
- Added type cast for conversion from float to int

Have tested with IAR, which starts and runs the idle thread. Changes
related to this is not included in this commit.

       Author: Thomas Tornblom <thomas.tornblom@iar.com>
       Signed-off-by: Thomas Tornblom <thomas.tornblom@iar.com>

       Note: Sign off authority needs to adhere to the [DCO](./dco.txt)
       rules.

Change-Id: I3e5229c0777623b128474af0311020ccacc1b797
diff --git a/cmake/Common/CompilerArmClangCommon.cmake b/cmake/Common/CompilerArmClangCommon.cmake
index de3fea4..9817473 100644
--- a/cmake/Common/CompilerArmClangCommon.cmake
+++ b/cmake/Common/CompilerArmClangCommon.cmake
@@ -56,6 +56,43 @@
 	include(Compiler/ARMClang-ASM)
 endif()
 
+function(compiler_get_preinclude_option_string INCLUDE RES)
+	set(${RES} "-include ${INCLUDE}" PARENT_SCOPE)
+endfunction()
+
+function(compiler_set_preinclude_file)
+	#Option (on/off) arguments.
+	set( _OPTIONS_ARGS GLOBAL)
+	#Single option arguments.
+	set( _ONE_VALUE_ARGS INCLUDE)
+	#List arguments
+	set( _MULTI_VALUE_ARGS TARGETS FILES)
+	cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN} )
+	if(NOT DEFINED _MY_PARAMS)
+        	message(FATAL_ERROR "compiler_set_preinclude_file: missing mandatory parameter INCLUDE.")
+	endif()
+	compiler_get_preinclude_option_string(${INCLUDE} _OPTION_STRING)
+	#If include is to be set globally, we ignore TARGETS and FILES
+	if(_MY_PARAMS_GLOBAL)
+		set_property(DIRECTORY ${CMAKE_SOURCE_DIR} APPEND PROPERTY COMPILE_OPTIONS "${_OPTION_STRING}")
+	else()
+	#If GLOBAL was not passed, then either TARGETS or FILES must be present
+		if(NOT DEFINED _MY_PARAM_TARGETS AND NOT DEFINED _MY_PARAM_FILES)
+			message(FATAL_ERROR "compiler_set_preinclude_file: missing mandatory parameter. Either TARGETS and/or FILES must be specified.")
+		endif()
+		#Iterate over targets. Note: call embedded_set_target_compile_flags to
+		#allow the target to be defined after this function call. This helps
+		#modularisation
+		foreach(_TGT IN_LISTS _MY_PARAM_TARGETS)
+			embedded_set_target_compile_flags(TARGET ${_TGT} LANGUAGE "C" FLAGS "${_OPTION_STRING}")
+		endforeach()
+		#Iterate over files
+		foreach(_FILE IN_LISTS _MY_PARAM_FILES)
+			set_property(FILE ${_FILE} APPEND PROPERTY COMPILE_OPTIONS "${_OPTION_STRING}")
+		endforeach()
+	endif()
+endfunction()
+
 function(compiler_set_linkercmdfile)
 	set( _OPTIONS_ARGS )							#Option (on/off) arguments.
 	set( _ONE_VALUE_ARGS TARGET PATH)				#Single option arguments.
diff --git a/cmake/Common/CompilerGNUARMCommon.cmake b/cmake/Common/CompilerGNUARMCommon.cmake
index 2f7639f..cf515cf 100644
--- a/cmake/Common/CompilerGNUARMCommon.cmake
+++ b/cmake/Common/CompilerGNUARMCommon.cmake
@@ -56,6 +56,43 @@
 	include(Compiler/GNUARM-ASM)
 endif()
 
+function(compiler_get_preinclude_option_string INCLUDE RES)
+	set(${RES} "-include ${INCLUDE}" PARENT_SCOPE)
+endfunction()
+
+function(compiler_set_preinclude_file)
+	#Option (on/off) arguments.
+	set( _OPTIONS_ARGS GLOBAL)
+	#Single option arguments.
+	set( _ONE_VALUE_ARGS INCLUDE)
+	#List arguments
+	set( _MULTI_VALUE_ARGS TARGETS FILES)
+	cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN} )
+	if(NOT DEFINED _MY_PARAMS)
+        	message(FATAL_ERROR "compiler_set_preinclude_file: missing mandatory parameter INCLUDE.")
+	endif()
+	compiler_get_preinclude_option_string(${INCLUDE} _OPTION_STRING)
+	#If include is to be set globally, we ignore TARGETS and FILES
+	if(_MY_PARAMS_GLOBAL)
+		set_property(DIRECTORY ${CMAKE_SOURCE_DIR} APPEND PROPERTY COMPILE_OPTIONS "${_OPTION_STRING}")
+	else()
+		#If GLOBAL was not passed, then either TARGETS or FILES must be present
+		if(NOT DEFINED _MY_PARAM_TARGETS AND NOT DEFINED _MY_PARAM_FILES)
+			message(FATAL_ERROR "compiler_set_preinclude_file: missing mandatory parameter. Either TARGETS and/or FILES must be specified.")
+		endif()
+		#Iterate over targets. Note: call embedded_set_target_compile_flags to
+		#allow the target to be defined after this function call. This helps
+		#modularisation
+		foreach(_TGT IN_LISTS _MY_PARAM_TARGETS)
+			embedded_set_target_compile_flags(TARGET ${_TGT} LANGUAGE "C" FLAGS "${_OPTION_STRING}")
+		endforeach()
+		#Iterate over files
+		foreach(_FILE IN_LISTS _MY_PARAM_FILES)
+			set_property(FILE ${_FILE} APPEND PROPERTY COMPILE_OPTIONS "${_OPTION_STRING}")
+		endforeach()
+	endif()
+endfunction()
+
 function(compiler_set_linkercmdfile)
 	set( _OPTIONS_ARGS )							#Option (on/off) arguments.
 	set( _ONE_VALUE_ARGS TARGET PATH)				#Single option arguments.