Build: Enable nested macros for image script

This change enables the usage of nested macros in flash_layout.h
for setting the offset and max size for secure and non-secure images.

Change-Id: I87e2aec67550debb4d36286ed46bfe935a9003c8
Signed-off-by: Sverteczky, Marcell <marcell.sverteczky@arm.com>
diff --git a/cmake/Common/CompilerArmClangCommon.cmake b/cmake/Common/CompilerArmClangCommon.cmake
index 53f143a..de3fea4 100644
--- a/cmake/Common/CompilerArmClangCommon.cmake
+++ b/cmake/Common/CompilerArmClangCommon.cmake
@@ -148,3 +148,75 @@
 function(compiler_generate_binary_output TARGET)
 	add_custom_command(TARGET ${TARGET} POST_BUILD COMMAND ${CMAKE_ARMCCLANG_FROMELF} ARGS --bincombined --output=$<TARGET_FILE_DIR:${TARGET}>/${TARGET}.bin $<TARGET_FILE:${TARGET}>)
 endfunction()
+
+# Function for creating a new target that preprocesses a .c file
+#INPUTS:
+#    SRC - (mandatory) - file to be preprocessed
+#    DST - (mandatory) - output file for the preprocessing
+#    TARGET_PREFIX - (optional) - prefix for the target that this function creates and which manages the preprocessing
+#    BEFORE_TARGET - (optional) - target which is dependent on the preprocessing target in the below function
+function(compiler_preprocess_file)
+	#Option (on/off) arguments.
+	set( _OPTIONS_ARGS)
+	#Single option arguments.
+	set( _ONE_VALUE_ARGS SRC DST TARGET_PREFIX BEFORE_TARGET)
+	#List arguments
+	set( _MULTI_VALUE_ARGS)
+	cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN} )
+
+	#Check passed parameters
+	if(NOT DEFINED _MY_PARAMS_SRC)
+		message(FATAL_ERROR "compiler_preprocess_file: mandatory parameter 'SRC' is missing.")
+	endif()
+
+	if(NOT DEFINED _MY_PARAMS_DST)
+		message(FATAL_ERROR "compiler_preprocess_file: mandatory parameter 'DST' is missing.")
+	endif()
+
+	if(DEFINED _MY_PARAMS_BEFORE_TARGET)
+		if(NOT TARGET ${_MY_PARAMS_BEFORE_TARGET})
+			message(FATAL_ERROR "compiler_preprocess_file: optional parameter 'BEFORE_TARGET' is not target.")
+		endif()
+	endif()
+
+	#The compiler flag might contain leading spaces which can fail the preprocess operation, these are removed
+	STRING(STRIP ${CMAKE_C_FLAGS_CPU} _MY_TEMP_CMAKE_C_FLAGS_CPU)
+	#If a string contains spaces, then it is inserted amongst quotation marks. Furthermore the compiler fails if it is
+	#called with multiple switches included in one quotation mark. If the extra spaces are replaced by semicolons,
+	#then the insertion will be advantageous for the compiler.
+	STRING(REPLACE " " ";" _MY_TEMP2_CMAKE_C_FLAGS_CPU ${_MY_TEMP_CMAKE_C_FLAGS_CPU})
+	set(_LOCAL_CMAKE_C_FLAGS_CPU "")
+	foreach(_C_FLAG IN LISTS _MY_TEMP2_CMAKE_C_FLAGS_CPU)
+		list(APPEND _LOCAL_CMAKE_C_FLAGS_CPU "${_C_FLAG}")
+	endforeach()
+
+	add_custom_command(OUTPUT ${_MY_PARAMS_DST}
+		COMMAND ${CMAKE_C_COMPILER} ${_LOCAL_CMAKE_C_FLAGS_CPU} -E -P -xc ${_MY_PARAMS_SRC} -o ${_MY_PARAMS_DST}
+		DEPENDS ${_MY_PARAMS_SRC}
+		COMMENT "Preprocess the ${_MY_PARAMS_SRC} file"
+	)
+
+	set(_MY_TARGET_PREFIX "")
+	if(TARGET ${_MY_PARAMS_TARGET_PREFIX})
+		set(_MY_TARGET_PREFIX "${_MY_PARAMS_TARGET_PREFIX}")
+	endif()
+	#The preprocessing related target name is obtained by indexing the file's name that is to be preprocessed
+	get_filename_component(_MY_FILENAME_TO_BE_INDEXED ${_MY_PARAMS_SRC} NAME_WE)
+	foreach(_SUFFIX RANGE 1 100)
+		if (NOT TARGET ${_MY_TARGET_PREFIX}_pp_${_MY_FILENAME_TO_BE_INDEXED}_${_SUFFIX})
+				set(_PREPROCESS_TARGET_NAME "${_MY_TARGET_PREFIX}_pp_${_MY_FILENAME_TO_BE_INDEXED}_${_SUFFIX}")
+				break()
+		endif()
+		if (_SUFFIX EQUAL 100)
+			message(FATAL_ERROR "You have called 'compiler_preprocess_file' too many times (${_SUFFIX} function calls).")
+		endif()
+	endforeach()
+
+	#Make the original target depend on the new one.
+	if(TARGET ${_MY_PARAMS_BEFORE_TARGET})
+		add_custom_target(${_PREPROCESS_TARGET_NAME} DEPENDS ${_MY_PARAMS_DST})
+		add_dependencies(${_MY_PARAMS_BEFORE_TARGET} ${_PREPROCESS_TARGET_NAME})
+	else()
+		add_custom_target(${_PREPROCESS_TARGET_NAME} ALL DEPENDS ${_MY_PARAMS_DST})
+	endif()
+endfunction()